How to deep copy with python script?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rentlau_64
Posts: 181
Joined: Tue Oct 21, 2014 9:47 am
Location: Pau (France)

How to deep copy with python script?

Post by rentlau_64 »

Hi all,

I would like to know how to realise a deep copy with a python script like we can do with Copy menu when we select one linked object?
Starting point is:
3Dview03.jpg
3Dview03.jpg (22.56 KiB) Viewed 3819 times
Ending point is: Now all children and parents are copied too
3Dview04.jpg
3Dview04.jpg (21.5 KiB) Viewed 3819 times
I know how to select all the parents and the children of an object

Code: Select all

    for obj in sel:
        for child in obj.OutList:
            FreeCADGui.Selection.addSelection(child)
            for parent in obj.InList:
                FreeCADGui.Selection.addSelection(parent)
                for child in parent.OutList:
                    FreeCADGui.Selection.addSelection(child)
 
but do not know how to recreate the hierarchy after copy...
Thank you for your help.

Rentlau_64
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to deep copy with python script?

Post by wmayer »

Make sure you have a recent developer version of 0.16. Then it's as easy as:

Code: Select all

App.ActiveDocument.copyObject(App.ActiveDocument.Pad001, True)
The second argument controls whether to make a deep copy (True) or not (False)
rentlau_64
Posts: 181
Joined: Tue Oct 21, 2014 9:47 am
Location: Pau (France)

Re: How to deep copy with python script?

Post by rentlau_64 »

wmayer,

Thank you for your quick answer.
I will update to 0.16 first and test.

Merci
Rentlau_64
rentlau_64
Posts: 181
Joined: Tue Oct 21, 2014 9:47 am
Location: Pau (France)

Re: How to deep copy with python script?

Post by rentlau_64 »

wmayer,

Two more questions relative to deep or simple copy:

How to change with python the link to the child and the link to the parent ? means to start from :

Code: Select all

Box
Pad
    |_Sketch
And to get :

Code: Select all

Box
Pad
    |_Sketch
Pad001
    |_Sketch
here in the previous example the Pad and Pad001 objects link both on the same Sketch object, and Skecth parent is Box.

and how create a link to parent or children with python script?

Thank you

Rentlau_64
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to deep copy with python script?

Post by wmayer »

Code: Select all

Box
Pad
    |_Sketch
Pad001
    |_Sketch
is this the output when you call "copyObject(App.ActiveDocument.Pad001, True)", i.e. the same sketch is used twice ?
rentlau_64
Posts: 181
Joined: Tue Oct 21, 2014 9:47 am
Location: Pau (France)

Re: How to deep copy with python script?

Post by rentlau_64 »

wmayer
is this the output when you call "copyObject(App.ActiveDocument.Pad001, True)", i.e. the same sketch is used twice ?
No the output of the function (FreeCAD 0.16) is similar to the following and the sketch is duplicated too as well as the parent (box)

Code: Select all

Box
Pad
    |_Sketch
Box001
Pad001
    |_Sketch001
I would like to know if there is possibility (with python) to link 2 objects to the same sketch ( ie copying the pad several time but keeping one sketch only in order to update all the copies of pad at once when you update the unique sketch) ?

Rentlau_64
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to deep copy with python script?

Post by wmayer »

I would like to know if there is possibility (with python) to link 2 objects to the same sketch ( ie copying the pad several time but keeping one sketch only in order to update all the copies of pad at once when you update the unique sketch) ?
Just do:

Code: Select all

copyObject(App.ActiveDocument.Pad, False)
Note: The tree view is not capable of showing several items for the sketch but it is used by the two pads then.
rentlau_64
Posts: 181
Joined: Tue Oct 21, 2014 9:47 am
Location: Pau (France)

Re: How to deep copy with python script?

Post by rentlau_64 »

wmayer ,
:D
Note: The tree view is not capable of showing several items for the sketch but it is used by the two pads then.
Because of that, I really never noticed it.

I just learn a big stuff thank you!

I really enjoy FreeCAD more and more
Rentlau_64
Post Reply