明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1182|回复: 8

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

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

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


  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. }

发表于 2020-11-3 08:56 | 显示全部楼层
C929 发表于 2020-11-2 18:40
我改了一下这串代码

发现得到的内容是这个字符串,而不是顶点坐标,能帮忙解答一下吗

拜托,你的返回结果的提示不正证明获取到了你想要的结果,正确的结果?
1,说明该函数没有报异常,有返回值,并且返回值不为空;
2、并且返回值就是List<Vertex2d>,你取其中的第二个,序号1,不就是你想要的第二个点?
3、没有人跟你说随便使用一个ToString()就给你写出坐标数值
回复 支持 0 反对 1

使用道具 举报

发表于 2020-11-3 09:03 | 显示全部楼层
C929 发表于 2020-11-2 18:40
我改了一下这串代码

发现得到的内容是这个字符串,而不是顶点坐标,能帮忙解答一下吗

老板已经把钱打到你的银行账号,不要还说老板没给你发工资。

评分

参与人数 1金钱 +20 收起 理由
C929 + 20 淡定

查看全部评分

回复 支持 0 反对 1

使用道具 举报

发表于 2020-11-2 14:02 | 显示全部楼层
public virtual Autodesk.AutoCAD.Geometry.Point3d GetPointAtParameter(double value)
    Autodesk.AutoCAD.DatabaseServices.Curve 的成员
试试这个?
发表于 2020-11-2 14:07 | 显示全部楼层
public virtual System.Collections.IEnumerator GetEnumerator()
    Autodesk.AutoCAD.DatabaseServices.Polyline2d 的成员
或者这个?遍历一下?没试过
发表于 2020-11-2 14:15 | 显示全部楼层
      https://forums.autodesk.com/t5/n ... -object/m-p/7737675

  /// <summary>
        /// Gets the vertices list of the polyline 2d.
        /// </summary>
        /// <param name="pl">The instance to which the method applies.</param>
        /// <returns>The vertices list.</returns>
        /// <exception cref="Autodesk.AutoCAD.Runtime.Exception">
        /// eNoActiveTransactions is thrown if the method is not called form a Transaction.</exception>
        public static List<Vertex2d> GetVertices(this Polyline2d pl)
        {
            Transaction tr = pl.Database.TransactionManager.TopTransaction;
            if (tr == null)
                throw new AcRx.Exception(AcRx.ErrorStatus.NoActiveTransactions);

            List<Vertex2d> vertices = new List<Vertex2d>();
            foreach (ObjectId id in pl)
            {
                Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
                if (vx.VertexType != Vertex2dType.SplineControlVertex)
                    vertices.Add(vx);
            }
            return vertices;
        }
 楼主| 发表于 2020-11-2 15:21 | 显示全部楼层
sieben 发表于 2020-11-2 14:15
https://forums.autodesk.com/t5/net/getting-the-number-of-vertices-for-a-polyline2d-object/m-p/ ...

这个代码用不起来 不会用呀
 楼主| 发表于 2020-11-2 18:40 | 显示全部楼层
sieben 发表于 2020-11-2 14:15
https://forums.autodesk.com/t5/net/getting-the-number-of-vertices-for-a-polyline2d-object/m-p/ ...

我改了一下这串代码

发现得到的内容是这个字符串,而不是顶点坐标,能帮忙解答一下吗
System.Collections.Generic.List`1[Autodesk.AutoCAD.DatabaseServices.Vertex2d]


  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using Application = Autodesk.AutoCAD.ApplicationServices.Application;

  13. namespace 读取XData
  14. {

  15.     public static class Class1
  16.     {
  17.         public static List<Vertex2d> GetVertices(this Polyline2d pl)
  18.         {
  19.             Transaction tr = pl.Database.TransactionManager.TopTransaction;
  20.             // if (tr == null)
  21.             // throw new AcRx.Exception(AcRx.ErrorStatus.NoActiveTransactions);

  22.             List<Vertex2d> vertices = new List<Vertex2d>();
  23.             foreach (ObjectId id in pl)
  24.             {
  25.                 Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
  26.                 if (vx.VertexType != Vertex2dType.SplineControlVertex)
  27.                     vertices.Add(vx);
  28.             }
  29.             return vertices;
  30.         }


  31.         [CommandMethod("GA", CommandFlags.Session)]

  32.         public static void GetAttribute()
  33.         {

  34.             //文档管理器
  35.             DocumentCollection acDocMgr = Application.DocumentManager;
  36.             //激活的文档
  37.             Document acDoc = acDocMgr.MdiActiveDocument;
  38.             Database acCurDb = acDoc.Database;
  39.             Editor ed = acDoc.Editor;

  40.             using (acDoc.LockDocument())
  41.             {
  42.                 // 启动事务
  43.                 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  44.                 {
  45.                     // 请求在图形区域选择对象
  46.                     PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
  47.                     // 如果提示状态OK,表示已选择对象
  48.                     if (acSSPrompt.Status == PromptStatus.OK)
  49.                     {
  50.                         SelectionSet acSSet = acSSPrompt.Value;
  51.                         //遍历选择集内的对象
  52.                         
  53.                         foreach (SelectedObject acSSObj in acSSet)
  54.                         {
  55.                             // 确认返回的是合法的SelectedObject对象
  56.                             if (acSSObj != null)
  57.                             {
  58.                                 //获取实体
  59.                                 Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead) as Entity;

  60.                                 
  61.                                     if (acEnt.GetRXClass().Name == "AcDb2dPolyline")
  62.                                     {

  63.                                         if (acEnt.XData != null)

  64.                                         {
  65.                                                                                     
  66.                                             Polyline2d pl = (Polyline2d)acEnt;

  67.                                             MessageBox.Show(GetVertices(pl).ToString(), "二维多段线坐标");
  68.                                             //MessageBox.Show(result, "提示");
  69.                                         }
  70.                                         else
  71.                                         {
  72.                                             //MessageBox.Show("该实体为空!", "提示");
  73.                                         }
  74.                                     }

  75.                                 
  76.                             }
  77.                         }

  78.                         // 保存新对象到数据库
  79.                         acTrans.Commit();




  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }

  85. }
发表于 2020-11-24 14:53 | 显示全部楼层
你的acFirstVer在foreach里一直被替换,所以最后结束的时候,你的acFirstVer里面只有一个点呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-7 07:54 , Processed in 0.399340 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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