使用API来编辑族时,使用doc.FamilyCreate.NewReferencePlane();创建参考平面。
//Revit Family API 创建参考平面
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdCreateReferencePlane : IExternalCommand
{
//添加垂直参考平面(Family API),通过平面上两点与向量。
void AddReferencePlane_VerticalOffset(Document doc, View view)
{
XYZ pt1 = new XYZ(-0.5, -2.0, 0.0);
XYZ pt2 = new XYZ(-0.5, 2.0, 0.0);
XYZ vec = XYZ.BasisZ;
ReferencePlane refPlane = doc.FamilyCreate.NewReferencePlane(pt1, pt2, vec, view);
refPlane.Name = "OffsetV";
}
//添加水平参考平面(Family API),通过平面上两点与向量。
void AddReferencePlane_HorizontalOffset(Document doc, View view)
{
XYZ pt1 = new XYZ(2.0, -0.5, 0.0);
XYZ pt2 = new XYZ(-2.0, -0.5, 0.0);
XYZ vec = XYZ.BasisZ;
ReferencePlane refPlane = doc.FamilyCreate.NewReferencePlane(pt1, pt2, vec, view);
refPlane.Name = "OffsetH";
}
//使用三个点来确定参考平面
void AddReferencePlane_VerticalOffset2(Document doc)
{
XYZ pt1 = new XYZ(-0.5, -2.0, 0.0);
XYZ pt2 = new XYZ(-0.5, 2.0, 0.0);
XYZ pt3 = new XYZ(-0.5, -1.0, 1.0);
View view = findElement(doc, typeof(ViewPlan), "Lower Ref. Level") as View;
if (view != null)
{
ReferencePlane refPlane = doc.FamilyCreate.NewReferencePlane2(pt1, pt2, pt3, view);
refPlane.Name = "OffsetV";
}
}
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Selection sel = app.ActiveUIDocument.Selection; Transaction ts = new Transaction(doc, "CreateReferencePlane");
ts.Start();
//垂直参考平面
AddReferencePlane_VerticalOffset(doc, doc.ActiveView);
//AddReferencePlane_VerticalOffset2(doc);
//水平参考平面
AddReferencePlane_HorizontalOffset(doc, doc.ActiveView); ts.Commit(); return Result.Succeeded;
}
//通过类型与名称找Element
Element findElement(Document _rvtDoc, Type targetType, string targetName)
{
// get the elements of the given type
//
FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType)); // parse the collection for the given name
// using LINQ query here.
//
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>(); if (elems.Count > )
{ // we should have only one with the given name.
return elems[];
} // cannot find it.
return null;
}
}