布尔运算
创建的实体大小应控制在500m以内,否则可能会生成失败。所需要生成的实体范围确实大于500m,那么可以先将元素缩小再进行布尔运算,得到结果再进行元素放大操作即可
布尔交计算(BoolleanIntersect)
该方法用于三维实体,取得实体集中的共有部分生成一个新的智能实体。
public static void CmdBooleanIntersectElement(string unparsed)
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
#region CreateSolidElement
#region Create profile
DPoint3d p1 = new DPoint3d(-1* uorPerMas, uorPerMas, 0);//定义形元素端点
DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
#endregion
DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
solid.AddToModel();//将实体元素写入模型
#endregion
#region CreateConeElement
ConeElement cone = new ConeElement(dgnModel,null,uorPerMas,uorPerMas,new DPoint3d(1.5*uorPerMas,0,uorPerMas), new DPoint3d(1.5 * uorPerMas, 0, 0), DMatrix3d.Identity,true);//定义圆台实体
cone.AddToModel();//将圆台实体元素写入模型
#endregion
Convert1.ElementToBody(out SolidKernelEntity entity1,solid,true,false,false);//将实体转成SolidKernelEntity
Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
SolidKernelEntity[] entities = {entity2};//定义实核实体集
Modify.BooleanIntersect(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔交运算
Convert1.BodyToElement(out Element resultElem, entity1, null,dgnModel);//将结果转换为元素
resultElem.AddToModel();//将元素写入模型空间
}
布尔减运算(BooleanSubtract)
该方法用于三维实体,去除被剪切实体与用于剪切的实体集中共有的部分,生成一个新的智能实体。首先我们需要将元素转换为实体,然后使用实体执行布尔减计算得到最终结果
public static void CmdBooleanSubtractElement(string unparsed)
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
#region CreateSolidElement
#region Create profile
DPoint3d p1 = new DPoint3d(-1 * uorPerMas, uorPerMas, 0);//定义体元素端点
DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
#endregion
DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
solid.AddToModel();//将实体元素写入模型
#endregion
#region CreateConeElement
ConeElement cone = new ConeElement(dgnModel, null, uorPerMas, uorPerMas, new DPoint3d(1.5 * uorPerMas, 0, uorPerMas), new DPoint3d(1.5 * uorPerMas, 0, 0), DMatrix3d.Identity, true);//定义圆台实体
cone.AddToModel();//将圆台实体元素写入模型
#endregion
Convert1.ElementToBody(out SolidKernelEntity entity1, solid, true, false, false);//将实体转成SolidKernelEntity
Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
SolidKernelEntity[] entities = { entity2 };//定义实核实体集
Modify.BooleanSubtract(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔减运算
Convert1.BodyToElement(out Element resultElem, entity1, null, dgnModel);//将结果转换为元素
resultElem.AddToModel();//将元素写入模型空间
}
布尔并运算(BooleanUnion)
该方法用于三维实体,取得实体集中的并集部分生成一个新的智能实体。首先我们需要将元素转换为实体,然后使用实体执行布尔并计算得到最终结果
public static void CmdBooleanUnionElement(string unparsed)
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
#region CreateSolidElement
#region Create profile
DPoint3d p1 = new DPoint3d(-1 * uorPerMas, uorPerMas, 0);//定义体元素端点
DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
#endregion
DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
solid.AddToModel();//将实体元素写入模型
#endregion
#region CreateConeElement
ConeElement cone = new ConeElement(dgnModel, null, uorPerMas, uorPerMas, new DPoint3d(1.5 * uorPerMas, 0, uorPerMas), new DPoint3d(1.5 * uorPerMas, 0, 0), DMatrix3d.Identity, true);//定义圆台实体
cone.AddToModel();//将圆台实体元素写入模型
#endregion
Convert1.ElementToBody(out SolidKernelEntity entity1, solid, true, false, false);//将实体转成SolidKernelEntity
Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
SolidKernelEntity[] entities = { entity2 };//定义实核实体集
Modify.BooleanUnion(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔并运算
Convert1.BodyToElement(out Element resultElem, entity1, null, dgnModel);//将结果转换为元素
resultElem.AddToModel();//将元素写入模型空间
}
布尔切割运算(BooleanCut)
该方法用于一条曲线以某个方向对实体进行切割的操作。首先我们需要将被切割的元素转化为实体,然后使用曲线对实体进行切割生成切割后的结果。在本案例中,首先创建一条用于切割的B样条曲线,然后使用B样条曲线定义CurveVector,在使用方向向量定义切割方向后对转换为实体后的元素切割,最终生成切割后的智能实体
public static void CmdBooleanCutElement(string unparsed)
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
#region CreateSolidElement
#region Create profile
DPoint3d p1 = new DPoint3d(-10 * uorPerMas, 10*uorPerMas, 0);//定义体元素端点
DPoint3d p2 = new DPoint3d(10 * uorPerMas, 10*uorPerMas, 0);
DPoint3d p3 = new DPoint3d(10 * uorPerMas, -10 * uorPerMas, 0);
DPoint3d p4 = new DPoint3d(-10 * uorPerMas, -10 * uorPerMas, 0);
DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
#endregion
DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
solid.AddToModel();//将实体元素写入模型
#endregion
Convert1.ElementToBody(out SolidKernelEntity entity, solid, true, false, false);//将实体转成SolidKernelEntity
#region CreateBSplineCurveElement
DPoint3d[] poles = new DPoint3d[6];//定义B样条曲线的节点
poles[0] = new DPoint3d(0, 10*uorPerMas, 0);//定义节点坐标
poles[1] = new DPoint3d(-5*uorPerMas, 5*uorPerMas, 0);
poles[2] = new DPoint3d(0,0,0);
poles[3] = new DPoint3d(5*uorPerMas,-5*uorPerMas,0);
poles[4] = new DPoint3d(2*uorPerMas, -8 * uorPerMas, 0);
poles[5] = new DPoint3d(0, -10 * uorPerMas, 0);
MSBsplineCurve curve = MSBsplineCurve.CreateFromPoles(poles, null, null, 4, false, false);//定义B样条曲线
BSplineCurveElement bSplineCurve = new BSplineCurveElement(dgnModel,null, curve);//定义B样条曲线元素
bSplineCurve.AddToModel();//将B样条曲线元素写入模型
#endregion
DVector3d vector = DVector3d.UnitZ;//定义切割法线
Modify.BooleanCut(ref entity, bSplineCurve.GetCurveVector(),Modify.CutDirectionMode.Both,Modify.CutDepthMode.All, 0, false, vector, 0);//用实核实体集中的实体与实体进行布尔切割运算
Convert1.BodyToElement(out Element resultElem, entity, null, dgnModel);//将结果转换为元素
resultElem.AddToModel();//将元素写入模型空间
}