获取当前Civil3D文档中的图层信息,并保存到文件

  1. [CommandMethod("GetLayerNames")]
  2. public void GetLayerNames()
  3. {
  4. var list = new List<string>();
  5. using (var tr = db.TransactionManager.StartTransaction())
  6. {
  7. var lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  8. foreach (var id in lt)
  9. {
  10. var ltr = tr.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
  11. list.Add($"{ltr.Name},{ltr.Color},{ltr.Description}");
  12. }
  13. tr.Commit();
  14. }
  15. var sfd = new SaveFileDialog();
  16. sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
  17. sfd.Title = "请选择要保存的位置:";
  18. sfd.Filter = "TXT文件|*.txt|所有文件|*.*";
  19. sfd.ShowDialog();
  20. var path = sfd.FileName;
  21. if (path == "") return;
  22. using (var fsWrite = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
  23. {
  24. var sw = new StreamWriter(fsWrite);
  25. foreach (var s in list) sw.WriteLine(s);
  26. }
  27. MessageBox.Show("保存成功!");
  28. }