Arch.addComponents example failure

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rishi
Posts: 2
Joined: Tue May 27, 2014 5:09 pm

Arch.addComponents example failure

Post by rishi »

Hello,

I am very new to FreeCAD, so I'm sure I'm making some very basic mistake, but when I try the simple Arch.addComponents scripting example from the doc, I get an error. For this example, I started a new session (FreeCAD 0.14, MacOSX, 64 bit, python version 2.7.5), created a new document, and then copied the example code into the console (line by line):

Code: Select all

import FreeCAD,Arch,Draft,Part
line = Draft.makeWire([FreeCAD.Vector(0,0,0),FreeCAD.Vector(2,2,0)])
wall = Arch.makeWall(line)
box = Part.makeBox(1,1,1)
Arch.addComponents(box,wall)
In response I get the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/FreeCAD.app/Contents/Mod/Arch/ArchCommands.py", line 90, in addComponents
if DraftGeomUtils.isValidPath(o.Shape) and (hostType == "Structure"):
AttributeError: 'Part.TopoShape' object has no attribute 'Shape'
I have tried various other flavors of this (add width and depth to the wall, making sure everything executes and shows up in the gui first), but get the same response. If I make the wall and the box visible in the gui by double clicking on the wall and hitting the OK button, then calling Part.show(box), I can run the addComponents method manually through the gui and it works. Is there another step that needs to be taken in the script to turn the box into something with a Shape attribute before adding it to the wall?

Thank you for your time!
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Arch.addComponents example failure

Post by yorik »

The problem is this: when you create the Part.Box, you are only creating a shape, not a document object. And the Arch.addComponent needs two (or more) objects.

You must see FreeCAD document objects, the ones you see in the 3D view and in the tree view, as "containers". They contain geometry (or not), that can be created, for example, from the Part workbench, or the Mesh workbench.

So when you create your box shape, you must create an object in the document, to "hold" it. There are 2 ways to do that:

Code: Select all

Part.show(box)
or

Code: Select all

myobj = FreeCAD.ActiveDocument.addObject("Part::Feature","MyObject")
myobj.Shape = box
The fist option is more simple, but doesn't let you specify an object name. The second is more complete. When creating an object of type "Part::Feature", it is an object already formatted to receive a shape from the Part module, and it already comes with a "Shape" property, and all you need to do is to put your box in it.

After that, this should work:

Code: Select all

Arch.addComponents(myobj,wall)
rishi
Posts: 2
Joined: Tue May 27, 2014 5:09 pm

Re: Arch.addComponents example failure

Post by rishi »

Great explanation, thanks!
Post Reply