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.