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

Revit .NET: Copy Some Visible Elements from an Opened Project to Another

$
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. Before, we tried to merge a side Revit document opened into memory through the Application.OpenDocumentFile call into the current opened Revit document but got an exception saying “Some of the elements cannot be copied, because they are view-specific”.

Previously, we tried the same on two opened projects in Revit but found the result was the same bad. In this post, let’s try to copy a few visible and very common elements (Walls) from an opened Revit project to another in the same Revit session.

if (CachedApp.Documents.Size >= 2)
{
    List<Document> docs = (from Document doc in CachedApp.Documents select doc).ToList();
    using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(docs[0], "MergeProjects2"))
    {
        trans.Start();

        FilteredElementCollector finalCollector = new FilteredElementCollector(docs[1]);
        ElementClassFilter filter1 = new ElementClassFilter(typeof(Wall));
        finalCollector.WherePasses(filter1);
        ICollection<ElementId> ids = finalCollector.ToElementIds();

        CopyPasteOptions cpOpts = new CopyPasteOptions();
        ElementTransformUtils.CopyElements(docs[1], ids, docs[0], Autodesk.Revit.DB.Transform.Identity, cpOpts);

        trans.Commit();
    }
}

This time, it succeeded! The walls from one opened Revit project was successfully copied over to another project also open in the same Revit session.
  CopyFromOneProjectToAnother
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