creating revolve using python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: creating revolve using python

Post by TheMarkster »

When setting a property of type Link you must specify a document object. For example, Revolve property "Source" is a Link type property. If you have an object named Shape to use as the property:

Code: Select all

Revolve.Source = FreeCAD.ActiveDocument.getObject("Shape")
Axis Link is not a Link type property. It is a LinkSub type property. (App::PropertyLinkSub). Setting this property requires the document object and the subobjects to use from that document object. A subobject can be a Face, Vertex, Edge, or Wire. These must be passed to the object as a tuple, the first element being the document object, the 2nd element being a list of strings naming the subobjects to use. For example, if you want to use Edge1 of Shape as the Axis Link for the Revolve property:

Code: Select all

revolve = FreeCAD.ActiveDocument.getObject("Revolve")
revolve.AxisLink = (FreeCAD.ActiveDocument.getObject("Shape"),["Edge1"])
For the example file you posted here you would want to use "Shape001" (the line/edge) as the profile to revolve and Shape.Edge1 as the Axis Link. You also need to set the angle to -90 degrees. You might be using radians currently. This can be converted in python as math.degrees(rad) where rad is the angle in radians.

Code: Select all

doc = FreeCAD.ActiveDocument
revolve = doc.addObject("Part::FeatureRevolve","Revolve2")
revolve.Source = doc.getObject("Shape001")
revolve.Angle = -90
revolve.AxisLink = (doc.getObject("Shape"),["Edge1"])
doc.recompute()
There is a tool to do this in the Gui in Curves Workbench called BlendSurface. Install Curves from the tools menu -> addon manager -> workbenches -> Curves. Chris is the author of that workbench. Usage: Select both faces and both edges to join, run the tool:

Faces and Edges selected:
Snip macro screenshot-e785dc.png
Snip macro screenshot-e785dc.png (36.55 KiB) Viewed 970 times
After running tool:
Snip macro screenshot-4469e8.png
Snip macro screenshot-4469e8.png (54.19 KiB) Viewed 970 times
One way to script this for your attached file (requires workbench be installed):

Code: Select all

doc = FreeCAD.ActiveDocument
revolve = doc.addObject("Part::Revolution","Revolve2")
revolve.Source = doc.getObject("Shape001")
revolve.Angle = -90
revolve.AxisLink = (doc.getObject("Shape"),["Edge1"])
doc.recompute()

Gui.Selection.clearSelection()
Gui.Selection.addSelection(doc.Name,'Shape002','Face1')
Gui.Selection.addSelection(doc.Name,'Shape002','Edge6')
Gui.Selection.addSelection(doc.Name,'Shape001','Edge1')
Gui.Selection.addSelection(doc.Name,'Shape003','Face1')
Gui.activateWorkbench('CurvesWorkbench')
Gui.runCommand('Curves_BlendSurf2',0)
It might also be possible to run this without the Gui up, but in a different manner. You would need to import the workbench, create the object, and set the properties as you do with Revolve. I don't know the exact syntax for this without studying more the curves source code.
kunal_shgkr
Posts: 9
Joined: Tue Feb 05, 2019 6:40 am

Re: creating revolve using python

Post by kunal_shgkr »

TheMarkster wrote: Sun Nov 21, 2021 5:20 pm When setting a property of type Link you must specify a document object. For example, Revolve property "Source" is a Link type property. If you have an object named Shape to use as the property:

Code: Select all

Revolve.Source = FreeCAD.ActiveDocument.getObject("Shape")
Axis Link is not a Link type property. It is a LinkSub type property. (App::PropertyLinkSub). Setting this property requires the document object and the subobjects to use from that document object. A subobject can be a Face, Vertex, Edge, or Wire. These must be passed to the object as a tuple, the first element being the document object, the 2nd element being a list of strings naming the subobjects to use. For example, if you want to use Edge1 of Shape as the Axis Link for the Revolve property:

Code: Select all

revolve = FreeCAD.ActiveDocument.getObject("Revolve")
revolve.AxisLink = (FreeCAD.ActiveDocument.getObject("Shape"),["Edge1"])
For the example file you posted here you would want to use "Shape001" (the line/edge) as the profile to revolve and Shape.Edge1 as the Axis Link. You also need to set the angle to -90 degrees. You might be using radians currently. This can be converted in python as math.degrees(rad) where rad is the angle in radians.

Code: Select all

doc = FreeCAD.ActiveDocument
revolve = doc.addObject("Part::FeatureRevolve","Revolve2")
revolve.Source = doc.getObject("Shape001")
revolve.Angle = -90
revolve.AxisLink = (doc.getObject("Shape"),["Edge1"])
doc.recompute()
There is a tool to do this in the Gui in Curves Workbench called BlendSurface. Install Curves from the tools menu -> addon manager -> workbenches -> Curves. Chris is the author of that workbench. Usage: Select both faces and both edges to join, run the tool:

Faces and Edges selected:

Snip macro screenshot-e785dc.png

After running tool:

Snip macro screenshot-4469e8.png

One way to script this for your attached file (requires workbench be installed):

Code: Select all

doc = FreeCAD.ActiveDocument
revolve = doc.addObject("Part::Revolution","Revolve2")
revolve.Source = doc.getObject("Shape001")
revolve.Angle = -90
revolve.AxisLink = (doc.getObject("Shape"),["Edge1"])
doc.recompute()

Gui.Selection.clearSelection()
Gui.Selection.addSelection(doc.Name,'Shape002','Face1')
Gui.Selection.addSelection(doc.Name,'Shape002','Edge6')
Gui.Selection.addSelection(doc.Name,'Shape001','Edge1')
Gui.Selection.addSelection(doc.Name,'Shape003','Face1')
Gui.activateWorkbench('CurvesWorkbench')
Gui.runCommand('Curves_BlendSurf2',0)
It might also be possible to run this without the Gui up, but in a different manner. You would need to import the workbench, create the object, and set the properties as you do with Revolve. I don't know the exact syntax for this without studying more the curves source code.
Thank you so much. This works when I hard code the shapes. I will try make the code work without hard coding the shapes. Thanks again.
Post Reply