Copy family between documents via API

By Joe Ye

Transferring standards and elements via API is hot API request before Revit 2014. Revit 2014 exposed the exciting API, copy element between documents. This give us the ability to do a lot of interesting jobs.

Here is an sample to transfer family between documents. This command copy family to another opened document in the same Revit session. It copies all the types of the picked family instance’s family.

 

using System;    using System.Collections.Generic;    using System.Text;    using System.Windows.Forms;         using  Autodesk.Revit .DB;    using Autodesk.Revit.UI;    using Autodesk.Revit .ApplicationServices;    using Autodesk.Revit.Attributes ;    using Autodesk.Revit.UI.Selection;                [TransactionAttribute(TransactionMode.Manual)]    public class RevitCommand : IExternalCommand    {        public Result Execute(ExternalCommandData commandData,           ref string messages, ElementSet elements)        {               UIApplication app = commandData.Application;          Document doc = app.ActiveUIDocument.Document;               Selection sel = app.ActiveUIDocument.Selection;               Reference ref1 = sel.PickObject(ObjectType.Element,             "pick a family instance");               FamilyInstance inst = doc.GetElement(ref1) as FamilyInstance;          if(inst == null)          {            messages = "No family instance was picked";            return Result.Failed;          }               FamilySymbol sym = inst.Symbol;                Document targetDoc = null;          foreach (Document doc1 in app.Application.Documents)          {            if (doc.Title != doc1.Title)            {              targetDoc = doc1;              break;            }          }               IList ids = new List();          foreach (FamilySymbol symbol in sym.Family.Symbols)          {            ids.Add(symbol.Id);          }               Transaction targetTrans = new Transaction(targetDoc);          targetTrans.Start("copyFamily");               ElementTransformUtils.CopyElements(doc, ids, targetDoc, null,             new CopyPasteOptions());          targetTrans.Commit();               return Result.Succeeded ;        }    }

Comments

7 responses to “Copy family between documents via API”

  1. Thanks Joe. Do you think its possible to use the CopyElements to copy an entire legend with real components to another project file?

  2. Hi Mike,
    I think this can be done by CopyElements().

  3. I’ve been trying to get the LocationPoint of a legend component and it doesn’t seem to work.

  4. OLIVES Daniel Avatar
    OLIVES Daniel

    Hi Mike,
    How i can use this code ?
    I am a beginner in c# with revit 2014;
    Thank you very much.

  5. Hi Mike,
    The Location property of the legend components doesn’t have valid value.
    Then you can use the Element.BoundingBox to get the component’s bounding box( contains max point and min point), and then calculate the center position of the legend object.

  6. Hi Joe. How can I get the transform between the host and linked files because they dont share the same origin.

  7. Hi Roland,
    Have you found a solution for this?

Leave a Reply to JoeCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading