chr
2024-08-12 e9d7a5ef4c17e4804fb988dd193ff7d1fa36d52b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using SolidWorks.Interop.swdocumentmgr;
using System;
using System.Runtime.InteropServices;
 
namespace PdmSwPlugin.Common.Util
{
    public class SwDMUtil
    {
        /// <summary>
        /// 凌创公司的 DM API 授权码
        /// </summary>
        public static string LinktronLicenseKey = @"SuzhouLinktronSystemsCoLtd:swdocmgr_general-11785-02051-00064-33793-08629-34307-00007-05128-58478-32321-57480-30765-00622-59999-16385-62752-47753-50558-31076-40652-29868-22926-54604-41449-54717-42289-44473-51665-47549-58701-53709-46485-03533-12733-37329-14337-29280-51290-50890-25690-25696-964";
 
 
        /// <summary>
        /// 获取一张工程图引用的3D图路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="error1"></param>
        /// <param name="error2"></param>
        /// <returns></returns>
        public static string GetDrawingRef(string filePath, out string errMsg)
        {
            errMsg = null;
            try
            {
                string originalExt;
                SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(filePath, out originalExt);
                if (docType != SwDmDocumentType.swDmDocumentDrawing)
                {
                    errMsg = "给定的文档不是一张工程图";
                    throw null;
                }
                SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
                SwDMApplication swDMApp = swDMClassFactory.GetApplication(LinktronLicenseKey);
                if (swDMApp == null)
                {
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = "SwDM 初始化失败";
                    return null;
                }
                SwDMDocument23 swDoc = (SwDMDocument23)swDMApp.GetDocument(filePath, docType, true, out var error1);
                if (swDoc == null)
                {
                    Marshal.ReleaseComObject(swDMApp);
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = $"SwDM 加载文档失败,异常【{error1}】";
                    return null;
                }
                var swSearchOpt = swDMApp.GetSearchOptionObject();
                object vBrokenRefs = null;
                object vIsVirtuals = null;
                object vTimeStamps = null;
                object vIsImported = null;
 
                string[] views = (string[])swDoc.GetAllExternalReferences5(swSearchOpt,
                    out vBrokenRefs, out vIsVirtuals, out vTimeStamps, out vIsImported);
                if (views == null || views.Length <= 0)
                {
                    Marshal.ReleaseComObject(swDoc);
                    Marshal.ReleaseComObject(swDMApp);
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = $"选定的工程图不包含有效的视图";
                    return null;
                }
                string result = views[0];
                Marshal.ReleaseComObject(swDoc);
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return null;
            }
        }
 
        public static double[] GetMassProperty(string filePath, out string errMsg)
        {
            try
            {
                errMsg = null;
                string originalExt;
                SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(filePath, out originalExt);
                if (docType == SwDmDocumentType.swDmDocumentUnknown)
                {
                    errMsg = "未知的文档类型";
                    return null;
                }
                SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
                SwDMApplication swDMApp = swDMClassFactory.GetApplication(LinktronLicenseKey);
                if (swDMApp == null)
                {
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = "SwDM 初始化失败";
                    return null;
                }
                SwDMDocument17 swDoc = (SwDMDocument17)swDMApp.GetDocument(filePath, docType, true, out var error1);
                if (swDoc == null)
                {
                    Marshal.ReleaseComObject(swDMApp);
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = $"SwDM 加载文档失败,异常【{error1}】";
                    return null;
                }
                SwDMConfigurationMgr dmConfigMgr = swDoc.ConfigurationManager;
                if (dmConfigMgr == null)
                {
                    Marshal.ReleaseComObject(swDoc);
                    Marshal.ReleaseComObject(swDMApp);
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = $"获取 ConfigurationManager 失败";
                    return null;
                }
                string configName = dmConfigMgr.GetActiveConfigurationName();
                SwDMConfiguration dmConfig = dmConfigMgr.GetConfigurationByName(configName);
                if (dmConfig == null)
                {
                    Marshal.ReleaseComObject(dmConfigMgr);
                    Marshal.ReleaseComObject(swDoc);
                    Marshal.ReleaseComObject(swDMApp);
                    Marshal.ReleaseComObject(swDMClassFactory);
                    errMsg = $"获取 Configuration 失败";
                    return null;
                }
                double[] datas = dmConfig.GetMassProperties(out SwDmMassPropError error2);
                errMsg = $"读取质量参数失败,【{error2}】";
                swDoc.CloseDoc();
                Marshal.ReleaseComObject(dmConfig);
                Marshal.ReleaseComObject(dmConfigMgr);
                Marshal.ReleaseComObject(swDoc);
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return datas;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return null;
            }
        }
    }
}