|
7#

楼主 |
发表于 2022-7-14 11:04:23
|
只看该作者
经典案例图书 Sub main()
On Error Resume Next
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
dbs = swApp.GetMaterialDatabases
If swModel.GetType() = 1 Then '如果当前文件是零件
ReturnColor swModel
ElseIf swModel.GetType() = 2 Then '如果当前文件是装配体
Components = swModel.GetComponents(False)
For Each SingleComponent In Components
Set swModel = SingleComponent.GetModelDoc()
If swModel.GetType() = 1 Then
ReturnColor swModel
'MsgBox "子零件体"
Else
'MsgBox "子装配体"
End If
Next
Else '如果当前文件是工程图
MsgBox "请打开一个零件或者装配体。"
End If
End Sub
'将颜色应用原有材质的颜色
Public Function ReturnColor(swDoc As PartDoc)
Dim sMatDB As String, Material As String, sMaterialDB_path As String, Configuration_Name As String
Set swFeat = swDoc.FirstFeature
While Not swFeat Is Nothing
swFeat.RemoveMaterialProperty
Set swSubFeat = swFeat.GetFirstSubFeature
While Not swSubFeat Is Nothing
swSubFeat.RemoveMaterialProperty
Set swSubFeat = swSubFeat.GetNextSubFeature
Wend
Set swFeat = swFeat.GetNextFeature
Wend
Configuration_Name = swDoc.GetActiveConfiguration.Name
Material = swDoc.GetMaterialPropertyName2(Configuration_Name, sMatDB)
For i = 0 To UBound(dbs)
If StrComp(Left(Right(dbs(i), Len(sMatDB) + 7), Len(sMatDB)), sMatDB) = 0 Then
sMaterialDB_path = dbs(i)
End If
Next i
'赋予零件材质默认的颜色
If Material <> "" Then
swDoc.SetMaterialPropertyName2 Configuration_Name, sMaterialDB_path, ""
swDoc.SetMaterialPropertyName2 Configuration_Name, sMaterialDB_path, Material
Else
'下面这句是将没有赋予材质的零件的颜色删除,如果不想让给没有材质的零件删除颜色,可以注释掉这一句
swDoc.SetMaterialPropertyName2 Configuration_Name, sMaterialDB_path, ""
End If
swDoc.EditRebuild
End Function
[/code] |
|