本帖最后由 天工开发团队 于 2025-1-7 14:17 编辑
可以用过ProgID启动天工CAD,置Interactive 和Visible属性为Flase。另一种做法是使用DesignManager,你这个情景比较推荐使用DesignManager来达到,下述为C#例程。
- TGDesignManager.Application designApp = null;
- TGDesignManager.Document doc = null;
- TGDesignManager.LinkedDocuments linkedDocs = null;
- TGDesignManager.Document linkedDoc = null;
- TGDesignManager.PropertySets propertySets = null;
- TGDesignManager.Property property = null;
- public void GetBomInfoFromDesignMgr()
- {
- try
- {
- try
- {
- // 连接到已有的TGDesignManager,此时没有存在的实例会抛出异常
- designApp = (TGDesignManager.Application)Marshal.GetActiveObject("DesignManager.Application");
- }
- catch
- {
- // 打开新的TGDesignManager
- designApp = new TGDesignManager.Application();
- designApp.Visible = 1;
- }
- // 打开想要获取BOM信息的装配文档
- doc = (TGDesignManager.Document)designApp.OpenFileInDesignManager(selectedFilePath);
- if (doc == null)
- {
- System.Windows.MessageBox.Show("文档正在被占用,请关闭文档后重试!");
- }
- linkedDocs = (TGDesignManager.LinkedDocuments)doc.LinkedDocuments[TGDesignManager.LinkTypeConstants.seLinkTypeAll];
- propertySets = designApp.PropertySets as TGDesignManager.PropertySets;
- //遍历装配文档下的子文件
- for (long i = 1; i <= linkedDocs.Count; i++)
- {
- linkedDoc = (TGDesignManager.Document)linkedDocs[i];
- // 获取子部件引用的数量
- int docNumber = linkedDoc.Occurrences;
- string fileName = linkedDoc.FullName;
- propertySets.Open(fileName, true);
- // 获取子部件BOM信息
- TGDesignManager.Properties summaryProperties = (TGDesignManager.Properties)propertySets.Item["SummaryInformation"];
- Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
- // 遍历需要使用int,并且从0开始遍历
- for (int j = 0; j < summaryProperties.Count; j++)
- {
- property = summaryProperties.Item[j] as TGDesignManager.Property;
- string name = property.Name;
- var value = property.Value.ToString();
- keyValuePairs.Add(name, value);
- }
- string bomInfo = DictionaryToString(keyValuePairs);
- // 使用 MessageBox 显示字典内容
- System.Windows.Forms.MessageBox.Show(bomInfo, "Dictionary Content", MessageBoxButtons.OK, MessageBoxIcon.Information);
- // 获取文档自定义属性集 (这里需要改成对应的属性名称)
- TGDesignManager.Properties customProperties = (TGDesignManager.Properties)propertySets.Item["Custom"];
- // 也可以直接使用对应的属性名称索引获取值
- //property = (TGDesignManager.Property)customProperties.Item["精度"];
- //string name_1 = property.Name;
- //string value_1 = property.Value.ToString();
- }
- designApp.Quit();
- }
- catch (Exception ex)
- {
- designApp.Quit();
- }
- }
复制代码
|