马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×

- [CommandMethod("Test")]
- public static void IsPolylineSeftInters()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Transaction tr = db.TransactionManager.StartTransaction();
- TypedValue typevalue = new TypedValue((int) DxfCode.Start, "LWPOLYLINE");
- SelectionFilter filter = new SelectionFilter(new TypedValue[1]{typevalue });
- PromptSelectionResult psr = ed.SelectAll( filter );
- if (psr.Status != PromptStatus.OK) return;
- using (tr)
- {
- try
- {
- SelectionSet ss = psr.Value;
- ObjectId[] ids = ss.GetObjectIds();
- List<Entity> entLst = new List<Entity>();
- for (int i = 0; i < ids.Length; i++)
- {
- Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);
- if (IsSelfIntersect(ent))
- {
- entLst.Add(ent);
- }
- }
- if (entLst.Count > 0)
- {
- for (int i = 0; i < entLst.Count; i++)
- {
- entLst[i].UpgradeOpen();
- entLst[i].ColorIndex = 1;
- entLst[i].Highlight();
- }
- }
- tr.Commit();
- }
- catch (Exception)
- {
-
- throw;
- }
- }
- }
- /// <summary>
- /// 判断Polyline是否自相交
- /// </summary>
- /// <param name="ent">Entity(Pline)</param>
- /// <returns>true or false</returns>
- private static bool IsSelfIntersect(Entity ent)
- {
- Point3dCollection pts = new Point3dCollection();
- ent.IntersectWith(ent, Intersect.OnBothOperands, pts, IntPtr.Zero, IntPtr.Zero);
- Curve pl = ent as Curve;
- Point3d ep = pl.EndPoint;
- double[] pams = new double[pts.Count];
- for (int i = 0; i < pams.Length ; i++)
- {
- pams[i] = pl.GetParameterAtPoint(pts[i]);
- }
- return (IsGrade(pams) == 1 | EndPointIsOn(ent, ep) | NumberInters( ent,pams)) ? true : false;
- }
- /// <summary>
- /// 交点数是否等于总顶点数 - 2
- /// </summary>
- /// <param name="ent"></param>
- /// <param name="pams"></param>
- /// <returns></returns>
- private static bool NumberInters(Entity ent, double[] pams)
- {
- Curve pl = ent as Curve;
- return Convert .ToInt32( pl.EndParam - 1) != pams.Length;
- }
- /// <summary>
- /// 判断交点数是否等于顶点
- /// </summary>
- /// <param name="pams"></param>
- /// <returns></returns>
- private static int IsGrade(double[] pams)
- {
- var numPlace = pams.SkipWhile( (num, index) => num -1 == index);
- return numPlace.Count() == 0 ? 0:1;
- }
- /// <summary>
- /// 判断端点是否和其它顶点重合
- /// </summary>
- /// <param name="ent">Entity(多义线)</param>
- /// <param name="p">端点</param>
- /// <returns>重合 true</returns>
- private static bool EndPointIsOn(Entity ent, Point3d p)
- {
- Curve pl = ent as Curve;
- int n = Convert.ToInt32(pl.EndParam);
- bool ret = false ;
- for (int i = 0; i < n-1; i++)
- {
- if (pl.GetPointAtParameter(Convert .ToDouble( i)).Equals(p))
- {
- ret = true;
- break;
- }
- }
- return ret;
- }
|