Handling Duplicate Types on CopyElements

By Augusto Goncalves

When using the ElementTransformUtils.CopyElements you may have the ‘Duplicate Types’ warning, as shown below.

duplicated_types

In fact this method have one override option to pass a CopyPasteOptions parameter that enable us to control how Revit will react when this happens. We can return one of the following:

  • UseDestinationTypes: Proceed with the paste operation and use the types with the same name in the destination document. 
  • Abort: Cancel the paste operation. 

To implement this, first create a new class that implements the interface IDuplicateTypeNamesHandler. This class should have one method that returns the action. In the sample below, it will use destination types.

public class CustomCopyHandler : IDuplicateTypeNamesHandler{  public DuplicateTypeAction OnDuplicateTypeNamesFound(    DuplicateTypeNamesHandlerArgs args)  {    return DuplicateTypeAction.UseDestinationTypes;  }}

Then, at the CopyElements call, create a object of the type CopyPasteOptions and set the handler.

// create an instanceCopyPasteOptions copyOptions = new CopyPasteOptions();copyOptions.SetDuplicateTypeNamesHandler(new CustomCopyHandler()); // now the copyElementTransformUtils.CopyElements(doc1, ids, doc2, Transform.Identity, copyOptions);

 

Thanks Arnošt Löbel for his post at our forums.


Comments

One response to “Handling Duplicate Types on CopyElements”

  1. Thanks Augusto for this good article.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading