SolidWorks*Excel+VBA-材质?不,这是灵魂
- 2026-11-07 17:54:13
SolidWorks*Excel+VBA-材质?不,这是灵魂灵魂提取: 装配默认 BYDESIGN(按设计) 灵魂注入: 装配没有材质,所以写到自定义属性中,这样一来后续工程图属性关联理论上可以做到模板统一。
源码奉上,写的比较糙 灵魂提取器: 接下来是注入的: 
已关注
关注
重播 分享 赞
已关注
关注
重播 分享 赞
Sub GetMats() '提取材质Dim swApp As SldWorks.SldWorks, FullModelPath As StringDim OpennedModel As PartDoc, k As Integer, posn As Long, SwErr As LongOn Error Resume NextSet swApp = GetObject(, "SldWorks.Application")If swApp Is Nothing ThenMsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人"Exit SubEnd IfWith ThisWorkbook.ActiveSheetIf myselcondi ThenswApp.CloseAllDocuments (True)For k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在提取材质:", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)If UCase(.Cells(posn, 4)) = ".SLDPRT" ThenFullModelPath = .Cells(posn, 2) & "\" & .Cells(posn, 3) & .Cells(posn, 4)Set OpennedModel = swApp.OpenDoc2(FullModelPath, swDocPART, True, False, True, SwErr)If OpennedModel Is Nothing Then.Cells(posn, 1) = "提取失败!"Else.Cells(posn, 11) = OpennedModel.GetMaterialPropertyName2("", "")Set OpennedModel = NothingswApp.QuitDoc FullModelPathIf .Cells(posn, 11) = "" Then.Cells(posn, 1) = "无材质!"Else.Cells(posn, 1) = "材质√"End IfEnd IfElse.Cells(posn, 1) = "装配,按设计".Cells(posn, 11) = "BYDESIGN"End IfNextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithErr.ClearApplication.StatusBar = FalseEnd Sub
Sub SetMats() '设置材质Dim swApp As SldWorks.SldWorks, FullModelPath As StringDim OpennedModel As ModelDoc2, PartModel As PartDoc, k As Integer, posn As Long, SwErr As LongDim MatsDb As String'On Error Resume NextSet swApp = GetObject(, "SldWorks.Application")If swApp Is Nothing ThenMsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人"Exit SubEnd IfMatsDb = ThisWorkbook.Sheets("SW0 参数设置").Range("C2").ValueWith ThisWorkbook.ActiveSheetIf myselcondi ThenswApp.CloseAllDocuments (True)For k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在设置材质:", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)FullModelPath = .Cells(posn, 2) & "\" & .Cells(posn, 3) & .Cells(posn, 4)If UCase(.Cells(posn, 4)) = ".SLDPRT" ThenSet OpennedModel = swApp.OpenDoc2(FullModelPath, swDocPART, False, False, True, SwErr)If OpennedModel Is Nothing Then.Cells(posn, 1) = "设置失败!"ElseSet PartModel = OpennedModelPartModel.SetMaterialPropertyName2 "", MatsDb, .Cells(posn, 11)If PartModel.GetMaterialPropertyName2("", "") <> .Cells(posn, 11) Then.Cells(posn, 1) = "设置失败!"ElsePartModel.GraphicsRedraw2.Cells(posn, 1) = "已设置材质"End IfSet PartModel = NothingEnd IfElseSet OpennedModel = swApp.OpenDoc2(FullModelPath, swDocASSEMBLY, False, False, True, SwErr)If OpennedModel Is Nothing Then.Cells(posn, 1) = "设置失败!"ElseOpennedModel.Extension.CustomPropertyManager("").Add3 "材料", swCustomInfoText, .Cells(posn, 11), swCustomPropertyReplaceValue.Cells(posn, 1) = "装配,定制属性"End IfEnd IfIf Not OpennedModel Is Nothing ThenOpennedModel.Save2 True: swApp.QuitDoc FullModelPathSet OpennedModel = NothingEnd IfNextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithErr.ClearApplication.StatusBar = FalseEnd Sub

本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。