文章目录

  • 创建C#工程
    • 创建工程
    • 书写代码

      创建C#工程

      创建工程

      1、创建C#的控制台应用程序工程
      中望CAD平台接口(读写标题栏)(c#) - 图1
      2、设置工程适用x64或x86
      右键点击解决方案,点击属性
      中望CAD平台接口(读写标题栏)(c#) - 图2
      3、选择代码优化
      中望CAD平台接口(读写标题栏)(c#) - 图3
      4、引入ZwDatabaseMgd、 ZwManaged,ZWCAD 2018 Type Library依赖库
      中望CAD平台接口(读写标题栏)(c#) - 图4

      书写代码

      ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using ZWCAD; using System.Runtime.InteropServices; using System.IO; using System.Text.RegularExpressions; using System.Xml; using ZwSoft.ZwCAD.DatabaseServices; using ZwSoft.ZwCAD.EditorInput; using ZwSoft.ZwCAD.ApplicationServices; namespace cadpttest { class Program { static void Main(string[] args) {
      1. ZWCAD.ZcadApplication m_cadApp = new ZWCAD.ZcadApplication();
      2. m_cadApp.Visible = false;
      3. ZcadDocument document = m_cadApp.Documents.Open(args[0]);//dwg文件路径
      4. ZcadBlockReference titleBlock = null;
      5. ZcadSelectionSet selSet = document.SelectionSets.Add("set1");
      6. Object groupCode = new short[] { (int)DxfCode.Start };
      7. Object dataCode = new Object[] { "INSERT" };
      8. selSet.Select(ZcSelect.zcSelectionSetAll, null, null, groupCode, dataCode);
      9. for (int i = 0; i < selSet.Count; i++)
      10. {
      11. ZcadEntity entity = selSet.Item(i);
      12. if ("AcDbBlockReference".Equals(entity.ObjectName))
      13. {
      14. ZcadBlockReference AcadBlockReference = (ZcadBlockReference)entity;
      15. Console.WriteLine(AcadBlockReference.Name);
      16. if (AcadBlockReference.Name.Contains("中车标题栏"))
      17. {
      18. titleBlock = AcadBlockReference;
      19. }
      20. }
      21. }
      22. if (titleBlock != null)
      23. {
      24. Object[] attrArrays = titleBlock.GetAttributes();
      25. for (int j = 0; j < attrArrays.Length; j++)
      26. {
      27. ZcadAttributeReference attr = (ZcadAttributeReference)attrArrays[j];
      28. Console.WriteLine("attr.TagString------->" + attr.TagString);
      29. Console.WriteLine("attr.TextString------->" + attr.TextString);
      30. attr.TextString = "需要设置的值";
      31. }
      32. }
      33. document.Save();
      34. document.Close(false, null);
      35. m_cadApp.Quit();
      } } }

``` 生成exe可执行文件