How to judge the type of model?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

How to judge the type of model?

Post by freecadlzh »

In Freecad, In part workbench ,use cube tools added a cube model named "Box", Now get the model and get the type using code:

Code: Select all

>>> ooo =FreeCAD.ActiveDocument.getObject("Box")
>>> type(ooo)
<class 'PrimitivePy'>
Now I wanted to judge the type of the model in python script:

Code: Select all

if type(ooo) == PrimitivePy:
    print("1")
When the script runs goes errors:
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'PrimitivePy' is not defined

So I changed the code as below:

Code: Select all

if type(ooo) == "PrimitivePy":
    print("1")
When script runs, no errors, but "print("1")" not runs.

So my problem is how to judge the type of the Cube model in "if" code?

Need helps , Thanks a lot.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to judge the type of model?

Post by openBrain »

I don't know why it's better to use this rather than ooo.TypeId, but you can do this with :

Code: Select all

if "'PrimitivePy'" in str(type(ooo)):
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to judge the type of model?

Post by freecadlzh »

openBrain wrote: Wed Dec 02, 2020 12:38 pm I don't know why it's better to use this rather than ooo.TypeId, but you can do this with :

Code: Select all

if "'PrimitivePy'" in str(type(ooo)):
Good! It is a way! Follow yours code , it can runs and print("1").

Any other way to code this ?

Because :

Code: Select all

>>> type(1)
<class 'int'>

>>> if type(1) == int:
...     print("1")
... 
1

So is there any other way to code?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to judge the type of model?

Post by openBrain »

freecadlzh wrote: Wed Dec 02, 2020 1:06 pm Any other way to code this ?
What are you trying to code ? Do you want to identify only boxes ? Or any Part primitive ? Or any 3D object ?
Post Reply