Making reference to an array of objects

Need help, or want to share a macro? Post here!
Post Reply
jcdmelo
Posts: 6
Joined: Mon Jul 15, 2019 8:08 pm

Making reference to an array of objects

Post by jcdmelo » Mon Jan 13, 2020 2:04 pm

Hi, everyone,

Thanks for an amazing piece of software! And to the many people who contributed to it!

I am a sort of novice in both FreeCAD and Python, but I have been able to solve my programming chores by experimenting and consulting on this Forum. Thanks again!

But now I am stuck in a situation for which I think I could benefit from this forum and its experts.

I am doing an array of extrusions ('ExtrusionArray') on a body ('Body') attached to a part ('Part'), using an array of sketches ('SketchArray'). These sketches were built from copies of the same sketch, and received proper names ('SketchNameArray'), so that I can refer to them using their internal names.

My code is as follows:

Code: Select all

#... (sketches creation and stuff)
for i in range (0, NumExtrusions):
   App.activeDocument().addObject('PartDesign::Body', SketchNameArray[i])
   App.activeDocument().Part.addObject(App.activeDocument().getObject(SketchNameArray[i]))
   App.activeDocument().MYPROBLEM.newObject("PartDesign::Pad", ExtrusionArray[i])
#...
   Gui.activeDocument().hide(SketchNameArray[i])
In 'MYPROBLEM' above resides, well, my problem. How do I refer to a body object, named 'SketchNameArray' which was added to 'Part' in the first line of the for loop?

If one of the sketches is named 'SketchForBody1', then line 1 reads as:

Code: Select all

	   App.activeDocument().addObject('PartDesign::Body', 'SketchForBody1')			# Fine!
Then, in the second line, I use the same name to retrieve the body object by using 'App.activeDocument().getObject(SketchNameArray)' which reads as:

Code: Select all

	App.activeDocument().Part.addObject((App.activeDocument().SketchForBody1)		# Fine!
Now, in the third line, the problem: I need to refer to the same body object (SketchForBody1), but not as an argument, but as an object. How to do that?

I tried:

Code: Select all

	1. App.activeDocument().getObject(SketchNameArray[i]).newObject("PartDesign::Pad", ExtrusionArray[i]) 
	2. App.activeDocument().App.activeDocument().getObject(SketchNameArray[i]).newObject("PartDesign::Pad", ...
	3. obj = App.activeDocument().addObject('PartDesign::Body', SketchNameArray[i]), followed by
	   App.activeDocument().obj.newObject("PartDesign::Pad", ExtrusionArray[i])
to no avail.

This problem will show up again when I try to refer to one of the items from 'ExtrusionArray'.

Thanks for any suggestions and pointers to documentation or a site.

Julio
User avatar
microelly2
Posts: 4626
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Making reference to an array of objects

Post by microelly2 » Mon Jan 13, 2020 2:16 pm

if you create two objects with the same name they will have different names

a= App.activeDocument().addObject('PartDesign::Body', 'aname')
b= App.activeDocument().addObject('PartDesign::Body', 'aname')

a.Name is 'aname'
but
b.Name is 'aname001'

To find back to the object you have to store the names of the created objects

yourlistofnames=[a.Name,b.Name]
and tan you get them
App.activeDocument().getObject[yourlistofnames[0]] --> a
App.activeDocument().getObject[yourlistofnames[1]] --> b
openBrain
Posts: 3406
Joined: Fri Nov 09, 2018 5:38 pm

Re: Making reference to an array of objects

Post by openBrain » Mon Jan 13, 2020 2:27 pm

Just set it in a variable (eg called 'myBody'). ;) You can do it directly at creation time :

Code: Select all

myBody= App.activeDocument().addObject('PartDesign::Body', SketchNameArray[i])
Or recover it later on the same way with the get function. ;)
jcdmelo
Posts: 6
Joined: Mon Jul 15, 2019 8:08 pm

Re: Making reference to an array of objects

Post by jcdmelo » Mon Jan 13, 2020 2:52 pm

Thanks to both microelly2 and openBrain,

Following your hints, I was able to solve MYPROBLEM (not a problem at all, really!)

My code now reads:

Code: Select all

... (sketchs creation and stuff)
for i in range (0, NumExtrusions):
   Body = App.activeDocument().addObject('PartDesign::Body', SketchNameArray[i])
   App.activeDocument().Part.addObject(Body)
   Extrusion = Body.newObject("PartDesign::Pad", ExtrusionArray[i])
...
   Gui.activeDocument().hide(SketchNameArray[i])
Simple enough, and it works!

Thanks again,
Julio
vocx
Posts: 3338
Joined: Thu Oct 18, 2018 9:18 pm

Re: Making reference to an array of objects

Post by vocx » Mon Jan 13, 2020 6:53 pm

jcdmelo wrote:
Mon Jan 13, 2020 2:52 pm
Simple enough, and it works!
...
Yes, it's a simple as storing the object in a variable and using it later.

Also see the Part_Feature#Scripting to understand the naming.

By the way, I got confused by you naming SketchNameArray. It sounds like it's a Sketch and not a Body.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
Post Reply

Who is online

Users browsing this forum: mnesarco and 2 guests