在solidworks没有打开任何文件的情况下,运行此宏。
此宏会打开指定的文件,修改其材质,保存并关闭文件。
Option Explicit
Sub main()
Dim filename As String
filename = "e0501-P0504"
Dim matname As String
matname = "PPH"
Call ChangeMaterialOfPart(filename, matname)
End Sub
Sub ChangeMaterialOfPart(filename As String, matname As String)
Const currentDir As String = "D:\崔胜利\凯帝隆\湖北武穴锂宝\solidworks\"
Const matlib As String = "C:/ProgramData/SolidWorks/SOLIDWORKS 2022/自定义材料/凯帝隆.sldmat"
Dim path As String
path = currentDir & filename & ".SLDPRT"
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.PartDoc
Set swApp = Application.SldWorks
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
' Open
Set swPart = swApp.OpenDoc6(path, 1, 0, "", longstatus, longwarnings)
'todo:遍历配置
swPart.SetMaterialPropertyName2 "默认<按加工>", matlib, matname
swPart.SetMaterialPropertyName2 "默认<按焊接>", matlib, matname
' Save
Dim swErrors As Long
Dim swWarnings As Long
boolstatus = swPart.Save3(1, swErrors, swWarnings)
' Close Document
swApp.CloseDoc swPart.GetPathName
Set swPart = Nothing
Set swApp = Nothing
End Sub
标签:Dim,String,swApp,solidworks,swPart,matname,零件,材质,filename
From: https://www.cnblogs.com/cuishengli/p/17430274.html