chr
2024-11-02 b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using PdmSwPlugin.Common.Constants;
using PdmSwPlugin.Common.Util.Pdm;
using PdmSwPlugin.PriceCheck.Model;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Data;
using System.IO;
using System.Xml;
 
namespace PdmSwPlugin.PriceCheck.Util
{
    public class ModelReader
    {
        public static CustomPropertyManager swCuspro;
 
 
        public static void RefreshModelInfo(SldWorks SwApp, ModelProperty modelProperty, out string errorInfo)
        {
            errorInfo = null;
            try
            {
                ModelDoc2 swDoc = SwApp.IActiveDoc2;
                modelProperty.Doc = swDoc;
                if (swDoc == null)
                {
                    errorInfo = "请打开模型";
                    return;
                }
                int docType = swDoc.GetType();
                string fullPath;
                ModelDoc2 swDoc1 = null;
                #region 获取模型doc
                if (docType == (int)swDocumentTypes_e.swDocASSEMBLY)
                {
                    // 装配体
                    fullPath = Path.GetDirectoryName(swDoc.GetPathName()) + "\\xmlData\\";
 
                    SelectionMgr swSelMgr = (SelectionMgr)swDoc.SelectionManager;
                    int nSelCount = swSelMgr.GetSelectedObjectCount2(-1);
                    if (nSelCount == 0)
                    {
                        errorInfo = "请选择一个零件";
                        return;
                    }
                    else if (nSelCount > 1)
                    {
                        errorInfo = "请选择一个零件";
                        return;
                    }
 
                    int nSelType = swSelMgr.GetSelectedObjectType3(1, -1);
                    if (nSelType == (int)swSelectType_e.swSelEDGES)//Edge边
                    {
                        Edge swEdge = (Edge)swSelMgr.GetSelectedObject6(1, -1);
                        Entity swEnt = (Entity)swEdge;
                        Component2 swCompnt = (Component2)swEnt.GetComponent();
                        swDoc1 = (ModelDoc2)swCompnt.GetModelDoc2();
                    }
                    else if (nSelType == (int)swSelectType_e.swSelFACES)//Face2面
                    {
                        Face2 swFace = (Face2)swSelMgr.GetSelectedObject6(1, -1);
                        Entity swEnt = (Entity)swFace;
                        Component2 swCompnt = (Component2)swEnt.GetComponent();
                        swDoc1 = (ModelDoc2)swCompnt.GetModelDoc2();
                    }
                    else if (nSelType == (int)swSelectType_e.swSelVERTICES)//Vertex点
                    {
                        Vertex myVertex = (Vertex)swSelMgr.GetSelectedObject6(1, -1);
                        Entity swEnt = (Entity)myVertex;
                        Component2 swCompnt = (Component2)swEnt.GetComponent();
                        swDoc1 = (ModelDoc2)swCompnt.GetModelDoc2();
                    }
                    else if (nSelType == (int)swSelectType_e.swSelCOMPONENTS)//Component2模型
                    {
                        Component2 swCompnt = (Component2)swSelMgr.GetSelectedObject6(1, -1);
                        swDoc1 = (ModelDoc2)swCompnt.GetModelDoc2();
                    }
 
                    if (swDoc1 == null)
                    {
                        errorInfo = "选择的模型可能处于压缩或轻化状态,数据读取失败";
                        return;
                    }
 
                    if (swDoc1.GetType() != (int)swDocumentTypes_e.swDocPART)
                    {
                        errorInfo = "请选择一个零件";
                        return;
                    }
 
                    if (!ModelUtil.Is_Special(Path.GetFileNameWithoutExtension(swDoc1.GetPathName())))
                    {
                        errorInfo = "请选择一个加工件";
                        return;
                    }
                }
                else if (docType == (int)swDocumentTypes_e.swDocPART)
                {
                    fullPath = Path.GetDirectoryName(swDoc.GetPathName()) + "\\xmlData\\";
                    swDoc1 = swDoc;
                }
                else if (docType == (int)swDocumentTypes_e.swDocDRAWING)
                {
                    string mdlNameTemp = Path.GetDirectoryName(swDoc.GetPathName()) + "\\" + Path.GetFileNameWithoutExtension(swDoc.GetPathName()) + ".SLDPRT";
                    if (File.Exists(mdlNameTemp))
                    {
                        ModelDoc2 docTemp = (ModelDoc2)SwApp.OpenDoc(mdlNameTemp, (int)swDocumentTypes_e.swDocPART);
                        if (docTemp == null)
                        {
                            errorInfo = "请在装配体、零件或零件工程图模型中操作";
                            return;
                        }
                        fullPath = Path.GetDirectoryName(swDoc.GetPathName()) + "\\xmlData\\";
                        swDoc1 = docTemp;
                    }
                    else
                    {
                        errorInfo = "当前目录下没有对应零件文档,请确认工程图图档与三维模型图档在相同目录下";
                        return;
                    }
                }
                else
                {
                    errorInfo = "请在装配体、零件或零件工程图模型模型中操作";
                    return;
                }
                GetModelProperty(swDoc1, modelProperty);
                GetModelInfo(swDoc1, modelProperty);
                #endregion
            }
            catch (Exception ex)
            {
                errorInfo = string.Format("获取模型信息时发生异常:{0}", ex.ToString());
            }
        }
 
 
 
        public static int GetModelProperty(ModelDoc2 swMdl, ModelProperty modelProperty)
        {
            try
            {
                ModelDocExtension swModelDocExt = swMdl.Extension;
                var ConfigNames = (string[])swMdl.GetConfigurationNames(); //所有配置名称
                Configuration swConfig = null;
                foreach (var configName in ConfigNames)//遍历所有配置
                {
                    swConfig = (Configuration)swMdl.GetConfigurationByName(configName);
                    swCuspro = swModelDocExt.get_CustomPropertyManager(configName);
                    if (swCuspro != null)
                        break;
                }
                string fileName = Path.GetFileNameWithoutExtension(swMdl.GetPathName());//swComponent.Name;
                string fullPath = Path.GetDirectoryName(swMdl.GetPathName()) + "\\" + fileName;
 
                string paramValue = string.Empty;
                string paramResolvedvalue = string.Empty;
 
                try
                {
                    modelProperty.name = Path.GetFileNameWithoutExtension(swMdl.GetPathName());
 
                    swCuspro.Get2(PropertyName.JGJ_TYPE, out paramValue, out paramResolvedvalue);
                    modelProperty.type = paramValue;
 
                    swCuspro.Get2(PropertyName.STUFF_TYPE, out paramValue, out paramResolvedvalue);
                    modelProperty.stuffType = paramValue;
 
                    swCuspro.Get2(PropertyName.SURFACE_TYPE, out paramValue, out paramResolvedvalue);
                    modelProperty.surfaceType = paramValue;
 
                    swCuspro.Get2(PropertyName.HEAT_TYPE, out paramValue, out paramResolvedvalue);
                    modelProperty.heatType = paramValue;
                }
                catch (Exception ex)
                {
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
 
            return 0;
        }
 
        public static int GetModelInfo(ModelDoc2 swMdl, ModelProperty modelInfo)
        {
            try
            {
                PartDoc swPart = (PartDoc)swMdl;
                //获取零件外形尺寸
                double[] dBox = (double[])swPart.GetPartBox(true);
                double dLen = (dBox[3] - dBox[0]) * 1000;//长
                dLen = Math.Round(dLen, 3);
                double dWid = (dBox[4] - dBox[1]) * 1000;//宽
                dWid = Math.Round(dWid, 3);
                double dHei = (dBox[5] - dBox[2]) * 1000;//高
                dHei = Math.Round(dHei, 3);
 
                double minValue = 0;
                if (dLen < dWid)
                {
                    minValue = dLen;
                    dLen = dWid;
                    dWid = minValue;
                }
                if (dLen < dHei)
                {
                    minValue = dLen;
                    dLen = dHei;
                    dHei = minValue;
                }
                if (dWid < dHei)
                {
                    minValue = dWid;
                    dWid = dHei;
                    dHei = minValue;
                }
 
 
                //获取模型质量属性
                ConfigurationManager swConfMgr = swMdl.ConfigurationManager;
                Configuration swConf = swConfMgr.ActiveConfiguration;
                string sConfName = swConf.Name;
                string sMaterialLibrary;
                string sMaterial = swPart.GetMaterialPropertyName2(sConfName, out sMaterialLibrary);//读取材质名称,
 
 
                MassProperty swMassProperty = swMdl.Extension.CreateMassProperty();
                double dMass = swMassProperty.Mass;//读取重量 单位Kg
                // dMass = Math.Round(dMass, 3);
                if (sMaterial == "") dMass = 0.0;
 
 
                double dSurfaceArea = swMassProperty.SurfaceArea;//读取表面积 单位㎡
                dSurfaceArea = Math.Round(dSurfaceArea * 1000000, 3); // 转成平方毫米
 
                double dVolume = swMassProperty.Volume; //立方米
                dVolume = Math.Round(dVolume * 1000000000, 3); // 立方毫米
 
                double dThick = 0.0;
                SheetMetalFolder swSheetMetalFolder = (SheetMetalFolder)swMdl.FeatureManager.GetSheetMetalFolder();
                if (swSheetMetalFolder != null)
                {
                    // 钣金
                    Feature swFeature = swSheetMetalFolder.GetFeature();
                    SheetMetalFeatureData swSheetMetalFeatureData = (SheetMetalFeatureData)swFeature.GetDefinition();
                    dThick = swSheetMetalFeatureData.Thickness;
                    dThick = Math.Round(dThick * 1000, 3);
                }
 
                modelInfo.length = dLen; //获取长参数值
                modelInfo.width = dWid; //获取宽参数值
                modelInfo.height = dHei; //获取高参数值
                // modelInfo. = 0; //获取厚度参数值
                modelInfo.area = dSurfaceArea; //获取面积参数值
                modelInfo.currentArea = dSurfaceArea;
 
                modelInfo.volume = dVolume; //获取体积参数值
                modelInfo.currentVolume = dVolume; //计算时体积
 
                modelInfo.weight = dMass; //获取质量参数值
                modelInfo.thickness = dThick; //获取钣金件厚度参数值
            }
            catch (Exception ex)
            {
                return 0;
            }
            return 0;
        }
 
 
 
        public static void WriteXmlData(XmlDocument tDoc, XmlElement paramRoot, string element, DataTable dtTemp)
        {
            #region 写入新的XML数据
            try
            {
                XmlElement toCompontAttr = tDoc.CreateElement(element);
                paramRoot.AppendChild(toCompontAttr);
                // DataTable dtTemp = new DataTable();
                //dtTemp = this.dgvMdlParam_JJ.DataSource as System.Data.DataTable;
                //写入XML数据
                foreach (DataRow category in dtTemp.Rows)
                {
                    //  string temp = category["DOCNAME"].ToString();
                    XmlElement Itemtick = tDoc.CreateElement("ITEM");
                    #region 给 Tick 增加属性值
 
                    for (int m = 0; m < category.ItemArray.Length; m++)
                    {
                        string columnName = category.Table.Columns[m].ColumnName;
                        string tempCompare = category[m].ToString();
 
                        XmlAttribute attr = tDoc.CreateAttribute(columnName);
                        if (category[m].ToString() == "")
                            attr.Value = "0";
                        else
                            attr.Value = category[m].ToString();
                        Itemtick.Attributes.Append(attr);
                    }
 
                    toCompontAttr.AppendChild(Itemtick);
 
                    #endregion
 
                }
            }
            catch (Exception ex)
            {
                //outputMsg = "WriteXmlData Error.....";
                //writelog2(outputMsg);
            }
 
            #endregion
 
        }
 
        public static void BindXMLData_Detail(XmlDocument doc, DataTable dtTempRead, string element)
        {
            try
            {
                XmlElement rootElem = doc.DocumentElement;
                XmlNodeList personNodes = rootElem.GetElementsByTagName(element); //获取 TypeCCheckReport 等各个检查项子节点集合 
                DataRow dr = null;
                foreach (XmlNode node in personNodes)
                {
                    dtTempRead.Clear();
                    dtTempRead.Rows.Clear();
                    XmlNodeList childNodes = ((XmlElement)node).GetElementsByTagName("ITEM");
                    if (childNodes.Count < 1)
                    {
                    }
                    else
                    {
                        foreach (XmlNode childnode in childNodes)
                        {
                            //dtParamMachine;
                            dr = dtTempRead.NewRow();
                            foreach (DataColumn category in dtTempRead.Columns)
                            {
                                string columnName = category.ColumnName;
                                string strName = ((XmlElement)childnode).GetAttribute(columnName); //获取模型名称
                                dr[columnName] = strName;
                            }
                            dtTempRead.Rows.Add(dr);
                        }
                        //this.dgvJJMaching.DataSource = dtParamMachine;
                    }
 
                }
            }
            catch (Exception ex)
            {
 
            }
 
        }
    }
}