Previously, we finally worked out a working version of the command merging two Revit projects. However, during its running, the Duplicate Types dialogs as follows would come up repeatedly to interrupt the merging process.
In addition to the UI flashing, it makes the project merging more inefficient. Is there a way to suppress the Duplicate Types dialog from showing up in the merging/copying process?
Yes, there is. We can derive a class from the IDuplicateTypeNamesHandler interface, create an instance of it, assign the instance to the CopyPasteOptions object and pass the object as the last argument to the ElementTransformUtils.CopyElements() call. Here we go.
public class IgoreDuplicateTypeNames : IDuplicateTypeNamesHandler
{
public DuplicateTypeAction OnDuplicateTypeNamesFound(DuplicateTypeNamesHandlerArgs args)
{
return DuplicateTypeAction.UseDestinationTypes;
}
}
…
using (StreamWriter sw = new StreamWriter(@"c:\temp\MergeProjects.txt"))
{
using (Autodesk.Revit.DB.Document doc = CachedApp.OpenDocumentFile(@"c:\temp\test.rvt"))
{
using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(CachedDoc, "MergeProjects6"))
{
trans.Start();
foreach (BuiltInCategory biCat in Enum.GetValues(typeof(BuiltInCategory)))
{
FilteredElementCollector finalCollector = new FilteredElementCollector(doc);
try
{
ElementCategoryFilter filter1 = new ElementCategoryFilter(biCat);
finalCollector.WherePasses(filter1);
}
catch
{
sw.WriteLine(string.Format("The category {0} cannot be filtered.", biCat));
continue;
};
ICollection<ElementId> ids = finalCollector.ToElementIds();
CopyPasteOptions cpOpts = new CopyPasteOptions();
cpOpts.SetDuplicateTypeNamesHandler(new IgoreDuplicateTypeNames());
try
{
if (ids.Count > 0)
{
ElementTransformUtils.CopyElements(doc, ids, CachedDoc, Autodesk.Revit.DB.Transform.Identity, cpOpts);
sw.WriteLine(string.Format("Elements/Types of the category {0} were successfully copied over.", biCat));
}
else
sw.WriteLine(string.Format("NO Elements/Types of the category {0}.", biCat));
}
catch
{
sw.WriteLine(string.Format("Error when copying elements/types of the category {0}.", biCat));
}
}
trans.Commit();
}
}
sw.Close();
}
…
Enjoy!
Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.