明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2269|回复: 4

CAD获取一个图形上所有直线的交点

[复制链接]
发表于 2012-7-20 11:04 | 显示全部楼层 |阅读模式
本帖最后由 jiangfei200809 于 2012-7-20 11:08 编辑
  1. #region 相交判断

  2.         [CommandMethod("crossCAD")]
  3.         public void CrossCAD()
  4.         {

  5.             TypedValue[] pTypeValue = new TypedValue[1];
  6.             pTypeValue[0] = new TypedValue((int)DxfCode.LayerName, “测试”);
  7.             SelectionFilter pSelectionFilter=new SelectionFilter(pTypeValue);

  8.             PromptSelectionResult selectResult = pDocument.Editor.SelectAll(pSelectionFilter);
  9.             SelectionSet pSelectionSet = selectResult.Value;

  10.             using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
  11.             {
  12.                 List crossPoint = new List();//存放相交点
  13.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(pSelectionSet.Count.ToString());

  14.                 for(int i=0;i= minX && y >= minY && x <= maxX && y<= maxY)
  15.                                     {
  16.                                         #region 确定第二条线的最大最小坐标

  17.                                         if (secondLine.StartPoint.X < secondLine.EndPoint.X)
  18.                                         {
  19.                                             minX = secondLine.StartPoint.X;
  20.                                             maxX = secondLine.EndPoint.X;
  21.                                         }
  22.                                         else
  23.                                         {
  24.                                             minX = secondLine.EndPoint.X;
  25.                                             maxX = secondLine.StartPoint.X;
  26.                                         }
  27.                                         if (secondLine.StartPoint.Y < secondLine.EndPoint.Y)
  28.                                         {
  29.                                             minY = secondLine.StartPoint.Y;
  30.                                             maxY = secondLine.EndPoint.Y;
  31.                                         }
  32.                                         else
  33.                                         {
  34.                                             maxY = secondLine.StartPoint.Y;
  35.                                             minY = secondLine.EndPoint.Y;
  36.                                         }

  37.                                         #endregion

  38.                                         if (x >= minX && y >= minY && x <= maxX && y <= maxY)
  39.                                         {
  40.                                             crossPoint.Add(new Point2d(x, y));
  41.                                         }

  42.                                     }
  43.                                 }

  44.                                 #endregion
  45.                             }

  46.                         }
  47.                     }
  48.                 }

  49.                 if (crossPoint.Count == 1)
  50.                 {
  51.                     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("交点为:"+crossPoint[0].X.ToString()+","+crossPoint[0].Y.ToString());
  52.                     return;
  53.                 }
  54.                 BlockTable pBlockTable = tran.GetObject(pDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
  55.                 BlockTableRecord pBlockTableRecord = tran.GetObject(pBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  56.                 for (int i = 0; i < crossPoint.Count; i++)
  57.                 {
  58.                     Circle pCircle = new Circle(new Point3d(crossPoint[i].X, crossPoint[i].Y, 0), Vector3d.ZAxis, 1);
  59.                     pCircle.ColorIndex = 0;

  60.                     pBlockTableRecord.AppendEntity(pCircle);
  61.                     tran.AddNewlyCreatedDBObject(pCircle, true);
  62.                 }

  63.                 tran.Commit();
  64.             }
  65.         }

  66.         #endregion


图层名称我用的是“测试”,大家根据自己需要改下。
通过以上代码可以将一个图层上所有直线相交点用圆的形式标出,希望大家有用

本帖子中包含更多资源

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

x
发表于 2012-7-20 12:17 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2012-7-20 12:23 编辑

纯数学的方法? 不过好像差点代码。。。
试下这段代码

  1.         [CommandMethod("tx1")]
  2.         public static void CrossCAD()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var sf =
  8.                 new SelectionFilter
  9.                 (
  10.                     new TypedValue[]{ new TypedValue(0, "line")}
  11.                 );
  12.             var res = ed.SelectAll(sf);
  13.             if (res.Status != PromptStatus.OK) return;
  14.             using (var tr = doc.TransactionManager.StartTransaction())
  15.             {
  16.                 var lines = new List<Line>();
  17.                 foreach(ObjectId id in res.Value.GetObjectIds())
  18.                 {
  19.                     lines.Add((Line)tr.GetObject(id, OpenMode.ForRead));
  20.                 }
  21.                 List<Point3d> pts = new List<Point3d>();
  22.                 for (int m = 0; m < lines.Count; m++)
  23.                 {
  24.                     LineSegment3d ls1 = new LineSegment3d(lines[m].StartPoint, lines[m].EndPoint);
  25.                     for (int n = m + 1; n < lines.Count; n++)
  26.                     {
  27.                         LineSegment3d ls2 = new LineSegment3d(lines[n].StartPoint, lines[n].EndPoint);
  28.                         CurveCurveIntersector3d cc3d = new CurveCurveIntersector3d(ls1, ls2, Vector3d.ZAxis);
  29.                         if (cc3d.NumberOfIntersectionPoints == 1)
  30.                         {
  31.                             pts.Add(cc3d.GetIntersectionPoint(0));
  32.                         }
  33.                     }
  34.                 }
  35.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  36.                 BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
  37.                 foreach (var pt in pts)
  38.                 {
  39.                     Circle c = new Circle(pt, Vector3d.ZAxis, 1);
  40.                     btr.AppendEntity(c);
  41.                     tr.AddNewlyCreatedDBObject(c, true);
  42.                 }
  43.                 tr.Commit();
  44.             }
  45.         }

 楼主| 发表于 2012-7-20 14:00 | 显示全部楼层
雪山飞狐_lzh 发表于 2012-7-20 12:17
纯数学的方法? 不过好像差点代码。。。
试下这段代码

缺代码吗 亲?纯数学运算的方法做的 通过构建二元一次方程 然后 判断相交点是否在线上...代码完全可行哈 当然你的代码要简洁点 哈哈
发表于 2019-3-26 18:39 | 显示全部楼层
不是AUTOLISP?看不懂,能否做成一个插件LSP
发表于 2019-5-16 18:42 | 显示全部楼层
学习了 感谢楼主
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 15:08 , Processed in 0.549370 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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