第二期视频回放

点击查看【bilibili】

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

代码及课件下载链接

第二期直播内容文字版

  1. 本期主要介绍了以下十几种元素的创建方法,您可以使用上方的链接直接下载工程,打开工程文件后若您的MicroStation安装在默认路径中,则可以直接编译,使用定义的命令查看效果以供学习。若您的MicroStation软件未安装在默认路径,则您可以参考以下文章,修改路径后即可正确运行。<br />[零基础怎么学MicroStation的二次开发](https://www.yuque.com/docs/share/19180260-82e2-4e6a-be60-5bbc87569b32?# 《零基础怎么学MicroStation的二次开发》)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21640708/1643251345606-969b6ee7-03a4-4710-936d-6794e854fa55.png#clientId=ub16ced0d-17a8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=731&id=u16750e93&margin=%5Bobject%20Object%5D&name=image.png&originHeight=731&originWidth=1306&originalType=binary&ratio=1&rotation=0&showTitle=false&size=78397&status=done&style=none&taskId=u3359436d-8b6d-4507-9f34-9da624b6bb9&title=&width=1306)<br />**图1 本次课程介绍元素一览**

线

线元素(LineElement)

  1. 该创建方式为首先创建坐标,然后使用坐标创建直线几何,再使用直线几何创建直线元素。
  1. public static void CmdCreateLine(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义三维点
  5. DPoint3d p2 = new DPoint3d(10000, 0, 0);
  6. DSegment3d segment = new DSegment3d(p1, p2);//定义直线图元
  7. LineElement line = new LineElement(dgnModel, null, segment);//定义直线元素
  8. line.AddToModel();//将直线元素写入模型空间
  9. }

image.png
图2 绘制结果

点串元素(PointStringElement)

  1. 第一种创建点串的方法为首先创建一个用于存储点串坐标点的数组,然后使用这个数组创建点串元素。
  1. public static void CmdCreatePointString1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义三维点
  5. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  6. DPoint3d p3 = new DPoint3d(20000, 0, 0);
  7. DPoint3d p4 = new DPoint3d(30000, -10000, 0);
  8. DPoint3d p5 = new DPoint3d(40000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4, p5 };//将三维点添加到三维点数组中
  10. PointStringElement pointString = new PointStringElement(dgnModel, null, pos, true);//定义点串
  11. pointString.AddToModel();//将点串元素写入模型空间
  12. }

image.png
图3 绘制结果
第二种创建点串的方法为首先创建一个用于存储点串坐标点的数组,同时可以使用变换矩阵对点坐标位置进行修改,最后使用这个数组创建点串元素。

  1. public static void CmdCreatePointString2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义三维点
  5. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  6. DPoint3d p3 = new DPoint3d(20000, 0, 0);
  7. DPoint3d p4 = new DPoint3d(30000, -10000, 0);
  8. DPoint3d p5 = new DPoint3d(40000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4, p5 };//将三维点添加到三维点数组中
  10. DMatrix3d m1 = DMatrix3d.Identity;//定义变换矩阵
  11. DMatrix3d m2 = DMatrix3d.Identity;
  12. DMatrix3d m3 = DMatrix3d.Identity;
  13. DMatrix3d m4 = DMatrix3d.Identity;
  14. DMatrix3d m5 = DMatrix3d.Identity;
  15. DMatrix3d[] ms = { m1, m2, m3, m4, m5 };//将变换矩阵添加到变换矩阵数组中
  16. PointStringElement pointString = new PointStringElement(dgnModel, null, pos, ms, true);//定义点串
  17. pointString.AddToModel();//将点串元素写入模型空间
  18. }

image.png
图4 绘制结果

线串元素(LineStringElement)

  1. 线串元素的创建方法为首先创建用于储存线串元素端点的坐标数组,然后使用这个数组创建线串元素。
  1. public static void CmdCreateLineString(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义三维点
  5. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  6. DPoint3d p3 = new DPoint3d(20000, 0, 0);
  7. DPoint3d p4 = new DPoint3d(30000, -10000, 0);
  8. DPoint3d p5 = new DPoint3d(40000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4, p5 };//将三维点添加到三维点数组中
  10. LineStringElement lineString = new LineStringElement(dgnModel,null,pos);//定义线串元素
  11. lineString.AddToModel();//将线串元素写入模型空间
  12. }

image.png
图5 绘制结果

弧元素(ArcElement)

  1. 第一种弧元素的创建方式是首先使用定义的圆心,长轴矢量与短轴矢量,构建出几何椭圆,然后利用定义的几何椭圆定义弧元素。
  1. public static void CmdCreateArc1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心
  5. DVector3d vector0 = new DVector3d(0, 20000, 0);//定义长轴矢量
  6. DVector3d vector90 = new DVector3d(-10000, 0, 0);//定义短轴矢量
  7. DEllipse3d ellipse = new DEllipse3d(centerPo, vector0, vector90);//定义几何椭圆
  8. ArcElement arc = new ArcElement(dgnModel, null, ellipse);//使用几何椭圆定义弧元素
  9. arc.AddToModel();//将弧元素写入模型空间
  10. }

image.png
图6 绘制结果
第二种弧元素的创建方式是使用定义圆心坐标,设定弧元素的主轴长,副轴长,旋转角,起始旋转角,扫掠角属性参数创建弧元素。

  1. public static void CmdCreateArc2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心
  5. ArcElement arc = new ArcElement(dgnModel, null, DPoint3d.Zero, 50000, 50000, 0, Angle.PI.Radians / 2, Angle.PI.Radians / 2);//使用几何椭圆定义弧元素
  6. /*
  7. * 定义弧元素
  8. * dgnModel:定义弧元素所在模型空间
  9. * templateElement:元素模板
  10. * center:弧元素的圆心
  11. * axis1:主轴长
  12. * axis2:副轴长
  13. * rotation:圆弧旋转角
  14. * start:圆弧起始旋转角
  15. * sweep:圆弧扫掠角
  16. */
  17. arc.AddToModel();//将弧元素写入模型空间
  18. }

image.png
图7 绘制结果
第二种弧元素的创建方式是使用定义圆心坐标,设定弧元素的主轴长,副轴长,圆弧变换矩阵(圆弧旋转角),起始旋转角,扫掠角属性参数创建弧元素。

  1. public static void CmdCreateArc3(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心
  5. DMatrix3d matrix = DMatrix3d.Identity;//定义变换矩阵
  6. ArcElement arc = new ArcElement(dgnModel, null, centerPo, 50000, 50000, DMatrix3d.Identity, Angle.PI.Radians / 3, Angle.PI.Radians);
  7. /*
  8. * 定义弧元素
  9. * dgnModel:定义弧元素所在模型空间
  10. * templateElement:元素模板
  11. * center:弧元素的圆心
  12. * axis1:主轴长
  13. * axis2:副轴长
  14. * rotation:圆弧变换矩阵(圆弧旋转角)
  15. * start:圆弧起始旋转角
  16. * sweep:圆弧扫掠角
  17. */
  18. arc.AddToModel();//将弧元素写入模型空间
  19. }

image.png
图8 绘制结果

复杂形元素(ComplexStringElement)

  1. 复杂形元素是将构成元素添加到复杂形元素的子部件中,添加完成后生成。需要注意的是,复杂元素子部件构成的线形需要首尾相连,且存在顺序要求,即前者的末端连接后者的首端,否则可能会出现生成的复杂形元素不符合预期的情况。
  1. public static void CmdCreateComplexString(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create line element
  5. DPoint3d p1 = DPoint3d.Zero;//定义三维点
  6. DPoint3d p2 = new DPoint3d(10000, 0, 0);
  7. DSegment3d segment = new DSegment3d(p1, p2);//定义直线图元
  8. LineElement line = new LineElement(dgnModel, null, segment);//定义直线元素
  9. #endregion
  10. #region Create arc element
  11. DPoint3d centerPo = DPoint3d.Zero;//定义圆心
  12. DMatrix3d matrix = DMatrix3d.Identity;//定义变换矩阵
  13. ArcElement arc = new ArcElement(dgnModel, null, centerPo, 10000, 10000, DMatrix3d.Identity, 0, Angle.PI.Radians/2);//定义弧元素
  14. #endregion
  15. #region Create linestring element
  16. DPoint3d p3 = new DPoint3d(0,10000,0);//定义三维点
  17. DPoint3d p4 = new DPoint3d(-10000, 10000, 0);
  18. DPoint3d p5 = new DPoint3d(-10000, 20000, 0);
  19. DPoint3d p6 = new DPoint3d(20000, 20000, 0);
  20. DPoint3d[] pos = { p3, p4, p5, p6 };//将三维点添加到三维点数组中
  21. LineStringElement lineString = new LineStringElement(dgnModel, null, pos);//定义线串元素
  22. #endregion
  23. ComplexStringElement complexString = new ComplexStringElement(dgnModel, null);//定义复杂线元素
  24. complexString.AddComponentElement(line);//添加复杂线元素的子部件
  25. complexString.AddComponentElement(arc);
  26. complexString.AddComponentElement(lineString);
  27. complexString.AddComponentComplete();//添加子部件完成
  28. complexString.AddToModel();//将复杂线元素写入模型空间
  29. }

image.png
图9 绘制结果

曲线元素(CurveElement)

  1. 第一种曲线元素的创建方式需要首先构建用于控制曲线轨迹的坐标点集,需要注意的是,在定义曲线元素时,点集中的坐标点个数需要大于等于6,否则无法成功创建。
  1. public static void CmdCreateCurve1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d[] pos = { DPoint3d.Zero,
  5. new DPoint3d(10000, 0, 0),
  6. new DPoint3d(10000, 10000, 0),
  7. new DPoint3d(20000, 10000, 0),
  8. new DPoint3d(20000, 0, 0),
  9. new DPoint3d(30000, 0, 0)
  10. };//定义曲线控制点坐标集
  11. CurveElement curve = new CurveElement(dgnModel, null, pos);//定义曲线元素 注意:三维点个数需大于等于6
  12. curve.AddToModel();//将曲线元素写入模型空间
  13. }

image.png
图10 绘制结果
第二种曲线元素的创建方式与上方方法的区别在于需要首先构建用于控制曲线轨迹的坐标点列表。同时坐标点数量需要大于等于6.

  1. public static void CmdCreateCurve2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. IList<DPoint3d> pos = new List<DPoint3d>
  5. {
  6. DPoint3d.Zero,
  7. new DPoint3d(10000, 0, 0),
  8. new DPoint3d(10000, 10000, 0),
  9. new DPoint3d(20000, 10000, 0),
  10. new DPoint3d(20000, 0, 0),
  11. new DPoint3d(30000, 0, 0)
  12. };//定义曲线控制点坐标列表
  13. CurveElement curve = new CurveElement(dgnModel, null, pos);//定义曲线元素 注意:三维点个数需大于等于6
  14. curve.AddToModel();//将曲线元素写入模型空间
  15. }

image.png
图11 绘制结果

B样条曲线元素(BSplineCurveElement)

  1. 第一种B样条曲线元素的创建方式是通过设置B样条曲线的控制点及起止端方向向量及MSInterpolationCurve属性后定义MSInterpolationCurve,然后通过MSInterpolationCurve创建B样条曲线元素。
  1. public static void CmdCreateBSplineCurve1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d[] pts = { new DPoint3d(0, 0, 0), new DPoint3d(1000, 1000, 0), new DPoint3d(1000, -1000, 0), new DPoint3d(0, -1000, 0), new DPoint3d(0, 0, 0) };//定义B样条曲线的节点
  5. DVector3d[] tangents = { new DVector3d(0, 0), new DVector3d(0, 0) };//设置B样条曲线的起端点切线方向
  6. MSInterpolationCurve interpolationCurve = MSInterpolationCurve.CreateFromPointsAndEndTangents(pts, true, 0, tangents, true, false, false, false);
  7. /*
  8. *根据节点和起端点切线方向定义插值曲线
  9. * points:插值点
  10. * removeDuplicatePoints:是否需要删除重复点
  11. * endTangents:端点切线方向向量
  12. * closedCurve:是否需要闭合曲线
  13. * colinearTangents:切线是否共线
  14. * chordLenTangents:是否仅相切弦
  15. * naturalTangents:是否为自然切线
  16. */
  17. BSplineCurveElement bsplineCurve = new BSplineCurveElement(dgnModel, null, interpolationCurve);//用插值曲线定义B样条曲线元素
  18. bsplineCurve.AddToModel();//将B样条曲线的实例写入模型
  19. }

image.png
图12 绘制结果
第二种B样条曲线元素的创建方式是通过定义B样条控制线上的点创建MSBsplineCurve,然后使用MSBsplineCurve创建B样条曲线元素。关于创建MSBsplineCurve的方式有很多种,具体您可以在帮助文档中查看其他不同的创建方法。

  1. public static void CmdCreateBSplineCurve2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义B样条曲线控制线
  5. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  6. DPoint3d p3 = new DPoint3d(20000, 0, 0);
  7. DPoint3d p4 = new DPoint3d(30000, -10000, 0);
  8. DPoint3d p5 = new DPoint3d(40000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4, p5 };//将控制点添加到控制点数组中
  10. MSBsplineCurve bsplineCurve = MSBsplineCurve.CreateFromPoints(pos);//使用控制点数组定义B样条曲线
  11. BSplineCurveElement bsplineCurveElem = new BSplineCurveElement(dgnModel, null, bsplineCurve);//用B样条曲线定义B样条曲线元素
  12. bsplineCurveElem.AddToModel();//将B样条曲线的实例写入模型
  13. }

image.png
图13 绘制结果

形元素(ShapeElement)

  1. 形元素的创建方式为首先定义形元素端点,而后添加到端点集中,然后使用端点集创建形元素。
  1. public static void CmdCreateShape(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = new DPoint3d(-10000, 10000, 0);//定义形元素端点
  5. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  6. DPoint3d p3 = new DPoint3d(10000, -10000, 0);
  7. DPoint3d p4 = new DPoint3d(-10000, -10000, 0);
  8. DPoint3d[] pos = { p1, p2, p3, p4 };//将面元素端点添加到形元素端点数组中
  9. ShapeElement shape = new ShapeElement(dgnModel,null, pos);//定义形元素
  10. shape.AddToModel();//将面元素写入模型
  11. }

image.png
图14 绘制结果

椭圆元素(EllipseElement)

  1. 第一种创建椭圆元素的方式是通过设置椭圆的圆心,主轴及副轴向量创建椭圆几何,然后使用椭圆几何创建椭圆元素。
  1. public static void CmdCreateEllipse1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心
  5. DVector3d vector0In = new DVector3d(20000, 0, 0);//定义主轴向量
  6. DVector3d vector90In = new DVector3d(0, 10000, 0);//定义副轴向量
  7. DEllipse3d ellipse = new DEllipse3d(centerPo, vector0In, vector90In);//定义椭圆几何
  8. EllipseElement ellipseElem = new EllipseElement(dgnModel, null, ellipse);//使用椭圆几何定义椭圆元素
  9. ellipseElem.AddToModel();
  10. }

image.png
图15 绘制结果
第二种创建椭圆元素的方法是通过设置椭圆元素中椭圆圆心,主轴长度,副轴长度及椭圆转角的方式直接创建椭圆元素。

  1. public static void CmdCreateEllipse2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心坐标
  5. EllipseElement ellipseElem = new EllipseElement(dgnModel, null, centerPo, 20000, 10000, 45);
  6. /*
  7. * 定义椭圆元素
  8. * dgnModel:写入椭圆元素的模型空间
  9. * templateElement:元素模板
  10. * center:椭圆圆心
  11. * axis1:主轴长度
  12. * axis2:副轴长度
  13. * rotation:椭圆转角
  14. */
  15. ellipseElem.AddToModel();
  16. }

image.png
图16 绘制结果
第三种创建椭圆元素的方法是通过设置椭圆元素中椭圆圆心,主轴长度,副轴长度及椭圆变换矩阵(椭圆转角)的方式直接创建椭圆元素。

  1. public static void CmdCreateEllipse3(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d centerPo = DPoint3d.Zero;//定义圆心坐标
  5. EllipseElement ellipseElem = new EllipseElement(dgnModel, null, centerPo, 20000, 10000, DMatrix3d.Identity);
  6. /*
  7. * 定义椭圆元素
  8. * dgnModel:写入椭圆元素的模型空间
  9. * templateElement:元素模板
  10. * center:椭圆圆心
  11. * axis1:主轴长度
  12. * axis2:副轴长度
  13. * rotation:椭圆变换矩阵(椭圆转角)
  14. */
  15. ellipseElem.AddToModel();//将椭圆元素写入模型空间
  16. }

image.png
图17 绘制结果

多线元素(MultilineElement)

  1. 多线元素需要首先对多线元素的轮廓进行定义,需要设置的属性主要有:轮廓的偏移值,轮廓的线样式,轮廓的偏移基点。然后对多线元素的法线方向,轨迹点进行定义,最后使用这些参数生成多线元素。
  1. public static void CmdCreateMultiLine(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获取当前模型所属文件
  5. MultilineStyle lineStyle = new MultilineStyle("testMultiline",dgnFile);//定义多线元素样式
  6. MultilineProfile profile1 = new MultilineProfile();//定义多线轮廓
  7. profile1.Distance = 0;//设置轮廓偏移基点
  8. profile1.UseLinestyle = true;//设置轮廓是否使用线样式
  9. profile1.Linestyle = 1;//设置轮廓线样式
  10. lineStyle.InsertProfile(profile1, 0);//将轮廓信息写入多线样式中
  11. MultilineProfile profile2 = new MultilineProfile();//定义多线轮廓
  12. profile2.Distance = -10000;//设置轮廓偏移基点
  13. lineStyle.InsertProfile(profile2, 1);//将轮廓信息写入多线样式中
  14. DVector3d vector = DVector3d.UnitZ;//设置多线法线方向向量
  15. DPoint3d p1 = DPoint3d.Zero;//设置多线轨迹控制点
  16. DPoint3d p2 = new DPoint3d(100000,0,0);
  17. DPoint3d p3 = new DPoint3d(200000, 200000, 0);
  18. DPoint3d[] pos = { p1,p2,p3};//定义轨迹点数组
  19. MultilineElement multiline = MultilineElement.CreateMultilineElement(dgnModel,null,lineStyle,1, vector, pos);
  20. /*
  21. * 定义多线元素
  22. * dgnModel:写入多线元素的模型空间
  23. * templateElement:元素模板
  24. * mlineStyle:多线样式
  25. * styleScale:样式缩放比例
  26. * normal:多线法线方向向量
  27. * points:轨迹点数组
  28. */
  29. multiline.AddToModel();//将多线元素写入模型空间
  30. }

image.png
图18 绘制结果

复杂形元素(ComplexShapeElement)

  1. 复杂形元素需要将构成的元素添加到复杂形元素的子部件中,添加完成后生成复杂形元素。需要注意的是,构成复杂形元素的曲线需要首尾相连,否则无法构建出预期的复杂形元素。其次在创建复杂形元素时,其子部件需要构成一个封闭的图形区域,否则程序会自动对复杂形元素进行封闭,可能会出现与预期不符的形态。
  1. public static void CmdCreateComplexShape(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create line
  5. DPoint3d p1 = DPoint3d.Zero;//定义直线端点
  6. DPoint3d p2 = new DPoint3d(100000,0,0);
  7. DSegment3d segment = new DSegment3d(p1,p2);//定义几何直线
  8. LineElement line = new LineElement(dgnModel,null,segment);//定义直线元素
  9. #endregion
  10. #region Create arc
  11. DPoint3d centerPo = DPoint3d.Zero;//定义弧圆心点
  12. ArcElement arc = new ArcElement(dgnModel,null, centerPo,100000,100000,0,0, Angle.PI.Radians / 2);//定义弧元素
  13. #endregion
  14. #region Create linestring
  15. DPoint3d p3 = new DPoint3d(0, 100000,0);//定义线串端点
  16. DPoint3d p4 = new DPoint3d(-100000, 100000, 0);
  17. DPoint3d p5 = new DPoint3d( -100000,0, 0);
  18. DPoint3d[] pos = { p3,p4,p5,p1};//定义线串点数组
  19. LineStringElement lineString = new LineStringElement(dgnModel ,null,pos);//定义线串元素
  20. #endregion
  21. ComplexShapeElement complexShape = new ComplexShapeElement(dgnModel,null);//定义复杂形元素
  22. complexShape.AddComponentElement(line);//添加子部件
  23. complexShape.AddComponentElement(arc);
  24. complexShape.AddComponentElement(lineString);
  25. complexShape.AddComponentComplete();//添加子部件完成
  26. complexShape.AddToModel();//将复杂元素写入模型
  27. }

image.png
图19 绘制结果

智能面(BrepCellHeaderElement)

  1. 智能面首先需要使用曲线定义出智能面的轮廓线,然后使用轮廓线生成实核实体,最后使用智能实体定义智能面。
  1. public static void CmdSmartSurface(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = DPoint3d.Zero;//定义曲线端点坐标
  5. DPoint3d p2 = new DPoint3d(100000, 0, 10000);
  6. DPoint3d p3 = new DPoint3d(100000, 100000, -20000);
  7. DPoint3d p4 = new DPoint3d(0, 100000, 30000);
  8. IList<DPoint3d> pos = new List<DPoint3d>() { p1, p2, p3, p4 };//将端点坐标添加到端点坐标列表中
  9. CurveVector curve = CurveVector.CreateLinear(pos, CurveVector.BoundaryType.Outer, true);//根据列表中的端点坐标定义曲线
  10. Create.BodyFromCurveVector(out SolidKernelEntity solid, curve, dgnModel);//使用曲线定义SolidEntity
  11. BrepCellHeaderElement brepCellHeader = new BrepCellHeaderElement(dgnModel, null, solid);//使用SolidEntity定义智能实体
  12. brepCellHeader.AddToModel();//将智能实体写入模型
  13. }

image.png
图20 绘制结果

圆台(锥)元素(ConeElement)

  1. 圆台(锥)元素通过设置圆台(锥)的顶部及底部的圆心坐标,圆心半径,及旋转矩阵后创建出相应的元素。
  1. public static void CmdCreateCone(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d topCenter = new DPoint3d(0,0, 100000);//定义顶部圆锥(台)圆心坐标
  5. DPoint3d bottomCenter = DPoint3d.Zero;//定义底部圆锥(台)圆心坐标
  6. ConeElement cone = new ConeElement(dgnModel,null,50000,100000,topCenter,bottomCenter,DMatrix3d.Identity,true);
  7. /*
  8. * 定义圆锥(台)元素
  9. * dgnModel:定义圆锥(台)元素的模型空间
  10. * templateElement:元素模板
  11. * topRadius:顶部圆锥(台)半径
  12. * bottomRadius:底部圆锥(台)半径
  13. * topCenter:顶部圆锥(台)圆心坐标
  14. * bottomCenter:底部圆锥(台)圆心坐标
  15. * rotation:应用于顶圆/底圆的旋转矩阵
  16. * isCapped:对于实体圆锥(台)来说为真
  17. */
  18. cone.AddToModel();//将圆锥(台)写入模型
  19. }

image.png
图21 绘制结果

拉伸面实体(SurfaceOrSolidElement)

  1. 创建拉伸面实体时需要首先对拉伸轮廓进行定义,然后使用方向向量对拉伸体的拉伸方向,拉伸长度进行定义,最后使用这些定义的参数生成拉伸面。注意:在创建拉伸实体时,若preferSolid参数为真,则生成拉伸体,否则生成拉伸面实体。
  1. public static void CmdCreateSurface1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create profile
  5. DPoint3d p1 = new DPoint3d(-10000, 10000, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  7. DPoint3d p3 = new DPoint3d(10000, -10000, 0);
  8. DPoint3d p4 = new DPoint3d(-10000, -10000, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  11. #endregion
  12. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  13. DVector3d extrudeVector = new DVector3d(10000, 10000, 100000);//定义拉伸向量
  14. SurfaceOrSolidElement surface = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, false);
  15. /*
  16. * 使用投影的方式定义拉伸形元素
  17. * dgnModel:定义拉伸形元素的模型空间
  18. * templateElement:元素模板
  19. * profile:投影轮廓面(截面)
  20. * origin:拉伸起点
  21. * extrudeVector:拉伸向量
  22. * transform:元素变换矩阵
  23. * preferSolid:生成拉伸体还是拉伸面(若为拉伸体则为true)
  24. */
  25. surface.AddToModel();//将拉伸面写入模型
  26. }

image.png
图22 绘制结果

旋转面实体(SurfaceOrSolidElement)

  1. 创建旋转面实体时需要首先对旋转轮廓进行定义,然后定义旋转面实体的中心点与旋转轴,最后使用这些参数创建旋转面实体。注意:在创建旋转实体时,若preferSolid参数为真,则生成旋转体,否则生成旋转面实体。
  1. public static void CmdCreateSurface2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create profile
  5. DPoint3d p1 = new DPoint3d(0, 0, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(0, 0, 10000);
  7. DPoint3d p3 = new DPoint3d(10000, 0, 10000);
  8. DPoint3d p4 = new DPoint3d(10000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  11. #endregion
  12. DPoint3d centerPo = new DPoint3d(-10000, 0, 0);//定义旋转形元素的中心点
  13. DVector3d axis = new DVector3d(0, 0, 1);//定义旋转形元素的旋转轴
  14. SurfaceOrSolidElement surface = SurfaceOrSolidElement.CreateRevolutionElement(dgnModel, null, shape, centerPo, axis, Angle.PI.Radians / 2, false);
  15. /*
  16. * 定义旋转形元素
  17. * dgnModel:定义旋转形元素的模型空间
  18. * templateElement:元素模板
  19. * profile:旋转截面
  20. * center:旋转形元素的中心点
  21. * axis:旋转轴线
  22. * sweepAngle:旋转扫掠角
  23. * preferSolid:生成旋转体还是旋转面(若为旋转体则为true)
  24. */
  25. surface.AddToModel();//将旋转形元素写入模型
  26. }

image.png
图23 绘制结果

拉伸实体(SurfaceOrSolidElement)

  1. 创建拉伸实体时需要首先对拉伸轮廓进行定义,然后使用方向向量对拉伸体的拉伸方向,拉伸长度进行定义,最后使用这些定义的参数生成拉伸面。注意:在创建拉伸实体时,若preferSolid参数为真,则生成拉伸体,否则生成拉伸面实体。
  1. public static void CmdCreateSolid1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create profile
  5. DPoint3d p1 = new DPoint3d(-10000, 10000, 0);//定义体元素端点
  6. DPoint3d p2 = new DPoint3d(10000, 10000, 0);
  7. DPoint3d p3 = new DPoint3d(10000, -10000, 0);
  8. DPoint3d p4 = new DPoint3d(-10000, -10000, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  11. #endregion
  12. DPoint3d origin = DPoint3d.Zero;//定义拉伸基点
  13. DVector3d extrudeVector = new DVector3d(10000, 10000, 100000);//定义拉伸向量
  14. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateProjectionElement(dgnModel, null, shape, origin, extrudeVector, DTransform3d.Identity, true);
  15. /*
  16. * 使用投影的方式定义拉伸体元素
  17. * dgnModel:定义拉伸体元素的模型空间
  18. * templateElement:元素模板
  19. * profile:投影轮廓面(截面)
  20. * origin:拉伸起点
  21. * extrudeVector:拉伸向量
  22. * transform:元素变换矩阵
  23. * preferSolid:生成拉伸体还是拉伸面(若为拉伸体则为true)
  24. */
  25. solid.AddToModel();//将拉伸体写入模型
  26. }

image.png
图24 绘制结果

旋转实体(SurfaceOrSolidElement)

  1. 创建旋转实体时需要首先对旋转轮廓进行定义,然后定义旋转实体的中心点与旋转轴,最后使用这些参数创建旋转实体。注意:在创建旋转实体时,若preferSolid参数为真,则生成旋转体,否则生成旋转面实体。
  1. public static void CmdCreateSolid2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region Create profile
  5. DPoint3d p1 = new DPoint3d(0, 0, 0);//定义形元素端点
  6. DPoint3d p2 = new DPoint3d(0, 0, 10000);
  7. DPoint3d p3 = new DPoint3d(10000, 0, 10000);
  8. DPoint3d p4 = new DPoint3d(10000, 0, 0);
  9. DPoint3d[] pos = { p1, p2, p3, p4 };//将形元素端点添加到形元素端点数组中
  10. ShapeElement shape = new ShapeElement(dgnModel, null, pos);//定义形元素
  11. #endregion
  12. DPoint3d centerPo = new DPoint3d(-10000, 0, 0);//定义旋转体元素的中心点
  13. DVector3d axis = new DVector3d(0, 0, 1);//定义旋转体元素的旋转轴
  14. SurfaceOrSolidElement solid = SurfaceOrSolidElement.CreateRevolutionElement(dgnModel, null, shape, centerPo, axis, Angle.PI.Radians / 2, true);
  15. /*
  16. * 定义旋转体元素
  17. * dgnModel:定义旋转体元素的模型空间
  18. * templateElement:元素模板
  19. * profile:旋转截面
  20. * center:旋转形元素的中心点
  21. * axis:旋转轴线
  22. * sweepAngle:旋转扫掠角
  23. * preferSolid:生成旋转体还是旋转面(若为旋转体则为true)
  24. */
  25. solid.AddToModel();//将旋转体元素写入模型
  26. }

image.png
图25 绘制结果

网格元素(MeshHeaderElement)

  1. 网格元素的创建方式有些复杂,首先需要创建多面体几何,若使用编程的方式创建多面体几何,需要首先确定多面体几何的端点坐标,然后根据网格的分布情况按照端点坐标的索引值连接形成局部网格并将信息添加到多面体几何中,最后使用多面体几何创建网格元素。
  1. public static void CmdCreateMesh(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. #region create polyface
  5. DPoint3d p1 = DPoint3d.Zero;//定义多面体端点坐标
  6. DPoint3d p2 = new DPoint3d(10000,0,0);
  7. DPoint3d p3 = new DPoint3d(0,10000,0);
  8. DPoint3d p4 = new DPoint3d(0,0,10000);
  9. IList<DPoint3d> pos = new List<DPoint3d>();//定义储存多面体坐标的列表
  10. pos.Add(p1);//将多面体端点坐标添加到列表中
  11. pos.Add(p2);
  12. pos.Add(p3);
  13. pos.Add(p4);
  14. PolyfaceHeader polyface = new PolyfaceHeader();//定义多面体几何
  15. polyface.Point= pos;//设置该多面体几何的端点集
  16. polyface.ActivateVectorsForIndexing(polyface);//激活多面体几何中的数据与索引
  17. List<int> indices = new List<int>();//定义索引列表
  18. indices.Add(1); indices.Add(2); indices.Add(3); //将索引号添加到列表中
  19. polyface.AddIndexedFacet(indices, null, null, indices);//添加索引信息到多面体几何中
  20. indices.Clear();//清空索引列表
  21. indices.Add(1); indices.Add(2); indices.Add(4);//继续将索引号添加到列表中
  22. polyface.AddIndexedFacet(indices, null, null, indices);
  23. indices.Clear();
  24. indices.Add(2); indices.Add(3); indices.Add(4);
  25. polyface.AddIndexedFacet(indices, null, null, indices);
  26. indices.Clear();
  27. indices.Add(1); indices.Add(3); indices.Add(4);
  28. polyface.AddIndexedFacet(indices, null, null, indices);
  29. #endregion
  30. MeshHeaderElement mesh = new MeshHeaderElement(dgnModel,null,polyface);//定义使用多面体几何网格元素
  31. mesh.AddToModel();//将网格元素添加到模型空间中
  32. }

image.png
图26 绘制结果

智能实体(SolidKernelEntity)-拉伸

  1. 与普通拉伸实体不同的是,智能拉伸实体是一个拉伸体到指定平面后生成的结果。首先需要创建一个拉伸体,然后再创建一个拉伸终点的平面,最后将参数输入后即生成实体拉伸到另一个平面的新实体。需要注意的是,平面必须完全将实体截断,否则无法生成最终的结果。
  1. public static void CmdCreateSmartSolid1(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d profilePo1 = new DPoint3d(-50000, 50000, 0);//定义拉伸体拉伸平面端点
  5. DPoint3d profilePo2 = new DPoint3d(50000, 50000, 0);
  6. DPoint3d profilePo3 = new DPoint3d(50000, -50000, 0);
  7. DPoint3d profilePo4 = new DPoint3d(-50000, -50000, 0);
  8. IList<DPoint3d> profilePos = new List<DPoint3d>() {profilePo1,profilePo2,profilePo3,profilePo4, profilePo1 };//定义拉伸体拉伸平面的端点列表
  9. CurveVector profileCurve = CurveVector.CreateLinear(profilePos,CurveVector.BoundaryType.Outer,false);//定义拉伸体拉伸平面轮廓线
  10. Create.BodyFromCurveVector(out SolidKernelEntity profileEntity,profileCurve,dgnModel);//将拉伸平面轮廓线转化为SolidKernelEntity
  11. DPoint3d extrudePo1 = new DPoint3d(-50000, 50000, -100000);//定义拉伸体截面端点
  12. DPoint3d extrudePo2 = new DPoint3d(50000, 50000, -200000);
  13. DPoint3d extrudePo3 = new DPoint3d(50000, -50000, -300000);
  14. DPoint3d extrudePo4 = new DPoint3d(-50000, -50000, -200000);
  15. IList<DPoint3d> extrudePos = new List<DPoint3d>() { extrudePo1, extrudePo2, extrudePo3, extrudePo4, extrudePo1 };//定义拉伸体截面端点列表
  16. CurveVector extrudeCurve = CurveVector.CreateLinear(extrudePos, CurveVector.BoundaryType.Outer, true);//定义拉伸体截面轮廓线
  17. Create.BodyFromCurveVector(out SolidKernelEntity extrudeEntity, extrudeCurve, dgnModel);//将截面轮廓线转化为SolidKernelEntity
  18. Create.BodyFromExtrusionToBody(out SolidKernelEntity solid,extrudeEntity,profileEntity,false);
  19. /*
  20. * 通过将平面实体拉伸到另一个平面实体来定义新实体
  21. * entityOut:新实体
  22. * extrudeToIn:修剪拉伸实体的平面实体
  23. * profileIn:要拉伸的平面实体。
  24. * reverseDirectionIn:拉伸方向是否与拉伸的平面实体法线方向相同或相反
  25. */
  26. Convert1.BodyToElement(out Element solidElem,solid,null,dgnModel);//将实体转换为元素
  27. solidElem.AddToModel();//将元素写入模型空间
  28. }

image.png
图27 绘制结果

智能实体(SolidKernelEntity)-轮廓放样

  1. 轮廓放样,即我们可以通过输入拉伸体的不同轮廓进行放样融合,形成一个新的实体。在智能实体的轮廓放样中,我们需要首先定义实体的多个轮廓,他们之间会根据计算进行融合过渡,同时,也可以通过定义轮廓线的方式硬性设置轮廓的放样轨迹,最终生成实体。需要注意的是,在轮廓放样中,若不规定轮廓轨迹,依然需要在轨迹线入参中填入数据,一般可以将轮廓线列表填入,但不可不填,否则会造成程序崩溃。
  1. public static void CmdCreateSmartSolid2(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. CurveVector[] profilesIn = new CurveVector[3];//定义截面曲线数组
  5. DPoint3d[] ptArr_Section0 = new DPoint3d[4];//定义截面端点数组
  6. ptArr_Section0[0] = new DPoint3d(-500, 500, 0);//定义截面端点
  7. ptArr_Section0[1] = new DPoint3d(500, 500, 0);
  8. ptArr_Section0[2] = new DPoint3d(500, -500, 0);
  9. ptArr_Section0[3] = new DPoint3d(-500, -500, 0);
  10. ShapeElement shp0 = new ShapeElement(dgnModel, null, ptArr_Section0);//定义形元素
  11. profilesIn[0] = CurvePathQuery.ElementToCurveVector(shp0);//将面转换为曲线
  12. DPoint3d[] ptArr_Section1 = new DPoint3d[4];
  13. ptArr_Section1[0] = new DPoint3d(0, 1000, 5000);
  14. ptArr_Section1[1] = new DPoint3d(1000, 0, 5000);
  15. ptArr_Section1[2] = new DPoint3d(0, -1000, 5000);
  16. ptArr_Section1[3] = new DPoint3d(-1000, 0, 5000);
  17. ShapeElement shp1 = new ShapeElement(dgnModel, null, ptArr_Section1);
  18. profilesIn[1] = CurvePathQuery.ElementToCurveVector(shp1);
  19. DPoint3d[] ptArr_Section2 = new DPoint3d[4];
  20. ptArr_Section2[0] = new DPoint3d(-500, 500, 10000);
  21. ptArr_Section2[1] = new DPoint3d(500, 500, 10000);
  22. ptArr_Section2[2] = new DPoint3d(500, -500, 10000);
  23. ptArr_Section2[3] = new DPoint3d(-500, -500, 10000);
  24. ShapeElement shp2 = new ShapeElement(dgnModel, null, ptArr_Section2);
  25. profilesIn[2] = CurvePathQuery.ElementToCurveVector(shp2);
  26. BentleyStatus result = Create.BodyFromLoft(out SolidKernelEntity entityOut, profilesIn, 3, profilesIn, 0, dgnModel, false, false);
  27. /*
  28. * 定义一组轮廓线放样定义实体
  29. * entityOut:定义的放样实体
  30. * profilesIn:横截面轮廓集
  31. * nProfilesIn:横截面数量
  32. * guidesIn:(可选)用于控制放样曲线轨迹的曲线集 注意:若不使用控制曲线,该位置可将横截面轮廓集填到此处,不可为空,否则会报错
  33. * nGuidesIn:控制放样曲线轨迹的曲线数量
  34. * modelRefIn:获取当前模型的比例单位
  35. * periodicIn:若为真,则会构造一个闭合曲面,其中第一条截面将作为最后一条截面
  36. * segmentIn:若为真,每个截面间将定义线性曲线而不做平滑
  37. */
  38. result = Convert1.BodyToElement(out Element eeh, entityOut, null, dgnModel);//将SolidKernelEntity转换为元素
  39. eeh.AddToModel();//将元素写入模型
  40. }

image.png
图28 绘制结果

智能实体(SolidKernelEntity)-拉伸

  1. 智能拉伸实体的创建方法与普通拉伸实体的创建方式类似。首先创建一个智能拉伸实体的外轮廓线和拉伸向量,记录拉伸长度及拉伸方向,然后使用该模型创建普通实体,最后将普通实体转化为智能实体。
  1. public static void CmdCreateSmartSolid3(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d p1 = new DPoint3d(10000,10000);//定义截面轮廓线端点
  5. DPoint3d p2 = new DPoint3d(10000,-10000);
  6. DPoint3d p3 = new DPoint3d(-10000,-10000);
  7. DPoint3d p4 = new DPoint3d(-10000,10000);
  8. IList<DPoint3d> pos = new List<DPoint3d>() { p1,p2,p3,p4};//定义轮廓线端点数组
  9. CurveVector curve = CurveVector.CreateLinear(pos,CurveVector.BoundaryType.Outer,true);//定义轮廓线
  10. DVector3d vector = new DVector3d(0,0,10000);//定义拉伸向量(包含拉伸长度及方向信息)
  11. DgnExtrusionDetail detail = new DgnExtrusionDetail(curve, vector,true);
  12. /*
  13. * 定义拉伸信息
  14. * baseCurve:截面轮廓曲线
  15. * extrusionVector:拉伸信息
  16. * capped:若启用端盖则为真
  17. */
  18. SolidPrimitive solid = SolidPrimitive.CreateDgnExtrusion(detail);//使用拉伸信息定义实体
  19. BentleyStatus result = Create.BodyFromSolidPrimitive(out SolidKernelEntity entityOut, solid,dgnModel);//使用拉伸实体定义SolidKernelEntity
  20. result = Convert1.BodyToElement(out Element eeh, entityOut, null, dgnModel);//将SolidKernelEntity转换为元素
  21. eeh.AddToModel();//将元素写入模型
  22. }

image.png
图29 绘制结果

智能实体(SolidKernelEntity)-扫掠

  1. 创建智能扫掠实体时需要首先使用曲线定义扫掠的轮廓线,然后定义扫掠的轨迹线,最后使用轮廓线及轨迹线,结合扫掠属性,比如说是否修复扫掠自交,是否在扫掠过程中旋转轮廓截面,沿路径扫掠截面是否需要变化等属性生成最终的智能扫掠实体。
  1. public static void CmdCreateSmartSolid4(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前的模型空间
  4. DPoint3d profilePo1 = new DPoint3d(1000, 1000,-10000);//定义截面轮廓线端点
  5. DPoint3d profilePo2 = new DPoint3d(1000, -1000, -10000);
  6. DPoint3d profilePo3 = new DPoint3d(-1000, -1000, -10000);
  7. DPoint3d profilePo4 = new DPoint3d(-1000, 1000, -10000);
  8. DPoint3d[] profilePos = { profilePo1 , profilePo2, profilePo3, profilePo4};//定义截面轮廓线端点集
  9. ShapeElement shape = new ShapeElement(dgnModel,null, profilePos);//定义形元素
  10. CurveVector curve= CurvePathQuery.ElementToCurveVector(shape);//使用图形元素获得轮廓线
  11. DPoint3d pathPo1 = new DPoint3d(0, 0, -10000);//定义扫掠轨迹线端点
  12. DPoint3d pathPo2 =new DPoint3d(0, 0,0);
  13. DPoint3d pathPo3 = new DPoint3d(10000,10000,10000);
  14. DPoint3d pathPo4 = new DPoint3d(-10000, -10000, 20000);
  15. DPoint3d pathPo5 = new DPoint3d(0, 0, 30000);
  16. DPoint3d pathPo6 = new DPoint3d(0, 0, 40000);
  17. DPoint3d[] pathPos = { pathPo1, pathPo2, pathPo3, pathPo4 , pathPo5, pathPo6};//定义扫掠轨迹端点集
  18. LineStringElement lineString = new LineStringElement(dgnModel,null, pathPos);//定义线串元素
  19. CurveVector pathCurve = lineString.GetCurveVector();//使用线串元素获得扫掠轨迹点
  20. Create.BodyFromSweep(out SolidKernelEntity entityOut, curve, pathCurve, dgnModel, false, true, false,
  21. null, null,null , null);
  22. /*
  23. * 使用横截面扫掠路径定义实体
  24. * entityOut:生成的新实体
  25. * profileIn:横断面轮廓(开放,闭合或带孔区域)
  26. * pathIn:扫掠轨迹线
  27. * modelRefIn:获取当前模型的比例单位
  28. * alignParallelIn:若为真,则轮廓与全局轴线保持固定角度,而不是与路径相切并锁定方向
  29. * selfRepairIn:若为真,则会尝试修复扫掠自交
  30. * createSheetIn:若为真,则会强制生成闭合的扫掠实体
  31. * lockDirectionIn:(可选)使截面轮廓与垂直于方向向量同时切线保持固定角度,仅当alignParallelIn为真时有效
  32. * twistAngleIn:(可选)在轮廓沿路径移动时旋转轮廓角度
  33. * scaleIn:(可选)在轮廓沿路径扫掠时缩放比例
  34. * scalePointIn:应用缩放需要缩放的轮廓端点
  35. */
  36. BentleyStatus result = Convert1.BodyToElement(out Element eeh, entityOut, null, dgnModel);//将SolidKernelEntity转换为元素
  37. eeh.AddToModel();//将元素写入模型
  38. }

image.png
图30 绘制结果

智能实体(SolidKernelEntity)-缝合

  1. 该方法的思路与创建网格元素有些类似,简单来说就是首先创建构成智能实体的子实体,然后将这些子实体缝合为一个整体的智能实体。首先创建组成智能实体的形元素,然后将形元素转换为实核实体,最后使用实核实体缝合成为智能实体。
  1. public static void CmdCreateSmartSolid5(string unparsed)
  2. {
  3. List<DPoint3d> points = new List<DPoint3d>();//定义坐标列表
  4. points.Add(new DPoint3d(0, 0, 0));//给列表添加坐标
  5. points.Add(new DPoint3d(10, 0, 0));
  6. points.Add(new DPoint3d(10, 10, 0));
  7. points.Add(new DPoint3d(0, 10, 0));
  8. ShapeElement shape1 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());//定义形元素
  9. points.Clear();//清空列表元素
  10. points.Add(new DPoint3d(0, 0, 0));
  11. points.Add(new DPoint3d(10, 0, 0));
  12. points.Add(new DPoint3d(10, 0, 10));
  13. points.Add(new DPoint3d(0, 0, 10));
  14. ShapeElement shape2 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());
  15. points.Clear();
  16. points.Add(new DPoint3d(0, 10, 0));
  17. points.Add(new DPoint3d(10, 10, 0));
  18. points.Add(new DPoint3d(10, 10, 10));
  19. points.Add(new DPoint3d(0, 10, 10));
  20. ShapeElement shape3 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());
  21. points.Clear();
  22. points.Add(new DPoint3d(10, 0, 0));
  23. points.Add(new DPoint3d(10, 10, 0));
  24. points.Add(new DPoint3d(10, 10, 10));
  25. points.Add(new DPoint3d(10, 0, 10));
  26. ShapeElement shape4 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());
  27. points.Clear();
  28. points.Add(new DPoint3d(0, 0, 0));
  29. points.Add(new DPoint3d(0, 10, 0));
  30. points.Add(new DPoint3d(0, 10, 10));
  31. points.Add(new DPoint3d(0, 0, 10));
  32. ShapeElement shape5 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());
  33. points.Clear();
  34. points.Add(new DPoint3d(0, 0, 10));
  35. points.Add(new DPoint3d(0, 10, 10));
  36. points.Add(new DPoint3d(10, 10, 10));
  37. points.Add(new DPoint3d(10, 0, 10));
  38. ShapeElement shape6 = new ShapeElement(Session.Instance.GetActiveDgnModel(), null, points.ToArray());
  39. SolidKernelEntity[] tools1 = new SolidKernelEntity[6];//定义实核实体列表
  40. Convert1.ElementToBody(out tools1[0], shape1, false, true, false);//将Shape元素转换为实核实体
  41. Convert1.ElementToBody(out tools1[1], shape2, false, true, false);
  42. Convert1.ElementToBody(out tools1[2], shape3, false, true, false);
  43. Convert1.ElementToBody(out tools1[3], shape4, false, true, false);
  44. Convert1.ElementToBody(out tools1[4], shape5, false, true, false);
  45. Convert1.ElementToBody(out tools1[5], shape6, false, true, false);
  46. BrepCellHeaderElement brepCellHeader = null;//初始化智能实体
  47. if (BentleyStatus.Success == Modify.SewBodies(out SolidKernelEntity[] sewn1, out SolidKernelEntity[] unsewn1, ref tools1, 6, 0, 1))//缝合实核实体
  48. {
  49. for (int i = 0; i < sewn1.Length; i++)//遍历缝合结果
  50. {
  51. brepCellHeader = new BrepCellHeaderElement(Session.Instance.GetActiveDgnModel(), null, sewn1[i]);//定义智能实体
  52. brepCellHeader.AddToModel();//将智能实体写入模型
  53. }
  54. }
  55. }

image.png
图31 绘制结果

其他

文字元素(TextElement)

  1. 在创建文字元素时,我们需要首先定义文本块的属性,其中包括文字样式,文字段落属性等,定义后再对文本块中输出的内容进行输入,最后生成文本元素。
  1. public static void CmdCreateText(string unparsed)
  2. {
  3. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获得定义文本元素的文件
  4. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得定义文本元素的模型空间
  5. TextBlockProperties txtBlockProp = new TextBlockProperties(dgnModel);//定义文本属性
  6. txtBlockProp.IsViewIndependent = false;//设置文本非独立于视图
  7. ParagraphProperties paraProp = new ParagraphProperties(dgnModel);//定义段落属性
  8. DgnTextStyle txtStyle = DgnTextStyle.GetSettings(dgnFile);//从激活的文件中获得文字样式
  9. RunProperties runProp = new RunProperties(txtStyle, dgnModel);//定义运行属性
  10. TextBlock txtBlock = new TextBlock(txtBlockProp, paraProp, runProp, dgnModel);//定义文本块
  11. txtBlock.AppendText("This is a textBlock Element");//设置文本块文字内容
  12. TextElement text = (TextElement) TextElement.CreateElement(null, txtBlock);//定义文本元素
  13. text.AddToModel();//将生成的文本元素写入模型
  14. }

image.png
图32 绘制结果

标签元素(TagElement)

  1. 创建标签元素需要首先确定标签依附的对象,在本案例中我们首先创建了一个用于依附的线元素,然后创建标签的属性定义,其中包含标签名称,标签值等,然后将标签集写入模型。最后在创建标签元素时将用于依附的线元素,标签定义输入生成结果。
  1. public static void CmdCreateTag(string unparsed)
  2. {
  3. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获得定义文本元素的文件
  4. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得定义文本元素的模型空间
  5. LineElement line = new LineElement(dgnModel, null, new DSegment3d(0, 0, 0, 50000, 0, 0));//定义需要标注的直线
  6. line.AddToModel();//将直线写入模型
  7. DgnTagDefinition[] tagDefs = new DgnTagDefinition[1];//定义标签定义数组
  8. tagDefs[0] = new DgnTagDefinition();//定义标签定义
  9. tagDefs[0].Name = "Line";//设置标签名称
  10. tagDefs[0].TagDataType = TagType.Double;//设置标签类型
  11. tagDefs[0].Value = 10.0;//设置标签值
  12. TagSetElement tagSet = new TagSetElement(dgnModel, tagDefs, tagDefs[0].Name, "", true, dgnFile, 0);//定义标签集元素
  13. tagSet.AddToModel();//将标签集元素写入模型
  14. DPoint3d origin = DPoint3d.FromXYZ(0, 0, 0);//设置标签元素插入点
  15. DMatrix3d orientation = DMatrix3d.Identity;//设置标签变换矩阵
  16. DgnTextStyle style = DgnTextStyle.GetSettings(dgnFile);//从当前激活的文件中获得文字样式
  17. TagElement tag = new TagElement(dgnModel, null, tagSet.SetName, tagSet.SetName, null, style, false, origin, orientation, line);//定义标注元素
  18. tag.AddToModel();//将标注元素写入模型
  19. }

image.png
图33 绘制结果

普通单元(CellHeaderElement)

  1. 在创建普通单元时我们需要将构成该单元的所有子元素添加到元素集中,然后使用元素集中的元素,指定创建普通单元的模型空间,名称,坐标,旋转矩阵等信息后生成结果。
  1. public static void CmdCreateCell(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得定义文本元素的模型空间
  4. DPoint3d[] ptArr = new DPoint3d[5];//定义形元素端点集
  5. ptArr[0] = new DPoint3d(-15 * 10000, -5 * 10000, 0 * 10000);//定义端点坐标
  6. ptArr[1] = new DPoint3d(-15 * 10000, 5 * 10000, 0 * 10000);
  7. ptArr[2] = new DPoint3d(-5 * 10000, 5 * 10000, 0 * 10000);
  8. ptArr[3] = new DPoint3d(-5 * 10000, -5 * 10000, 0 * 10000);
  9. ptArr[4] = new DPoint3d(-15 * 10000, -5 * 10000, 0 * 10000);
  10. ShapeElement shapeEle = new ShapeElement(dgnModel, null, ptArr);//定义形元素
  11. DPlacementZX dPlaceZX = DPlacementZX.Identity;//定义椭圆放置平面
  12. dPlaceZX.Origin = new DPoint3d(-10 * 10000, 0, 0);//设置平面原点
  13. DEllipse3d ellipse = new DEllipse3d(dPlaceZX, 5 * 10000, 5 * 10000, Angle.Zero, Angle.TWOPI);//定义几何椭圆
  14. EllipseElement elliElem = new EllipseElement(dgnModel, null, ellipse);//定义椭圆元素
  15. List<Element> listElems = new List<Element>();//定义单元组成元素集
  16. listElems.Add(shapeEle);//将构成元素添加到元素集中
  17. listElems.Add(elliElem);
  18. DPoint3d ptOri = new DPoint3d();//设置单元放置位置
  19. DMatrix3d rMatrix = DMatrix3d.Identity;//设置变换矩阵
  20. DPoint3d ptScale = new DPoint3d(1, 1, 1);//设置缩放比例
  21. CellHeaderElement cellHeaderEle = new CellHeaderElement(dgnModel, "CellElementSample", ptOri, rMatrix, listElems);//定义单元
  22. cellHeaderEle.AddToModel();//将单元写入模型空间
  23. }

image.png
图34 绘制结果

共享单元(SharedCellElement)

  1. 在创建共享单元前需要首先在模型中定义共享单元定义。在创建共享单元定义时需要将构成共享单元定义的子元素添加到定义元素中,而后写入模型。然后在创建共享单元时设置其共享单元定义,最后将定义的共享单元写入模型。
  1. public static void CmdCreateSharedCell(string unparsed)
  2. {
  3. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得定义文本元素的模型空间
  4. DPoint3d[] ptArr = new DPoint3d[5];//定义形元素端点集
  5. ptArr[0] = new DPoint3d(-15 * 10000, -5 * 10000, 0 * 10000);//定义端点坐标
  6. ptArr[1] = new DPoint3d(-15 * 10000, 5 * 10000, 0 * 10000);
  7. ptArr[2] = new DPoint3d(-5 * 10000, 5 * 10000, 0 * 10000);
  8. ptArr[3] = new DPoint3d(-5 * 10000, -5 * 10000, 0 * 10000);
  9. ptArr[4] = new DPoint3d(-15 * 10000, -5 * 10000, 0 * 10000);
  10. ShapeElement shapeEle = new ShapeElement(dgnModel, null, ptArr);//定义形元素
  11. DPlacementZX dPlaceZX = DPlacementZX.Identity;//定义椭圆放置平面
  12. dPlaceZX.Origin = new DPoint3d(-10 * 10000, 0, 0);//设置平面原点
  13. DEllipse3d ellipse = new DEllipse3d(dPlaceZX, 5 * 10000, 5 * 10000, Angle.Zero, Angle.TWOPI);//定义几何椭圆
  14. EllipseElement elliElem = new EllipseElement(dgnModel, null, ellipse);//定义椭圆元素
  15. SharedCellDefinitionElement sharedCellDefinition = new SharedCellDefinitionElement(dgnModel,"test");//定义共享单元定义元素
  16. sharedCellDefinition.AddChildElement(shapeEle);//将构成元素添加到共享单元定义元素中
  17. sharedCellDefinition.AddChildElement(elliElem);
  18. sharedCellDefinition.AddChildComplete();//添加构件完成
  19. sharedCellDefinition.AddToModel();//将共享单元定义元素写入模型
  20. DPoint3d origin = DPoint3d.Zero;//设置插入点坐标
  21. DPoint3d scale = new DPoint3d(1,1,1);//设置缩放比例
  22. SharedCellElement sharedCell = new SharedCellElement(dgnModel,null,"testCell", origin,DMatrix3d.Identity, scale);//定义共享单元元素
  23. sharedCell.SetDefinitionId(sharedCellDefinition.ElementId);//指定该共享单元的定义元素
  24. TransformInfo transInfo = new TransformInfo(DTransform3d.Identity);//定义变换信息
  25. sharedCell.ApplyTransform(transInfo);//对共享单元元素应用变换信息
  26. sharedCell.AddToModel();//将共享单元添加到模型中
  27. }

image.png
图35 绘制结果

标注元素(DimensionElement)

  1. 创建标注元素时需要首先设定标注样式,然后创建一个抽象类DimensionCreateData的具体实现应用设置的标注样式,最后在设置标注的起点及终点及标注线高度等属性后生成结果。
  1. public static void CmdCreateDimension(string unparsed)
  2. {
  3. DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获得当前激活的文件
  4. DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得当前激活的模型空间
  5. //获取当前dgn文件中名字为"DimStyle"的标注样式,尺寸标注元素的外貌由上百个属性控制,而标注样式是一组预先设置好的属性
  6. //获取了预先订制好的标注样式之后,还可以调用DimensionStyle下的各种SetXXX成员函数修改设置的属性
  7. DimensionStyle dimStyle = new DimensionStyle("DimStyle", dgnFile);//定义标注样式
  8. dimStyle.SetBooleanProp(true, DimStyleProp.Placement_UseStyleAnnotationScale_BOOLINT);//设置标注样式
  9. dimStyle.SetDoubleProp(1, DimStyleProp.Placement_AnnotationScale_DOUBLE);
  10. dimStyle.SetBooleanProp(true, DimStyleProp.Text_OverrideHeight_BOOLINT);
  11. dimStyle.SetDistanceProp(0.5 * 10000, DimStyleProp.Text_Height_DISTANCE, dgnModel);
  12. dimStyle.SetBooleanProp(true, DimStyleProp.Text_OverrideWidth_BOOLINT);
  13. dimStyle.SetDistanceProp(0.4 * 10000, DimStyleProp.Text_Width_DISTANCE, dgnModel);
  14. dimStyle.SetBooleanProp(true, DimStyleProp.General_UseMinLeader_BOOLINT);
  15. dimStyle.SetDoubleProp(0.01, DimStyleProp.Terminator_MinLeader_DOUBLE);
  16. dimStyle.SetBooleanProp(true, DimStyleProp.Value_AngleMeasure_BOOLINT);
  17. dimStyle.SetAccuracyProp((byte)AnglePrecision.Use1Place, DimStyleProp.Value_AnglePrecision_INTEGER);
  18. int alignInt = (int)DimStyleProp_General_Alignment.True;
  19. StatusInt status = dimStyle.SetIntegerProp(alignInt, DimStyleProp.General_Alignment_INTEGER);
  20. dimStyle.GetIntegerProp(out int valueOut, DimStyleProp.General_Alignment_INTEGER);
  21. DgnTextStyle textStyle = new DgnTextStyle("TestStyle", dgnFile);//设置文字样式
  22. LevelId lvlId = Settings.GetLevelIdFromName("Default");//设置图层
  23. CreateDimensionCallbacks callbacks = new CreateDimensionCallbacks(dimStyle, textStyle, new Symbology(), lvlId, null);//尺寸标注元素的构造函数会调用DimensionCreateData的各个成员函数去获取定义尺寸标注元素需要的各种参数
  24. DimensionElement dimEle = new DimensionElement(dgnModel, callbacks, DimensionType.SizeArrow);//定义标注元素
  25. if (dimEle.IsValid)//判断标注元素是否有效
  26. {
  27. DPoint3d pt1 = DPoint3d.Zero, pt2 = DPoint3d.FromXY(10000 * 10, 10000 * 0);//设置插入点坐标
  28. dimEle.InsertPoint(pt1, null, dimStyle, -1);//对标注元素设置插入点
  29. dimEle.InsertPoint(pt2, null, dimStyle, -1);
  30. dimEle.SetHeight(10000);//设置尺寸标注元素的高度
  31. dimEle.SetRotationMatrix(DMatrix3d.Identity);//设置变换信息
  32. dimEle.AddToModel();//将标注元素写入模型
  33. }
  34. }
  35. class CreateDimensionCallbacks : DimensionCreateData
  36. {
  37. private DimensionStyle m_dimStyle;
  38. private DgnTextStyle m_textStyle;
  39. private Symbology m_symbology;
  40. private LevelId m_levelId;
  41. private DirectionFormatter m_directionFormatter;
  42. public CreateDimensionCallbacks(DimensionStyle dimStyle, DgnTextStyle textStyle, Symbology symb, LevelId levelId, DirectionFormatter formatter)
  43. {
  44. m_dimStyle = dimStyle;
  45. m_textStyle = textStyle;
  46. m_symbology = symb;
  47. m_levelId = levelId;
  48. m_directionFormatter = formatter;
  49. }
  50. public override DimensionStyle GetDimensionStyle()
  51. {
  52. return m_dimStyle;
  53. }
  54. public override DgnTextStyle GetTextStyle()
  55. {
  56. return m_textStyle;
  57. }
  58. public override Symbology GetSymbology()
  59. {
  60. return m_symbology;
  61. }
  62. public override LevelId GetLevelId()
  63. {
  64. return m_levelId;
  65. }
  66. public override int GetViewNumber()
  67. {
  68. return 0;
  69. }
  70. //此函数返回的旋转矩阵与GetViewRotation返回的旋转矩阵共同定义了尺寸标注元素的方向
  71. public override DMatrix3d GetDimensionRotation()
  72. {
  73. return DMatrix3d.Identity;
  74. }
  75. public override DMatrix3d GetViewRotation()
  76. {
  77. return DMatrix3d.Identity;
  78. }
  79. //用于从数字方向值构造字符串。
  80. public override DirectionFormatter GetDirectionFormatter()
  81. {
  82. return m_directionFormatter;
  83. }
  84. }

image.png
图36 绘制结果