- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[CommandMethod("PolylineToCSV", "plcsv",CommandFlags.Modal | CommandFlags.UsePickSet)]
public static void ExportPolyPointsToCSV()
{
Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
string dec = ((short)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("LUPREC")).ToString();
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForRemoval = "\n >> Nothing selected....";
pso.MessageForAdding = "\n >> Select a single polyline >> ";
pso.AllowDuplicates = false;
pso.SingleOnly = true;
SelectionFilter sf = new SelectionFilter
(new TypedValue[] { new TypedValue(0, "lwpolyline") });
PromptSelectionResult res = ed.GetSelection(pso, sf);
if (res.Status != PromptStatus.OK) return;
StringBuilder sb = new StringBuilder();
ObjectId[] ids = res.Value.GetObjectIds();
Polyline poly = (Polyline)tr.GetObject(ids[0], OpenMode.ForRead, false);
for (int i = 0; i < poly.NumberOfVertices; i++)
{
Point2d pt = poly.GetPoint2dAt(i);
string vexstr = string.Format("{0:f" + dec + "};{1:f" + dec + "}", pt.X, pt.Y);
sb.AppendLine(vexstr);
}
String csvFile = String.Empty;
System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
sfd.ValidateNames = true;
sfd.Title = "Save polyline vertices to CSV file";
sfd.DefaultExt = ".csv";
sfd.InitialDirectory=@"C:\Test\";
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
csvFile = sfd.FileName;
// write point to defined file
using (StreamWriter sw = new StreamWriter(csvFile))
{
sw.Write(sb.ToString());
sw.Flush();
}
sfd.Dispose();// non resident object, so kill 'em
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);
}
} |
|