SVG import

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

SVG import

Postby shoogen » Thu Jan 05, 2012 11:21 am

Yesterday I started to investigate why the SVG import didn’t work for me in 0.12 on Windows.
1. Opening and importing does not work from the GUI. There are instructions shown on the python console, but either command fails with an Exception ([Errno 9] Bad file descriptor)
Issuing the same command from the python console, works.
My first idea was a problem with backslashes in the path, but I have no clue how to test that.
2. If I import a closed path without any filling, I just get to see points of the shape but no edges. A "ViewFit" zooms to these points but not to the (much bigger) Bounding box of the shape. After downgrade to a wire, I get to see all the edges and even “ViewFit” works as I expect.
3. The docstring in the importSVG file is a bit misleading, as there is some support for qubic Bezier curves. I think I would be desirable to have full support for the path element, which would include quadratic curves and shorthand/smoothed instructions (s,S,t,T) for Bezier curves .I don’t know which programs do use these commands.
4. I have recognized a function to detect straight segments in Bezier curve and to replace them with line segments. I would like to know why this has been implemented. And if anyone sees a need for this functionality also for quadratic Bezier curves.
Personally, I don’t like the idea of having the precision of four decimal places hardcoded there. Even, as it is applied before any matrix operation (scaling). I think the resolution and precision of an SVG bound to screen coordinates and can be quite arbitrary in a CAD context.

As far as I understood the Part documentation a quadratic Bezier curve could be created by simply passing one pole.

The shorthand or smoothed command could be implemented by keeping the last used pole. Mirroring the pole with respect to the start point should be easy.
Last edited by shoogen on Wed Jan 11, 2012 11:41 am, edited 1 time in total.
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: SVG import

Postby yorik » Thu Jan 05, 2012 12:35 pm

Hi shoogen,

For me importing svg files works OK (debian), outside a little module import bug I fixed now. I think the GUI part of file import is common to all open/import operations in FreeCAD, do other filetypes (.fcstd, etc...) open correctly?

About the imported files not displaying correctly, usually when you get a shape that only display points, it is because a face got created but has a problem that prevents it from rendering correctly, for example edges crossing each other. If you can attach the file here (you might need to zip it for the forum to accept it) I can have a look if you want. But in general terms, the svg importer is not much used, so it has still a number of bugs, probably.

The hard-coded precision and other things of that nature are mostly due to my inexperience in coding, specially at the time I did that part. There is now an internal precision setting in the Draft module, I just updated the SVG importer to use it.

About line segments, my own use of imported SVG files is mostly for architecture drawings that come from pdf files that I converted with inkscape. So it is important for me to have lines, and sometimes inkscape produce bezier segments that are totally straight. But honestly I didn't venture much into the whole bezier curves world, the current support is very rough, as you saw.

In any case if you would like the svg importer to behave another way, it would be fairly easy to add a setting in the preferences, so users can choose which way they prefer.

If you want to work on this, feel free to do it, I'll include your changes. I'm pretty happy that someone else looks at that code actually...
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: SVG import

Postby shoogen » Thu Jan 05, 2012 3:12 pm

Hi yorik
Thanks a lot for your prompt answers and fixes!
I tried to open other file types. So far the problem only occurs with SVG. My next theory is that, when opening a file via the GUI there is no STDOUT opened for the import script (in Windows). In that case a single ‘print’ statement would cause that exception.
EDIT:
Sometimes the svg file gets importet partialy.

Inkscape is my favorite SVG editing tool. And I’m happy to hear that the import SVG module is design to handle inkscapes output. Inkscapes does not use the shorthand syntax. If entered in its XML Editor the path is immediately converted to the explicit syntax. So far I haven’t seen any SVG file using quadratic Bezier curves (which, of course a have a single pole and not three ;) yet. So there is no urgent need for this. But maybe I’ll send you a patch ;)
And again thanks for SVG import
Last edited by shoogen on Fri Jan 06, 2012 1:34 pm, edited 1 time in total.
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: SVG import

Postby yorik » Thu Jan 05, 2012 7:25 pm

Hmm next time I'm on windows I'll have a look at that import issue.
About the svg bezier curves, honestly it is not much my area... I'll be glad if you can put some ideas there!
I saw you have a freecad git repo, I'm still learning how to use git, but it should make collaboration pretty easy
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: SVG import

Postby shoogen » Sat Jan 07, 2012 3:15 pm

As support for the 'Path' is now complete. I'd like to talk about the other primitives.
I've seen code for the elements path, circle, line, rect and text.
Therefore support for ellipse, polyline, polygon and image is missing.
1. I think that the image Element has least use to Freecad. But there might be some usefull functions in the Image workbench to handle it.
2. polyline should be trivial to implement using existing code from the path element.
3. For the polygon element there sould be usable code in the Draft Module, but i haven't had a look at it yet.
3. For the ellipse element there is a suitable Constructor in the Part Module.
The question whether there is any need to make the Freecad objects parametric, as it would be possible for a polygon.
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: SVG import

Postby yorik » Sat Jan 07, 2012 3:53 pm

Actually at the moment all the output of the svg importer is non-parametric. Until now nobody expressed interest in having it another way, so it stayed like that.
For the DXF importer for example, there is now a setting in the draft preferences that allows to choose between parametric or not or even all geometry bundled into one object (which imports blind-fast). One day we could do the same for the SVG importer. But frankly at the moment I would do it the easiest way. Indeed there is an easy to use makePolygon() function in the Draft module, but it would also be easy to produce a dumb shape too.

And also, more and more people will want to "skip" the draft module and pass the imported stuff directly to the sketcher (which can already be done with the appropriate draft tool), so I wouldn't bother much with that now.

You seem to have had a pretty deep look at the freecad internals already, nice! I think that if you are willing to work further on this, which would be totally cool, we can setup a git way, no? like a branch I can easily merge, so you don't need to make patches (Sorry, still I'm still a git newbie...)

Thanks anyway for your work so far
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: SVG import

Postby shoogen » Sat Jan 07, 2012 9:50 pm

Hi Yorik
as don't like sourforges model of handling git write access. And i dont think write access would be suitabel in my case.I created a repo on github currently in only contains the master branch and my personal branch. You can browse it via https://github.com/5263/free-cad or fetch from it via git://github.com/5263/free-cad.git
Feel free to fetch for testing. But please be warned not merge as i have just tested that it doesn't break old functionality.

I'd like to know why it is neccesary to catch a ValueError in the code for the path element. Is this also needed in the code for polygon/polyline.
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: SVG import

Postby yorik » Sat Jan 07, 2012 10:36 pm

Ok, no worries, you tell me when you see it fit to merge. Anyway I will also test before.

About the ValueError handling, if I remember well, it is because some apps separate path data like: M 1.4 2.5 L 4.6 5.6, other use commas, like M 1.4,2.5 L 4.6,5.6 (gimp path export, IIRC) and sometimes the splitting was not right, and some character got appended to the number.
User avatar
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: SVG import

Postby shoogen » Sun Jan 08, 2012 7:37 pm

I found out that is is allowed not to have any whitespace sourounding the commads. For Example:
"M100,100L200,200"
Your code would handle some of these cases but not all posible.
I think we have to implement a custom split function to parse these strings or utilise Regular expressions.

pitfal No1: All commands are letters but 'e' determins an exponent
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: SVG import

Postby yorik » Sun Jan 08, 2012 7:48 pm

Entirely agree with you. The whole parser could actually be rewritten a more elegant and effective way, and of course use regexes.

I tested mainly with 3 kinds of svg files: exported from inkscape, from gimp's path export, and from blender's pantograph plugin (which isn't maintained anymore). But it's a long time ago, might be time to make a new set of test files...
cron