文章目录
- 创建C#工程
- 创建工程
- 书写代码
创建C#工程
创建工程
1、创建C#的控制台应用程序工程
2、设置工程适用x64或x86
右键点击解决方案,点击属性
3、选择代码优化
4、引入ZwDatabaseMgd、 ZwManaged,ZWCAD 2018 Type Library依赖库
书写代码
``` 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) {
} } }ZWCAD.ZcadApplication m_cadApp = new ZWCAD.ZcadApplication();
m_cadApp.Visible = false;
ZcadDocument document = m_cadApp.Documents.Open(args[0]);//dwg文件路径
ZcadBlockReference titleBlock = null;
ZcadSelectionSet selSet = document.SelectionSets.Add("set1");
Object groupCode = new short[] { (int)DxfCode.Start };
Object dataCode = new Object[] { "INSERT" };
selSet.Select(ZcSelect.zcSelectionSetAll, null, null, groupCode, dataCode);
for (int i = 0; i < selSet.Count; i++)
{
ZcadEntity entity = selSet.Item(i);
if ("AcDbBlockReference".Equals(entity.ObjectName))
{
ZcadBlockReference AcadBlockReference = (ZcadBlockReference)entity;
Console.WriteLine(AcadBlockReference.Name);
if (AcadBlockReference.Name.Contains("中车标题栏"))
{
titleBlock = AcadBlockReference;
}
}
}
if (titleBlock != null)
{
Object[] attrArrays = titleBlock.GetAttributes();
for (int j = 0; j < attrArrays.Length; j++)
{
ZcadAttributeReference attr = (ZcadAttributeReference)attrArrays[j];
Console.WriteLine("attr.TagString------->" + attr.TagString);
Console.WriteLine("attr.TextString------->" + attr.TextString);
attr.TextString = "需要设置的值";
}
}
document.Save();
document.Close(false, null);
m_cadApp.Quit();
``` 生成exe可执行文件