这个代码能读取二维多段线第一个顶点坐标,怎么才能读取所有顶点的坐标呢?
这个代码能读取二维多段线第一个顶点坐标,怎么才能读取所有顶点的坐标呢?
求教
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 二维多段线实验
{
public class Class1
{
public static void Pl2d()
{
// 获取当前文档和数据库,启动事务
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以读模式打开块表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写模式打开块表记录模型空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl,
OpenMode.ForWrite) as BlockTableRecord;
// 创建 2D 多段线
Polyline2d acPoly2d = new Polyline2d();
// 将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPoly2d);
acTrans.AddNewlyCreatedDBObject(acPoly2d, true);
// 先将多段线添加到块表记录,然后才能给它添加顶点
Point3dCollection acPts2dPoly = new Point3dCollection();
acPts2dPoly.Add(new Point3d(1, 1, 0));
acPts2dPoly.Add(new Point3d(2, 2, 0));
acPts2dPoly.Add(new Point3d(3, 2, 0));
acPts2dPoly.Add(new Point3d(3, 3, 0));
acPts2dPoly.Add(new Point3d(4, 3, 0));
foreach (Point3d acPt3d in acPts2dPoly)
{
Vertex2d acVer2d = new Vertex2d(acPt3d, 0,0, 0, 0);
acPoly2d.AppendVertex(acVer2d);
acTrans.AddNewlyCreatedDBObject(acVer2d, true);
}
// 获取 2D 多段线的第 1 个坐标
Point3dCollection acPts3d = new Point3dCollection();
Vertex2d acFirstVer = null;
foreach (ObjectId acObjIdVert in acPoly2d)
{
acFirstVer = acTrans.GetObject(acObjIdVert, OpenMode.ForRead) as Vertex2d;
acPts3d.Add(acFirstVer.Position);
break;
}
// 获取 2D 多段线的第 1 个顶点
// Z 坐标值用 Elevation 属性值
Point3d pFirstVer = new Point3d(acFirstVer.Position.X,
acFirstVer.Position.Y,
acPoly2d.Elevation);
Application.ShowAlertDialog("The first vertex has the following " +
"coordinates:" + "\nOCS: " + pFirstVer.ToString());
acTrans.Commit();
}
}
}
}
C929 发表于 2020-11-2 18:40
我改了一下这串代码
发现得到的内容是这个字符串,而不是顶点坐标,能帮忙解答一下吗
拜托,你的返回结果的提示不正证明获取到了你想要的结果,正确的结果?
1,说明该函数没有报异常,有返回值,并且返回值不为空;
2、并且返回值就是List<Vertex2d>,你取其中的第二个,序号1,不就是你想要的第二个点?
3、没有人跟你说随便使用一个ToString()就给你写出坐标数值 C929 发表于 2020-11-2 18:40
我改了一下这串代码
发现得到的内容是这个字符串,而不是顶点坐标,能帮忙解答一下吗
老板已经把钱打到你的银行账号,不要还说老板没给你发工资。 public virtual Autodesk.AutoCAD.Geometry.Point3d GetPointAtParameter(double value)
Autodesk.AutoCAD.DatabaseServices.Curve 的成员
试试这个? public virtual System.Collections.IEnumerator GetEnumerator()
Autodesk.AutoCAD.DatabaseServices.Polyline2d 的成员
或者这个?遍历一下?没试过 https://forums.autodesk.com/t5/net/getting-the-number-of-vertices-for-a-polyline2d-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;
} sieben 发表于 2020-11-2 14:15
https://forums.autodesk.com/t5/net/getting-the-number-of-vertices-for-a-polyline2d-object/m-p/ ...
这个代码用不起来 不会用呀:'( 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
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
namespace 读取XData
{
public static class Class1
{
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;
}
public static void GetAttribute()
{
//文档管理器
DocumentCollection acDocMgr = Application.DocumentManager;
//激活的文档
Document acDoc = acDocMgr.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
using (acDoc.LockDocument())
{
// 启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 请求在图形区域选择对象
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
// 如果提示状态OK,表示已选择对象
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
//遍历选择集内的对象
foreach (SelectedObject acSSObj in acSSet)
{
// 确认返回的是合法的SelectedObject对象
if (acSSObj != null)
{
//获取实体
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForRead) as Entity;
if (acEnt.GetRXClass().Name == "AcDb2dPolyline")
{
if (acEnt.XData != null)
{
Polyline2d pl = (Polyline2d)acEnt;
MessageBox.Show(GetVertices(pl).ToString(), "二维多段线坐标");
//MessageBox.Show(result, "提示");
}
else
{
//MessageBox.Show("该实体为空!", "提示");
}
}
}
}
// 保存新对象到数据库
acTrans.Commit();
}
}
}
}
}
} 你的acFirstVer在foreach里一直被替换,所以最后结束的时候,你的acFirstVer里面只有一个点呀
页:
[1]