Is there a smart way to copy a group and all it's child objects? Best if it would work on nested groups and their childs too. Something like the GUI tool "Edit --> Duplicate selection" just in Python.
bernd
copy a group and all it's objects
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: copy a group and all it's objects
Use the method Document.copyObject(object, with_dependencies=False) and set the 2nd parameter to True.
Re: copy a group and all it's objects
Sorry to dig out this topic but... Is this answer still valid?
Documentation says so but when I try :
I get an error saying that copyObject accepts no keyword argument. If I place True as second argument, no error but elements under copied group are the same not a copy.
(I'll add FreeCAD commit later but it is about a week old And if I just place. If it is supposed to still work. I'll update to last commit before.)
Documentation says so but when I try :
Code: Select all
doc = FreeCAD.ActiveDocument
doc.copyObject(doc.some_object_name, with_dependencies=False)
(I'll add FreeCAD commit later but it is about a week old And if I just place. If it is supposed to still work. I'll update to last commit before.)
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
Re: copy a group and all it's objects
Here's the implementation in C++, of std::vector<DocumentObject*> Document::copyObject(). The Python wrapper just calls this.
https://github.com/FreeCAD/FreeCAD/blob ... .cpp#L3968
I'm not sure if it's actually meant to duplicate the "dependencies", or just place them under the new copied object.
https://github.com/FreeCAD/FreeCAD/blob ... ent.h#L265
Code: Select all
/** Copy objects from another document to this document
*
* @param recursive: if true, then all objects this object depends on are
* copied as well. By default \a recursive is false.
*
* @return Returns the list of objects copied.
*/
std::vector<DocumentObject*> copyObject(
const std::vector<DocumentObject*> &objs, bool recursive=false);
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: copy a group and all it's objects
Thanks vocx. I found this impletation before and also tried :
As expected I still get an error as it doesn't accept any keyword argument.
However I tried with walls and it works as expected.
My current conclusions :
Code: Select all
doc = FreeCAD.ActiveDocument
doc.copyObject(doc.some_object_name, recursive=True)
However I tried
Code: Select all
doc.copyObject(doc.some_object_name, True)
My current conclusions :
- I have an issue in my custom "Part::FeaturePython" object
- copyObject need a documentation little fix or an implementation little fix to correspond to documentation.
Code: Select all
OS: Manjaro Linux (GNOME/gnome)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19 (Git)
Build type: Release
Branch: master
Hash: f4bc889b4d69ec35dfcc52e4eec1a5a5f97d8140
Python version: 3.7.4
Qt version: 5.13.0
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/Switzerland (fr_CH)
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
Re: copy a group and all it's objects
As the error message says keywords are not accepted. So, either doI get an error saying that copyObject accepts no keyword argument.
Code: Select all
doc.copyObject(doc.some_object_name, True)
Code: Select all
doc.copyObject(doc.some_object_name)
Re: copy a group and all it's objects
I found what was making this acting wrong in my custom "Part::FeaturePython". I added a PropertyLink and linked it to itself (It might sounds silly but the goal was to keep a reference to original object in the copy) :
This was making copyObject not acting as expected.
I don't know if it should considered only as a misuse or something which can be improved (bug ?). I noticed that you cannot link an object to itself through UI which tends to make me think more of a misuse.
Code: Select all
obj = make_my_custom_feature_python("Face")
obj.addProperty("App::PropertyLink", "OriginalBoundary", category_name)
face.OriginalBoundary = face
I don't know if it should considered only as a misuse or something which can be improved (bug ?). I noticed that you cannot link an object to itself through UI which tends to make me think more of a misuse.
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.