Naming shapes during creation in a script
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 15
- Joined: Tue May 06, 2014 3:58 pm
Naming shapes during creation in a script
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
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
Re: Naming shapes during creation in a script
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)
Re: Naming shapes during creation in a script
I moved this topic from the "Help on using FreeCAD" forum to Python scripting & macros where questions about scripting should go.
-
- Posts: 15
- Joined: Tue May 06, 2014 3:58 pm
Re: Naming shapes during creation in a script
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
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")
Re: Naming shapes during creation in a script
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:
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;
Re: Naming shapes during creation in a script
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
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.
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
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.
Re: Naming shapes during creation in a script
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"
instead of
you could write
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
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()
Code: Select all
FreeCADGui.ActiveDocument.Pmetthk_Shield.Transparency = 50
Code: Select all
metthk_Shield.ViewObject.Transparency = 50
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
-
- Posts: 15
- Joined: Tue May 06, 2014 3:58 pm
Re: Naming shapes during creation in a script
lots of useful advice. Thanks guys.