第三期视频回放

点击查看【bilibili】

第三期代码及课件下载地址

代码及课件下载地址

第三期直播内容文字版

Slide2.PNG
图一 本期介绍内容一览

MicroStation单位

  1. 这里我们着重讨论一下MicroStation二次开发中单位的问题,因为元素是基于屏幕分辨率创建的,而我们需要写入的构件,如果需要与我们当前的单位所匹配,那么就必须在创建构件时,获得实际单位与屏幕分辨率的比值。<br /> 关于修改主单位可以依次点击:File > Settings > File > Design File Settings<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643332516524-7be7ae37-aae8-496b-8bb5-119add22f83c.png#clientId=u66ca6dd3-20bd-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=611&id=u5bee5cfc&margin=%5Bobject%20Object%5D&name=image.png&originHeight=611&originWidth=867&originalType=binary&ratio=1&rotation=0&showTitle=false&size=319239&status=done&style=none&taskId=u2393faeb-c8ca-4000-8432-3f37dc82c82&title=&width=867)<br />**图2 文件设置**<br />**关于设置子单位的初衷:**<br />**设置子单位主要是为了英制单位服务的。我们知道1英尺等于12英寸,对于英制来说,我们可以将主单位设置为英尺,子单位设置为英寸。这样当您想输入2'5"时就可以直接输入2:5而不需要将5"先换算成0.41666666666667'进而换算为2.41666666666667。这种巧妙的设计对于公制来说反而显得多此一举了。**
  2. 在代码实现中,我们主要获取三种值,分别为用于获取获取主单位与分辨率单位的比值的UorPerMaster,用于获取子单位与分辨率单位的比值的UorPerSub和用于获取米与分辨率单位的比值的UorPerMeter。对于一些需要与主单位相同的情景,我们使用UorPerMaster进行单位的换算,但是,当我们在做一些标准构件时,比如说集水坑,钢筋,管线等,他不会因为当前主单位的不同尺寸有所改变,此时就需要使用UorPerMeter,保证在不同主单位条件下他们的换算关系都是基于分辨率与米之间的。
  1. public static void CmdUnitConversion(string unparsed)
  2. {
  3. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  4. double uorMeter = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter;//获取米与分辨率单位的比值
  5. double uorPerSub = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerSub;//获取子单位与分辨率单位的比值
  6. MessageBox.Show("UorPerMaster = "+ uorPerMas+ "\nUorPerMeter = "+uorMeter+ "\nuorPerSub = "+ uorPerSub);//提示框输出信息
  7. }

元素变换

变换矩阵(DMatrix3d)

  1. 使用变换矩阵对元素进行移动,旋转等操作,它的用法十分灵活,可以使用它进行多维变换。
  1. public static void CmdElementTransformByMatrix(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. DMatrix3d matrix = new DMatrix3d(1, 0, 0, 0, 0, 1, 0, 1, 0);//定义变换矩阵
  12. DTransform3d trans = new DTransform3d(matrix);//定义变换几何
  13. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  14. shape.ApplyTransform(transform);//对形元素应用变换信息
  15. shape.AddToModel();//将在内存中定义形元素写入磁盘上文件的模型中
  16. }
  1. 在本案例中,首先在XY平面内创建了一个边长为10的形元素,然后使用变换矩阵,将Y坐标和Z坐标调换后对形元素应用。生成结果我们可以看到,在XZ平面生成了一个边长为10的形元素,其中原来Y的坐标值变成了Z值。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643333990705-a2292e8e-3bb2-4a09-8f82-de152635ade2.png#clientId=u3f260f35-21a1-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=553&id=uc88df10a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=553&originWidth=882&originalType=binary&ratio=1&rotation=0&showTitle=false&size=46791&status=done&style=none&taskId=u61680f4d-1c38-4ecf-aa4b-0c05ad57d82&title=&width=882)<br />**图3 生成结果**

平移(Move)

  1. 当我们需要对元素进行平移时,若使用变换矩阵的方式则需要首先对变换矩阵进行计算,十分麻烦,使用DTransform3d.FromTranslation( )可以直接在输入参数中指定平移元素的XYZ坐标值,使用起来更加方便。
  1. public static void CmdElementMove(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d startPo = DPoint3d.Zero;//定义直线起点
  6. DPoint3d endPo = new DPoint3d(10* uorPerMas,0,0);//定义直线终点
  7. DSegment3d segment = new DSegment3d(startPo,endPo);//定义几何直线
  8. LineElement line = new LineElement(dgnModel,null, segment);//定义线元素
  9. DPoint3d transInfo = new DPoint3d(0,5*uorPerMas,0);//定义平移信息 在本文中即向上平移五个单位
  10. DTransform3d trans = DTransform3d.FromTranslation(transInfo);//定义变换几何
  11. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  12. line.ApplyTransform(transform);//对线元素应用变换信息
  13. line.AddToModel();//将线元素写入模型
  14. }
  1. 在本案例中,首先在内存中定义了一个起点在基点,端点在(10,0,0)的直线,然后通过DPoint3d定义XYZ三个方向的平移距离,最后直线进行应用。从结果我们可以看到,最初被定义的直线向Y方向平移了5个单位。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643334355990-684b4c4a-9ff7-4e6c-938b-28e7c6597d42.png#clientId=u3f260f35-21a1-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=549&id=ucc471ea9&margin=%5Bobject%20Object%5D&name=image.png&originHeight=549&originWidth=875&originalType=binary&ratio=1&rotation=0&showTitle=false&size=41849&status=done&style=none&taskId=u60bd6e89-38b6-4fd0-b256-76eb5a535c6&title=&width=875)<br />**图4 生成结果**

绕基点轴旋转(Rotation)

  1. 当我们需要对元素进行旋转时,我们可以直接使用DTransform3d.Rotation( )方法,其中,第一个入参定义旋转轴的向量,第二个参数记录旋转角度。
  1. public static void CmdElementRotateWithDefaultAxis(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d startPo = new DPoint3d(-10 * uorPerMas, 0, 0);//定义直线起点
  6. DPoint3d endPo = new DPoint3d(10 * uorPerMas, 0, 0);//定义直线终点
  7. DSegment3d segment = new DSegment3d(startPo, endPo);//定义几何直线
  8. LineElement line = new LineElement(dgnModel, null, segment);//定义线元素
  9. DVector3d vector = DVector3d.UnitZ;//定义旋转轴向量
  10. Angle angle = new Angle();//定义角度
  11. angle.Degrees = 45;//设置旋转角度为45度
  12. DTransform3d trans = DTransform3d.Rotation(vector, angle);//定义变换几何
  13. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  14. line.ApplyTransform(transform);//对线元素应用变换信息
  15. line.AddToModel();//将线元素写入模型
  16. }
  1. 在本案例中,首先创建了一个起点在(-1000),终点在(1000)的直线,然后定义相关属性,这里我们设置其旋转轴方向为Z方向,旋转45度。生成的结果即在线元素生成时旋转了45度。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643334969511-d8da322d-22d0-4530-8b84-de104bc284c8.png#clientId=u3f260f35-21a1-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=552&id=u220d9a74&margin=%5Bobject%20Object%5D&name=image.png&originHeight=552&originWidth=925&originalType=binary&ratio=1&rotation=0&showTitle=false&size=47645&status=done&style=none&taskId=ue3faeeef-2987-44cf-bc33-658837d9c2b&title=&width=925)<br />**图5 生成结果**

绕自定义轴旋转(Rotation)

  1. 当我们需要以指定的基准轴对元素进行旋转时,上方的方法就无法满足要求了,因此我们需要使用DTransform3d.FromRotationAroundLine( ),在这个方法中我们需要首先依次定义旋转基点,旋转轴,旋转角度生成结果。
  1. public static void CmdElementRotateWithCustomAxis(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d startPo = new DPoint3d(-10 * uorPerMas, 0, 0); ;//定义直线起点
  6. DPoint3d endPo = new DPoint3d(10 * uorPerMas, 0, 0);//定义直线终点
  7. DSegment3d segment = new DSegment3d(startPo, endPo);//定义几何直线
  8. LineElement line = new LineElement(dgnModel, null, segment);//定义线元素
  9. DVector3d vector = DVector3d.UnitZ;//定义旋转轴向量
  10. Angle angle = new Angle();//定义角度
  11. angle.Degrees = 45;//设置旋转角度为45度
  12. DTransform3d trans = DTransform3d.FromRotationAroundLine(new DPoint3d(5*uorPerMas,5*uorPerMas,0),vector, angle);//定义变换几何
  13. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  14. line.ApplyTransform(transform);//对线元素应用变换信息
  15. line.AddToModel();//将线元素写入模型
  16. }

在本案例中,我首先首先创建了一个起点在(-10,0,0),终点在(10,0,0)的直线,然后定义相关属性,这里我们设置其旋转轴方向为基点坐标,Z方向,旋转45度,生成的结果即在线元素基于设置的基点生成时旋转了45度。下图中我们可以看到,在模型中创建的起点在(-10,0,0),终点在(10,0,0)的直线,绕设置基点(5,5,0)点旋转45度的结果与程序生成的结果一致。
image.png
图6 生成结果

整体缩放(Scale)

  1. 当我们需要对元素做整体的缩放处理时,就需要使用到DTransform3d.Scale( ),其中的系数指的是缩放的比例。
  1. public static void CmdElementScale1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(-5*uorPerMas,5*uorPerMas,0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(5*uorPerMas,5*uorPerMas,0);
  7. DPoint3d p3 = new DPoint3d(5*uorPerMas,-5*uorPerMas,0);
  8. DPoint3d p4 = new DPoint3d(-5*uorPerMas,-5*uorPerMas);
  9. DPoint3d[] pos = { p1,p2,p3,p4};//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel,null,pos);//使用端点集定义形元素
  11. DTransform3d trans = DTransform3d.Scale(2);//定义变换几何
  12. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  13. shape.ApplyTransform(transform);//对形元素应用变换信息
  14. shape.AddToModel();//将形元素写入模型
  15. }
  1. 在本案例中,首先定义了一个边长为10的形元素,然后我们对这个形元素进行2倍的缩放处理,从结果可以看出,原本边长为10的正方形形元素经过缩放处理后变成了边长为20的形元素。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643340035980-1ebb81ae-026b-46bf-9fdc-1e40d65a2652.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=547&id=u332b0a2d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=547&originWidth=777&originalType=binary&ratio=1&rotation=0&showTitle=false&size=46223&status=done&style=none&taskId=ud7e7d946-0b61-49c9-ab23-637d4ed0893&title=&width=777)<br />**图7 生成结果**

部分缩放(Scale)

  1. 当我们需要对指定的轴线方向进行拉伸缩放时,上面的整体缩放就无法满足我们的需求了,此时需要使用DTransform3d.Scale( ),并分别输入XYZ方向的缩放大小,此时元素将按照三个方向分别进行拉伸。
  1. public static void CmdElementScale2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(-5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(-5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. DTransform3d trans = DTransform3d.Scale(2,1,1);//定义变换几何
  12. TransformInfo transform = new TransformInfo(trans);//使用变换几何定义变换信息
  13. shape.ApplyTransform(transform);//对形元素应用变换信息
  14. shape.AddToModel();//将形元素写入模型
  15. }
  1. 在本案例中,首先定义了一个边长为10的正方形,然后使用部分缩放对X方向进行缩放,Y方向和Z方向不进行缩放,从结果我们可以看到,X方向被拉伸了2倍,Y方向和Z方向未进行拉伸。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643340933952-c0390eeb-c54a-4043-aa70-1f862339466e.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=545&id=u8a293c4d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=545&originWidth=906&originalType=binary&ratio=1&rotation=0&showTitle=false&size=49805&status=done&style=none&taskId=u59e19e08-942f-4dee-8055-f115e83000b&title=&width=906)<br />**图8 生成结果**

镜像(Mirror)

  1. 当我们需要对元素进行镜像时,就需要使用DTransform3d.TryMirrorPointToPoint( ),首先我们需要定义两个镜像点,元素会基于这两个点进行镜像。
  1. public static void CmdElementMirror(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. shape.AddToModel();//将形元素写入模型
  12. DPoint3d mirrorPo1 = new DPoint3d(5 * uorPerMas,0,0);//定义镜像点1
  13. DPoint3d mirrorPo2 = new DPoint3d(-5 * uorPerMas, 0, 0);//定义镜像点2
  14. DTransform3d.TryMirrorPointToPoint(mirrorPo1, mirrorPo2, out DTransform3d mirrorTrans);//根据两镜像点定义变换几何
  15. TransformInfo transform = new TransformInfo(mirrorTrans);//使用变换几何定义变换信息
  16. shape.ApplyTransform(transform);//对形元素应用变换信息
  17. shape.AddToModel();//将形元素写入模型
  18. DSegment3d segment = new DSegment3d(mirrorPo1,mirrorPo2);//定义几何直线
  19. LineElement line = new LineElement(dgnModel,null, segment);//定义线元素
  20. line.AddToModel();//将线元素写入模型
  21. }
  1. 在本案例中,首先我们在X轴右侧创建了一个长为5,宽为10的矩形形元素,然后通过我们定义的两个镜像点对元素进行镜像操作,右侧生成的形元素即为镜像后生成的结果。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643341498054-c7b48cb0-6eeb-4899-94bd-330dcd6f4d8c.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=549&id=u5dd06beb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=549&originWidth=791&originalType=binary&ratio=1&rotation=0&showTitle=false&size=48025&status=done&style=none&taskId=u646c7383-f22e-4c18-bf11-768c2fdfb17&title=&width=791)<br />**图9 生成结果**

复制(Copy)

  1. 当我们需要对元素进行复制时,我们就需要使用到DoCopy( )方法,这种复制的方法不限于在当前模型空间内复制,还可以执行跨模型的复制操作。这里需要注意的是,对ElementCopyContext对象调用Dispose是必要的。ElementCopyContextDispose方法执行一些无法在终结器中完成的任务。如果程序未能调用Dispose,则复制的元素可能不完整。
  1. public static void CmdElementCopy(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. shape.AddToModel();//将形元素写入模型
  12. using (ElementCopyContext context = new ElementCopyContext(dgnModel))
  13. context.DoCopy(shape);
  14. /*
  15. * 在大多数情况下,您不需要设置任何选项。以上代码将图元从一个模型复制到另一个模型,确保复制的图元具有所有属性
  16. * 对ElementCopyContext对象调用Dispose是必要的。ElementCopyContext的Dispose方法执行一些无法在终结器中完成的任务。因此,如果程序未能调用Dispose,则复制的元素可能不完整
  17. */
  18. }
  1. 在本案例中,我们首先创建了一个形元素,然后在当前模型中使用复制方法对形元素进行复制。从结果我们可以看到模型中生成了两个形元素。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643341953293-25fa2339-9e46-47ff-98e0-25f784332411.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=548&id=ufc345e36&margin=%5Bobject%20Object%5D&name=image.png&originHeight=548&originWidth=733&originalType=binary&ratio=1&rotation=0&showTitle=false&size=36209&status=done&style=none&taskId=u60900a05-daf9-4e16-b214-5082d6a061b&title=&width=733)<br />**图10 生成结果**

删除(Delete)

  1. 当我们对元素进行管理时,肯定会对元素执行删除操作,使用到的方法即为DeleteFromModel( )。
  1. public static void CmdElementDelete(string unparsed)//Case:ElementDeleteExample
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. long id = 1428;//定义需要获取的元素ID
  5. Element elem= dgnModel.FindElementById(new ElementId(ref id));//使用ID在模型中寻找所属元素
  6. if(elem!=null)//判断元素是否被成功获取
  7. {
  8. elem.DeleteFromModel();//在模型中删除获取到的元素
  9. }
  10. }
  1. 在本案例中,我们预先在模型中创建了一个形元素,我们在属性界面获得该元素的ID,然后使用元素ID找到对应的元素,使用DeleteFromModel( )删除元素。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643343096433-106b2a7b-4536-4600-bc88-f7a7edf43ca0.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=550&id=u292328a8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=550&originWidth=797&originalType=binary&ratio=1&rotation=0&showTitle=false&size=65955&status=done&style=none&taskId=ua5bb6d12-0a13-4aef-8247-2a26fe18b72&title=&width=797)<br />**图11 元素属性界面**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643343329741-9b280cdc-a442-4151-8c25-0a7a0240cd23.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=547&id=uacd610c2&margin=%5Bobject%20Object%5D&name=image.png&originHeight=547&originWidth=913&originalType=binary&ratio=1&rotation=0&showTitle=false&size=42813&status=done&style=none&taskId=u3ae6aa1a-ceec-437b-a802-ecb04c8f94d&title=&width=913)<br />**图12 执行结果**

填充

实体填充(SolidFill)

我们可以使用AddSolidFill( )对元素进行实体填充,其中,第一个值为填充颜色索引值,与MicroStation中的颜色索引对应。

  1. public static void CmdElementSolidFill(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. shape.AddSolidFill(3,true);
  12. /*
  13. * 对支持的元素添加实体填充
  14. * fillColor:填充颜色,若与边框颜色一致则输入null
  15. * alwaysFilled:是否遵守“填充视图”属性
  16. */
  17. shape.AddToModel();//将形元素写入模型
  18. }
  1. 我们可以看到,生成元素的填充已被修改为索引为3(红色)的实体填充。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643348259490-96da9513-1824-49f7-8092-d74078c93f32.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=540&id=u1039801d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=540&originWidth=917&originalType=binary&ratio=1&rotation=0&showTitle=false&size=45187&status=done&style=none&taskId=u6acf36fb-cc32-48a4-83c8-8afb803519a&title=&width=917)<br />**图13 生成元素属性**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643348675418-d4811c6c-2da2-4e9b-a992-4e0166b75230.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=547&id=u1f8efb52&margin=%5Bobject%20Object%5D&name=image.png&originHeight=547&originWidth=923&originalType=binary&ratio=1&rotation=0&showTitle=false&size=24136&status=done&style=none&taskId=uc4732bbe-6288-4a93-801d-dcb207989af&title=&width=923)<br />**图14 生成结果**

梯度填充(GradientFill)

我们可以使用AddGradientFill( )对元素进行渐变填充,在附加填充之前,需要首先对梯度填充属性进行定义,可定义的属性包括渐变颜色,渐变程度,填充角度,填充色调等。

  1. public static void CmdElementGradientFill(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. GradientSymbology gSymb = new GradientSymbology();//定义梯度填充设置集
  12. RgbColorDef[] colors = new RgbColorDef[2];//定义颜色容器
  13. double[] values = new double[2];//定义储存填充程度容器
  14. colors[0] = new RgbColorDef(255, 0, 0);//设置颜色定义
  15. colors[1] = new RgbColorDef(0, 0, 255);
  16. values[0] = 0.0;//设置填充程度定义
  17. values[1] = 1.0;
  18. gSymb.Mode = GradientMode.None;//设置梯度填充模式
  19. gSymb.Angle = 10.0;//设置梯度填充角度
  20. gSymb.Tint = 2.0;//设置梯度填充色调
  21. gSymb.Shift = 3.0;//设置梯度填充转换
  22. gSymb.SetKeys(colors, values);//将填充颜色与程度添加到设置集
  23. shape.AddGradientFill(gSymb);//对形元素添加渐变填充
  24. shape.AddToModel();//将形元素导入模型空间
  25. }
  1. 我们可以看到,生成元素的填充已被修改为我们所定义的渐变填充。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643348594918-2a272b60-552a-415d-8848-2ff0e28d1830.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=546&id=u622de249&margin=%5Bobject%20Object%5D&name=image.png&originHeight=546&originWidth=919&originalType=binary&ratio=1&rotation=0&showTitle=false&size=46411&status=done&style=none&taskId=u539646db-993b-4c11-97d1-1bf9eb53246&title=&width=919)<br />**图15 生成元素属性**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643348644974-fd939454-f65a-43f2-88d4-7226adad2719.png#clientId=u98b7e6b9-f304-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=536&id=u7abc9e2c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=536&originWidth=914&originalType=binary&ratio=1&rotation=0&showTitle=false&size=50789&status=done&style=none&taskId=u22b2a396-2955-4ea1-af2e-b00c23783a9&title=&width=914)<br />**图16 生成结果**

图案填充(PatternFill)

  1. 当我们需要使用图案进行填充,或者使用二维进行出图操作时,往往需要使用特定的填充图案指代材质。在这个过程中可能就会涉及到需要与CAD中的图案填充效果保持一致,那么就需要使用图案填充实现。在属性中,我们不仅可以规定填充颜色,间距,还可以通过.pat文件中的信息构建出我们所需要的图案填充。<br /> 关于制作图案填充的更多信息,请参考:[[MSCE]关于图案填充的问题](https://communities.bentley.com/communities/other_communities/chinafirst/f/microstation-projectwise/141119/msce)
  1. public static void CmdElementPatternFill(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. PatternParams param = new PatternParams();//初始化模式定义
  12. param.HoleStyle = PatternParamsHoleStyleType.Normal;//设置孔洞样式为普通
  13. param.Color = 5;//设置颜色为紫色(索引为5)
  14. param.PrimarySpacing = 0.1*uorPerMas;//设置线段间隔距离为0.1m
  15. DwgHatchDefLine defLine = new DwgHatchDefLine();//初始化DWG填充定义
  16. DwgHatchDefLine[] defLines = { defLine };//将DWG填充定义放入其数组中
  17. shape.AddPattern(param, defLines, 0);//对形元素添加图案填充
  18. shape.AddToModel();//将形元素导入模型空间
  19. }

材质填充(MaterialFill)

  1. 当我们需要对元素进行材质填充时,需要首先找到存放该材质的材质表和材质图表,最后将材质挂接到元素上,实现材质填充的最终效果。
  1. public static void CmdElementMaterialFill(string unparsed)
  2. {
  3. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//定义当前激活的文件信息
  4. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  5. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  6. MaterialTable matTbl = new MaterialTable(dgnFile);//定义材料表
  7. matTbl.Name = "MyMaterialTable";//定义材料表名称
  8. PaletteInfo[] palInfo = MaterialManager.GetPalettesInSearchPath("MS_MATERIAL");//从MS_MATERIAL的环境变量定义路径下读取材料图表
  9. if (palInfo.Length < 1)//判断是否获取到材料图表
  10. {
  11. MessageCenter.Instance.ShowInfoMessage("Can't get palette", null, true);//输出错误信息
  12. return;//返回
  13. }
  14. for (int i = 0; i < palInfo.Count(); i++)//遍历材料图表
  15. {
  16. if (palInfo[i].Name == "lightwidgets")//判断材料图表是否名为lightwidgets
  17. {
  18. matTbl.AddPalette(palInfo[i]);//添加材料图表至材料表
  19. break;//跳出循环
  20. }
  21. else if (i == palInfo.Count() - 1)//若未找到名为lightwidgets的材料图表
  22. {
  23. MessageCenter.Instance.ShowErrorMessage("Can't find material lib named lightwidgets, please check",
  24. "Can't find material lib named lightwidgets, please check",
  25. true);//输出错误信息
  26. }
  27. }
  28. MaterialManager.SetActiveTable(matTbl, dgnModel);//设置当前材料表为激活图表
  29. MaterialManager.SaveTable(matTbl);//保存材料表
  30. MaterialId id = new MaterialId("Pavers BC2");//查找名为Pavers BC2的材料
  31. DPoint3d[] pos = { DPoint3d.Zero, new DPoint3d(uorPerMas, 0, 0), new DPoint3d(uorPerMas, uorPerMas, 0) };//定义坐标数组
  32. ShapeElement shape = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, pos);//定义形元素的实例
  33. SurfaceOrSolidElement solid = SolidElement.CreateProjectionElement(dgnModel, null, shape, new DPoint3d(1000, 0, 0),
  34. new DVector3d(0, 0, 10*uorPerMas), DTransform3d.Identity, true);//定义拉伸实体元素
  35. MaterialPropertiesExtension propertiesExtension = MaterialPropertiesExtension.GetAsMaterialPropertiesExtension(solid);//为拉伸实体元素设置材料属性
  36. propertiesExtension.AddMaterialAttachment(id);//添加嵌入的材料信息
  37. propertiesExtension.StoresAttachmentInfo(id);//保存拉伸实体元素的材料信息
  38. propertiesExtension.AddToModel();//将拉伸实体写入模型
  39. }
  1. 在本案例中,我们需要挂接名为Pavers BC2的材质,因此我们需要首先找到其所在的材料图表——lightwidgets,然后在图表中根据名称获得。材质图表需要放置在环境变量MS_MATERIAL指定的路径下才可被成功获取。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643349687621-d562033b-0c4d-4469-a617-c40871063088.png#clientId=uc7750905-e612-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=547&id=ub211f43b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=547&originWidth=922&originalType=binary&ratio=1&rotation=0&showTitle=false&size=55908&status=done&style=none&taskId=u9b32050c-84e2-4c86-955f-58c9a490e92&title=&width=922)<br />**图17 生成元素材料属性**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643349707970-b967d087-288a-4a54-8922-952d9eae60d2.png#clientId=uc7750905-e612-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=542&id=u5e8ca3f1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=542&originWidth=926&originalType=binary&ratio=1&rotation=0&showTitle=false&size=74608&status=done&style=none&taskId=u0efca304-ccdc-4730-9302-7f3222a4cd6&title=&width=926)<br />**图18 生成结果**

透明度(Transpancy)

  1. 当我们需要做出类似透视得效果或者机制时,但是又不想让外部构件直接隐藏,体现内外部构件的相互关系,此时可能会需要将外部构件的透明度提高。
  1. public static void CmdChangeElementTranspancy(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//定义当前激活的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. DPoint3d p1 = new DPoint3d(5 * uorPerMas, 5 * uorPerMas, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 5 * uorPerMas, 0);
  7. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -5 * uorPerMas, 0);
  8. DPoint3d p4 = new DPoint3d(5 * uorPerMas, -5 * uorPerMas);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将端点添加到端点集中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//使用端点集定义形元素
  11. ElementPropertiesSetter setter = new ElementPropertiesSetter();//用于修改元素属性
  12. setter.SetColor((uint)4);//设置元素颜色索引为4
  13. setter.SetTransparency(0.5);//设置透明度为50
  14. setter.Apply(shape);//将属性修改应用于形元素
  15. shape.AddToModel();//将形元素写入模型空间
  16. }
  1. 在本案例中,首先将形元素的颜色索引值设置为4(黄色),然后对透明度进行调整。需要说明的是,ElementPropertiesSetter不仅不可修改元素颜色,透明度,还可以对字体,图层,线形样式等多种属性进行设置调整。<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643350513460-cc36def4-8af6-484b-97da-75237d52fad7.png#clientId=uc7750905-e612-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=547&id=u0e6ccd82&margin=%5Bobject%20Object%5D&name=image.png&originHeight=547&originWidth=917&originalType=binary&ratio=1&rotation=0&showTitle=false&size=44382&status=done&style=none&taskId=ubc0c57dd-03c7-4be0-8f32-3e77ce4ceb6&title=&width=917)<br />**图19 生成元素属性**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643350569851-29646127-09c2-4c06-b1b5-f6678a64cb74.png#clientId=uc7750905-e612-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=537&id=u151bcde3&margin=%5Bobject%20Object%5D&name=image.png&originHeight=537&originWidth=918&originalType=binary&ratio=1&rotation=0&showTitle=false&size=23721&status=done&style=none&taskId=ub617076c-9ede-468f-8635-f29cf36e751&title=&width=918)<br />**图20 生成结果**

布尔运算

  1. 注意:创建的实核实体大小应控制在500m以内,否则可能会生成失败。若需要生成的实核实体范围缺失大于500m,那么可以先将元素缩小进行布尔运算,得到结果再进行元素放大操作即可。

布尔交计算(BooleanIntersect)

  1. 该方法用于三维实体,取得实体集中的共有部分生成一个新的智能实体。需要注意的是,创建的实核实体大小应控制在500m以内,否则可能会生成失败。首先我们需要将元素转换为实核实体,然后使用实核实体执行布尔交计算得到最终结果。
  1. public static void CmdBooleanIntersectElement(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. #region CreateSolidElement
  6. #region Create profile
  7. DPoint3d p1 = new DPoint3d(-1* uorPerMas, uorPerMas, 0);//定义形元素端点
  8. DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
  9. DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
  10. DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
  11. DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
  12. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  13. #endregion
  14. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  15. DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
  16. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
  17. solid.AddToModel();//将实体元素写入模型
  18. #endregion
  19. #region CreateConeElement
  20. 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);//定义圆台实体
  21. cone.AddToModel();//将圆台实体元素写入模型
  22. #endregion
  23. Convert1.ElementToBody(out SolidKernelEntity entity1,solid,true,false,false);//将实体转成SolidKernelEntity
  24. Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
  25. SolidKernelEntity[] entities = {entity2};//定义实核实体集
  26. Modify.BooleanIntersect(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔交运算
  27. Convert1.BodyToElement(out Element resultElem, entity1, null,dgnModel);//将结果转换为元素
  28. resultElem.AddToModel();//将元素写入模型空间
  29. }

image.png
图21 布尔交生成结果
image.png
图22 布尔交生成结果

布尔减运算(BooleanSubtract)

  1. 该方法用于三维实体,去除被剪切实核实体与用于剪切的实核实体集中共有的部分,生成一个新的智能实体。首先我们需要将元素转换为实核实体,然后使用实核实体执行布尔减计算得到最终结果。
  1. public static void CmdBooleanSubtractElement(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. #region CreateSolidElement
  6. #region Create profile
  7. DPoint3d p1 = new DPoint3d(-1 * uorPerMas, uorPerMas, 0);//定义体元素端点
  8. DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
  9. DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
  10. DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
  11. DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
  12. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  13. #endregion
  14. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  15. DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
  16. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
  17. solid.AddToModel();//将实体元素写入模型
  18. #endregion
  19. #region CreateConeElement
  20. 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);//定义圆台实体
  21. cone.AddToModel();//将圆台实体元素写入模型
  22. #endregion
  23. Convert1.ElementToBody(out SolidKernelEntity entity1, solid, true, false, false);//将实体转成SolidKernelEntity
  24. Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
  25. SolidKernelEntity[] entities = { entity2 };//定义实核实体集
  26. Modify.BooleanSubtract(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔减运算
  27. Convert1.BodyToElement(out Element resultElem, entity1, null, dgnModel);//将结果转换为元素
  28. resultElem.AddToModel();//将元素写入模型空间
  29. }

image.png
图23 布尔减生成结果
image.png
图24 布尔减生成结果

布尔并运算(BooleanUnion)

  1. 该方法用于三维实体,取得实体集中的并集部分生成一个新的智能实体。首先我们需要将元素转换为实核实体,然后使用实核实体执行布尔并计算得到最终结果。
  1. public static void CmdBooleanUnionElement(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. #region CreateSolidElement
  6. #region Create profile
  7. DPoint3d p1 = new DPoint3d(-1 * uorPerMas, uorPerMas, 0);//定义体元素端点
  8. DPoint3d p2 = new DPoint3d(uorPerMas, uorPerMas, 0);
  9. DPoint3d p3 = new DPoint3d(uorPerMas, -uorPerMas, 0);
  10. DPoint3d p4 = new DPoint3d(-uorPerMas, -uorPerMas, 0);
  11. DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
  12. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  13. #endregion
  14. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  15. DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
  16. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
  17. solid.AddToModel();//将实体元素写入模型
  18. #endregion
  19. #region CreateConeElement
  20. 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);//定义圆台实体
  21. cone.AddToModel();//将圆台实体元素写入模型
  22. #endregion
  23. Convert1.ElementToBody(out SolidKernelEntity entity1, solid, true, false, false);//将实体转成SolidKernelEntity
  24. Convert1.ElementToBody(out SolidKernelEntity entity2, cone, true, false, false);//将圆台实体元素转成SolidKernelEntity
  25. SolidKernelEntity[] entities = { entity2 };//定义实核实体集
  26. Modify.BooleanUnion(ref entity1, ref entities, entities.Count());//用实核实体集中的实体与实体进行布尔并运算
  27. Convert1.BodyToElement(out Element resultElem, entity1, null, dgnModel);//将结果转换为元素
  28. resultElem.AddToModel();//将元素写入模型空间
  29. }

image.png
图25 布尔并生成结果
image.png
图26 布尔并生成结果

布尔切割运算(BooleanCut)

  1. 该方法用于一条曲线以某个方向对实核实体进行切割的操作。首先我们需要将被切割的元素转化为实核实体,然后使用曲线对实核实体进行切割生成切割后的结果。在本案例中,首先创建了一条用于切割的B样条曲线,然后使用B样条曲线定义CurveVector,在使用方向向量定义切割方向后对转换为实核实体后的元素切割,最终生成切割后的智能实体。
  1. public static void CmdBooleanCutElement(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//获取主单位与分辨率单位的比值
  5. #region CreateSolidElement
  6. #region Create profile
  7. DPoint3d p1 = new DPoint3d(-10 * uorPerMas, 10*uorPerMas, 0);//定义体元素端点
  8. DPoint3d p2 = new DPoint3d(10 * uorPerMas, 10*uorPerMas, 0);
  9. DPoint3d p3 = new DPoint3d(10 * uorPerMas, -10 * uorPerMas, 0);
  10. DPoint3d p4 = new DPoint3d(-10 * uorPerMas, -10 * uorPerMas, 0);
  11. DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到面元素端点数组中
  12. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  13. #endregion
  14. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  15. DVector3d extrudeVector = new DVector3d(0, 0, uorPerMas);//定义拉伸向量
  16. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//定义实体元素
  17. solid.AddToModel();//将实体元素写入模型
  18. #endregion
  19. Convert1.ElementToBody(out SolidKernelEntity entity, solid, true, false, false);//将实体转成SolidKernelEntity
  20. #region CreateBSplineCurveElement
  21. DPoint3d[] poles = new DPoint3d[6];//定义B样条曲线的节点
  22. poles[0] = new DPoint3d(0, 10*uorPerMas, 0);//定义节点坐标
  23. poles[1] = new DPoint3d(-5*uorPerMas, 5*uorPerMas, 0);
  24. poles[2] = new DPoint3d(0,0,0);
  25. poles[3] = new DPoint3d(5*uorPerMas,-5*uorPerMas,0);
  26. poles[4] = new DPoint3d(2*uorPerMas, -8 * uorPerMas, 0);
  27. poles[5] = new DPoint3d(0, -10 * uorPerMas, 0);
  28. MSBsplineCurve curve = MSBsplineCurve.CreateFromPoles(poles, null, null, 4, false, false);//定义B样条曲线
  29. BSplineCurveElement bSplineCurve = new BSplineCurveElement(dgnModel,null, curve);//定义B样条曲线元素
  30. bSplineCurve.AddToModel();//将B样条曲线元素写入模型
  31. #endregion
  32. DVector3d vector = DVector3d.UnitZ;//定义切割法线
  33. Modify.BooleanCut(ref entity, bSplineCurve.GetCurveVector(),Modify.CutDirectionMode.Both,Modify.CutDepthMode.All, 0, false, vector, 0);//用实核实体集中的实体与实体进行布尔切割运算
  34. Convert1.BodyToElement(out Element resultElem, entity, null, dgnModel);//将结果转换为元素
  35. resultElem.AddToModel();//将元素写入模型空间
  36. }

image.png
图27 布尔切割生成结果
image.png
图28 布尔切割生成结果

巩固练习(二)参考代码

实现思路:首先创建形元素和拉伸向量后创建拉伸实体,然后再根据放置的位置创建文字元素和标注元素。

  1. public static void CmdPracticeWork(string unparsed)
  2. {
  3. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//定义当前激活的文件
  4. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  5. #region Create profile
  6. int H = 700 * 10000;
  7. int H1 =125 * 10000, H2 = 125 * 10000;
  8. int H3 = 275 * 10000;
  9. int H4 = 75 * 10000, B4 = 75 * 10000;
  10. int H5 = 100 * 10000;
  11. int B3 = 125 * 10000;
  12. int B1 = 400 * 10000;
  13. int B2 = 300 * 10000;
  14. int B = 150 * 10000;
  15. DPoint3d p1 = new DPoint3d(-1*0.5*B1, 0, 0);//定义体元素端点
  16. DPoint3d p2 = new DPoint3d(-1*0.5*B1,H2,0);
  17. DPoint3d p3 = new DPoint3d(-0.5*B,H2+H5,0);
  18. DPoint3d p4 = new DPoint3d(-0.5*B,H2+H5+H3,0);
  19. DPoint3d p5 = new DPoint3d(-0.5*B2,H2+H5+H3+H4, 0);
  20. DPoint3d p6 = new DPoint3d(-0.5*B2,H, 0);
  21. DPoint3d p7 = new DPoint3d(0.5*B2,H,0);
  22. DPoint3d p8 = new DPoint3d(0.5*B2, H2 + H5 + H3 + H4,0);
  23. DPoint3d p9 = new DPoint3d(0.5 * B, H2 + H5 + H3,0);
  24. DPoint3d p10 = new DPoint3d(0.5 * B,H2 + H5, 0);
  25. DPoint3d p11 = new DPoint3d(0.5 * B1, H2,0);
  26. DPoint3d p12 = new DPoint3d(0.5 * B1, 0, 0);
  27. DPoint3d[] pos = { p1, p2, p3, p4,p5,p6,p7,p8,p9,p10,p11,p12 };//将面元素端点添加到面元素端点数组中
  28. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  29. #endregion
  30. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  31. DVector3d extrudeVector = new DVector3d(0, 0,-12000*10000);//定义拉伸向量
  32. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);//使用投影的方式定义拉伸体元素
  33. solid.AddToModel();//将拉伸体写入模型
  34. DPoint3d d1 = new DPoint3d(-0.5*B2, H + 50*10000,0);
  35. DPoint3d d2 = new DPoint3d(0.5*B2, H+ 50 * 10000,0);
  36. DPoint3d[] dimensionPos1 = { d1, d2 };
  37. DimensionElement dimEle1 = CreateDimensionElement(dgnFile,dgnModel , dimensionPos1);
  38. dimEle1.AddToModel();
  39. DPoint3d d3 = new DPoint3d(-0.5*B2, H + 10 * 10000,0);
  40. DPoint3d d4 = new DPoint3d(-0.5*B, H+ 10 * 10000,0);
  41. DPoint3d d5 = new DPoint3d(0.5*B, H + 10 *10000, 0);
  42. DPoint3d d6 = new DPoint3d(0.5*B2,H + 10 *10000, 0);
  43. DPoint3d[] dimensionPos2 = { d3, d4,d5,d6 };
  44. DimensionElement dimEle2 = CreateDimensionElement(dgnFile, dgnModel, dimensionPos2);
  45. dimEle2.AddToModel();
  46. TextBlockProperties txtBlockProp = new TextBlockProperties(dgnModel);//定义文本属性
  47. txtBlockProp.IsViewIndependent = false;//设置文本非独立于视图
  48. ParagraphProperties paraProp = new ParagraphProperties(dgnModel);//定义段落属性
  49. DgnTextStyle txtStyle = DgnTextStyle.GetSettings(dgnFile);//从激活的文件中定义文字样式
  50. RunProperties runProp = new RunProperties(txtStyle, dgnModel);//定义运行属性
  51. TextBlock txtBlock = new TextBlock(txtBlockProp, paraProp, runProp, dgnModel);//定义文本块
  52. txtBlock.AppendText("Side View");//设置文本块文字内容
  53. DPoint3d txtOrigin = new DPoint3d(0,-2*10000,0);
  54. txtBlock.SetUserOrigin(txtOrigin);//设置文本块放置位置
  55. TextElement text = (TextElement)TextElement.CreateElement(null, txtBlock);//定义文本元素
  56. DTransform3d trans = DTransform3d.Scale(40);
  57. TransformInfo transInfo = new TransformInfo(trans);
  58. text.ApplyTransform(transInfo);
  59. text.AddToModel();//将生成的文本元素写入模型
  60. }
  61. private static DimensionElement CreateDimensionElement(DgnFile dgnFile, DgnModel dgnModel, DPoint3d[]pos)
  62. {
  63. //获取当前dgn文件中名字为"DimStyle"的标注样式,尺寸标注元素的外貌由上百个属性控制,而标注样式是一组预先设置好的属性
  64. //获取了预先订制好的标注样式之后,还可以调用DimensionStyle下的各种SetXXX成员函数修改设置的属性
  65. DimensionStyle dimStyle = new DimensionStyle("DimStyle", dgnFile);//定义标注样式
  66. dimStyle.SetBooleanProp(true, DimStyleProp.Placement_UseStyleAnnotationScale_BOOLINT);//设置标注样式
  67. dimStyle.SetDoubleProp(1, DimStyleProp.Placement_AnnotationScale_DOUBLE);
  68. dimStyle.SetBooleanProp(true, DimStyleProp.Text_OverrideHeight_BOOLINT);
  69. dimStyle.SetDistanceProp(20 * 10000, DimStyleProp.Text_Height_DISTANCE, dgnModel);
  70. dimStyle.SetBooleanProp(true, DimStyleProp.Text_OverrideWidth_BOOLINT);
  71. dimStyle.SetDistanceProp(20 * 10000, DimStyleProp.Text_Width_DISTANCE, dgnModel);
  72. dimStyle.SetBooleanProp(true, DimStyleProp.General_UseMinLeader_BOOLINT);
  73. dimStyle.SetDoubleProp(0.01, DimStyleProp.Terminator_MinLeader_DOUBLE);
  74. dimStyle.SetBooleanProp(true, DimStyleProp.Value_AngleMeasure_BOOLINT);
  75. dimStyle.SetAccuracyProp((byte)AnglePrecision.Use1Place, DimStyleProp.Value_AnglePrecision_INTEGER);
  76. int alignInt = (int)DimStyleProp_General_Alignment.True;
  77. StatusInt status = dimStyle.SetIntegerProp(alignInt, DimStyleProp.General_Alignment_INTEGER);
  78. dimStyle.GetIntegerProp(out int valueOut, DimStyleProp.General_Alignment_INTEGER);
  79. DgnTextStyle textStyle = new DgnTextStyle("TestStyle", dgnFile);//设置文字样式
  80. LevelId lvlId = Settings.GetLevelIdFromName("Default");//设置图层
  81. CreateDimensionCallbacks callbacks = new CreateDimensionCallbacks(dimStyle, textStyle, new Symbology(), lvlId, null);//尺寸标注元素的构造函数会调用DimensionCreateData的各个成员函数去获取定义尺寸标注元素需要的各种参数
  82. DimensionElement dimEle = new DimensionElement(dgnModel, callbacks, DimensionType.SizeStroke);//定义标注元素
  83. if (dimEle.IsValid)//判断标注元素是否有效
  84. {
  85. for(int i=0;i<pos.Count();i++)
  86. {
  87. dimEle.InsertPoint(pos[i], null, dimStyle, -1);//对标注元素设置插入点
  88. }
  89. dimEle.SetHeight(50*10000);//设置尺寸标注元素的高度
  90. dimEle.SetRotationMatrix(DMatrix3d.Identity);//设置变换信息
  91. }
  92. return dimEle;
  93. }
  94. class CreateDimensionCallbacks : DimensionCreateData
  95. {
  96. private DimensionStyle m_dimStyle;
  97. private DgnTextStyle m_textStyle;
  98. private Symbology m_symbology;
  99. private LevelId m_levelId;
  100. private DirectionFormatter m_directionFormatter;
  101. public CreateDimensionCallbacks(DimensionStyle dimStyle, DgnTextStyle textStyle, Symbology symb, LevelId levelId, DirectionFormatter formatter)
  102. {
  103. m_dimStyle = dimStyle;
  104. m_textStyle = textStyle;
  105. m_symbology = symb;
  106. m_levelId = levelId;
  107. m_directionFormatter = formatter;
  108. }
  109. public override DimensionStyle GetDimensionStyle()
  110. {
  111. return m_dimStyle;
  112. }
  113. public override DgnTextStyle GetTextStyle()
  114. {
  115. return m_textStyle;
  116. }
  117. public override Symbology GetSymbology()
  118. {
  119. return m_symbology;
  120. }
  121. public override LevelId GetLevelId()
  122. {
  123. return m_levelId;
  124. }
  125. public override int GetViewNumber()
  126. {
  127. return 0;
  128. }
  129. //此函数返回的旋转矩阵与GetViewRotation返回的旋转矩阵共同定义了尺寸标注元素的方向
  130. public override DMatrix3d GetDimensionRotation()
  131. {
  132. return DMatrix3d.Identity;
  133. }
  134. public override DMatrix3d GetViewRotation()
  135. {
  136. return DMatrix3d.Identity;
  137. }
  138. //用于从数字方向值构造字符串。
  139. public override DirectionFormatter GetDirectionFormatter()
  140. {
  141. return m_directionFormatter;
  142. }
  143. }

MicroStation二次开发基础教学(三) - 图11
图29 生成结果展示
MicroStation二次开发基础教学(三) - 图12
图30 生成结果展示