Mental Exercise: SVG Language Interpreter

(AI translated)

It all starts with a small nonsense: I decided to put a USB port logo on a 3D printed product. I couldn’t quickly find any STL file (for free or download without registration). So I thought to myself—well, there’s an SVG illustration, it’s a pure vector, I just need to “extrude” it upwards and everything will be great.

And then it started… My 3D printing and modeling program doesn’t accept such files. Well, it’s imperfect and not intended for that. The online converter also didn’t work:

We see how it cut off a piece from the square.

This requires something more serious: CorelDRAW, AutoCAD… I’m more familiar with Corel, so I open that SVG in Corel What a mess! Firefox or some Chrome handles the file perfectly! You can try it yourself.

Well, the SVG file is essentially a text file with various graphic commands crammed into XML. With Notepad, I edited the file a bit and got a slightly corrected version of the SVG

Now the image is correct (I removed the white background along the way).

We export it to DWG/DFX, open AutoCAD to make it 3D and get frustrated:

What the hell? Why so few points? Where are the curves?

And then the “horses” started running away with me (“Меня понесло”). I completely forgot the initial task and decided to write the converter myself… First, I decided to understand how the pure unpacked SVG works, i.e., I decided to write an SVG language interpreter. Although they say the file format was created by W3C around 1999, I feel that its foundations come from the 1960s, when some pioneer, like HP or Xerox, was creating mathematical models for vector description.

Thus, a new project was born that no one needed. However, it was a very serious exercise for the brain:

The program is divided into several parts:

  • Windows printing/drawing/control
  • Text file preprocessor – the program removes all abbreviations, compressions (e.g., not writing zero) and converts it into “canonical form.”
  • SVG language interpreter – the program takes one command, collects parameters, calculates coordinates.
  • Geometry and trigonometry module which calculates and draws graphics. (this module is partly from the internet, there is a URL source code)

The program can print the result to a printer or pdf. That list next to the picture is the preprocessor-interpreted initial code:

M481.114 37.464l16.415-28.96 16.834 28.751-12.164.077-.174 70.181c.988-.552 2.027-1.09 3.096-1.643 6.932-3.586 15.674-8.11 15.998-28.05h-8.533V53.251h24.568V77.82h-7.611c-.334 25.049-11.627 30.892-20.572 35.519-3.232 1.672-6.012 3.111-6.975 5.68l-.09 36.683a14.503 14.503 0 0 1 10.68 14.02 14.5 14.5 0 0 1-14.533 14.532 14.5 14.5 0 0 1-14.533-14.532 14.504 14.504 0 0 1 9.454-13.628l.057-22.801c-2.873-1.613-5.62-2.704-8.139-3.705-11.142-4.43-18.705-7.441-18.857-33.4a14.381 14.381 0 0 1-10.43-13.869c0-7.946 6.482-14.428 14.428-14.428 7.946 0 14.428 6.482 14.428 14.428 0 6.488-4.21 11.889-10.004 13.74.116 20.396 5.54 22.557 13.528 25.732 1.61.641 3.303 1.312 5.069 2.114l.214-86.517-12.154.076z

I extracted this code from the initial SVG file. The same one that Corel couldn’t read… By the way, my software tries to interpret all commands. Unfortunately, there wasn’t time to debug everything. For example, I don’t know the difference between a “quadratic Bézier” curve and a “smooth quadratic Bézier” curve.

So I wrote cooler code than the Corel Draw programmers…

And I’m not at all sorry: SVG interpreter VB.NET source code and compiled exe and setup.exe. This is a working version with its bugs. I don’t know if there will ever be a newer version.

How do you like Levo’s mental exercise? There will be no continuation because my quasi-Covid ended and I will have to go to work again and there will be no time to play. This project took 3 evenings, including writing this article.

P.S. The initial SVG is not optimal. I could manually write a shorter (fewer commands) and the image would be the same. And who needs such “precise” numbers?

P.S.2 My preprocessed SVG interpreter worked perfectly with the online SVG2STL converter: USB LOGO STL. As the initial result was achieved. Whether it’s worth converting to STL or AMF, I don’t know anymore. 🙂

About Administrator

I am owner of this site.
This entry was posted in AI translated, Anything and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *