Files
Set or remove material from a part or selected body
Code
Module: mMain
'Demonstrates how to set or remove a material from a part _
or selected body.
'Preconditions for...
' SetPartMaterial: part is open.
' SetSelBodyMaterial: selected body is open.
' RemovePartMaterial: part is open.
' RemoveSelBodyMaterial: part is open and body is selected.
'Results for...
' SetPartMaterial: material is added to part.
' SetSelBodyMaterial: material is added to selected body.
' RemovePartMaterial: material is removed from part.
' RemoveSelBodyMaterial: material is removed from selected body.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub SetPartMaterial()
Dim strMatName As String
Dim strConfigName As String
Dim strMatDb As String
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
strMatName = "Plain Carbon Steel"
strConfigName = Empty 'Empty = active config
strMatDb = Empty
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel
swPart.SetMaterialPropertyName2 strConfigName, strMatDb, strMatName
Debug.Print swPart.GetMaterialPropertyName2(strConfigName, strMatDb)
Debug.Print strMatDb
End Sub
Sub SetSelBodyMaterial()
Dim strMatName As String
Dim strConfigName As String
Dim strMatDb As String
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody As SldWorks.Body2
strMatName = "Plain Carbon Steel"
strConfigName = "1" 'Exact name must be specified
strMatDb = Empty
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swBody = swSelMgr.GetSelectedObject6(1, -1)
swBody.SetMaterialProperty strConfigName, Empty, strMatName
Debug.Print swBody.GetMaterialPropertyName(strConfigName, strMatDb)
Debug.Print strMatDb
End Sub
Sub RemovePartMaterial()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim strConfigName As String
strConfigName = Empty 'Empty = active config
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel
swPart.SetMaterialPropertyName2 strConfigName, Empty, Empty
Debug.Print swPart.GetMaterialPropertyName2(strConfigName, Empty)
End Sub
Sub RemoveSelBodyMaterial()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody As SldWorks.Body2
Dim strConfigName As String
strConfigName = "1" 'Exact name must be specified
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swBody = swSelMgr.GetSelectedObject6(1, -1)
swBody.SetMaterialProperty strConfigName, Empty, Empty
Debug.Print swBody.GetMaterialPropertyName(strConfigName, Empty)
End Sub