Set label of Solid

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
drmacro
Veteran
Posts: 8867
Joined: Sun Mar 02, 2014 4:35 pm

Set label of Solid

Post by drmacro »

It's always a riddle with the internals, it it a shape, or shell, or solid... :roll:

But, the real question here is in the code below the 'cube' shows up in the tree view with a label 'Shape'

But, the object 'cube' in the code doesn't have a Label property. And, of course, cube.Label fails.

Code: Select all

import Part

# vector 1
v1 = FreeCAD.Vector(0,0,0)
# vector 2
v2 = FreeCAD.Vector(1,0,0)

# edge between v1 and v2
edge = Part.makeLine(v1,v2)

# Face
face = edge.extrude(FreeCAD.Vector(0,1,0))

# Cube
cube = face.extrude(FreeCAD.Vector(0,0,1))
cube.Label='anothercube'
Part.show(cube)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
drmacro
Veteran
Posts: 8867
Joined: Sun Mar 02, 2014 4:35 pm

Re: Set label of Solid

Post by drmacro »

Never mind, got it. :oops:

Code: Select all

import Part

# vector 1
v1 = FreeCAD.Vector(0,0,0)
# vector 2
v2 = FreeCAD.Vector(1,0,0)

# edge between v1 and v2
edge = Part.makeLine(v1,v2)

# Face
face = edge.extrude(FreeCAD.Vector(0,1,0))

# Cube
cube = face.extrude(FreeCAD.Vector(0,0,1))
thing=Part.show(cube)
thing.Label='MyLabel'
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Set label of Solid

Post by TheMarkster »

Just be aware Part.show() did not return the object until fairly recently and will not do so in 0.19. An alternative is:

Code: Select all

thing = doc.addObject("Part::Feature","Shape")
thing.Shape = cube
thing.Label = "MyLabel" #or name the object with addObject()
drmacro
Veteran
Posts: 8867
Joined: Sun Mar 02, 2014 4:35 pm

Re: Set label of Solid

Post by drmacro »

TheMarkster wrote: Sun May 08, 2022 3:12 pm Just be aware Part.show() did not return the object until fairly recently and will not do so in 0.19. An alternative is:

Code: Select all

thing = doc.addObject("Part::Feature","Shape")
thing.Shape = cube
thing.Label = "MyLabel" #or name the object with addObject()
Yes, I did indeed see that. Thanks.

No problem for me in 0.20. But the guy I was talking with was using 0.19.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Set label of Solid

Post by onekk »

Hello in the first try object cube was a TopoShape and only DocumentObjects have a Name and a Label property.

So until created or "promoted" to a TopoShape with one of the methods shown in other posts object is simply a "shape". has no "Name" and "Label" properties.

Only to explain why objects had no "Name" or "Label", mostly for "future readers".

:D

Regards

Carlo D.
Last edited by onekk on Mon May 16, 2022 9:30 am, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Set label of Solid

Post by Roy_043 »

I don't think that is correct. Aren't Shape and TopoShape synonymous?
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Set label of Solid

Post by onekk »

Roy_043 wrote: Mon May 16, 2022 6:45 am I don't think that is correct. Aren't Shape and TopoShape synonymous?
What is not correct?

The key point of my answer is the difference between a TopoShape that is a "solid", but has no Name and Label (and Label2 if applicable) and a DocumentObject, from my post:
only DocumentObjects have a Name and a Label property.
Usually "Shape" property of a DocumentObject is a TopoShape (in FC terms), I put "usually" as I don't know if there are other object that could go in the "Shape" property.

Probably one of the most "not well explained" things about FC is the 3 levels of Objects, Curves (Geometries), TopoShapes and DocumentObjects. (not counting the Geom2D things)

I don't know other CADs, but this seems to me a logic things, but probably to some peoples this is not true, or it is managed differently from other "mainstream" CADs.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Set label of Solid

Post by Roy_043 »

onekk wrote: Mon May 16, 2022 7:02 am What is not correct?
onekk wrote: Mon May 16, 2022 4:55 am So until created or "promoted" to a TopoShape with one of the methods shown object is simply a "shape".
A shape already is a TopoShape.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Set label of Solid

Post by onekk »

Ok see the redundancy, I'm correcting the post.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply