AnsweredAssumed Answered

Save PNG to vault with variables

Question asked by Lasse Nielsen on Nov 21, 2019
Latest reply on Nov 21, 2019 by Amen Allah Jlili

Hello Forum


Since the TaskAddIn is incapable of creating PNGs I have started making my own macro.


My objective is to save a PNG of one of the sheets in the drawing, grab the Serial number of the drawing and ad it to the data card of the PNG before checking in the PNG


So far I have successfully created a macro that opens the drawing in question, activates the correct sheet, grabs the Serial number from the drawings CustomProperties and creates the PNG from a SaveAs command.


When the macro reaches Set pEnumVar = file.GetEnumeratorVariable it fails. My guess it that the macro simply is faster than the PDM system. My reason for this is:


If I run the macro using F8 (step into) all the way down to before Set pEnumVar = file.GetEnumeratorVariable, then waits for the PNG to appear in my vault (Windows Explorer) using F5 and then continue using F8 to step further into the macro it works all the way till the end.


Any ideas on how to get this to work?


Option Explicit


Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawing As SldWorks.DrawingDoc
Dim swCustPropMgr As SldWorks.CustomPropertyManager


Dim strFullPath As String
Dim strFolderPath As String
Dim strFileName As String


Dim pdmVault As Object


Dim i As Integer
Dim blnError As Boolean


Sub main()


    'Initialize macro
    Set swApp = Application.SldWorks
    strFullPath = "<Filepath>"
    strFolderPath = "<Path>"
    'Strings for testing in VBA editor
    strFullPath = "C:\MyVault\Test area\Test file.SLDDRW"
    strFolderPath = "C:\MyVault\Test area\"


    'Append \ to path if missing
    If Right(strFolderPath, 1) <> "\" Then
        strFolderPath = strFolderPath & "\"
    End If
    'Limit the macro to SLDDRW files
    If LCase(Right(strFullPath, 6)) <> "slddrw" Then
    End If
    'Grabs filename only
    i = InStrRev(strFullPath, "\")
    strFileName = Right(strFullPath, Len(strFullPath) - i)
    strFileName = Left(strFileName, Len(strFileName) - 7)
    'Create vault object
    Set pdmVault = CreateObject("ConisioLib.EdmVault")
    pdmVault.loginauto "MyVault", 0
    'Open drawing
    Set swModel = swApp.OpenDoc6(strFullPath, 3, 1, Empty, Empty, Empty)


    'Activate Picture sheet
    Set swDrawing = swModel
    blnError = swDrawing.ActivateSheet("Picture")
    'Verify sheet is activated
    If blnError = False Then
    End If
    'Get info from the drawings CustomProperties
    Dim SerialNo As String
    Dim Resolved As String
    Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
    swCustPropMgr.Get2 "Serie_NR", SerialNo, Resolved
    'Set user settings
    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffScreenOrPrintCapture, 1
    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffPrintDPI, 300
    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swTiffPrintPaperSize, swDwgPaperSizes_e.swDwgPaperA3size
    'Rebuild drawing
    swModel.ForceRebuild3 False
    'Save sheet as PNG
    swModel.SaveAs strFolderPath & strFileName & ".PNG"
    'Create file and folder object
    Dim folder As Object
    Set folder = pdmVault.GetFolderFromPath(strFolderPath)


    Dim file As Object
    Set file = pdmVault.GetFileFromPath(strFolderPath & strFileName & ".PNG", folder)
   'Add metadata to datacard
    Dim pEnumVar As Object
    Set pEnumVar = file.GetEnumeratorVariable
    pEnumVar.SetVar "Serie_NR", "", SerialNo, False
    'Check in
    If True = file.IsLocked Then
        file.UnlockFile 0, "Checked in by Macro - Created by LQN"
    End If
End Sub