今天是2018.7.17日。我开始第一次写revit二次开发的博客。下面的代码就是今天的标题,根据梁拆分楼板
环境:revit2018
我主要讲解一下思路。一块大的楼板,它的下面和边缘都是有梁的。这些梁与板进行连接操作后就会得到一个个被分一个个小闭环。
图1-1
如图1-1所示,这就是梁与楼板连接后的图像。
取得楼板的上表面,再从上表面中取曲线环。再将这些曲线环作为新的楼板的参数,楼板就分割完成了。
namespace SplitFloorByBeam
{
[Transaction(TransactionMode.Manual)]
public class splitFloorByBeam : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
FloorSlectionFileter floorSlectionFileter = new FloorSlectionFileter();
var references = uidoc.Selection.PickObjects(ObjectType.Element, floorSlectionFileter);
List<Element> beamList = new List<Element>();
List<Element> floorList = new List<Element>();
foreach (var refer in references)
{
Element elem = doc.GetElement(refer) as Element;
if (elem.Category.Name == "结构框架")
{
beamList.Add(elem);
}
else if (elem.Category.Name == "楼板")
{
floorList.Add(elem);
}
} //将梁、板分离
var floor = floorList[0] as Floor;
//将梁与楼板进行连接,目的为了取得最小的封闭环。
using (Transaction tran = new Transaction(doc))
{
tran.Start("Join");
foreach (var beam in beamList)
{
if (!JoinGeometryUtils.AreElementsJoined(doc, floor, beam))//判断是否连接
{
JoinGeometryUtils.JoinGeometry(doc, floor, beam);//连接操作
JoinGeometryUtils.SwitchJoinOrder(doc, floor, beam);//取反操作
}
}
tran.Commit();
}
//取得最小的封闭环数列
var faceReferences1 = HostObjectUtils.GetTopFaces(floor);
GeometryObject topFaceGeo1 = floor.GetGeometryObjectFromReference(faceReferences1[0]);
Face topFace1 = topFaceGeo1 as Face;
var loopList = topFace1.GetEdgesAsCurveLoops();//最小封闭环
Level level = doc.GetElement(floor.LevelId) as Level;
FloorType floorType = floor.FloorType;
//根据封闭的曲线数组进行新的楼板创建
using (Transaction tran1 = new Transaction(doc))
{
tran1.Start("split");
doc.Delete(floor.Id);//删除旧楼板
foreach (CurveLoop curveloop in loopList)
{
var curveList = curveloop.ToArray();//曲线链分离成曲线数组
CurveArray curveArray = new CurveArray();
foreach (var i in curveList)//数组变组合
{
curveArray.Append(i);
}
Floor newfloor = doc.Create.NewFloor(curveArray, floorType, level, false);
}
tran1.Commit();
}
return Result.Succeeded;
}
/// <summary>
/// 仅供选择要分段的梁和与之相连接的柱
/// </summary>
public class FloorSlectionFileter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem.Category.Name == "楼板" || elem.Category.Name=="结构框架")
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}
}
}