作者: littleboy 2025-03-04 16:19:07

Sw_编辑零部件属性

当我们以还原的方式打开装配体时,其零部件也是有加载到程序内存里来的。按理是可以不用打开零部件再编辑零件的属性的。

通过object[] vComponents = swAssy.GetComponents(false);获得零部件集合,

再遍历集合里的零部件,用 swCustPropMgr = swCompModel.Extension.CustomPropertyManager[“”];去对属性进行编辑。

代码

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
public static void Prop()
{
// 获取当前活动的文档
AssemblyDoc swAssy = (AssemblyDoc)swApp.ActiveDoc;
CustomPropertyManager swCustPropMgr;
// 写入属性之前,将装配体设定轻化到还原
swAssy.ResolveAllLightWeightComponents(true);
// 获取零部件,true仅获取顶层组件,false是全部
object[] vComponents = swAssy.GetComponents(false);
foreach (object SingleComponent in vComponents)
{
// 获取零部件的自定义属性
Component2 swComponent = (Component2)SingleComponent;
ModelDoc2 swCompModel = swComponent.GetModelDoc2();
if (swCompModel == null)
{
continue;
}
if (swCompModel.GetType() == (int)swDocumentTypes_e.swDocPART)
{
swCustPropMgr = swCompModel.Extension.CustomPropertyManager[""];
swCustPropMgr.Add3("Length", (int)swCustomInfoType_e.swCustomInfoYesOrNo, "X", (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);
}
else if (swCompModel.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)
{
swCustPropMgr = swCompModel.Extension.CustomPropertyManager[""];
swCustPropMgr.Add3("Length", (int)swCustomInfoType_e.swCustomInfoText, "X", (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);
}
}
}