这是遍历宏加入分离宏,但是并不是绝对的稳定,有时候会出错
Dim TopDocPathOnly As String
Dim PartsCollect() As String '遍历清单(阵列)111
Dim InCollectCount As Double '遍历清单长度
Dim CustomInfoQTY As String
Sub main()
Set swApp = Application.SldWorks 'SW对象
Set TopDoc = swApp.ActiveDoc '总装对象
If TopDoc.GetType <> 2 Then Exit Sub '不是装配=退出
TopDocPathSplit = Split(TopDoc.GetPathName, "") '分割
TopDocName = TopDocPathSplit(UBound(TopDocPathSplit)) '总装文件名称
TopDocName = Left(TopDocName, Len(TopDocName) - 7) '总装文件名称(排除.SLDASM)
TopDocPathOnly = Mid(TopDoc.GetPathName, 1, InStrRev(TopDoc.GetPathName, "", -1)) '總裝整個目錄名稱
TopConfString = TopDoc.GetActiveConfiguration.Name '總裝配置名稱
CustomInfoQTY = "数量" '可按个人喜好修改预设值
InCollectCount = 1 '遍历清单长度基数
ReDim PartsCollect(InCollectCount) '定义阵列项数
SubAsm TopDoc, TopConfString '遍历
Beep
MsgBox "完成"
End Sub
Function SubAsm(AsmDoc, ConfString)
Set swApp = Application.SldWorks 'SW对象
Set TopDoc = swApp.ActiveDoc '总装对象
TopDocPathSplit = Split(TopDoc.GetPathName, "") '分割
TopDocName = TopDocPathSplit(UBound(TopDocPathSplit)) '总装文件名称
TopDocName = Left(TopDocName, Len(TopDocName) - 7) '总装文件名称(排除.SLDASM)
TopDocPathOnly = Mid(TopDoc.GetPathName, 1, InStrRev(TopDoc.GetPathName, "", -1)) '總裝整個目錄名稱
TopConfString = TopDoc.GetActiveConfiguration.Name '總裝配置名稱
Set Configuration = AsmDoc.GetConfigurationByName(ConfString)
Set RootComponent = Configuration.GetRootComponent
Components = RootComponent.GetChildren
For Each Child In Components
Set ChildModel = Child.GetModelDoc
If Not (ChildModel Is Nothing) Then '排除抑制及轻化
ChildConfString = Child.ReferencedConfiguration '零件配置名称
ChildType = ChildModel.GetType
ChildPathSplit = Split(Child.GetPathName, "") '分割
ChildName = ChildPathSplit(UBound(ChildPathSplit)) '零件文件名称
ChildPathOnly = Mid(Child.GetPathName, 1, InStrRev(Child.GetPathName, "", -1)) '總裝整個目錄名稱
If ChildPathOnly = Replace(ChildPathOnly, TopDocPathOnly, "") Then SamePath = False Else SamePath = True '零件是否在总装目录或以下目录
If (Not Child.ExcludeFromBOM) And (Not Child.IsEnvelope) Then '跳过:不包括在材料明細表中 及 封套
BYL_Name = ChildModel.CustomInfo2(ChildConfString, "BYL") '备用量属性名称
BYL = ChildModel.CustomInfo2(ChildConfString, BYL_Name) '备用量
If (BYL = 0) Or (BYL = "") Then BYL = 1 '备用量除错
inCollect = False '重置判断变量
For Each PartinCollect In PartsCollect '判断是否已在遍历清单內
If ChildConfString & "@" & ChildName = PartinCollect Then inCollect = True
Next
If inCollect Then '已在遍历清单內
ht_Qty = ChildModel.CustomInfo2(ChildConfString, CustomInfoQTY) + 1 * BYL
ChildModel.DeleteCustomInfo2 ChildConfString, CustomInfoQTY
ChildModel.AddCustomInfo3 ChildConfString, CustomInfoQTY, 30, ht_Qty
Else '不在遍历清单內(首次处理)
ChildModel.DeleteCustomInfo2 ChildConfString, CustomInfoQTY
ChildModel.AddCustomInfo3 ChildConfString, CustomInfoQTY, 30, BYL
InCollectCount = InCollectCount + 1
ReDim Preserve PartsCollect(InCollectCount) '重新定义阵列项数(保留內含数据)
PartsCollect(InCollectCount - 1) = ChildConfString & "@" & ChildName '加入到遍历清单中
ChildModel.SetUserPreferenceIntegerValue swUnitSystem, swUnitSystem_Custom '单位系统=Custom
ChildModel.SetUserPreferenceIntegerValue swUnitsMassPropMass, swUnitsMassPropMass_Kilograms '重量单位设定为kg(可按喜好加入設定)
ChildModel.SetUserPreferenceIntegerValue swUnitsMassPropMass, swUnitsMassPropMass_Grams '设质量定单位为克
ChildModel.DeleteCustomInfo2 "", "重量" '删除重量自定义属性
ChildModel.DeleteCustomInfo2 ChildConfString, "重量" '删除重量配置特定属性
ChildModel.AddCustomInfo3 ChildConfString, "重量", 30, Chr(34) & "SW-Mass@@" & ChildConfString & "@*" & ChildName & Chr(34) '加入重量配置特定属性
ChildModel.SetUserPreferenceIntegerValue swUnitsMassPropLength, swCM '设定体积单位为厘米
ChildModel.SetUserPreferenceIntegerValue swUnitsMassPropVolume, swUnitsMassPropVolume_Centimeters3 '设定单位每立方厘米
ChildModel.DeleteCustomInfo2 "", "体积" '删除体积自定义属性
ChildModel.DeleteCustomInfo2 ChildConfString, "体积" '删除体积配置特定属性
ChildModel.AddCustomInfo3 ChildConfString, "体积", 30, Chr(34) & "SW-Volume@@" & ChildConfString & "@*" & ChildName & Chr(34) '加入体积配置特定属性
ChildModel.SetUserPreferenceIntegerValue swUnitsMassPropDecimalPlaces, 8 '质量及体积小数后8位
ConfName = ChildModel.GetActiveConfiguration.Name
Set CustPropMgr = ChildModel.Extension.CustomPropertyManager(ConfName)
Path_Name = ChildModel.GetPathName() '取得"路径名称及扩展名",不管扩展名是否应藏
S1 = InStrRev(Path_Name, "\") '\符号在路径之最后位置数
Name_C = Right(Path_Name, Len(Path_Name) - S1) '取得件号_名称.扩展名"
b = Len(Name_C)
e = Right(Name_C, 7)
If e = ".SLDPRT" Or e = ".SLDASM" Or e = ".sldprt" Or e = ".sldasm" Then
g = Left(Name_C, b - 7)
Else
g = Name_C
End If
k = Len(g)
For I = 1 To Len(g)
If Asc(Mid$(g, I, 1)) < 0 Then
w = I '确定第一个汉字的位置
Exit For
End If
Next
For I = 0 To Len(g) - 1
If Asc(Mid$(g, k - I, 1)) < 0 Then
X = k - I '确定最后一个汉字的位置
Exit For
End If
Next
If w > 0 Then
If w = 1 Then
s = Left(g, X) '汉字在前数字在后的情况
T = Right(g, k - X)
Else
s = Right(g, k - w + 1) '数字在前汉字在后的情况
T = Left(g, w - 1)
End If
Else
s = "" '纯数字的情况
T = g
End If
If k - ((X - w) + 1) = 1 Or k - ((X - w) + 1) = 0 Then '纯名称的情况
T = ""
s = g
Else
End If
'删除栏
CustPropMgr.Delete ("代号")
CustPropMgr.Delete ("名称")
CustPropMgr.Delete ("客户")
'新增
CustPropMgr.Add2 "代号", swCustomInfoText, T
CustPropMgr.Add2 "名称", swCustomInfoText, s
CustPropMgr.Add2 "客户", swCustomInfoText, TopConfString
ChildModel.SketchManager.Insert3DSketch True '插入三低草图
ChildModel.SketchManager.Insert3DSketch True '离开三低草图
End If
If ChildType = 2 Then
SubAsm ChildModel, ChildConfString '如果是装配则向下遍历
End If
End If
End If
Next
End Function
|