Loft in a Python script

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Loft in a Python script

Post by vrytar »

I am designing an aircraft fuselage shown below. I created the cross sections as wires in a Python script and showed them using:
for i in range(max):
Part.show(w)

They appeared on the screen and got new names in the Tree view: Shape, Shape001, ... Shape014. I was then able to use the Loft icon, select all the cross sections and create a solid. The Python console listed the following commands:
App.getDocument('Unnamed').addObject('Part::Loft','Loft')
App.getDocument('Unnamed').ActiveObject.Sections=[App.getDocument('Unnamed').Shape001, App.getDocument('Unnamed').Shape.....
App.getDocument('Unnamed').ActiveObject.Solid=True
App.getDocument('Unnamed').ActiveObject.Ruled=False

This is good, but I want to do all of this in the Python script. How can I reference the shapes from there? I understand they are document objects, but I couldn't find a way to interact with them from Python directly. For one thing I'd like to give them better names than ShapeXXX. For another, I wish to define the loft sections using a list, as the number of sections may change any time. Ideally, I'd like to have something like
App.getDocument('Unnamed').addObject('Part::Loft','fuselage')
fuselage.Sections=[w]

I am using FreeCAD 0.13 on Windows7. Can anybody help?

Image
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Re: Loft in a Python script

Post by vrytar »

Well, looks like I got lucky with the following.

for i in range(max):
Part.show(w)
gogo = App.ActiveDocument.ActiveObject

fuselage = App.ActiveDocument.addObject('Part::Loft','Loft')
App.ActiveDocument.ActiveObject.Sections=gogo
App.ActiveDocument.ActiveObject.Solid=True
App.ActiveDocument.ActiveObject.Ruled=False
App.ActiveDocument.recompute()

Still, if there is a better or a more standard way, please post it here.
wmayer
Founder
Posts: 19963
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Loft in a Python script

Post by wmayer »

If you do NOT need each profile of your loft as own document object and if you don't need the loft to be parametric you can go a much more direct way:

Code: Select all

loft = Part.makeLoft(w,True, False)
Part.show(loft)
This is good, but I want to do all of this in the Python script. How can I reference the shapes from there? I understand they are document objects, but I couldn't find a way to interact with them from Python directly. For one thing I'd like to give them better names than ShapeXXX. For another, I wish to define the loft sections using a list, as the number of sections may change any time. Ideally, I'd like to have something like
The Part.show() command is just a shortcut for creating a part object and assigning the shape to it. But you have no control over the naming. So, you can write:

Code: Select all

w=...
max=...

gogo=[]
for i in range(max):
    obj=App.ActiveDocument.addObject("Part::Feature","Give me a better name")
    obj.Shape=w[i]
    gogo.append(obj)

fuselage = App.ActiveDocument.addObject('Part::Loft','Loft')
fuselage.Sections=gogo
fuselage.Solid=True
fuselage.Ruled=False
App.ActiveDocument.recompute()
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

Re: Loft in a Python script

Post by vrytar »

Oh, thats simple! I knew there must be an easy way. Thank you for putting me on the right track.

I can handle the math and algorithms, but I am still somewhat baffled by the different types of objects used here.
Post Reply