Improvements in SVG

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Improvements in SVG

Post by vocx »

The import of SVGs occurs in Mod/Draft/importSVG.py. This takes the SVG text, and converts it to Part Shapes.

The export to SVG is in the same file, however, it actually uses the getSVG.py module. This takes the FreeCAD Shapes and outputs a string with the representation of the SVG object.

This getSVG module is also used by Arch in the Arch SectionPlane, and thus by TechDraw to produce the TechDraw ArchView.

Pull request #3749 tries to improve getSVG by cleaning up the code, separating it into functions, instead of them being inside a huge function. More optimizations could be done, such as writing a single function for a single type of object (dimension, angular dimension, pipe, ..., and general "Shape", the last one).

A new member is very good at SVG, so maybe he can look into our code and tell us what looks okay, and what doesn't so we can investigate.
mikeprice99 wrote: Thu Jul 30, 2020 5:41 pm ping
Current issues
* getSVG produces wrong export of Arch Pipe.
* split the different if-then-else branches of getSVG in separate functions for ease of use.
* importSVG may choke on some badly constructed SVGs; maybe we need to make it more robust.
* Conversion from an imported SVG to Sketch is a good workflow, but it seems it is currently broken through the use of Draft Draft2Sketch. If I remember a previous conversation the problem is that the geometry created during import is composed of BSplines instead of simple straight lines, so it fails calculating some normals.
* Split importSVG into importer and exporter, just like importIFC and exportIFC in Arch.

Threads:
* Inkscape svg file convert to sketcher file
* Regression in Draft To Sketch; this thread explains the issue with the Draft to Sketch conversion. We probably need to rollback some changes that were introduced. Notice that the functions in DraftGeomUtils.py were moved to different modules, so now the problem seems to be in draftgeoutils/geometry.py
* Simple test file of the import error, SVG cannot be converted to a sketch
Last edited by vocx on Sat Oct 10, 2020 2:27 am, edited 1 time in total.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
chakkree
Posts: 327
Joined: Tue Jun 30, 2015 12:58 am
Location: Bangkok Thailand

Re: Improvements in SVG

Post by chakkree »

+1
mikeprice99
Posts: 100
Joined: Sat Jun 27, 2020 9:06 am
Location: Dorset, England

Re: Improvements in SVG

Post by mikeprice99 »

Following on from the discussion in the Inkscape svg file convert to sketcher file I am having a little look.

One thing that really aggravates me already: every time I open an svg, even though I've set disableUnitScaling in preferences, it keeps on asking me whether I want to use 96dpi or 90 dpi.

So immediately I recommend we change line 736 to

Code: Select all

  if FreeCAD.GuiUp and not self.disableUnitScaling:
------------------------
Line 717 reads the internally set document name inks_doc_name = attrs.getValue('sodipodi:docname')
That can be very misleading: if someone has renamed the svg file, that name will be different. So the subsequent message
This SVG file (" + inks_doc_name + ") has an unrecognised format which means the dpi could not be determined; assuming 96 dpi
will be misleading. It needs the actual filename that is being imported.

For example, in Inkscape svg file convert to sketcher file the actual filename is Inkscape_fingerClampProfile.svg but the sodipodi:docname is clip-rod.svg
mikeprice99
Posts: 100
Joined: Sat Jun 27, 2020 9:06 am
Location: Dorset, England

Re: Improvements in SVG

Post by mikeprice99 »

vocx wrote: Thu Jul 30, 2020 6:09 pm If I remember a previous conversation the problem is that the geometry created during import is composed of BSplines instead of simple straight lines,
The conversion actually correctly converts lines BUT it then applies a matrix conversion which seems to change the lines to bsplines.

By editing importSVG.py I've managed to disable the global matrix transformation generated for the svg element (as long as "Disable Units Scaling" is set in preferences). Then if you keep the path out of any group, the lines remain as lines.

I've attached my new version of importSVG.py to my last post in Inkscape svg file convert to sketcher file
Last edited by mikeprice99 on Fri Jul 31, 2020 10:42 am, edited 1 time in total.
mikeprice99
Posts: 100
Joined: Sat Jun 27, 2020 9:06 am
Location: Dorset, England

Re: Improvements in SVG

Post by mikeprice99 »

I also wonder when importing an svg whether it would be smart to show a dialog offering scaling choices
ImportSVG2.png
ImportSVG2.png (3.96 KiB) Viewed 2340 times
(and remove the preference "Disable Unit Scaling")

EDIT: typo - adobe default should be 72dpi
Last edited by mikeprice99 on Sat Aug 01, 2020 3:46 pm, edited 1 time in total.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Improvements in SVG

Post by vocx »

mikeprice99 wrote: Fri Jul 31, 2020 9:58 am ...
I've attached my new version of importSVG.py to my last post in Inkscape svg file convert to sketcher file
Please don't fragment the conversation. Post anything related to the code or development here. That other thread was opened by a regular user, so it is intended to help regular users; we cannot expect the conversation to turn too technical there, it isn't the place to discuss about the code.

Do you know how to use Git and GitHub? In that case I suggest you open a branch with your changes so that it is easier to review; attaching modified files directly to forum posts is not the best idea, particularly when the files are big, like the case of importSVG.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
johnwang
Veteran
Posts: 1345
Joined: Sun Jan 27, 2019 12:41 am

Re: Improvements in SVG

Post by johnwang »

mikeprice99 wrote: Fri Jul 31, 2020 10:23 am to show a dialog
no,no dialog. It is painful everytime have to decide. Better have a option in system settings.

There are one dialog when import saying the svg is not created by freecad, this should also be removed.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
mikeprice99
Posts: 100
Joined: Sat Jun 27, 2020 9:06 am
Location: Dorset, England

Re: Improvements in SVG

Post by mikeprice99 »

johnwang wrote: Sat Aug 01, 2020 6:20 am no,no dialog. It is painful everytime have to decide. Better have a option in system settings.
There are one dialog when import saying the svg is not created by freecad, this should also be removed.
Good point. I suggest the options in system settings should be those I've given in my dummy dialog instead of the single "Disable unit scaling"
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Improvements in SVG

Post by vocx »

johnwang wrote: Sat Aug 01, 2020 6:20 am no,no dialog. It is painful everytime have to decide. Better have a option in system settings.
Dialogs can be useful in certain situations. If you don't want to see any dialog, then you should be using a script to run the import function from the Python console. Anything that is done through buttons is inherently graphical and you should expect some graphical interaction.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
chrisb
Veteran
Posts: 53945
Joined: Tue Mar 17, 2015 9:14 am

Re: Improvements in SVG

Post by chrisb »

vocx wrote: Sat Aug 01, 2020 2:56 pm Dialogs can be useful in certain situations. If you don't want to see any dialog, then you should be using a script to run the import function from the Python console. Anything that is done through buttons is inherently graphical and you should expect some graphical interaction.
This very general comment is of course correct, but nevertheless it is tedious to answer multiple times the same dialog in the same way. An extreme example would be to ask the user on every input dimension about the unit.
So the question is how high is the probability that you select the same option every time or during a session. I would guess that at least for a session it is very high and thus it would be convenient to ask the user only once per session. Or have three options: 1) Show dialog every time, 2) show it once per session 3) use a preset option.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply