SolidWorks 宏代码:钣金零件属性自动化处理
引用于 cad 在 2025年4月22日, 下午6:02SolidWorks 宏代码:钣金零件属性自动化处理
一、标题释义
本宏代码围绕 SolidWorks 软件中钣金零件属性操作展开,核心功能是将切割清单的属性自动更新至自定义属性,“自动化处理” 突出其通过代码实现批量、自动操作的特点,所以将标题定为 **《SolidWorks 钣金零件切割清单属性自动化提取与自定义属性更新宏》** ,精准概括其核心功能与应用场景。
二、功能与作用总述
该宏代码专为 SolidWorks 软件设计,聚焦于钣金零件文件处理。其核心作用是在钣金零件中插入宏特征,并将切割清单内的边界框区域、长度、宽度等关键属性,以及钣金厚度、切割长度、切除数量、折弯数量等相关属性,自动更新或复制到零件的自定义属性中。通过自动化操作,减少手动输入和处理属性数据的工作量,提升设计流程中属性管理的效率和准确性,避免人为操作可能出现的错误,尤其适用于需要频繁处理钣金零件属性数据的场景。
三、代码结构与执行流程分析
(一)声明部分Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim dimTypes As Variant
Dim dimValue As Variant
Dim icons(2) As String
Dim vIcons As Variant
Dim myPath As String
Dim myFolder As String
Dim myFeat As Feature
Dim myMethods(8) As String
Dim vMethods As Variant
通过Option Explicit强制要求所有变量必须显式声明,增强代码的可读性和稳定性。随后声明了一系列变量,包括 SolidWorks 应用程序对象swApp、零件文档对象Part ,用于存储属性类型和值的dimTypes和dimValue,以及与宏特征相关的图标、路径、方法等变量,为后续操作提供数据载体和对象引用。
(二)main子过程Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
'If (Part Is Nothing) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
'If (Part.GetType <> 1) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
'If (Part.GetBendState = 0) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
myPath = swApp.GetCurrentMacroPathName
myFolder = swApp.GetCurrentMacroPathFolder & "\"
myMethods(0) = myPath
myMethods(1) = "BoundingBoxArea"
myMethods(2) = "swmRebuild"
myMethods(3) = myPath
myMethods(4) = "BoundingBoxArea"
myMethods(5) = "swmEdit"
myMethods(6) = ""
myMethods(7) = ""
myMethods(8) = ""
vMethods = myMethods
'icons(0) = myFolder + "BoundingBoxAreaBlank.bmp"
'icons(1) = myFolder + "BoundingBoxAreaBlank.bmp"
'icons(2) = myFolder + "BoundingBoxAreaBlank.bmp"
'vIcons = icons
Set myFeat = Part.FeatureManager.InsertMacroFeature3("提取切割清单属性值到属性VBA-不要编辑", "", vMethods, Empty, Empty, Empty, dimTypes, dimValue, Empty, Empty, swMacroFeatureByDefault + swMacroFeatureAlwaysAtEnd + swMacroFeatureEmbedMacroFile)
End Sub
首先获取 SolidWorks 应用程序对象和当前活动文档对象。原代码中注释掉了三段用于判断当前文档是否为钣金零件的代码,若恢复这些判断逻辑,可确保宏仅在钣金零件文件中执行,增强代码适用性和准确性,避免在非目标文件中运行导致错误。
获取当前宏的路径和文件夹路径,并设置myMethods数组,该数组包含宏文件路径、宏特征名称以及关联的操作方法(如swmRebuild重建方法和swmEdit编辑方法),为插入宏特征做准备。
注释掉了设置图标的相关代码,推测原意图是为宏特征设置显示图标,若有相关需求可恢复代码并配置合适图标。
最后通过Part.FeatureManager.InsertMacroFeature3方法在当前零件中插入一个名为 “提取切割清单属性值到属性 VBA - 不要编辑” 的宏特征,并关联之前设置的方法和相关参数,完成宏特征的创建与初始化。
(三)swmRebuild函数Public Function swmRebuild(swAppIn As Variant, partIn As Variant, featureIn As Variant) As Variant
Dim swPart As ModelDoc2
Dim swFeat As SldWorks.Feature
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim strValue(13) As String
Dim swBodyFolder As SldWorks.BodyFolder
Dim Currentlanguage As String
Set swApp = swAppIn
Currentlanguage = swApp.GetCurrentLanguage
Set swPart = swApp.ActiveDoc
Set swFeat = swPart.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName = "SolidBodyFolder" Or swFeat.GetTypeName = "CutListFolder" Or swFeat.GetTypeName = "SubWeldFolder" Then
'Set swBodyFolder = swFeat.GetSpecificFeature2
'swBodyFolder.UpdateCutList
Set swCustPropMgr = swFeat.CustomPropertyManager
If Currentlanguage = "chinese-simplified" Then
swFeat.CustomPropertyManager.Get4 "边界框长度", False, strValue(0), strValue(1) 'Bounding Box Length
swFeat.CustomPropertyManager.Get4 "边界框宽度", False, strValue(2), strValue(3) 'Bounding Box Width
swFeat.CustomPropertyManager.Get4 "钣金厚度", False, strValue(4), strValue(5)
swFeat.CustomPropertyManager.Get4 "切割长度-外部", False, strValue(6), strValue(7) 'Cutting Length-Outer
swFeat.CustomPropertyManager.Get4 "切割长度-内部", False, strValue(8), strValue(9) 'Cutting Length-Inner
swFeat.CustomPropertyManager.Get4 "切除", False, strValue(10), strValue(11) 'Cut Outs
swFeat.CustomPropertyManager.Get4 "折弯", False, strValue(12), strValue(13) 'Bends
ElseIf Currentlanguage = "english" Then
swFeat.CustomPropertyManager.Get4 "Bounding Box Length", False, strValue(0), strValue(1)
swFeat.CustomPropertyManager.Get4 "Bounding Box Width", False, strValue(2), strValue(3)
swFeat.CustomPropertyManager.Get4 "Sheet Metal Thickness", False, strValue(4), strValue(5)
swFeat.CustomPropertyManager.Get4 "Cutting Length-Outer", False, strValue(6), strValue(7)
swFeat.CustomPropertyManager.Get4 "Cutting Length-Inner", False, strValue(8), strValue(9)
swFeat.CustomPropertyManager.Get4 "Cut Outs", False, strValue(10), strValue(11)
swFeat.CustomPropertyManager.Get4 "Bends", False, strValue(12), strValue(13)
End If
End If
'If strValue(1) <> "" Then
' Exit Do
' End If
Set swFeat = swFeat.GetNextFeature
Loop
'Dim MyMassProp As SldWorks.MassProperty
'Set MyMassProp = swPart.Extension.CreateMassProperty
'MyMassProp.UseSystemUnits = True
'Debug.Print MyMassProp.Mass
Set swCustPropMgr = swPart.Extension.CustomPropertyManager("")
swPart.DeleteCustomInfo2 "", "长" '删除属性栏上摘要信息的数据
swPart.DeleteCustomInfo2 "", "宽"
swPart.DeleteCustomInfo2 "", "厚"
swPart.DeleteCustomInfo2 "", "外切长度"
swPart.DeleteCustomInfo2 "", "内切长度"
swPart.DeleteCustomInfo2 "", "切割总长(m)"
swPart.DeleteCustomInfo2 "", "穿孔数"
swPart.DeleteCustomInfo2 "", "折弯刀数"
swPart.DeleteCustomInfo2 "", "喷塑面积(m2)"
swCustPropMgr.Add3 "长", 30, strValue(1), 1 'Value in model units
swCustPropMgr.Add3 "宽", 30, strValue(3), 1 'Value in model units
swCustPropMgr.Add3 "厚", 30, strValue(5), 1 'Value in model units
swCustPropMgr.Add3 "外切长度", 30, strValue(7), 1 'Value in model units
swCustPropMgr.Add3 "内切长度", 30, strValue(9), 1
swCustPropMgr.Add3 "切割总长(m)", 30, Round((Val(strValue(7)) + Val(strValue(9))) / 1000, 2), 1 'Change decimal places by replace 2, value is in m
swCustPropMgr.Add3 "穿孔数", 30, strValue(11), 1
swCustPropMgr.Add3 "折弯刀数", 30, strValue(13), 1
swCustPropMgr.Add3 "喷塑面积(m2)", 30, Round(Val(strValue(1)) * Val(strValue(3)) / 1000000 * 2, 2), 1
swmRebuild = True
End Function
该函数负责从切割清单相关特征中提取属性值,并更新到零件的自定义属性中。首先获取当前活动文档、相关特征对象和当前语言环境。
通过循环遍历零件的所有特征,当找到类型为 “SolidBodyFolder”“CutListFolder” 或 “SubWeldFolder” 的特征时,获取其自定义属性管理器。根据当前语言环境(中文简体或英文),从属性管理器中提取边界框长度、宽度、钣金厚度等多种属性值,并存储在strValue数组中。
删除零件原有的一些自定义属性(如 “长”“宽” 等),然后将提取到的属性值进行处理(如计算切割总长、喷塑面积)后,添加到新的自定义属性中,完成属性的更新与添加操作,最后返回True表示函数执行成功。
(四)swmEdit函数Public Function swmEdit(swAppIn As Variant, partIn As Variant, featureIn As Variant) As Variant
swApp.SendMsgToUser ("提取切割清单和名称代号宏特征无需编辑")
swmEdit = True
End Function
此函数用于在用户尝试编辑插入的宏特征时,通过swApp.SendMsgToUser向用户发送提示信息 “提取切割清单和名称代号宏特征无需编辑”,明确告知用户该宏特征不需要手动编辑,避免不必要的操作,发送提示后返回True。
SolidWorks 宏代码:钣金零件属性自动化处理
一、标题释义
本宏代码围绕 SolidWorks 软件中钣金零件属性操作展开,核心功能是将切割清单的属性自动更新至自定义属性,“自动化处理” 突出其通过代码实现批量、自动操作的特点,所以将标题定为 **《SolidWorks 钣金零件切割清单属性自动化提取与自定义属性更新宏》** ,精准概括其核心功能与应用场景。
二、功能与作用总述
该宏代码专为 SolidWorks 软件设计,聚焦于钣金零件文件处理。其核心作用是在钣金零件中插入宏特征,并将切割清单内的边界框区域、长度、宽度等关键属性,以及钣金厚度、切割长度、切除数量、折弯数量等相关属性,自动更新或复制到零件的自定义属性中。通过自动化操作,减少手动输入和处理属性数据的工作量,提升设计流程中属性管理的效率和准确性,避免人为操作可能出现的错误,尤其适用于需要频繁处理钣金零件属性数据的场景。
三、代码结构与执行流程分析
(一)声明部分
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim dimTypes As Variant
Dim dimValue As Variant
Dim icons(2) As String
Dim vIcons As Variant
Dim myPath As String
Dim myFolder As String
Dim myFeat As Feature
Dim myMethods(8) As String
Dim vMethods As Variant
通过Option Explicit强制要求所有变量必须显式声明,增强代码的可读性和稳定性。随后声明了一系列变量,包括 SolidWorks 应用程序对象swApp、零件文档对象Part ,用于存储属性类型和值的dimTypes和dimValue,以及与宏特征相关的图标、路径、方法等变量,为后续操作提供数据载体和对象引用。
(二)main子过程
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
'If (Part Is Nothing) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
'If (Part.GetType <> 1) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
'If (Part.GetBendState = 0) Then
'MsgBox " Please open a sheet metal part document. "
'End
'Exit Sub
'End If
myPath = swApp.GetCurrentMacroPathName
myFolder = swApp.GetCurrentMacroPathFolder & "\"
myMethods(0) = myPath
myMethods(1) = "BoundingBoxArea"
myMethods(2) = "swmRebuild"
myMethods(3) = myPath
myMethods(4) = "BoundingBoxArea"
myMethods(5) = "swmEdit"
myMethods(6) = ""
myMethods(7) = ""
myMethods(8) = ""
vMethods = myMethods
'icons(0) = myFolder + "BoundingBoxAreaBlank.bmp"
'icons(1) = myFolder + "BoundingBoxAreaBlank.bmp"
'icons(2) = myFolder + "BoundingBoxAreaBlank.bmp"
'vIcons = icons
Set myFeat = Part.FeatureManager.InsertMacroFeature3("提取切割清单属性值到属性VBA-不要编辑", "", vMethods, Empty, Empty, Empty, dimTypes, dimValue, Empty, Empty, swMacroFeatureByDefault + swMacroFeatureAlwaysAtEnd + swMacroFeatureEmbedMacroFile)
End Sub
首先获取 SolidWorks 应用程序对象和当前活动文档对象。原代码中注释掉了三段用于判断当前文档是否为钣金零件的代码,若恢复这些判断逻辑,可确保宏仅在钣金零件文件中执行,增强代码适用性和准确性,避免在非目标文件中运行导致错误。
获取当前宏的路径和文件夹路径,并设置myMethods数组,该数组包含宏文件路径、宏特征名称以及关联的操作方法(如swmRebuild重建方法和swmEdit编辑方法),为插入宏特征做准备。
注释掉了设置图标的相关代码,推测原意图是为宏特征设置显示图标,若有相关需求可恢复代码并配置合适图标。
最后通过Part.FeatureManager.InsertMacroFeature3方法在当前零件中插入一个名为 “提取切割清单属性值到属性 VBA - 不要编辑” 的宏特征,并关联之前设置的方法和相关参数,完成宏特征的创建与初始化。
(三)swmRebuild函数
Public Function swmRebuild(swAppIn As Variant, partIn As Variant, featureIn As Variant) As Variant
Dim swPart As ModelDoc2
Dim swFeat As SldWorks.Feature
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim strValue(13) As String
Dim swBodyFolder As SldWorks.BodyFolder
Dim Currentlanguage As String
Set swApp = swAppIn
Currentlanguage = swApp.GetCurrentLanguage
Set swPart = swApp.ActiveDoc
Set swFeat = swPart.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName = "SolidBodyFolder" Or swFeat.GetTypeName = "CutListFolder" Or swFeat.GetTypeName = "SubWeldFolder" Then
'Set swBodyFolder = swFeat.GetSpecificFeature2
'swBodyFolder.UpdateCutList
Set swCustPropMgr = swFeat.CustomPropertyManager
If Currentlanguage = "chinese-simplified" Then
swFeat.CustomPropertyManager.Get4 "边界框长度", False, strValue(0), strValue(1) 'Bounding Box Length
swFeat.CustomPropertyManager.Get4 "边界框宽度", False, strValue(2), strValue(3) 'Bounding Box Width
swFeat.CustomPropertyManager.Get4 "钣金厚度", False, strValue(4), strValue(5)
swFeat.CustomPropertyManager.Get4 "切割长度-外部", False, strValue(6), strValue(7) 'Cutting Length-Outer
swFeat.CustomPropertyManager.Get4 "切割长度-内部", False, strValue(8), strValue(9) 'Cutting Length-Inner
swFeat.CustomPropertyManager.Get4 "切除", False, strValue(10), strValue(11) 'Cut Outs
swFeat.CustomPropertyManager.Get4 "折弯", False, strValue(12), strValue(13) 'Bends
ElseIf Currentlanguage = "english" Then
swFeat.CustomPropertyManager.Get4 "Bounding Box Length", False, strValue(0), strValue(1)
swFeat.CustomPropertyManager.Get4 "Bounding Box Width", False, strValue(2), strValue(3)
swFeat.CustomPropertyManager.Get4 "Sheet Metal Thickness", False, strValue(4), strValue(5)
swFeat.CustomPropertyManager.Get4 "Cutting Length-Outer", False, strValue(6), strValue(7)
swFeat.CustomPropertyManager.Get4 "Cutting Length-Inner", False, strValue(8), strValue(9)
swFeat.CustomPropertyManager.Get4 "Cut Outs", False, strValue(10), strValue(11)
swFeat.CustomPropertyManager.Get4 "Bends", False, strValue(12), strValue(13)
End If
End If
'If strValue(1) <> "" Then
' Exit Do
' End If
Set swFeat = swFeat.GetNextFeature
Loop
'Dim MyMassProp As SldWorks.MassProperty
'Set MyMassProp = swPart.Extension.CreateMassProperty
'MyMassProp.UseSystemUnits = True
'Debug.Print MyMassProp.Mass
Set swCustPropMgr = swPart.Extension.CustomPropertyManager("")
swPart.DeleteCustomInfo2 "", "长" '删除属性栏上摘要信息的数据
swPart.DeleteCustomInfo2 "", "宽"
swPart.DeleteCustomInfo2 "", "厚"
swPart.DeleteCustomInfo2 "", "外切长度"
swPart.DeleteCustomInfo2 "", "内切长度"
swPart.DeleteCustomInfo2 "", "切割总长(m)"
swPart.DeleteCustomInfo2 "", "穿孔数"
swPart.DeleteCustomInfo2 "", "折弯刀数"
swPart.DeleteCustomInfo2 "", "喷塑面积(m2)"
swCustPropMgr.Add3 "长", 30, strValue(1), 1 'Value in model units
swCustPropMgr.Add3 "宽", 30, strValue(3), 1 'Value in model units
swCustPropMgr.Add3 "厚", 30, strValue(5), 1 'Value in model units
swCustPropMgr.Add3 "外切长度", 30, strValue(7), 1 'Value in model units
swCustPropMgr.Add3 "内切长度", 30, strValue(9), 1
swCustPropMgr.Add3 "切割总长(m)", 30, Round((Val(strValue(7)) + Val(strValue(9))) / 1000, 2), 1 'Change decimal places by replace 2, value is in m
swCustPropMgr.Add3 "穿孔数", 30, strValue(11), 1
swCustPropMgr.Add3 "折弯刀数", 30, strValue(13), 1
swCustPropMgr.Add3 "喷塑面积(m2)", 30, Round(Val(strValue(1)) * Val(strValue(3)) / 1000000 * 2, 2), 1
swmRebuild = True
End Function
该函数负责从切割清单相关特征中提取属性值,并更新到零件的自定义属性中。首先获取当前活动文档、相关特征对象和当前语言环境。
通过循环遍历零件的所有特征,当找到类型为 “SolidBodyFolder”“CutListFolder” 或 “SubWeldFolder” 的特征时,获取其自定义属性管理器。根据当前语言环境(中文简体或英文),从属性管理器中提取边界框长度、宽度、钣金厚度等多种属性值,并存储在strValue数组中。
删除零件原有的一些自定义属性(如 “长”“宽” 等),然后将提取到的属性值进行处理(如计算切割总长、喷塑面积)后,添加到新的自定义属性中,完成属性的更新与添加操作,最后返回True表示函数执行成功。
(四)swmEdit函数
Public Function swmEdit(swAppIn As Variant, partIn As Variant, featureIn As Variant) As Variant
swApp.SendMsgToUser ("提取切割清单和名称代号宏特征无需编辑")
swmEdit = True
End Function
此函数用于在用户尝试编辑插入的宏特征时,通过swApp.SendMsgToUser向用户发送提示信息 “提取切割清单和名称代号宏特征无需编辑”,明确告知用户该宏特征不需要手动编辑,避免不必要的操作,发送提示后返回True。