Revit Family API 创建参考平面
使用API来编辑族时,使用doc.FamilyCreate.NewReferencePlane();创建参考平面。
from:http://greatverve.cnblogs.com/p/NewReferencePlane.html
//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 > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
}
[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 > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。