Check if an object is a solid

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Check if an object is a solid

Post by usbhub »

Hello,
for my macro I have to check, whether an object is a solid or not.
To example:
Body (PartDesign): solid
Sketch: no solid
Plane: no solid
Helix: no solid
Cube: solid
Loft: solid
Loft without checking creating volume: no solid

I hope, you understand, what I mean with solid.

Thanks in advance!
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Check if an object is a solid

Post by TheMarkster »

Code: Select all

if App.ActiveDocument.Helix.Shape.Solids:
    print("is a solid")
User avatar
wandererfan
Veteran
Posts: 6321
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Check if an object is a solid

Post by wandererfan »

usbhub wrote: Fri Sep 13, 2019 9:35 pm for my macro I have to check, whether an object is a solid or not.
A shape can contain solids without being a Solid.

Code: Select all

>>> f = App.ActiveDocument.Fusion
>>> fs = f.Shape
>>> fs.ShapeType
'Compound'
>>> fs.Solids
[<Solid object at 0x564a0c9ec060>]
>>> 
If you want to know if the shape contains solids, use @TheMarkster's method.

If you need to know that it is a Solid in the topological sense, use ShapeType.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Check if an object is a solid

Post by usbhub »

TheMarkster wrote: Fri Sep 13, 2019 10:06 pm

Code: Select all

if App.ActiveDocument.Helix.Shape.Solids:
    print("is a solid")
Thank you, but I use this already in my macro, but for a body in a model it doesn't works. It's a legal body without errors or something else. The file is on my laptop, but I will try it on my computer and report.

usbhub
User avatar
wandererfan
Veteran
Posts: 6321
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Check if an object is a solid

Post by wandererfan »

usbhub wrote: Sun Sep 15, 2019 5:10 pm Thank you, but I use this already in my macro, but for a body in a model it doesn't works. It's a legal body without errors or something else. The file is on my laptop, but I will try it on my computer and report.
A Body is a DocumentObject, so it can never be a "Solid". Only Shapes can be Solids.

You could use "App.ActiveDocument.myBody.Shape.Solids".
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Check if an object is a solid

Post by vocx »

usbhub wrote: Sun Sep 15, 2019 5:10 pm but for a body in a model it doesn't works. It's a legal body without errors or something else.
As wandererfan says, a PartDesign Body isn't a "solid" by itself, given that it can contain an Origin, a coordinate system, datum planes, lines, points, etc.

A PartDesign Body is a kind of container. Perhaps what you could do is iterate over its elements, and check that its "tip" is a solid, and then assume that the entire Body is a solid as well.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Check if an object is a solid

Post by usbhub »

Thank you all.
I solved the problem with another way. The check, if the object is a solid was for check, if it's possible to convert to mesh. Now I put the creation of the mesh in a try block and if an error occurs, I know, I can't create a mesh from the shape.
chrisb
Veteran
Posts: 54213
Joined: Tue Mar 17, 2015 9:14 am

Re: Check if an object is a solid

Post by chrisb »

I'm not sure what you are aiming at, but if it is supposed to be a general tool it should be able to handle bodies as well. At least from the GUI a body can be meshed.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Check if an object is a solid

Post by openBrain »

usbhub wrote: Tue Sep 17, 2019 4:07 pm I solved the problem with another way. The check, if the object is a solid was for check, if it's possible to convert to mesh.
I'm not able to check ATM but I'd bet that a shell can also be converted to mesh. ;)
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Check if an object is a solid

Post by usbhub »

openBrain wrote: Tue Sep 17, 2019 5:23 pm
usbhub wrote: Tue Sep 17, 2019 4:07 pm I solved the problem with another way. The check, if the object is a solid was for check, if it's possible to convert to mesh.
I'm not able to check ATM but I'd bet that a shell can also be converted to mesh. ;)
Oh yes, I think that's right.But that's not too bad. It is only for converting objects to a mesh for rendering / raytracing.

In retrospect, my question was not quite correct, but many thanks to everyone.

usbhub

PS: What is ATM?
Post Reply