Book a Demo

Author Topic: C# Create "shallow copy" of diagram  (Read 5347 times)

Tom@s

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
C# Create "shallow copy" of diagram
« on: May 24, 2021, 07:14:22 pm »
Hi there,
Is there some way how to make a "shallow copy" of the diagram in C#? (I couldn't find anything helpful in the official documentation.)

Additional info:
A shallow copy means to create a new diagram(new diagram ID) with the same content as the original diagram has but elements are not duplicated.

There is a way how to do it in EA:
1) Copy diagram
2) Paste diagram -> Type of Copy -> Shallow - links to existing elements

Thanks

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: C# Create "shallow copy" of diagram
« Reply #1 on: May 24, 2021, 08:52:19 pm »
Hi there,
Is there some way how to make a "shallow copy" of the diagram in C#? (I couldn't find anything helpful in the official documentation.)

Additional info:
A shallow copy means to create a new diagram(new diagram ID) with the same content as the original diagram has but elements are not duplicated.

There is a way how to do it in EA:
1) Copy diagram
2) Paste diagram -> Type of Copy -> Shallow - links to existing elements

Thanks
It's conceptually quite simple...
For each diagram object in the original diagram, create a diagram object in the new diagram with the same Object Id, but with the Diagram ID of the new diagram.

Repeat for each connector.

Does that help?
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Tom@s

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: C# Create "shallow copy" of diagram
« Reply #2 on: May 25, 2021, 03:25:09 am »
Hello Paolo,
Thank you but it doesn't work. I didn't find a way how to copy Diagram objects via some existing function. ObjectID does not exist - there are only InstanceID and ElementID.

Probably there is only one way how to make shallow copy(it works):

//shallow copy
        public void ShallowCopy(EA.Diagram diagram, EA.Collection diagrams)
        {
            EA.Collection diagramObjects = diagram.DiagramObjects;
            EA.Collection diagramLinks = diagram.DiagramLinks;
           
            //copy diagram
            EA.Diagram copyDiagram = diagrams.AddNew(diagram.Name + "_COPY", "BPMN2.0::Business Process");
            copyDiagram.cx = diagram.cx;
            copyDiagram.cy = diagram.cy;
            copyDiagram.ExtendedStyle = diagram.ExtendedStyle;
            copyDiagram.FilterElements = diagram.FilterElements;
            copyDiagram.HighlightImports = diagram.HighlightImports;
            copyDiagram.IsLocked = diagram.IsLocked;
            copyDiagram.MetaType = diagram.MetaType;
            copyDiagram.Notes = diagram.Notes;
            copyDiagram.Orientation = diagram.Orientation;
            copyDiagram.Scale = diagram.Scale;
            copyDiagram.ShowDetails = diagram.ShowDetails;
            copyDiagram.ShowPackageContents = diagram.ShowPackageContents;
            copyDiagram.ShowPrivate = diagram.ShowPrivate;
            copyDiagram.ShowProtected = diagram.ShowProtected;
            copyDiagram.ShowPublic = diagram.ShowPublic;
            copyDiagram.Stereotype = diagram.Stereotype;
            copyDiagram.StereotypeEx = diagram.StereotypeEx;
            copyDiagram.StyleEx = diagram.StyleEx;
            copyDiagram.Version = diagram.Version;
            copyDiagram.Swimlanes = diagram.Swimlanes;
            copyDiagram.Update();
           
            //copy diagram objects
            foreach (EA.DiagramObject diagramObject in diagramObjects)
            {
                EA.DiagramObject copyDiagramObject = copyDiagram.DiagramObjects.AddNew("", "");
                copyDiagramObject.InstanceID = diagramObject.InstanceID;
                copyDiagramObject.ElementID = diagramObject.ElementID;
                copyDiagramObject.ElementDisplayMode = diagramObject.ElementDisplayMode;
                copyDiagramObject.left = diagramObject.left;
                copyDiagramObject.right = diagramObject.right;
                copyDiagramObject.top = diagramObject.top;
                copyDiagramObject.bottom = diagramObject.bottom;
                copyDiagramObject.Sequence = diagramObject.Sequence;
                copyDiagramObject.BackgroundColor = diagramObject.BackgroundColor;
                copyDiagramObject.BorderColor = diagramObject.BorderColor;
                copyDiagramObject.BorderLineWidth = diagramObject.BorderLineWidth;
                copyDiagramObject.FeatureStereotypesToHide = diagramObject.FeatureStereotypesToHide;
                copyDiagramObject.FontBold = diagramObject.FontBold;
                copyDiagramObject.FontColor = diagramObject.FontColor;
                copyDiagramObject.FontItalic = diagramObject.FontItalic;
                copyDiagramObject.fontName = diagramObject.fontName;
                copyDiagramObject.fontSize = diagramObject.fontSize;
                copyDiagramObject.FontUnderline = diagramObject.FontUnderline;
                copyDiagramObject.IsSelectable = diagramObject.IsSelectable;
                copyDiagramObject.Style = diagramObject.Style;
                copyDiagramObject.TextAlign = diagramObject.TextAlign;               
                copyDiagramObject.Update();
            }

            //copy diagram links for connector layouts
            foreach (EA.DiagramLink diagramLink in diagramLinks) {
                EA.DiagramLink copyDiagramLink = copyDiagram.DiagramLinks.AddNew("","");
                copyDiagramLink.ConnectorID = diagramLink.ConnectorID;
                copyDiagramLink.Geometry = diagramLink.Geometry;
                copyDiagramLink.HiddenLabels = diagramLink.HiddenLabels;
                copyDiagramLink.InstanceID = diagramLink.InstanceID;
                copyDiagramLink.IsHidden = diagramLink.IsHidden;
                copyDiagramLink.LineColor = diagramLink.LineColor;
                copyDiagramLink.LineStyle = diagramLink.LineStyle;
                copyDiagramLink.LineWidth = diagramLink.LineWidth;
                copyDiagramLink.Style = diagramLink.Style;
                copyDiagramLink.SuppressSegment = diagramLink.SuppressSegment;
                copyDiagramLink.Update();
            }
        }
« Last Edit: May 26, 2021, 06:42:37 pm by Tom@s »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Create "shallow copy" of diagram
« Reply #3 on: May 25, 2021, 05:25:00 am »
Why is that not working? Have you checked the database if the diagramobjects are actually there?
Have you reloaded the diagram?

The only thing that seems fishy at first sight is the fact that you try to copy the instanceID. I think you aren't supposed to do that.

Geert

Tom@s

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: C# Create "shallow copy" of diagram
« Reply #4 on: May 26, 2021, 06:49:09 pm »
Hi Geert,
The code I've sent works perfectly for the shallow copy.

I just mentioned that I can not access ObjectID - at least not in C#. In C# I can access only to ElementID or InstanceID.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# Create "shallow copy" of diagram
« Reply #5 on: May 26, 2021, 06:54:17 pm »
ObjectID only exists as the database column. The equivalent in the API is ElementID

Geert
« Last Edit: May 26, 2021, 10:58:43 pm by Geert Bellekens »

Tom@s

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: C# Create "shallow copy" of diagram
« Reply #6 on: May 26, 2021, 08:20:37 pm »
Thank you to make it clear. (I was talking in the context of API for C#)