using SolidWorks.Interop.sldworks; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xarial.XCad.Data; namespace Laservall.Solidworks.Extension { public static class SWDocWriter { // 根据文档与属性模型,写入自定义属性 public static void WriteDocProperties(IModelDoc2 doc, Model.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(); var val = p.GetValue(partPropModel); if (val != null) { // 设置自定义属性值 SetDocProperty(doc, displayName, val.ToString()); } } }); }); } } /// /// /// /// /// /// public static void SetDocProperty(IModelDoc2 model, string propertyName,string val) { if (model == null) { } else { //swModel = (ModelDoc2)swApp.ActiveDoc; ModelDocExtension swModelDocExt = model.Extension; // Get the custom property data CustomPropertyManager swCustProp = swModelDocExt.get_CustomPropertyManager(""); swCustProp.Set2(propertyName, val); } } } }