Quantcast
Channel: RevitNetAddinWizard & NavisworksNetAddinWizard
Viewing all articles
Browse latest Browse all 872

Revit .NET: Try to Merge Two Revit Projects (.RVT Files)

$
0
0

It may not be uncommon at all to merge two Revit projects (.RVT files) but we haven’t seen any code addressing it. In this post, let’s try to merge two Revit projects programmatically with the Revit API and C#.

Here we go:

using (Autodesk.Revit.DB.Document doc = CachedApp.OpenDocumentFile(@"c:\temp\test.rvt"))
{
    using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(CachedDoc, "MergeProjects"))
    {
        trans.Start();

        FilteredElementCollector finalCollector = new FilteredElementCollector(doc);
        ElementIsElementTypeFilter filter1 = new ElementIsElementTypeFilter(false);
        finalCollector.WherePasses(filter1);
        ElementIsElementTypeFilter filter2 = new ElementIsElementTypeFilter(true);
        finalCollector.UnionWith((new FilteredElementCollector(doc)).WherePasses(filter2));
        ICollection<ElementId> ids = finalCollector.ToElementIds();

        CopyPasteOptions cpOpts = new CopyPasteOptions();
        ElementTransformUtils.CopyElements(doc, ids, CachedDoc, Autodesk.Revit.DB.Transform.Identity, cpOpts);

        trans.Commit();
    }

    doc.Close(false);
}

Nothing should be wrong with the above code, but when it was run from an external command, the following exception came up:

“Autodesk.Revit.Exceptions.ArgumentException: Some of the elements cannot be copied, because they are view-specific.
Parameter name: elementsToCopy
   at Autodesk.Revit.DB.ElementTransformUtils.CopyElements(Document sourceDocument, ICollection`1 elementsToCopy, Document destinationDocument, Transform transform, CopyPasteOptions options)
   at …”

So, the Autodesk.Revit.DB.ElementTransformUtils.CopyElements() method simply rejected to copy some so called ‘view-specific’ elements and aborted the whole operation. Since a useful Revit project definitely contains view-specific elements, the simple task of merging Revit projects cannot be done due to this severe limitation. It is frustrating!

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.


Viewing all articles
Browse latest Browse all 872

Trending Articles