How to implement IDuplicateTypesNameHandler, for the CopyPasteOptions Class?

How to implement IDuplicateTypesNameHandler, for the CopyPasteOptions Class?

Anonymous
Not applicable
2,688 Views
2 Replies
Message 1 of 3

How to implement IDuplicateTypesNameHandler, for the CopyPasteOptions Class?

Anonymous
Not applicable

Hello,

 

I create a function to load an specific TextType, needed on my addin.

 

Here's what the function does

 

  1. First the function validates that the name of the text type exist on the current project
  2. If it doesn't exist the addin copy from a resource a base Revit project file that contains the text type, and open it.
  3. Once opened it search for the TextType and copied with the method ElementTransformUtils.CopyElements to the current Revit Project.
  4. Finally the base Revit project is closed

It works fine, but each time the TextType is copied I received a warning message saying the following Types already exist but are different.

And show the following details

Arrow 30 Degree

Arrowhead: Arrow 30 Degree.

 

Since I'm not copying those elements I would like to remove the message.

 

Searching on the Revit documentation I found that the CopyPasteOptions could remove it, using the SetDuplicateTypeNamesHandler method that uses IDuplicateTypeNamesHandler, but I haven't found how to implement that interface.

 

I'll appreciate any help

Thanks

 

Here is the snippet of my function

 

        internal static void LoadTextTypes()
        {
            FilteredElementCollector textNoteTypes = new FilteredElementCollector(Revit_App.RevDef.RevitDoc);
            textNoteTypes.OfClass(typeof(TextNoteType));
            var type = from text in textNoteTypes
                       where text.Name == "Vialdi Text"
                       select text as TextNoteType;
            if (type.Count() > 0)
                return;
            string path = System.IO.Path.Combine(Revit_App.App.AppDirectory.FullName, "baseProject.rvt");
            System.IO.File.WriteAllBytes(path, Media.BaseProject);
            Document baseDocument = Revit_App.RevDef.RevitApp.OpenDocumentFile(path);
            textNoteTypes = new FilteredElementCollector(baseDocument);
            textNoteTypes.OfClass(typeof(TextNoteType));
            type = from text in textNoteTypes
                   where text.Name == "Vialdi Text"
                   select text as TextNoteType;
            List<ElementId> textTypes = new List<ElementId>();
            textTypes.Add(type.First().Id);
            Transaction tra = new Transaction(Revit_App.RevDef.RevitDoc, "copy");
            tra.Start();
            ElementTransformUtils.CopyElements(baseDocument, textTypes, Revit_App.RevDef.RevitDoc, null, new CopyPasteOptions());
            tra.Commit();
            baseDocument.Close();
        }

 

And here is the warning message

Duplicate Tyeps

0 Likes
Accepted solutions (1)
2,689 Views
2 Replies
Replies (2)
Message 2 of 3

arnostlobel
Alumni
Alumni
Accepted solution

Hello Miguel Alanis:

 

I believe the problem is caused by the existence of nested families (types) referenced by the text type you attempt to copy. In other words, you do copy a type that does not exist yet, but which contains other nested types that do exists already in the target document, either as standalone or nested in other families.

 

In your particular case it seems you do not worry about that, therefore you should simply set the copy option to ignore such cases, meaning you give it an instance of your custom IDuplicateTypeNamesHandler.

 

CopyPasteOptions options = new CopyPasteOptions();
Options.SetDuplicateTypeNamesHandler(new MyCopyHandler());

 

Where the class MyCopyHandler is an implementation of the IDuplicateTypeNamesHandler, for example:

public MyCopyHandler : IDuplicateTypeNamesHandler
{
   public DuplicateTypeAction OnDuplicateTypeNamesFound()
   {
      return DuplicateTypeAction.UseDestinationTypes;
   }
}

  

(Note: I typed the above code directly in this post just to illustrate the case. Please make sure to adjust it so it compiles 😉

 

Thank you

 

Arnošt Löbel

Sr. Principal Engineer

Autodesk, Revit R&D

Arnošt Löbel
Message 3 of 3

Anonymous
Not applicable

Thanks for the solution

0 Likes