优秀的编程知识分享平台

网站首页 > 技术文章 正文

Autocad开发笔记:点和闭合多段线的位置关系

nanyue 2024-07-18 22:30:00 技术文章 5 ℃

在Autocad的二次开发中经常需要判断点和多段线的位置关系,点和闭合多段线的位置关系,一个最简便的方法是利用class Autodesk.AutoCAD.DatabaseServices.MPolygon的IsPointInsideMPolygon方法进行判断。

MPolygon需要引用AcMPolygonMGD动态链接库。


[CommandMethod("PINPT1")]

public void PINPT1()

{

Document doc = Application.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

Database db = doc.Database;

PromptEntityOptions peo = new PromptEntityOptions("\npolyline");

peo.SetRejectMessage("only a polyline");

peo.AddAllowedClass(typeof(Polyline), true);

PromptEntityResult per = ed.GetEntity(peo);

if (per.Status != PromptStatus.OK) return;

using(Transaction tr = db.TransactionManager.StartTransaction())

{

try

{

double tol = 1e-4;

MPolygon mpolygon = new MPolygon();

Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);

mpolygon.AppendLoopFromBoundary(pline, true, tol);

PromptPointOptions ppo = new PromptPointOptions("\npick a point");

ppo.AllowNone = true;

while (true)

{

PromptPointResult ppr = ed.GetPoint(ppo);

if (ppr.Status != PromptStatus.OK) break;

IntegerCollection integers = mpolygon.IsPointInsideMPolygon(ppr.Value, tol);

PointContainment result = PointContainment.Outside;

if (integers.Count > 0)

{

result = PointContainment.Inside;

if (mpolygon.IsPointOnLoopBoundary(ppr.Value, integers[0], tol))

result = PointContainment.OnBoundary;

}

ed.WriteMessage($"\n{result}");

}

}

catch(System.Exception exn)

{

ed.WriteMessage($"\nError {exn.Message}");

}

}

}

猜你喜欢

最近发表
标签列表