Import SVG and Convert to Sketch

A place to share learning material: written tutorials, videos, etc.
TheMarkster
Posts: 125
Joined: Thu Apr 05, 2018 1:53 am

Import SVG and Convert to Sketch

Postby TheMarkster » Tue Jul 10, 2018 7:16 pm

Any (relatively simple) image can be imported into FreeCAD and converted into a 3d object. The first step is to convert the image into SVG format. There is an online tool free to use that seems to work very well:

https://image.online-convert.com/convert-to-svg

You can upload your image to their server or you can copy/paste the url to the image, and then download the converted file. Once you have the SVG file it's simple to import it into FreeCAD. First, create a new document, then select Import from the File menu, select your SVG file, and choose to import it as geometry (not as a drawing). You will get a number of objects from this import.

Select all the objects and go to the Draft workbench. Choose the make sketch tool, which will (if all goes well) produce multiple sketches (one from each of the objects produced during the SVG import). You can hide (or delete) all of the original import objects. Now select all the different sketches and go to the Sketcher workbench. From the Sketch menu select the merge sketches tool. You can then delete all the other sketches that were produced in the previous step.

Now you have a single sketch comprised mostly of bsplines with a lot (a lot) of degrees of freedom. It will not be really feasible to do much editing with that sketch, but you can extrude it in Part workbench, or apply Part Design features to this sketch quite easily in most cases.
svg_import_example.gif
svg_import_example.gif (920.92 KiB) Viewed 139 times
We can also use the Curves workbench (You must install the Curves workbench via the Tools -> Addon Manager.) to map the SVG imported sketch to the surface of a cylinder. I have a macro here:

https://github.com/mwganson/SvgToCylinder

that automates this process, creates a cylinder of the appropriate size, and extrudes the curved sketch. Unfortunately, the solids produced from the curved sketch are often invalid. This is because we have to choose a direction for the extrude process and, no matter which direction we pick, a highly curved shape will intersect itself at some point along the way. The direction chosen in the macro is based on the center of gravity of the shape being extruded, but with long shapes this is still problematic. SVG files comprised of individual small shapes will work better than SVG files comprised of larger shapes. For example, an image of a bunch of small balloons will work better than one big balloon.

Here is an animated gif of a butterfly image import using the macro:
svg_import_example2-opt.gif
svg_import_example2-opt.gif (1001.53 KiB) Viewed 139 times
Some (many) of the sketches produced (lots of b-splines) will be too much for the Sketcher editor to deal with, but can still be extruded or padded, and additional sketches used to modify the model.
chrisb
Posts: 9050
Joined: Tue Mar 17, 2015 9:14 am

Re: Import SVG and Convert to Sketch

Postby chrisb » Tue Jul 10, 2018 9:47 pm

Thanks for the report. I would like to add two points:
- If you use inkscape for the conversion you can simplify the path there.
- As you said, you get a lot of points and bsplines. Thus it might be sensible not to put it all in one sketch.
User avatar
microelly2
Posts: 3669
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Import SVG and Convert to Sketch

Postby microelly2 » Fri Jul 13, 2018 9:06 am

chrisb wrote:
Tue Jul 10, 2018 9:47 pm
- As you said, you get a lot of points and bsplines. Thus it might be sensible not to put it all in one sketch.
There is a need to have simplification methods to reduce the number of points in a path/wire without to big changes in geometry.
There are some tasks like
find segments where the points belong nearly to an arc
find segments where the poinbts belong nearly to a line etc ...
I did some work into this direction, but at them moment I have other priorities.
Some suggestions:
https://youtu.be/QXEPoVpmIiI