Mstn二次开发过程中,我们经常要编写创建各种各样图形元素的程序,其中有一种需求是用一条轮廓线沿着指定的路径扫掠构造一个三维图形元素。Mstn SDK的C#接口中有一个函数Bentley.DgnPlatformNE.Create.BodyFromSweep是专门来完成这项工作的,这个函数在C:\Program Files\Bentley\MicroStation CONNECT Edition\MicroStation\Bentley.DgnDisplayNet.dll这个程序集中,使用此函数之前我们需要先在项目中引用一下这个程序集。在VS中可以看到此函数的原型如下所示:
public static BentleyStatus BodyFromSweep(out SolidKernelEntity entityOut, CurveVector profileIn, CurveVector pathIn, DgnModelRef modelRefIn, bool alignParallelIn, bool selfRepairIn, bool createSheetIn, DVector3d lockDirectionIn, ValueType twistAngleIn, ValueType scaleIn, DPoint3d scalePointIn);其中第一个参数entityOut是out类型的,返回的是创建的扫略体。其他参数都是传入参数,用来控制扫略体的生成过程。第二个参数profileIn是用来构造扫略体的轮廓线。第三个参数pathIn是用来构造扫略体的路径。第四个参数modelRefIn即新生成的体所属的Model,一般就是当前的激活Model。第五个参数alignParallelIn用来控制扫略过程中轮廓的角度,如果传true时,扫略过程中轮廓与全局轴而不是与路径上轮廓经过点的切向量保持固定角度。第六个参数selfRepairIn用来控制是否尝试修复自交的情况。第七个参数createSheetIn用来控制生成的结果是否是一个体。第八个参数lockDirectionIn只有在第五个参数alignParallelIn传false时才会影响扫略体生成的过程,其可以指定轮廓扫略过程中相对于路径点处切向量投影到与lockDirectionIn垂直面后的角度。第九个参数twistAngleIn可以指定轮廓扫略过程中的旋转角度。最后两个参数用来控制扫略过程中轮廓的放大倍数。通常我们很少会用到最后四个参数,大部分情况下传递null值即可。如下一段代码就简单演示了一下如何使用这个函数生成一个扫略体:
public static void TestBodyFromSweep() { double uerPerMe = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter; DEllipse3d elli = DEllipse3d.FromCenterRadiusNormal(DPoint3d.Zero, uerPerMe, DVector3d.UnitX); CurvePrimitive cvePriCirc = CurvePrimitive.CreateArc(elli); CurveVector cveProfile = new CurveVector(CurveVector.BoundaryType.Outer); cveProfile.Add(cvePriCirc); IList<DPoint3d> poles = new List<DPoint3d>() { DPoint3d.Zero,new DPoint3d(10* uerPerMe, 0,0), new DPoint3d(20 * uerPerMe, -5 * uerPerMe, 0), new DPoint3d(30 * uerPerMe, 5 * uerPerMe, 0) }; MSBsplineCurve msBsp = MSBsplineCurve.CreateFromPoles(poles, null, null, 4, false, true); CurvePrimitive cvePriBsp = CurvePrimitive.CreateBsplineCurve(msBsp); CurveVector cvePath = new CurveVector(CurveVector.BoundaryType.Open); cvePath.Add(cvePriBsp); SolidKernelEntity solidKernelEntity = null; Create.BodyFromSweep(out solidKernelEntity, cveProfile, cvePath, Session.Instance.GetActiveDgnModelRef(), false, true, true, null, null, null, null); Element ele = null; Convert1.BodyToElement(out ele, solidKernelEntity, null, Session.Instance.GetActiveDgnModelRef()); if (null != ele) ele.AddToModel(); }
如下图所示是我们这段程序的运行结果:
可以看到生成的是一个面,而不是一个封闭的体。这是因为我们给BodyFromSweep的createSheetIn参数传递的是true,如果我们改为false生成的结果就变为一个封闭的体了,如下图所示: