Macro: Retrieving custom props from Part while in a Drawing?
Macro: Retrieving custom props from Part while in a Drawing?
(OP)
I have a macro which I pieced together from another one found on the web. It saves a drawing as a pdf and names the file with a concatenated list of properties. My problem is that the properties I want it to look at are in the part file, not the drawing, and I don't know how to get the macro to look in the part as opposed to the current/active document (the drawing) which it is printing.
------------------------
Here is part of the code:
Set swCustPrpMgr = swModel.Extension.CustomPropertyManager("")
'Get Custom Property
swCustPrpMgr.Get3 "Part Number", False, "", vPartNumber
swCustPrpMgr.Get3 "Description", False, "", vDescription
swCustPrpMgr.Get3 "Revision", False, "", vRevision
swCustPrpMgr.Get3 "Date", False, "", vDate
filename = vPartNumber & "_" & vDescription & "_" & vRevision & "_" & vDate & ".PDF"
boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
-----------------------------------------
Thanks for any help,
James
------------------------
Here is part of the code:
Set swCustPrpMgr = swModel.Extension.CustomPropertyManager("")
'Get Custom Property
swCustPrpMgr.Get3 "Part Number", False, "", vPartNumber
swCustPrpMgr.Get3 "Description", False, "", vDescription
swCustPrpMgr.Get3 "Revision", False, "", vRevision
swCustPrpMgr.Get3 "Date", False, "", vDate
filename = vPartNumber & "_" & vDescription & "_" & vRevision & "_" & vDate & ".PDF"
boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
-----------------------------------------
Thanks for any help,
James






RE: Macro: Retrieving custom props from Part while in a Drawing?
Sub CheckDrawing()
Set swDraw = swModel
InitialDocName = swModel.GetPathName
Set swInitialSheet = swDraw.GetCurrentSheet
InitialSheet = swInitialSheet.GetName
ToggleShtMtl = 0
ToggleModel = 0
ToggleShtSize = 0
SheetNumber = 1
'Check all Sheet Views for Sheet Metal Flat Pattern
SheetCount = swDraw.GetSheetCount
vSheetName = swDraw.GetSheetNames
For i = 0 To SheetCount - 1
SheetName = vSheetName(i)
boolstatus = swDraw.ActivateSheet(SheetName)
Set swSheet = swDraw.Sheet(SheetName)
If ToggleShtMtl = 0 Then
Call FindShtMtlFlat
End If
Next i
Call GetModelProps
If ToggleShtSize = 0 And ToggleShtMtl = 0 Then
Call SaveParasolid
Call SavePDF
ElseIf ToggleShtSize = 0 And ToggleShtMtl = 1 Then
Call SaveDWG
Call SavePDF
ElseIf ToggleShtSize = 1 And ToggleShtMtl = 0 Then
Call SavePDF
ElseIf ToggleShtSize = 1 And ToggleShtMtl = 1 Then
MsgBox "Drawing has a Sheet Metal flat pattern view, but the PartShtSize is not 01, 02, 03, or 04." & vbNewLine & "Please correct PartShtSize or remove view/configuration."
ElseIf ToggleShtSize = 2 Then
MsgBox "PartShtSize in model is not recognized." & vbNewLine & "PartShtSize must be 01, 02, 03, or 04 to export as a parasolid with this macro."
End If
End Sub
Sub FindShtMtlFlat()
Set swView = swDraw.GetFirstView
Do Until (Right(ViewConfigName, 15) = "SM-FLAT-PATTERN") Or swView Is Nothing
ViewConfigName = swView.ReferencedConfiguration
If Right(ViewConfigName, 15) = "SM-FLAT-PATTERN" And (ToggleShtMtl = 0) Then
Set swModel = swView.ReferencedDocument
ToggleShtMtl = 1
SheetNumber = SheetNumber + i
ShtMtlSheet = swSheet.GetName()
Exit Do
ElseIf (ViewConfigName <> "") And (ToggleShtMtl = 0) Then
Set swModel = swView.ReferencedDocument
End If
Set swView = swView.GetNextView
Do Until (Right(ViewConfigName, 15) = "SM-FLAT-PATTERN") Or swView Is Nothing
ViewConfigName = swView.ReferencedConfiguration
If (Right(ViewConfigName, 15) = "SM-FLAT-PATTERN") And (ToggleShtMtl = 0) Then
Set swModel = swView.ReferencedDocument
ToggleShtMtl = 1
SheetNumber = SheetNumber + i
ShtMtlSheet = swSheet.GetName()
Exit Do
ElseIf (ViewConfigName <> "") And (ToggleShtMtl = 0) Then
Set swModel = swView.ReferencedDocument
End If
Set swView = swView.GetNextView
Loop
Loop
End Sub
Sub GetModelProps()
CheckPartShtSize = swModel.GetCustomInfoValue(ViewConfigName, "PartShtSize")
CheckPartBase = swModel.GetCustomInfoValue(ViewConfigName, "PartBase")
CheckRevision = swModel.GetCustomInfoValue(ViewConfigName, "Revision")
If CheckRevision = "-" Then
CheckRevision = "0"
End If
If Len(CheckPartShtSize) = 2 And Left(CheckPartShtSize, 1) = "0" And 0 < Right(CheckPartShtSize, 1) And Right(CheckPartShtSize, 1) < 5 Then
ElseIf Len(CheckPartShtSize) = 2 And Left(CheckPartShtSize, 1) = "0" And 4 < Right(CheckPartShtSize, 1) And Right(CheckPartShtSize, 1) < 10 Then
ToggleShtSize = 1
Else
ToggleShtSize = 2
End If
End Sub
Sub SaveParasolid()
boolstatus = swModel.Extension.SelectByID2(ViewConfigName, "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModel.ShowConfiguration2(ViewConfigName)
If Len(SheetNumber) = 1 And IsNumeric(CheckPartBase) And Len(CheckPartBase) = 6 And Len(CheckRevision) = 1 Then
longstatus = swModel.SaveAs3(PreReleasePath & CheckPartBase & SheetNumber & CheckRevision & ".X_T", 0, 0)
End If
End Sub
Sub SaveDWG()
Set swDraw = swApp.ActivateDoc2(InitialDocName, True, 2)
swDraw.ActivateSheet (ShtMtlSheet)
Set swSheet = swDraw.Sheet(ShtMtlSheet)
swModel.ViewZoomtofit2
swModel.ClearSelection2 True
If Len(CheckPartShtSize) = 2 And Left(CheckPartShtSize, 1) = "0" And 0 < Right(CheckPartShtSize, 1) And Right(CheckPartShtSize, 1) < 5 And Len(SheetNumber) = 1 And IsNumeric(CheckPartBase) And Len(CheckPartBase) = 6 And Len(CheckRevision) = 1 Then
longstatus = swDraw.SaveAs3(PreReleasePath & CheckPartBase & SheetNumber & CheckRevision & ".DWG", 0, 0)
End If
End Sub
Sub SavePDF()
Set swDraw = swApp.ActivateDoc2(InitialDocName, True, 2)
swDraw.ActivateSheet (InitialSheet)
Set swSheet = swDraw.Sheet(InitialSheet)
swModel.ViewZoomtofit2
swModel.ClearSelection2 True
If ToggleShtSize = 0 And IsNumeric(CheckPartBase) And Len(CheckPartBase) = 6 And Len(CheckRevision) = 1 Then
longstatus = swDraw.SaveAs3(PreReleasePath & CheckPartBase & "1" & CheckRevision & ".PDF", 0, 0)
ElseIf ToggleShtSize = 1 And IsNumeric(CheckPartBase) And Len(CheckPartBase) = 6 And Len(CheckRevision) = 1 Then
longstatus = swDraw.SaveAs3(PreReleasePath & CheckPartBase & "1" & CheckRevision & ".PDF", 0, 0)
End If
End Sub
Sub SaveECNNumber()
If IsNumeric(ECNNumber) And Len(ECNNumber) = 5 Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set MyFile = fs.OpenTextFile(ECNTextPath, 2, True)
MyFile.Write ECNNumber
MyFile.Close
End If
End Sub
James Spisich
Design Engineer, CSWP
RE: Macro: Retrieving custom props from Part while in a Drawing?
What version of Solidworks this macro runs ?
Best Regard, SolidWorks Engineer
http://solidworksengineer.com
http://sketchupengineer.com
RE: Macro: Retrieving custom props from Part while in a Drawing?
For 99% of drawings, the model of interest is in the default view of the first sheet. So, from a drawing, get the first sheet, then the default view, then the model associated with the default view.
Take note of the differences between ModelDoc, DrawingDoc, PartDoc, and AssemblyDoc objects. Your drawing needs to be in accessed vie DrawingDoc object to get hold of the views.