Extracting Bezier control points

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Extracting Bezier control points

Post by vrytar »

I am creating a part completely in Python. However, at one point I need to import Bezier curves from an external SVG file, resize them and place in the right position. I've been able to do this with
import importSVG
mycurve = importSVG.insert("filename.svg","Unnamed")
Gui.SendMsgToActiveView("ViewFit")

I don't want to depend on an external file, though. I want my Python script self contained. So I'd like to extract the control points of the Bezier curves and replace the import part with something like
points = [coordinates extracted from the imported and correctly positioned curves]
mycurve = makeBCurve(points)

How can I extract the control points coordinates? I tried to play with mycurve after the import, but it doesn't seem to have any obvious attributes.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Extracting Bezier control points

Post by shoogen »

The svg importer scale the curves with the transformGeometry command, which changes the bezier curves to BSpline curves.
You could extract the coordinates directly from the SVG file and construct python instructions, that would recreate the curves. This means that would have to scale the corrdinates to mm by yourself. If your SVG files uses relatives coordinates you would have to calculate absolute coordinates by yourself.

You could as well simply save the "shape" in a python string in your python script. You would then recreate the shape using shape.importBrepFromString() function.

You could as well save the control points manually. But since you are dealing with a BSpline, that means that you have to save additional parameters like the degree, the multicities and the weights as well.
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Re: Extracting Bezier control points

Post by vrytar »

Wow, exportBrepToString seems to be exactly what I need! This forum is amazing. Thanks for putting me on the right track.
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Re: Extracting Bezier control points

Post by vrytar »

By the way, this functionality is probably not very widely known. It might be a nice touch to add it under the Export menu.
Save as: Python script (*.py)
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Extracting Bezier control points

Post by shoogen »

vrytar wrote:By the way, this functionality is probably not very widely known.
But so, is the need to embed shapes in python scripts. Usually you would save them as individual BREP files. Or create them from scratch using topological operations.
vrytar wrote:It might be a nice touch to add it under the Export menu.
Save as: Python script (*.py)
This won't be intuitive to must users either. But if you need this function regularity you can write the export script that registers the .py file extension. We have some examples in the Wiki or in the Draft, Arch and OpenSCAD workbench. The registration of the extension happens in Init.py or InitGui.py of the Mddule.
Post Reply