找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 802|回复: 0

[每日一码] 轻量线抽点程序,用于在保持曲线形状的情况下抽掉多余点

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2017-5-5 11:41:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
网络收集的代码

[C#] 纯文本查看 复制代码
[CommandMethod("Simply")]
        public void SimplyPoly()
        {
            ObjectId[] polyIdArr;
            double dLimit = 0.5;
            ed.WriteMessage("\n选择多段线:");
            PromptSelectionResult proSelRes = ed.GetSelection();
            if (proSelRes.Status == PromptStatus.OK)
            {
                polyIdArr = proSelRes.Value.GetObjectIds();
            }
            else
            {
                return;
            }
            PromptDoubleOptions douOpt = new PromptDoubleOptions("\n输入滤波阀值:");
            douOpt.DefaultValue = 0.5;
            PromptDoubleResult douRes = ed.GetDouble(douOpt);
            if (douRes.Status == PromptStatus.OK)
            {
                dLimit = douRes.Value;
            }
            else
            {
                return;
            }
            foreach (ObjectId polyId in polyIdArr)
            {
                bool bTreat = false;
                bool bClosed = false;
                Point3dCollection sptArr = new Point3dCollection();
                Point3dCollection eptArr = new Point3dCollection();
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    Polyline poly = trans.GetObject(polyId, OpenMode.ForRead) as Polyline;
                    if (poly != null)
                    {
                        bClosed = poly.Closed;
                        bTreat = true;
                    }                    
                    trans.Commit();
                } // end of using
                if (bTreat)
                {
                    GetPolyList(ref sptArr, polyId, false);
                    if (bClosed)
                    {
                        sptArr.Add(sptArr[0]);
                    }
                    Lvbo(ref sptArr, ref eptArr, 0, sptArr.Count - 1, dLimit, bClosed);
                    if (!bClosed)
                    {
                        eptArr.Add(sptArr[sptArr.Count - 1]);
                    }
                    if (eptArr.Count < sptArr.Count)
                    {
                        SetPolyCrood(polyId, ref eptArr);
                    }
                } // end of if(bTreat)
            } // end of foreach
        }


[C#] 纯文本查看 复制代码
 /// <summary>
        /// 求pt1到pt2的距离
        /// </summary>
        static public double DistOfPt2d(Point2d pt1, Point2d pt2)
        {
            return Math.Sqrt((pt1.X - pt2.X) * (pt1.X - pt2.X) + (pt1.Y - pt2.Y) * (pt1.Y - pt2.Y));
        }
        /// <summary>
        /// 求pt1,pt2的方位角
        /// </summary>
        static public double GetAngle(Point2d pt1, Point2d pt2)
        {
            double dDist = DistOfPt2d(pt1, pt2);
            Debug.Assert(dDist != 0);
            return Math.Asin((pt1.Y - pt2.Y) / dDist);
        }
        /// <summary>
        /// 求pt到pt1,pt2连线的垂距
        /// </summary>
        static double Chuiju(Point2d pt, Point2d pt1, Point2d pt2, bool bClosed)
        {
            if (bClosed)
            {
                return DistOfPt2d(pt, pt1);
            }
            else
            {
                double dAngle = GetAngle(pt1, pt) - GetAngle(pt1, pt2);
                if (dAngle < 0.0)
                {
                    dAngle += Math.PI * 2.0;
                }
                return Math.Abs(DistOfPt2d(pt, pt1) * Math.Sin(dAngle));
            } //end of if ... else
        }


[C#] 纯文本查看 复制代码
/// <summary>
        /// 设置多段线各个节点坐标,仅支持轻量线
        /// </summary>
        public void SetPolyCrood(ObjectId polyId, ref Point3dCollection ptListArr)
        {
            if (ptListArr.Count <= 1)
            {
                return;
            }
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Entity ent = trans.GetObject(polyId, OpenMode.ForWrite) as Entity;
                Debug.Assert(ent != null);
                //轻量线
                if (ent is Polyline)
                {
                    Polyline lwpoly = ent as Polyline;
                    double dWidth = lwpoly.ConstantWidth;
                    int nNumVertices = lwpoly.NumberOfVertices;
                    int i;
                    for (i = 0; i < nNumVertices; i++ )
                    {
                        if (i >= ptListArr.Count)
                        {
                            lwpoly.RemoveVertexAt(ptListArr.Count);
                        }
                        else
                        {
                            lwpoly.SetPointAt(i, new Point2d(ptListArr.X, ptListArr.Y));
                        } //end of if ... else
                    }
                    for (i = nNumVertices; i < ptListArr.Count; i++ )
                    {
                        lwpoly.AddVertexAt(i, new Point2d(ptListArr.X, ptListArr.Y), 0.0, dWidth, dWidth);
                    }
                }
                trans.Commit();
            } // end of using
        }
        /// <summary>
        /// 取得多段线节点,支持重量线和轻量线两种类型
        /// </summary>
        /// <param name="bFullNode">该参数仅对重量线起作用,是否输出全部节点</param>
        public void GetPolyList(ref Point3dCollection ptListArr, ObjectId polyId, bool bFullNode)
        {
            ptListArr.Clear();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Entity ent = trans.GetObject(polyId, OpenMode.ForRead) as Entity;
                Debug.Assert(ent != null);
                //轻量线
                if (ent is Polyline)
                {
                    Polyline lwPoly = ent as Polyline;
                    for (int i = 0; i < lwPoly.NumberOfVertices; i++ )
                    {
                        ptListArr.Add(lwPoly.GetPoint3dAt(i));
                    }
                }
                //重量线根据bFullNode参数以及节点类型,判断是否添加到输出数组
                else if (ent is Polyline2d)
                {
                    Polyline2d poly2d = ent as Polyline2d;
                    foreach (ObjectId vertexid in poly2d)
                    {
                        bool bAdd = false;
                        Vertex2d vertex = trans.GetObject(vertexid, OpenMode.ForRead) as Vertex2d;
                        Debug.Assert(vertex != null);
                        if (vertex.VertexType == Vertex2dType.SimpleVertex)
                        {
                            bAdd = true;
                        }
                        else
                        {
                            if (bFullNode)
                            {
                                if (vertex.VertexType == Vertex2dType.SplineFitVertex || vertex.VertexType == Vertex2dType.CurveFitVertex)
                                {
                                    bAdd = true;
                                }
                            } // end of  if (bFullNode)
                            else 
                            {
                                if (vertex.VertexType == Vertex2dType.SplineControlVertex)
                                {
                                    bAdd = true;
                                }
                            } // end of if (bFullNode) ... else
                        } // end of if (vertex.VertexType == Vertex2dType.SimpleVertex) ... else
                        if (bAdd)
                        {
                            ptListArr.Add(vertex.Position);
                        }
                    } // end of foreach
                }
                trans.Commit();
            } //end of using
        }


[C#] 纯文本查看 复制代码
/// <summary>
        /// 对二维坐标进行滤波处理
        /// </summary>
        public void Lvbo(ref Point3dCollection sourcePtArr, ref Point3dCollection exPtArr, int nStartIndex, int nEndIndex, double dLimit, bool bClosed)
        {
            if (dLimit <= 0.0)
            {
                return;
            }
            Debug.Assert(nStartIndex < nEndIndex);
            Debug.Assert(nEndIndex < sourcePtArr.Count);
            Point2d pt1 = new Point2d(sourcePtArr[nStartIndex].X, sourcePtArr[nStartIndex].Y);
            Point2d pt2 = new Point2d(sourcePtArr[nEndIndex].X, sourcePtArr[nEndIndex].Y);
            double dMaxDist = 0.0;
            int nMIndex = -1;
            for (int i = nStartIndex + 1; i < nEndIndex; i++ )
            {
                Point2d pt = new Point2d(sourcePtArr[i].X, sourcePtArr[i].Y);
                double dDist = Chuiju(pt, pt1, pt2, bClosed);
                if (dDist > dMaxDist)
                {
                    dMaxDist = dDist;
                    nMIndex = i;
                }
            } // end of for
            if (dMaxDist < dLimit)
            {
                exPtArr.Add(sourcePtArr[nStartIndex]);
            }
            else
            {
                Lvbo(ref sourcePtArr, ref exPtArr, nStartIndex, nMIndex, dLimit, false);
                Lvbo(ref sourcePtArr, ref exPtArr, nMIndex, nEndIndex, dLimit, false);
            }
        }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 21:30 , Processed in 0.174769 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表