Naming shapes during creation in a script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
steve.white
Posts: 15
Joined: Tue May 06, 2014 3:58 pm

Naming shapes during creation in a script

Post by steve.white »

When parts are created in a script their Viewprovider base classes are named "Shape", "Shape001", "Shape002" as are their labels in the Gui tree view.

mypart = Part.makeBox(580E-6, 290E-6, 3E-6, Base.Vector(0,0,0))

The labels in the Gui tree can be renamed

FreeCADGui.ActiveDocument.Shape.Object.Label = "metthk_Shield"

However, the ViewProvider base classes retains their original names "Shape", "Shape001", "Shape002" and I always have to refer to these names when changing colors, transparency, etc

Is there a method of setting these names when creating the parts?

For my project I wish to handle a number of parts created in a script and then output their individual vertices to an FEA solver package. Each mesh of each object/shape get handled separately in the solver (multi-object capacitance extraction). It would be easier to have my own recognisable names in the ViewProvider base class naming for scripting purposes.

Steve
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Naming shapes during creation in a script

Post by shoogen »

in oder to set a name of an object you need to create it using the addObject Method instead of using Part.show(shape)

Code: Select all

obj=App.ActiveDocument.addObject('Part::Feature','mybox')
obj.Shape=Part.makeBox(1.0,2.0,3.0)
User avatar
NormandC
Veteran
Posts: 18589
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: Naming shapes during creation in a script

Post by NormandC »

I moved this topic from the "Help on using FreeCAD" forum to Python scripting & macros where questions about scripting should go.
steve.white
Posts: 15
Joined: Tue May 06, 2014 3:58 pm

Re: Naming shapes during creation in a script

Post by steve.white »

Thank you shoogen. I am most grateful for that and I should have remembered that addObject allows naming.

I condensed the line structure and also added name to new modified parts and put this script here for others to gain insight. Your suggestion now also avoids the need to rename the GUI tree items as they will automatically be given names in addObject calls. I distinguish these names from objects with prefix letter 'P'.

The script generates named shapes of conductors and dielectrics that can be output in an extended script into surfaces (meshes) together with other information in a form that can be processed by an external solver. It is now easy to see which surface shape I am editing.
Steve

Code: Select all

import FreeCAD, Part, FreeCADGui
from FreeCAD import Base
from FreeCAD import Gui

FreeCAD.newDocument()

# Adding new named shapes from standard part creation
metthk 			= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk').Shape  = Part.makeBox(280E-6, 290E-6, 3E-6, Base.Vector(0,0,0))
metthk_Pad 		= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk_Pad').Shape 	= Part.makeBox(60E-6, 60E-6, 3E-6, Base.Vector(100E-6,60E-6,0))
metthk_SiO_out 	= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk_SiO_out').Shape = Part.makeBox(70E-6, 70E-6, 3E-6, Base.Vector(95E-6, 55E-6,0))
metthk_SiO_in 	= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk_SiO_in').Shape 	= Part.makeBox(60E-6, 60E-6, 3E-6, Base.Vector(100E-6,60E-6,0))

# Creating new named parts from booleans between parts
metthk_SiO 		= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk_SiO').Shape  	= metthk_SiO_out.cut(metthk_SiO_in)
metthk_Shield 	= FreeCAD.ActiveDocument.addObject('Part::Feature','Pmetthk_Shield').Shape  = metthk.cut(metthk_SiO_out)

# Change transparency of shapes
FreeCADGui.ActiveDocument.Pmetthk_Shield.Transparency = 50
FreeCADGui.ActiveDocument.Pmetthk_Pad.Transparency = 50
FreeCADGui.ActiveDocument.Pmetthk_SiO.Transparency = 50

# Change colors of shapes
FreeCADGui.ActiveDocument.getObject("Pmetthk_Shield").ShapeColor = (0.1,0.9,0.1)
FreeCADGui.ActiveDocument.Pmetthk_Pad.ShapeColor = (0.9,0.1,0.1)
FreeCADGui.ActiveDocument.Pmetthk_SiO.ShapeColor = (0.1,0.1,0.9)

# Display and arrange the objects in document
FreeCAD.ActiveDocument.recompute()
FreeCAD.ActiveDocument.Label = "ElectrodeParasitics" # Give the project name
FreeCAD.ActiveDocument.recompute()
FreeCADGui.SendMsgToActiveView("ViewFit")
FreeCADGui.ActiveDocument.ActiveView.viewAxometric()

print("T01: end of script")
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Naming shapes during creation in a script

Post by ickby »

Hello,

just as note and in case it may be useful for your purposes, instead of dump objects with a box shape you can add parametric boxes which are easier modified later:

Code: Select all

box = App.ActiveDocument.addObject("Part::Box","Box")
box.Height = 15;
User avatar
teobo
Posts: 410
Joined: Fri Feb 21, 2014 11:23 am

Re: Naming shapes during creation in a script

Post by teobo »

Hello,
can understand what you miss.
You ask yourself how to get grip of the FreeCAD-object after having created it from a Shape. (Pardon I lack the exact terms, once again.)
So what I do: I to cycle over that list to find mine. In the snipped I take the last added.
Part.show(myshape)
CP0=App.ActiveDocument.Objects[len(App.ActiveDocument.Objects)-1]
#thats how I did it sofar :P

There is as well a function getObjectbyLabel or the like.
App.ActiveDocument.getObjectsByLabel(
Up to now I did not encounter real problems of finding back document-shapes. Difficulties I found in finding faces and edges.

So thats my tow pence.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Naming shapes during creation in a script

Post by shoogen »

guys, don't clutter up your scripts with endless boiler plate code.
don't use "App" unless you define it. I would prefer to use "FreeCAD" as it works also when the script is invoked from a bare python interpreter. (after "import FreeCAD", of course)
I would use all this find operations only for objects that you don't create in the script. For everything else, you could keep a reference. "doc" is far more readable then "App.ActiveDocument"

Code: Select all

doc= FreeCAD.ActiveDocument or FreeCAD.newDocument()
instead of

Code: Select all

FreeCADGui.ActiveDocument.Pmetthk_Shield.Transparency = 50
you could write

Code: Select all

metthk_Shield.ViewObject.Transparency = 50
if you kept the reference to the object.
Finding Objects by name works fine, when you run your script once in an empty Document. But as soon as the names you supply are already taken you run into problems. You end up in silently modifying the wrong objects. And since the names and the labels don't need to match, it might take a while for anyone to realize, what is cause.
The example code in the python console is needs does not make any assumptions about the state of your document. It just prints out the names of the objects involved.
But in really good scripts you should not need to find objects by name or label. If you created them you keep a reference, if not you use the selection or an observer.
Just my two cents
steve.white
Posts: 15
Joined: Tue May 06, 2014 3:58 pm

Re: Naming shapes during creation in a script

Post by steve.white »

lots of useful advice. Thanks guys.
Post Reply