SolidWorks*Excel+VBA-尺寸?材质?属性?单拉一个算咋回事,齐活!
- 2026-11-12 20:27:27
SolidWorks*Excel+VBA-尺寸?材质?属性?单拉一个算咋回事,齐活!齐活了,提取尺寸必提取材质,也可单拉材质,或提取所有属性,图号分离,自定义属性等。 代码通用化,涵盖尺寸,材质,图号,图名,篇幅较长,容易脑壳疼。情况太多,单就装配和零件就得分开写。 初步测试暂无问题,可能有未考虑到的bug,谨慎使用,欢迎评论区交流。 附xml中搭配写法 回调函数,根据xml对应不同按钮,传递不同参数来调用对应方法。 
已关注
关注
重播 分享 赞
Sub GetSwModelInf(Stype As String) '获取模型信息,尺寸,材质,自定义属性Dim swApp As SldWorks.SldWorks, FullModelPath As StringDim OpennedModel As ModelDoc2, PartModel As PartDoc, SwErr As Long, PropVal As StringDim k As Integer, posn As Long, Pbox As Variant, dx As Double, dy As Double, dz As DoubleDim Sizes As String, ModelMat As String, ModelCode As String, ModelName As StringDim Results As String, SizeResult As String, MatResult As String, PropResult As String, StatusMsg As String, AsmMsg As StringDim StypeArr(), MsgArr(), Suffix(), ModelType(), OpenVal As LongOn Error Resume NextSet swApp = GetObject(, "SldWorks.Application")If swApp Is Nothing ThenMsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人"Exit SubEnd IfStypeArr = Array("SizeAndMat", "Mat", "prop"): MsgArr = Array("尺寸/材质", "材质", "属性")Suffix = Array(".SLDPRT", ".SLDASM"): ModelType = Array(swDocPART, swDocASSEMBLY)StatusMsg = MsgArr(WorksheetFunction.Match(Stype, StypeArr, 0) - 1)With ThisWorkbook.ActiveSheetIf myselcondi ThenswApp.CloseAllDocuments (True)For k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在提取" & StatusMsg & ":", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)FullModelPath = .Cells(posn, 2) & "\" & .Cells(posn, 3) & .Cells(posn, 4)OpenVal = ModelType(WorksheetFunction.Match(UCase(.Cells(posn, 4)), Suffix, 0) - 1)Set OpennedModel = swApp.OpenDoc2(FullModelPath, OpenVal, False, False, True, SwErr)If OpennedModel Is Nothing ThenResults = "模型打开失败!"ElseSelect Case StypeCase "SizeAndMat"If OpenVal = swDocPART ThenPbox = OpennedModel.GetPartBox(False)dx = Round(Pbox(3) - Pbox(0), 2): dy = Round(Pbox(4) - Pbox(1), 2): dz = Round(Pbox(5) - Pbox(2), 2)Sizes = dx & "*" & dy & "*" & dz: Erase PboxElseOpennedModel.Extension.CustomPropertyManager("").Get2 "外型尺寸", PropVal, SizesAsmMsg = "装配:"End IfIf (dx = 0 And dy = 0 And dz = 0) Or Sizes = "" ThenSizeResult = "无尺寸;"Sizes = ""ElseIf Sizes = .Cells(posn, 7) ThenSizeResult = "尺寸未改;"ElseSizeResult = "新尺寸;"End If.Cells(posn, 7) = Sizes: Sizes = ""GoTo GetNextCase "Mat"GetNext: If OpenVal = swDocPART ThenSet PartModel = OpennedModelModelMat = PartModel.GetMaterialPropertyName2("", "")Set PartModel = NothingElseOpennedModel.Extension.CustomPropertyManager("").Get2 "材料", PropVal, ModelMatAsmMsg = "装配:"End IfIf ModelMat = "" ThenMatResult = "无材质;"ElseIf ModelMat = .Cells(posn, 11) ThenMatResult = "材质未改;"ElseMatResult = "新材质;"End If.Cells(posn, 11) = ModelMat: ModelMat = ""Case "prop"OpennedModel.Extension.CustomPropertyManager("").Get2 "外型尺寸", PropVal, SizesOpennedModel.Extension.CustomPropertyManager("").Get2 "图号", PropVal, ModelCodeOpennedModel.Extension.CustomPropertyManager("").Get2 "图名", PropVal, ModelNameIf .Cells(posn, 7) <> Sizes Then PropResult = "尺寸;"If .Cells(posn, 12) <> ModelCode Then PropResult = PropResult & "图号;"If .Cells(posn, 13) <> ModelName Then PropResult = PropResult & "图名;"'...其余属性依次拓展If PropResult = "" Then PropResult = "属性未改;".Cells(posn, 7) = Sizes: Sizes = "".Cells(posn, 12) = ModelCode: ModelCode = "".Cells(posn, 13) = ModelName: ModelName = ""GoTo GetNextEnd SelectSet OpennedModel = Nothing: swApp.QuitDoc FullModelPathResults = AsmMsg & SizeResult & MatResult & PropResultAsmMsg = "": SizeResult = "": MatResult = "": PropResult = ""End If.Cells(posn, 1) = Results: Results = ""NextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithErr.ClearSet swApp = NothingApplication.StatusBar = FalseEnd SubSub SetSwModelInf(Stype As String) '设置模型信息,尺寸,材质,自定义属性Dim swApp As SldWorks.SldWorks, FullModelPath As StringDim MatsDb As StringDim OpennedModel As ModelDoc2, PartModel As PartDoc, SwErr As Long, PropVal As StringDim k As Integer, posn As LongDim Sizes As String, ModelMat As String, ModelCode As String, ModelName As StringDim Results As String, MatResult As String, PropResult As String, StatusMsg As String, AsmMsg As StringDim StypeArr(), MsgArr(), Suffix(), ModelType(), OpenVal As LongOn Error Resume NextSet swApp = GetObject(, "SldWorks.Application")If swApp Is Nothing ThenMsgBox "请先打开SolidWorks!", vbExclamation, "不正经的机械仙人"Exit SubEnd IfStypeArr = Array("Mat", "prop"): MsgArr = Array("材质", "属性")Suffix = Array(".SLDPRT", ".SLDASM"): ModelType = Array(swDocPART, swDocASSEMBLY)StatusMsg = MsgArr(WorksheetFunction.Match(Stype, StypeArr, 0) - 1)MatsDb = ThisWorkbook.Sheets("SW0 参数设置").Range("C2").ValueWith ThisWorkbook.ActiveSheetIf myselcondi ThenswApp.CloseAllDocuments (True)For k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在设置" & StatusMsg & ":", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)FullModelPath = .Cells(posn, 2) & "\" & .Cells(posn, 3) & .Cells(posn, 4)OpenVal = ModelType(WorksheetFunction.Match(UCase(.Cells(posn, 4)), Suffix, 0) - 1)Set OpennedModel = swApp.OpenDoc2(FullModelPath, OpenVal, False, False, True, SwErr)If OpennedModel Is Nothing ThenResults = "模型打开失败!"ElseSelect Case StypeCase "Mat"SetNext: If OpenVal = swDocPART ThenSet PartModel = OpennedModelModelMat = PartModel.GetMaterialPropertyName2("", "")ElseOpennedModel.Extension.CustomPropertyManager("").Get2 "材料", PropVal, ModelMatAsmMsg = "装配:"End IfIf ModelMat = .Cells(posn, 11) ThenMatResult = "材质未改;"ElseIf OpenVal = swDocPART ThenIf PartModel.GetMaterialPropertyName2("", "") <> .Cells(posn, 11) ThenMatResult = "设置材质失败!"ElseMatResult = "设置材质;"End IfSet PartModel = NothingElseOpennedModel.Extension.CustomPropertyManager("").Add3 "材料", swCustomInfoText, .Cells(posn, 11), swCustomPropertyReplaceValueMatResult = "设置材质;"End IfEnd IfModelMat = ""Case "prop"OpennedModel.Extension.CustomPropertyManager("").Get2 "外型尺寸", PropVal, SizesOpennedModel.Extension.CustomPropertyManager("").Get2 "图号", PropVal, ModelCodeOpennedModel.Extension.CustomPropertyManager("").Get2 "图名", PropVal, ModelNameIf Sizes <> .Cells(posn, 7) ThenPropResult = "尺寸;"OpennedModel.Extension.CustomPropertyManager("").Add3 "外型尺寸", swCustomInfoText, .Cells(posn, 7), swCustomPropertyReplaceValueEnd IfSizes = ""If ModelCode <> .Cells(posn, 12) ThenPropResult = PropResult & "图号;"OpennedModel.Extension.CustomPropertyManager("").Add3 "图号", swCustomInfoText, .Cells(posn, 12), swCustomPropertyReplaceValueEnd IfModelCode = ""If ModelName <> .Cells(posn, 13) ThenPropResult = PropResult & "图名;"OpennedModel.Extension.CustomPropertyManager("").Add3 "图名", swCustomInfoText, .Cells(posn, 13), swCustomPropertyReplaceValueEnd IfModelName = ""'...其余属性依次拓展If PropResult = "" ThenPropResult = "属性未改;"ElsePropResult = "设置属性;"End IfGoTo SetNextEnd SelectOpennedModel.Save2 True: swApp.QuitDoc FullModelPath: Set OpennedModel = NothingResults = AsmMsg & MatResult & PropResultAsmMsg = "": MatResult = "": PropResult = ""End If.Cells(posn, 1) = Results: Results = ""NextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithErr.ClearSet swApp = NothingApplication.StatusBar = FalseEnd SubSub CalSwModelInf() '图号分离,可拓展计算属性Dim k As Integer, posn As Long, CodeName As VariantDim Result As StringWith ThisWorkbook.ActiveSheetIf myselcondi ThenFor k = 1 To SelnumberProcessBarUpdater k, Selnumber, "正在计算:", "共" & Selnumber & "个,第" & k & "个 ..."posn = Selarray(k)CodeName = Split(.Cells(posn, 3), "_")Select Case UBound(CodeName)Case -1Result = ""Case 0.Cells(posn, 13) = CodeName(0)Result = "仅图名;"Case 1.Cells(posn, 12) = CodeName(0).Cells(posn, 13) = CodeName(1)Result = "图号分离;"End Select.Cells(posn, 1) = Result: Result = ""NextElseMsgBox "未选择正确行,程序结束!", vbExclamation, "不正经的机械仙人"End IfEnd WithApplication.StatusBar = FalseEnd Sub
<buttonid="SWnull_GetSwModelInf_SizeAndMat"label="提取尺寸"size="large"image="FilesSizes"onAction="myftn"/><splitButtonid="Split01"size="large"><buttonid="SWnull_GetSwModelInf_Mat"label="提取材质"image="getMaterial"onAction="myftn"/><menuid="Menu01"><buttonid="SWnull_SetSwModelInf_Mat"label="设置材质"image="setMaterial"onAction="myftn"/></menu></splitButton><splitButtonid="Split02"size="large"><buttonid="SWnull_CalSwModelInf"label="计算属性"image="props"onAction="myftn"/><menuid="Menu02"><buttonid="SWnull_SetSwModelInf_prop"label="设置属性"image="setprops"onAction="myftn"/><buttonid="SWnull_GetSwModelInf_prop"label="提取属性"image="getprops"onAction="myftn"/></menu></splitButton>
Sub myftn(Control As IRibbonControl) '回调Dim PrgID As String, PrgArr As VariantPrgID = Control.ID: PrgArr = Split(PrgID, "_")On Error GoTo nendIf UBound(PrgArr) > 1 ThenApplication.Run PrgArr(1), PrgArr(2)ElseApplication.Run PrgArr(1)End IfExit Subnend: MsgBox "遇到错误,已中止!", vbExclamation, "不正经的机械仙人"Err.ClearEnd Sub

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