How to "Deep copy" an instance

Hi! When I try to copy an instance, if I use new_instance = instance.dup, the returned object's Hash value is same as the original one. That means if I modify new_instance, the original instance will change at same time. I tried many times to get a "full copy" ,but all failed. Could you please help me? Thanks.


  • In the Navigator pane, when you copy a master (not instance, of)
    it will ask you whether you want to make a deep or shallow copy.

    Maybe you want to duplicate the master, not the instance, and
    make it uniquely named.

    If you want to change contents of used cells below, then you
    can either duplicate masters that get used, or duplicate the
    entire hierarchy.

    Somebody else will have to tell you -how- to duplicate the
    master in scripting.

  • Hi,

    You could duplicate the cell you want to copy deep and all of its called cells as well. Then, inside these duplicated cells you make all the instances point to these newly duplicated cells... Not sure if there is a method who does this on the fly...



  • @Default I think you mean when scripting, right?

    Problem is that "dup" will create a copy in the Python/Ruby sense, but the underlying "Instance" object still acts as a pointer into the database. That is the nature of the object and dup does not change that.

    I wonder what "deep copy" means. In the hierarchical sense, a deep copy means that if you copy an instance to a cell A you expect A to be copied (e.g. to A2) and then the new instance points to this cell. So you can modify the new A2 cell without changing layout on the other places.

    If you mean deep copy in the programming sense, you create another instance (with different location etc.) to the same cell. This you can do by using the "CellInstArray" object which is the actual instance data structure:

    # gets a copy of the data structure -> CellInstArray
    cia = instance.cell_inst()
    ... modify the CellInstArray object as you wish
    # insert the modified instance as a new cell instance:

    In the documentation "CellInstArray" is sometimes referred to as the "working object". This means a detached data structure that describes an object. "Instance" is the same thing, but living inside the database. So "Instance" is rather a reference or a pointer.

    BTW: the same concept applies to "Shape" (the reference/pointer to a geometrical primitive) and the working objects ("Box", "Polygon", "Path" etc.).


  • Thank you Matthias!
    But I have an another question:Shape class has no cell_inst(),If I want to duplicate a shape object(not point to same shape in database),What should I do?Thanks a lot!

  • That depends what you want to do.

    I understand that you want to modify the object. A plain copy does not make much sense, right?

    So when you want to copy-and-modify and shape, you need to focus on a specific shape type. Most shape types (with the exception of texts) can be converted into a polygon. That is the working object for this kind of shape like CellInstArray is for instances. So the code is like this:

      if not shape.is_text():
        polygon = shape.polygon
        # ... do something with polygon ...
        shapes.insert(polygon)   # insert the modified polygon

    (Side note: if you want to transfer an object you can also insert the Instance or Shape object into a new different or Shapes container respectively).


  • Another option could be to Flatten and then Make Cell. You can pick levels and do it top down, push and pop, if the complexity is low. This is something I've found efficient (but it's all manual, not scripted).

    So do you need a script to generalize this activity or ease it in the future? Or just get it done one time, for now?
Sign In or Register to comment.