问题描述: 

二次开发过程中经常会对一些样条曲线进行求平面的计算来获取Plane做进一步的数据分析,经常会遇到反馈某些样条曲线GetPlane返回错误码eNonCoplanarGeometry。

问题分析:

经过分析,大部分原因是由于样条曲线的多个顶点z坐标不在同一个平面如图:54的坐标点是-25,而其他点z坐标都是0。

步骤指引:

参考如下代码将拟合点54强行设置成0,注意图纸上显示的是UCS坐标系,而SetFitPointAt设置的是WCS坐标系,需要用GetFitPointAt获取这个点,然后转成ucs把z坐标强行设置成0 ,再转回wcs去用SetFitPointAt设置。

[CommandMethod("Example_Polyline_GetPlane")]
        public void Example_Polyline_GetPlane()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            try
            {
                // Select a polyline
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
                peo.SetRejectMessage("\nSelected object is not a polyline!");
                peo.AddAllowedClass(typeof(Spline), false);
                PromptEntityResult per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK)
                    return;

                Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                Matrix3d wcs2ucs = ucs.Inverse();

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // Open polyline for read
                    Spline polyline = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Spline;
                    if (polyline == null)
                    {
                        ed.WriteMessage("\nSelected object is not a polyline!");
                        return;
                    }

                    Point3d wcsPt = polyline.GetFitPointAt(53);

                    // 2. WCS -> UCS
                    Point3d ucsPt = wcsPt.TransformBy(wcs2ucs);

                    // 3. Force UCS Z = 0
                    Point3d ucsFlatPt = new Point3d(
                        ucsPt.X,
                        ucsPt.Y,
                        0.0
                    );

                    // 4. UCS -> WCS
                    Point3d newWcsPt = ucsFlatPt.TransformBy(ucs);

                    // 5. Set back
                    polyline.SetFitPointAt(53, newWcsPt);

                    //Get plane information
                   Plane plane = polyline.GetPlane();

                    // Display plane information
                    Point3d origin = plane.GetCoordinateSystem().Origin;
                    Vector3d normal = plane.Normal;

                    ed.WriteMessage("\nPolyline plane information:");
                    ed.WriteMessage($"\nOrigin: ({origin.X:F2}, {origin.Y:F2}, {origin.Z:F2})");
                    ed.WriteMessage($"\nNormal: ({normal.X:F4}, {normal.Y:F4}, {normal.Z:F4})");

                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage($"\nError occurred: {ex.Message}");
            }
        }


英文空间链接


★中望CAD 二次开发无法对样条曲线GetPlane失败是什么原因?https://confluence.zwcad.com/pages/viewpage.action?pageId=308805800

  • No labels