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)
|
{
|
|
}
|
|
}
|
}
|
}
|