- 积分
- 577
- 明经币
- 个
- 注册时间
- 2012-3-15
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
通过简单的向量计算判断点与线段位置关系,没有设置点到线段距离容差,所以点击线段时不会判断在线段上
- [CommandMethod("test")]
- public void Test()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
-
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- PromptPointOptions ptOpts = new PromptPointOptions("选择第一点A");
- PromptPointResult ptRes = ed.GetPoint(ptOpts);
- if (ptRes.Status != PromptStatus.OK)
- {
- throw new Autodesk.AutoCAD.Runtime.Exception();
- }
- Point3d pt1 = ptRes.Value;
-
- ptOpts = new PromptPointOptions("\n选择第二点B");
- ptRes = ed.GetPoint(ptOpts);
- if (ptRes.Status != PromptStatus.OK)
- {
- throw new Autodesk.AutoCAD.Runtime.Exception();
- }
- Point3d pt2 = ptRes.Value;
-
- //生成AB线段
- Line lineAB = new Line(pt1,pt2);
- AppendEntity(lineAB);
- trans.Commit();
- Vector3d vec1 = pt2 - pt1;
- ed.WriteMessage(vec1.ToString()+"\n");
-
- while (true)
- {
- ptOpts = new PromptPointOptions("\n选择第三点C");
- ptRes = ed.GetPoint(ptOpts);
- if (ptRes.Status != PromptStatus.OK)
- {
- throw new Autodesk.AutoCAD.Runtime.Exception();
- }
- Point3d pt3 = ptRes.Value;
- Vector3d vec2 = pt3 - pt1;
-
- //向量叉乘
- double result = vec2.X*vec1.Y-vec1.X*vec2.Y;
-
- if (result>0)
- {
- ed.WriteMessage("C点在AB线右侧");
- }else if (result<0)
- {
- ed.WriteMessage("C点在AB线左侧");
- }else if (result==0)
- {
- ed.WriteMessage("C点在AB线上");
- }
- }
- }
- }
- public static ObjectId AppendEntity(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entid;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
- entid = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent,true);
- trans.Commit();
- }
- return entid;
- }
|
|