我有一个DWG文件,并在那里绘制了一些折线。
我想提取这些折线的坐标,并使用easting northing coord系统将它们保存到csv中。
是否可以在Autocad中执行类似的操作?
BR, 爱奥尼斯
答案 0 :(得分:0)
您可以编写一个autocad .NET插件(c#),它迭代特定图层的所有行,并将该行的每个点导出到csv。有关第一个示例,请参阅https://knowledge.autodesk.com/support/autocad/learn-explore/caas/video/youtube/watch-v-5a50QULamuU.html。这是一个基本的例子,它创建了一个插件,可以使用命令" Action"来启动。但缺少线路输出的部分。此插件在每个图层的图形中生成随机点和线。 autocad API还有其他几个教程。您可以使用此示例代码来启动项目。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(AcadLayerPlugin.LayerPlugin))]
namespace AcadLayerPlugin
{
public class LayerPlugin
{
[CommandMethod("Action")]
public static void Action()
{
//System.Windows.Forms.MessageBox.Show("Huhu");
Database database = HostApplicationServices.WorkingDatabase;
try
{
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
SymbolTable symTable = (SymbolTable)transaction.GetObject(database.LayerTableId, OpenMode.ForRead);
foreach (ObjectId id in symTable)
{
LayerTableRecord symbol = (LayerTableRecord)transaction.GetObject(id, OpenMode.ForWrite);
//TODO: Access to the symbol
//Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nName: {0}", symbol.Name));
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = transaction.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a line that starts at 5,5 and ends at 12,3
Random r = new Random();
Line acLine = new Line(new Point3d(r.NextDouble(), r.NextDouble(), r.NextDouble()),
new Point3d(r.NextDouble(), r.NextDouble(), r.NextDouble()));
acLine.LayerId = id;
Autodesk.AutoCAD.DatabaseServices.DBPoint point = new DBPoint(new Point3d(r.NextDouble(), r.NextDouble(), r.NextDouble()));
point.LayerId = id;
MText acMText = new MText();
acMText.Location = new Point3d(r.NextDouble(), r.NextDouble(), r.NextDouble());
acMText.Width = 8;
acMText.Contents = "Ohlsen was here.";
acMText.LayerId = id;
acBlkTblRec.AppendEntity(acLine);
acBlkTblRec.AppendEntity(point);
acBlkTblRec.AppendEntity(acMText);
transaction.AddNewlyCreatedDBObject(acLine, true);
transaction.AddNewlyCreatedDBObject(point, true);
transaction.AddNewlyCreatedDBObject(acMText, true);
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nName: {0}", symbol.Name));
// Save the new object to the database
transaction.Commit();
}
transaction.Commit();
}
}catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}