Class CopyPasting

java.lang.Object
com.nomagic.magicdraw.copypaste.CopyPasting

@OpenApiAll public class CopyPasting extends Object

Provides API for copy-pasting elements in the model and symbol styles in diagrams.

You can copy model elements and symbols either to another location in the same project or to another project. This must be done in the same session. There are two modes of making copies of model elements:
  • Deep copying (with new data)
  • Shallow copying (with reused data)

Example 1: Copying an element

 Element element = ...; // Element to copy/paste.
 Element parent = ...;  // Parent to which the element has to be pasted: either the same project or another project.

 SessionManager sessionManager = SessionManager.getInstance();
 sessionManager.createSession("Clone");

 // 3rd parameter indicates whether element name uniqueness should be preserved in the parent.
 CopyPasting.copyPasteElement(element, parent, true);

 sessionManager.closeSession();
 

Example 2: Copying multiple elements and symbols

 List elements = ...; // Elements to copy/paste.
 List views = ...; // Symbols to copy/paste.
 Element parent = ...;  // Parent to which elements should be pasted: either the same project or another project.

 BaseElement symbolParent = ...; // Parent to which symbols should be pasted.

 SessionManager sessionManager = SessionManager.getInstance();
 sessionManager.createSession("Clone");

 // 4th parameter indicates whether deep or shallow copy is applied.
 // 5th parameter indicates whether element name uniqueness should be preserved in the parent.
 List baseElements = CopyPasting.copyPasteElements(views, parent, symbolParent, true, true);

 sessionManager.closeSession();
 

Example 3: Copying style from one symbol to another

 PresentationElement source = ...; // Symbol with symbol properties (style) to copy
 PresentationElement target = ...; // Symbol to paste style on

 SessionManager sessionManager = SessionManager.getInstance();
 sessionManager.createSession("Clone Symbol Style");

 CopyPasting.copyPasteSymbolsStyle(source, target)

 sessionManager.closeSession();
 

Example 4: Copying style from symbol to multiple symbols

 PresentationElement source = ...; // Symbol with symbol properties (style) to copy
 Collection targets = ...; // Symbols to paste style on

 SessionManager sessionManager = SessionManager.getInstance();
 sessionManager.createSession("Clone Symbol Style");

 CopyPasting.copyPasteSymbolsStyle(source, targets)

 sessionManager.closeSession();
 
See Also:
  • Constructor Details

    • CopyPasting

      public CopyPasting()
  • Method Details

    • copyPasteElement

      @CheckForNull public static Element copyPasteElement(Element elementToCopy, Element parent)
      Makes a copy of the specified element and places it under the parent (namespace). Does not increase element names in the given namespace.

      Assumes that session is started by SessionManager.

      Parameters:
      elementToCopy - element to copy.
      parent - owner to which to add copied element.
      Returns:
      cloned element.
    • copyPasteElement

      @CheckForNull public static Element copyPasteElement(Element elementToCopy, Element parent, boolean increaseElementNames)
      Makes a copy of the specified element and places it under the parent (namespace).

      Assumes that session is started by SessionManager.

      Parameters:
      elementToCopy - element to copy.
      parent - owner to which to add copied element.
      increaseElementNames - indicates if element names should be increased in given namespace to avoid duplication.
      Returns:
      cloned element.
    • copyPasteElements

      public static List copyPasteElements(List<? extends Element> elementsToCopy, Element parent)
      Makes a copy of specified elements and places them under the given parent (namespace). Does not increase element names in the given namespace.

      Assumes that session is started by SessionManager.

      Parameters:
      elementsToCopy - collection of Element} objects to be copied.
      parent - parent where objects should be placed.
      Returns:
      list of copies.
    • copyPasteElements

      public static List copyPasteElements(List<? extends Element> elementsToCopy, Element parent, boolean increaseElementNames)
      Makes a copy of specified elements and places them under the given parent (namespace).

      Assumes that session is started by SessionManager.

      Parameters:
      elementsToCopy - collection of Element} objects to be copied.
      parent - parent where objects should be placed.
      increaseElementNames - indicates if element names should be increased in given namespace to avoid duplication.
      Returns:
      list of copies.
    • copyPasteElements

      public static List<BaseElement> copyPasteElements(Collection<? extends BaseElement> objectsToCopy, @CheckForNull BaseElement dataParent, BaseElement viewParent, boolean withNewData, boolean increaseElementNames)
      Clones given objects (model elements or symbols) and establishes correct relationships. Returns correct copies. These copies can contain views and model elements (mixed).
      Parameters:
      objectsToCopy - objects to copy.
      dataParent - data parent.
      viewParent - parent of symbols.
      withNewData - indicates whether pasting should be done with new data (deep or shallow copy).
      increaseElementNames - indicates whether elements names should be increased.
      Returns:
      cloned elements.
    • copyPasteSymbolsStyle

      public static void copyPasteSymbolsStyle(PresentationElement sourceSymbol, PresentationElement targetSymbol) throws ReadOnlyElementException
      Copies symbol style from provided source symbol and pastes on target symbol.

      Assumes that session is started by SessionManager.

      Parameters:
      sourceSymbol - symbol to copy style from
      targetSymbol - symbol to paste style on
      Throws:
      ReadOnlyElementException - when target symbol is read only or symbol's owner is read only.
    • copyPasteSymbolsStyle

      public static void copyPasteSymbolsStyle(PresentationElement sourceSymbol, Collection<PresentationElement> targetSymbols) throws ReadOnlyElementException
      Copies symbol style from provided source symbol and pastes it on each target symbol.

      Assumes that session is started by SessionManager.

      Parameters:
      sourceSymbol - symbol to copy style from
      targetSymbols - symbols to paste style on
      Throws:
      ReadOnlyElementException - when any of the target symbols is read only or symbol's owner is read only.
    • copyPasteSymbolsStyle

      public static void copyPasteSymbolsStyle(Collection<PresentationElement> sourceSymbols, Collection<PresentationElement> targetSymbols) throws ReadOnlyElementException
      Copies symbol style from one of the source symbols to target symbols. For each target symbol first found appropriate source symbol is selected. Symbols of the same element type are preferred.

      Assumes that session is started by SessionManager.

      Parameters:
      sourceSymbols - symbols to copy style from
      targetSymbols - symbols to paste style on
      Throws:
      ReadOnlyElementException - when any of the target symbols is read only or symbol's owner is read only.