Create line based generic model family instance

By Joe Ye

When create a line based generic model family instance manually in the Revit project document or family document, Revit reminds you to select “Place on Face” or Plance on Work Plane” in the contextual Ribbon tab. After pick one of them, you can draw the instance by pick one point, and then next point to create the family instance.

As the base face or work plane is necessary when creating the family instance, we need to provide the base face or work plane, and also the base line when calling NewFamilyInstance(). This override of NewFamilyInstance can be used.

NewFamilyInstance(Line line, FamilySymbol symbol, View view);

Note: the base line passed in should be in the view that is passed in. Otherwise it fails to create the generic model family instance. The error message is that the line is not in the specified view.

 

Here is the code snippet showing the usage.

using System;    using System.Collections.Generic;    using System.Text;    using System.Windows.Forms;         using Autodesk.Revit.DB;    using Autodesk.Revit.UI;    using Autodesk.Revit.DB.Structure;    using Autodesk.Revit.ApplicationServices;    using Autodesk.Revit.Attributes;              [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;        Transaction trans = new Transaction(doc, "ExComm");        trans.Start();             //For simplicity, and focusing on the important point,        //Get the target family type via its Id.         //Please replace this line with the code to get the lin        //e based generic model FamilySymbol.             FamilySymbol symbol = doc.get_Element(new ElementId(6732))            as FamilySymbol;        Line line = app.Application.Create.NewLineBound(            new XYZ(0, 0, 0), new XYZ(10, 10, 0));             //For simplicity, use the active view.         //Note: before run this command, set the plan view which         //elevation is 0 to be the active view.        //This can make sure the line is in this plan view.             FamilyInstance inst = doc.FamilyCreate.NewFamilyInstance(            line, symbol, doc.ActiveView);        trans.Commit();             return Result.Succeeded;      }    }        After creating the family instance, we can change the base curve of the family instance. However you cannot assign the curve that is out of the host view. The new base curve required to be in this view.

Comments

2 responses to “Create line based generic model family instance”

  1. Hello, Dear Sir.
    Thanks for API.
    But I tried to do that exercise, but it doesn’t work, may be I use Revit 2015?
    Is it possible can you explain me how to create this API and how to run it correct in Revit 2015?
    Thanks for your answer.
    My best regards,

  2. Bobby Jones Avatar
    Bobby Jones

    Joe,
    Thank you! This method does allow placement of a generic line based family and allows movement by adjusting the location curve, but what I would really like is to host the family to a named Reference Plane, as can be done in the Revit UI. I’m able to get a reference to the sketchplane of the desired reference plane, but all overloads of the NewFamilyInstance() fail when passed the reference. Is it possible? Thanks.

Leave a Reply to Bobby JonesCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading