copy a group and all it's objects

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
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

copy a group and all it's objects

Post by bernd »

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
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: copy a group and all it's objects

Post by wmayer »

Use the method Document.copyObject(object, with_dependencies=False) and set the 2nd parameter to True.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: copy a group and all it's objects

Post by bernd »

:D
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

Re: copy a group and all it's objects

Post by Cyril »

Sorry to dig out this topic but... Is this answer still valid?
Documentation says so but when I try :

Code: Select all

doc = FreeCAD.ActiveDocument
doc.copyObject(doc.some_object_name, with_dependencies=False)
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.)
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: copy a group and all it's objects

Post by vocx »

Cyril wrote: Tue Sep 17, 2019 10:43 am ...
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.
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.
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

Re: copy a group and all it's objects

Post by Cyril »

Thanks vocx. I found this impletation before and also tried :

Code: Select all

doc = FreeCAD.ActiveDocument
doc.copyObject(doc.some_object_name, recursive=True)
As expected I still get an error as it doesn't accept any keyword argument.

However I tried

Code: Select all

doc.copyObject(doc.some_object_name, True)
with walls and it works as expected.

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.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: copy a group and all it's objects

Post by wmayer »

I get an error saying that copyObject accepts no keyword argument.
As the error message says keywords are not accepted. So, either do

Code: Select all

doc.copyObject(doc.some_object_name, True)
to copy objects recursively or

Code: Select all

doc.copyObject(doc.some_object_name)
to copy only the passed object
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

Re: copy a group and all it's objects

Post by Cyril »

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) :

Code: Select all

obj = make_my_custom_feature_python("Face")
obj.addProperty("App::PropertyLink", "OriginalBoundary", category_name)
face.OriginalBoundary = face
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.
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
Post Reply