×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro: Retrieving custom props from Part while in a Drawing?

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
 

RE: Macro: Retrieving custom props from Part while in a Drawing?

This is a snippet from my macro to export a SW drawing to pdf, dwg, and/or parasolid.  It exports to PDF always, dwg when the model contains a "sheet metal" part, and parasolid otherwise except for our actual assemblies based on our part numbering scheme.

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 you need to do is get access to the part (or assembly) that is in the 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.   

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources