Instructions
- Preconditions
- drawing is open containing at least one view referencing a part or assy.
- Result
- document-level custom properties used in referenced model are copied to drawing custom properties.
Note that this will find the first part or assy used in the drawing and will copy its custom properties. If multiple assemblies or parts are used throughout the drawing then this macro will still only copy the custom properties for one of them.
Files
Copy part or assembly custom properties to drawing custom properties
Code
'Preconditions: drawing is open containing at least one view referencing a part or assy
'Results: document-level custom properties used in referenced model are copied to drawing _
custom properties
'Note: This will find the first part or assy used in the drawing and will copy its _
custom properties. If multiple assemblies or parts are used throughout the drawing then _
this macro will still only copy the custom properties for one of them.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swView As SldWorks.View
Dim swDrawComp As SldWorks.DrawingComponent
Dim swComp As SldWorks.Component2
Dim swRefModel As SldWorks.ModelDoc2
Dim vCompCustPropNames As Variant
Dim vCompCustPropTypes As Variant
Dim vCompCustPropValues As Variant
Dim i As Integer
Dim strRefModelPath As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
strRefModelPath = swView.GetReferencedModelName
If strRefModelPath <> "" Then
If UCase(Right(strRefModelPath, 3)) = "ASM" Then
Set swDrawComp = swView.RootDrawingComponent
If Not swDrawComp Is Nothing Then
Set swComp = swDrawComp.Component
Set swRefModel = swComp.GetModelDoc2
Set swCustPropMgr = swRefModel.Extension.CustomPropertyManager(Empty)
Call CopyCustProps
End If
Else
swApp.DocumentVisible False, swDocPART
Set swRefModel = swApp.OpenDoc6(strRefModelPath, swDocPART, 1, Empty, Empty, Empty)
Set swCustPropMgr = swRefModel.Extension.CustomPropertyManager(Empty)
Call CopyCustProps
swApp.DocumentVisible True, swDocPART
End If
Exit Do
End If
Set swView = swView.GetNextView
Loop
End Sub
Private Sub CopyCustProps()
swCustPropMgr.GetAll vCompCustPropNames, vCompCustPropTypes, vCompCustPropValues
Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
For i = 0 To UBound(vCompCustPropNames)
swCustPropMgr.Add2 vCompCustPropNames(i), vCompCustPropTypes(i), vCompCustPropValues(i)
Next i
End Sub