找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 374|回复: 0

[研讨] 这个代码能读取二维多段线第一个顶点坐标,怎么才能读取所有顶点的坐标呢?

[复制链接]
发表于 2020-11-2 11:56:11 | 显示全部楼层 |阅读模式

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

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

×
这个代码能读取二维多段线第一个顶点坐标,怎么才能读取所有顶点的坐标呢?
求教
  1. using Autodesk.AutoCAD.Runtime;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Geometry;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;

  10. namespace 二维多段线实验
  11. {
  12.     public class Class1
  13.     {
  14.         [CommandMethod("Pl2d")]
  15.         public static void Pl2d()
  16.         {
  17.             // 获取当前文档和数据库,启动事务
  18.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  19.             Database acCurDb = acDoc.Database;
  20.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  21.             {
  22.                 // 以读模式打开块表
  23.                 BlockTable acBlkTbl;
  24.                 acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
  25.                 OpenMode.ForRead) as BlockTable;
  26.                 // 以写模式打开块表记录模型空间
  27.                 BlockTableRecord acBlkTblRec;
  28.                 acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  29.                 OpenMode.ForWrite) as BlockTableRecord;
  30.                 // 创建 2D 多段线
  31.                 Polyline2d acPoly2d = new Polyline2d();
  32.                 // 将新对象添加到块表记录和事务
  33.                 acBlkTblRec.AppendEntity(acPoly2d);

  34.                 acTrans.AddNewlyCreatedDBObject(acPoly2d, true);
  35.                 // 先将多段线添加到块表记录,然后才能给它添加顶点
  36.                 Point3dCollection acPts2dPoly = new Point3dCollection();
  37.                 acPts2dPoly.Add(new Point3d(1, 1, 0));
  38.                 acPts2dPoly.Add(new Point3d(2, 2, 0));
  39.                 acPts2dPoly.Add(new Point3d(3, 2, 0));
  40.                 acPts2dPoly.Add(new Point3d(3, 3, 0));
  41.                 acPts2dPoly.Add(new Point3d(4, 3, 0));
  42.                 foreach (Point3d acPt3d in acPts2dPoly)
  43.                 {
  44.                     Vertex2d acVer2d = new Vertex2d(acPt3d, 0,0, 0, 0);
  45.                     acPoly2d.AppendVertex(acVer2d);
  46.                     acTrans.AddNewlyCreatedDBObject(acVer2d, true);
  47.                 }
  48.                 // 获取 2D 多段线的第 1 个坐标
  49.                 Point3dCollection acPts3d = new Point3dCollection();
  50.                 Vertex2d acFirstVer = null;


  51.                 foreach (ObjectId acObjIdVert in acPoly2d)
  52.                 {
  53.                     acFirstVer = acTrans.GetObject(acObjIdVert, OpenMode.ForRead) as Vertex2d;
  54.                     acPts3d.Add(acFirstVer.Position);
  55.                     break;
  56.                 }
  57.                 // 获取 2D 多段线的第 1 个顶点
  58.                 // Z 坐标值用 Elevation 属性值
  59.                 Point3d pFirstVer = new Point3d(acFirstVer.Position.X,
  60.                 acFirstVer.Position.Y,
  61.                 acPoly2d.Elevation);
  62.      
  63.                 Application.ShowAlertDialog("The first vertex has the following " +
  64.                 "coordinates:" + "\nOCS: " + pFirstVer.ToString());
  65.            
  66.                 acTrans.Commit();
  67.             }
  68.         }
  69.                           
  70.     }
  71. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-26 13:37 , Processed in 0.204293 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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