How to extract subelement of a solid?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

How to extract subelement of a solid?

Post by xianyu »

01 Sep 2022 Edit: I think I should change the new title, the original title was "Can FeaturePython create subobjects or subelement?"

hi,
Can App::FeaturePython or Part::FeaturePython create subobjects or subelement like pictures?
01.png
01.png (8.18 KiB) Viewed 1199 times
My idea is to use FeaturePython to create a cube, then I can display its six faces.
Last edited by xianyu on Thu Sep 01, 2022 7:37 am, edited 2 times in total.
Freecad novice, A Python enthusiast
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Can FeaturePython create subobjects or subelement?

Post by Chris_G »

xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Can FeaturePython create subobjects or subelement?

Post by xianyu »

Chris_G wrote: Thu Aug 11, 2022 9:51 am This should be the solution :
https://forum.freecadweb.org/viewtopic. ... 74#p472474
this is very helpful for me.
Also I have a question, if I create a cube with "Part::FeaturePython",how do I extract and display its six faces in "App::Part" of combo view
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Can FeaturePython create subobjects or subelement?

Post by onekk »

xianyu wrote: Thu Aug 18, 2022 10:53 am
Chris_G wrote: Thu Aug 11, 2022 9:51 am This should be the solution :
https://forum.freecadweb.org/viewtopic. ... 74#p472474
this is very helpful for me.
Also I have a question, if I create a cube with "Part::FeaturePython",how do I extract and display its six faces in "App::Part" of combo view
probably iterating over:

Code: Select all

obj.Shape.Faces
will do the job:

Code: Select all

shape_do = Part.show(Part.makeBox(10,20,10))
shape_do.ViewObject.Visibility = False

for f_idx, face in enumerate(shape_do.Shape.Faces):
    Part.show(face, f"face_{f_idx}")
This is for a simple cube, but even a Part:FeaturePython must have Shape property, to use in a similar way.

Hope it helps.

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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Can FeaturePython create subobjects or subelement?

Post by xianyu »

onekk wrote: Thu Aug 18, 2022 11:43 am

Code: Select all

for f_idx, face in enumerate(shape_do.Shape.Faces):
    Part.show(face, f"face_{f_idx}")
I'm sorry, I don't quite understand the meaning of this code, do you have a specific explanation.
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Can FeaturePython create subobjects or subelement?

Post by onekk »

xianyu wrote: Mon Aug 22, 2022 8:11 am ...
I'm sorry, I don't quite understand the meaning of this code, do you have a specific explanation.
With this part I've created a parallelepiped object as documentObject (object that appear in the TreeView).

Code: Select all

shape_do = Part.show(Part.makeBox(10,20,10))
shape_do.ViewObject.Visibility = False
It work on 0.20 as Part.show() will return a documentObject this was not true in 0.19 if I remember well.

This can be very handy as you can use the documentObject for further operations, like I've done hiding the cube setting his property Visibility property to False.


Taking his Shape property:

Code: Select all

shape_do.Shape
I'm using the underlying TopoShape and I iterate over his Faces property using:

Code: Select all

for f_idx, face in enumerate(shape_do.Shape.Faces):
This code will take the Faces and output using Python enumerate an index (f_idx) and the corresponding object (face), then with:

Code: Select all

    Part.show(face, f"face_{f_idx}")
It create six new objects named face_0, face_1 and so on using the Python string format f"" method.


Hope to have been more clear now.

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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Can FeaturePython create subobjects or subelement?

Post by xianyu »

onekk wrote: Mon Aug 22, 2022 9:08 am I'm using the underlying TopoShape and I iterate over his Faces property using:

Code: Select all

for f_idx, face in enumerate(shape_do.Shape.Faces):
This code will take the Faces and output using Python enumerate an index (f_idx) and the corresponding object (face), then with:
@onekk Thank you so much, your advice was very helpful!! I think I need a little more time to understand this code.The version I am using is 0.19 and the return value of Part.show() is "None". I changed some of the code.

Code: Select all

fp = FreeCAD.ActiveDocument.addObject('Part::FeaturePython', 'cube')
shape_do = Part.makeBox(10,20,10)
fp.Shape = shape_do
fp.ViewObject.Proxy = 0
for f_idx, face in enumerate(fp.Shape.Faces):
   fp.addObject(Part.show(face, f"face_{f_idx}"))
I encountered this error.But I don't want to use "App::DocumentObjectGroup", is there any other way to put these six faces under "FeaturePython"?

Code: Select all

FeaturePython' object has no attribute 'addObject'
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Can FeaturePython create subobjects or subelement?

Post by onekk »

xianyu wrote: Mon Aug 22, 2022 11:01 am I encountered this error.But I don't want to use "App::DocumentObjectGroup", is there any other way to put these six faces under "FeaturePython"?

Code: Select all

FeaturePython' object has no attribute 'addObject'
Using 0.19 is not an option as Part.show is not returning a DocumentObject so even if the code above will work, you have nothing to pass to the addObject() supposing that a documentObject is needed for this nonexistent function.


No a Part::FeaturePython have only a Shape property, so you have no way to put multiple objects on it.

See maybe if not already done:

https://wiki.freecadweb.org/Scripted_objects

https://wiki.freecadweb.org/Create_a_Fe ... ect_part_I
https://wiki.freecadweb.org/Create_a_Fe ... ct_part_II


You could think of making a Property that hold a list of faces and a "whole solid" entry that will modify the Shape returned to show only the desired face or the whole solid, but I see no use of such a thing, as a changing shape could create many problems if used.

Probably using some more advanced techniques, you could show only the face or highlight it, but this is beyond my knowledge as it involve to modify the ViewObject part that is manage by Coin3D, and I'm not very skilled in such things, I've only scratched the surface.


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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Can FeaturePython create subobjects or subelement?

Post by xianyu »

onekk wrote: Mon Aug 22, 2022 11:10 am Using 0.19 is not an option as Part.show is not returning a DocumentObject so even if the code above will work, you have nothing to pass to the addObject() supposing that a documentObject is needed for this nonexistent function.
hello i am back
I have installed version 0.20 these days, following your advice, I have a little success now
What type of DocumentObject does "Part.show()" return, is it "Part::FeaturePython"? Can I use "ViewObject" if I don't use "show()"?
I know that different DocumentObject have different properties, like "FeaturePython" doesn't have "addObject", but "App::Part" has "addObject". How can I check the type
I'm sorry. I'm a little stupid. I ask too many questions

edit: Also is the DocumentObject returned by "Part.show()" stored in what form (like a list, tuple?) and how do I find it (I want to make some modifications, deletions, in a python script)
Last edited by xianyu on Fri Aug 26, 2022 6:32 am, edited 2 times in total.
Freecad novice, A Python enthusiast
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Can FeaturePython create subobjects or subelement?

Post by openBrain »

xianyu wrote: Fri Aug 26, 2022 5:58 am What type of DocumentObject does "Part.show()" return, is it "Part::FeaturePython"? Can I use "ViewObject" if I don't use "show()"? I know that different DocumentObject have different properties, like "FeaturePython" doesn't have "addObject", but "App::Part" has "addObject". How can I check the type
'Part.show()' returns a 'Part::Feature' object, which is a basic non-parametric object holding a single shape.
You can check an object type with

Code: Select all

OBJECT.TypeId
Actually, 'Part.show()' automates a 2-step process, adding a 'Part::Feature' object to the active document, then assigning to it the shape passed as argument. This is just how we used to do it in 0.19 when 'show()' didn't return the created object. ;)

'ViewObject' property of an object is the reference to the the GUI representation of an object (literally to its ViewProvider).
It is available on any object, but only in GUI mode. So if you're script has to work also in CLI mode, you should use it carefully.

'Part::FeaturePython' isn't a container (which doesn't state that it can't have childs) while a 'App::Part' is. Hence only the latter has the 'addObject' method.
Post Reply