How to extract subelement of a solid?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
How to extract subelement of a solid?
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? My idea is to use FeaturePython to create a cube, then I can display its six faces.
hi,
Can App::FeaturePython or Part::FeaturePython create subobjects or subelement like pictures? 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
Re: Can FeaturePython create subobjects or subelement?
This should be the solution :
https://forum.freecadweb.org/viewtopic. ... 74#p472474
https://forum.freecadweb.org/viewtopic. ... 74#p472474
Re: Can FeaturePython create subobjects or subelement?
this is very helpful for me.Chris_G wrote: ↑Thu Aug 11, 2022 9:51 am This should be the solution :
https://forum.freecadweb.org/viewtopic. ... 74#p472474
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
Re: Can FeaturePython create subobjects or subelement?
probably iterating over:xianyu wrote: ↑Thu Aug 18, 2022 10:53 amthis is very helpful for me.Chris_G wrote: ↑Thu Aug 11, 2022 9:51 am This should be the solution :
https://forum.freecadweb.org/viewtopic. ... 74#p472474
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
Code: Select all
obj.Shape.Faces
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}")
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Can FeaturePython create subobjects or subelement?
I'm sorry, I don't quite understand the meaning of this code, do you have a specific explanation.onekk wrote: ↑Thu Aug 18, 2022 11:43 amCode: Select all
for f_idx, face in enumerate(shape_do.Shape.Faces): Part.show(face, f"face_{f_idx}")
Freecad novice, A Python enthusiast
Re: Can FeaturePython create subobjects or subelement?
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
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
Code: Select all
for f_idx, face in enumerate(shape_do.Shape.Faces):
Code: Select all
Part.show(face, f"face_{f_idx}")
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Can FeaturePython create subobjects or subelement?
@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.onekk wrote: ↑Mon Aug 22, 2022 9:08 am I'm using the underlying TopoShape and I iterate over his Faces property using: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
for f_idx, face in enumerate(shape_do.Shape.Faces):
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}"))
Code: Select all
FeaturePython' object has no attribute 'addObject'
Freecad novice, A Python enthusiast
Re: Can FeaturePython create subobjects or subelement?
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.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'
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Can FeaturePython create subobjects or subelement?
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
Re: Can FeaturePython create subobjects or subelement?
'Part.show()' returns a 'Part::Feature' object, which is a basic non-parametric object holding a single shape.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
You can check an object type with
Code: Select all
OBJECT.TypeId
![Wink ;)](./images/smilies/icon_e_wink.gif)
'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.