using Laservall.Solidworks.Model; using SolidWorks.Interop.sldworks; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Shapes; using Xarial.XCad.Documents; using Xarial.XCad.SolidWorks.Documents; namespace Laservall.Solidworks.Extension { public static class SWDocReader { public static void ReadDocProperties(IModelDoc2 doc,PartPropModel partPropModel) { if (doc != null) { partPropModel.GetType().GetProperties().ToList().ForEach(p => { // Get DisplayName attribute p.CustomAttributes.ToList().ForEach(attr => { if (attr.AttributeType == typeof(System.ComponentModel.DisplayNameAttribute)) { var displayName = attr.ConstructorArguments[0].Value.ToString(); // Get custom property value by display name var val2 = GetDocProperty(doc, displayName); if (val2 != null) { // 判断属性类型 switch (p.PropertyType.Name) { case "Double": if (double.TryParse(val2, out double d)) { p.SetValue(partPropModel, d); } break; case "Int32": if (int.TryParse(val2, out int i)) { p.SetValue(partPropModel, i); } break; case "Boolean": if (bool.TryParse(val2, out bool b)) { p.SetValue(partPropModel, b); } break; default: p.SetValue(partPropModel, val2); break; } } } }); //var val = partComponent.Component.CustomPropertyManager[p.Name]; //if (val != null) //{ // p.SetValue(partPropModel, val); //} }); } } /// /// 获取当前激活配置的自定义属性 /// /// /// /// public static string GetDocProperty(IModelDoc2 model , string propertyName) { if (model == null) { return "未打开任何文档"; } else { ModelDocExtension swModelDocExt = default(ModelDocExtension); CustomPropertyManager swCustProp = default(CustomPropertyManager); string val = ""; string valout = ""; int status; //swModel = (ModelDoc2)swApp.ActiveDoc; swModelDocExt = model.Extension; // Get the custom property data swCustProp = swModelDocExt.get_CustomPropertyManager(""); //status = swCustProp.Get4(propertyName, false, out val, out valout); status = swCustProp.Get6(propertyName, false, out val,out valout,out bool wasResolved,out bool _); Debug.Print("Value: " + val); Debug.Print("Evaluated value: " + valout); //Debug.Print("Up-to-date data: " + status); if (wasResolved) { return valout; } else { return val; } } } } }