指點不敢當,用的都是悶老大和萊大的代碼,我只是加加減減瞎湊和的。
回到主題,看來您並沒有"仔細"檢查代碼內容,所以忽略了其中不同的地方。
- Dim TopDocPathOnly As String
- Dim PartsCollect() As String '遍歷清單(陣列)
- Dim InCollectCount As Double '遍歷清單長度
- Dim CustomInfoQTY As String
- Dim AssemQTY As String
- Dim TotalQTY 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 = "單機用量"
- AssemQTY = InputBox("請輸入本次訂單數量:", "")
- TotalQTY = "本次訂單總用量"
- If CustomInfoQTY = "" Then Exit Sub '按下取消離開宏
- InCollectCount = 1 '遍歷清單長度基數
- ReDim PartsCollect(InCollectCount) '定義陣列項數
- SubAsm TopDoc, TopConfString '遍歷
- Beep
- MsgBox "完成"
- End Sub
- Function SubAsm(AsmDoc, ConfString)
- 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 SamePath And (Not Child.ExcludeFromBOM) And (Not Child.IsEnvelope) Then '跳過:不在總裝目錄或其往下目錄 或 不包括在材料明細表中 或 是個封套
- UNIT_OF_MEASURE = ChildModel.CustomInfo2(ChildConfString, "備用倍數")
- If (UNIT_OF_MEASURE = 0) Or (UNIT_OF_MEASURE = "") Then UNIT_OF_MEASURE = 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 * UNIT_OF_MEASURE
- all_Qty = AssemQTY * ht_Qty
- ChildModel.DeleteCustomInfo2 ChildConfString, CustomInfoQTY
- ChildModel.AddCustomInfo3 ChildConfString, CustomInfoQTY, 30, ht_Qty
- ChildModel.DeleteCustomInfo2 ChildConfString, "本次訂單數量"
- ChildModel.AddCustomInfo3 ChildConfString, "本次訂單數量", 30, AssemQTY
- ChildModel.DeleteCustomInfo2 ChildConfString, TotalQTY
- ChildModel.AddCustomInfo3 ChildConfString, TotalQTY, 30, all_Qty
- Else '不在遍歷清單內(首次處理)
- all_Qty = AssemQTY * UNIT_OF_MEASURE
- ChildModel.DeleteCustomInfo2 ChildConfString, CustomInfoQTY
- ChildModel.AddCustomInfo3 ChildConfString, CustomInfoQTY, 30, UNIT_OF_MEASURE
- ChildModel.DeleteCustomInfo2 ChildConfString, "本次訂單數量"
- ChildModel.AddCustomInfo3 ChildConfString, "本次訂單數量", 30, AssemQTY
- ChildModel.DeleteCustomInfo2 ChildConfString, TotalQTY
- ChildModel.AddCustomInfo3 ChildConfString, TotalQTY, 30, all_Qty
- InCollectCount = InCollectCount + 1 '遍歷清單長度基數+1
- ReDim Preserve PartsCollect(InCollectCount) '重新定義陣列項數(保留內含數據)
- PartsCollect(InCollectCount - 1) = ChildConfString & "@" & ChildName '加入到遍歷清單中
- ChildModel.SetUserPreferenceIntegerValue swUnitSystem, swUnitSystem_Custom '單位系統=Custom
- End If
- If ChildType = 2 Then
- SubAsm ChildModel, ChildConfString '如果是裝配則向下遍歷
- End If
-
- End If
- End If
- Next
- End Function
复制代码
|