最近开发用到零件(Part),所以花了一定时间来研究一下零件的创建。首先是通过RevitAPI查找零件创建的类
主要用的两个方法:
PartUtils.CreateParts(Document,ICollection(ElementId)) 创建零件
PartUtils.DivideParts 分割零件
Reference floorReference = wrapper.UiApp.PickFaceReference("请拾取一个面:", SelectionFilters.Floor);
Floor floor = null;
if (floorReference != null)
{
floor = wrapper.Doc.GetElement(floorReference) as Floor;
}
if (floor == null)
{
return Result.Failed;
}
Transaction trans = new Transaction(wrapper.Doc, "CreatePart");
trans.Start();
try
{
GeometryObject geoObj = floor.GetGeometryObjectFromReference(floorReference);
PlanarFace planarFace = geoObj as PlanarFace;
if (planarFace != null)
{
List<Curve> curveList = new List<Curve>();
XYZ faceDir = planarFace.FaceNormal;
Plane plane = Plane.CreateByNormalAndOrigin(planarFace.FaceNormal, planarFace.Origin);
SketchPlane sketchPlane = SketchPlane.Create(wrapper.Doc, plane);
EdgeArrayArray edgeArrayArray = planarFace.EdgeLoops;
foreach (EdgeArray edgeArray in edgeArrayArray)
{
foreach (Edge edge in edgeArray)
{
Curve curve = edge.AsCurve();
curveList.Add(curve);
}
}
List<XYZ> xyzList = new List<XYZ>();
curveList.ForEach(p => xyzList.Add(p.StartPoint()));
PartUtils.CreateParts(wrapper.Doc, new List<ElementId>() { floor.Id });
List<Curve> cList = new List<Curve>();
//ReferencePlane referencePlane = wrapper.Doc.Create.NewReferencePlane(xyzList[0], xyzList[2], planarFace.FaceNormal, wrapper.ActiveView);
wrapper.Doc.Regenerate();
List<ElementId> idList = PartUtils.GetAssociatedParts(wrapper.Doc, floor.Id, true, true).ToList();
XYZ dir1 = xyzList[3].Subtract(xyzList[1]).Normalize();
Line line = Line.CreateBound(xyzList[1] - dir1, xyzList[3] + dir1);
XYZ dir = xyzList[2].Subtract(xyzList[0]).Normalize();
Line line2 = Line.CreateBound(xyzList[0] - dir, xyzList[2] + dir);
PartUtils.DivideParts(wrapper.Doc, idList, new List<ElementId>() { }, new List<Curve>() { line, line2 }, sketchPlane.Id);
}
trans.Commit();
}
catch (Exception ex)
{
trans.RollBack();
}