hexagon logo

Saving Pdf and Csv reports without prompting the user

Currently I am trying to write a C# app that will run a PCDmis program and then save the reports with specific names, in a specific folder based on some inputs from another source. I found an example of what I'm trying to do but it involves changing the prg file directly and I was wondering if I can do the same thing in C#?

I found this code from from 2021 but it's an embedded script.

GROUP_PRINT_COMMANDS=GROUP/SHOWALLPARAMS=YES
ASSIGN/NUMOOT=GETPROGRAMINFO("NUMOOT")
ASSIGN/PROG_NAME=GETPROGRAMINFO ("PARTNAME")
ASSIGN/DATE=STR(SYSTEMDATE ("dMMMyyyy"))
ASSIGN/TIME=STR(SYSTEMTIME ("HHmmss"))
ASSIGN/REJECT="____REJECT____"
IF/NUMOOT=="0"
ASSIGN/REPORT_NAME="O:\\CMM PROGRAMS\\CMM_PDF_PC DMIS\\AG0OFFLINETEST\\"+PROG_NAME+"__"+VAR_SERIAL_ NUMBER+"__"+VAR_RUNNUMBER+"__"+DATE+"__"+TIME+"__. PDF"
END_IF/
ELSE/
ASSIGN/REPORT_NAME="O:\\CMM PROGRAMS\\CMM_PDF_PC DMIS\\AG0OFFLINETEST\\"+PROG_NAME+"__"+VAR_SERIAL_ NUMBER+"__"+VAR_RUNNUMBER+"__"+DATE+"__"+TIME+"__" +REJECT+".PDF"
END_ELSE/
PRINT/REPORT,EXEC MODE=END,$
TO_FILE=ON,AUTO=1,AUTO OPEN=OFF,$
TO_PRINTER=OFF,COPIES=1,$
TO_DMIS_REPORT=OFF,FILE_OPTION=OVERWRITE,FILENAME= [B]REPORT_NAME[/B],$
REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMENSIONS=NO,$
TO_EXCEL=OFF,$
PREVIOUS_RUNS=KEEP_INSTANCES
ENDGROUP/ID=GROUP_PRINT_COMMANDS
Parents
  • We do something similar, but maybe not exactly what you need. Here's what we do. The operator runs the programs, at the beginning of the program, they get a set of inputs to enter their name, job number, and sample number. The program runs and it saves the file to a location that looks like this: <Root Folder>\CMM Inspection Results\<Part Number>\<Job Number>\<filename> where <filename> = <output filename> - <Job Number> - <Sample Number>.pdf

    How this works from a programmer perspective is that we end up having 4 variables that have to be in the program for the automation to work and then at the end of the program we call a .BAS script, the .BAS script reads the program and gets the variables values and then saves the file to the right location. So if we're going to modify an old program, we would copy and paste the variables from the template into the program at the beginning of the program and then copy and paste the last line which calls the .BAS script. In our programs we have all printing turned off, both in the File -> Printing -> Report window print setup mentioned and we don't add any print report commands to our programs. If we need to change how the printing works, we're just going to change the one .BAS file that every program points to.

    Here is the key lines of code in our PCDMIS program:

    OPERATOR =COMMENT/INPUT,NO,FULL SCREEN=NO,
    Please enter Operator Number/Name
    JOB =COMMENT/INPUT,NO,FULL SCREEN=NO,
    Please enter JOB Number
    SEQUENCE =COMMENT/INPUT,NO,FULL SCREEN=NO,
    Please enter Part Sequence (1,2,3, etc.)
    ASSIGN/PART_NUMBER="Insert_PART_NUMBER_Here"
    ASSIGN/JOB_NUMBER=JOB.INPUT
    ASSIGN/SAMPLE_NUMBER=SEQUENCE.INPUT
    ASSIGN/OUTPUT_FILENAME= "THIS NAMES PDF FILE"
    
    ...
    
    CS1 =SCRIPT/FILENAME= S:\0 - CMM SUBROUTINES AND SCRIPTS\PRINTPDFREPORT.BAS
    FUNCTION/Main,SHOW=YES,,
    STARTSCRIPT/
    ENDSCRIPT/


    The programmer changes "Insert_PART_NUMBER_Here" to the actual part number, and "THIS NAMES PDF FILE" to the beginning part of the filename so something like "Part Number - Final Inspection".

    This is the code in PRINTPDFREPORT.BAS, although I changed the filepath as it was a little too descriptive to be shared publicly.

    Sub Main
    
    Dim App As Object
    Set App = CreateObject("Pcdlrn.Application")
    Dim PartProg As Object
    Set PartProg = App.ActivePartProgram
    Dim RepWin As Object
    Set RepWin = PartProg.ReportWindow
    Dim EditWin As Object
    Set EditWin = PartProg.EditWindow
    Dim MyFSO As FileSystemObject
    Set MyFSO = CreateObject("Scripting.FileSystemObject")
    
    Dim rootDirectory As String
    rootDirectory = "C:\CMM Inspection Results"
    
    '  Get variables From PCDMIS
    Dim partNumber As String
    partNumber = PartProg.GetVariableValue ("PART_NUMBER").StringValue
    Dim lotNumber As String
    Set lotNumber = PartProg.GetVariableValue ("JOB_NUMBER").StringValue
    Dim sampleNumber As String
    Set sampleNumber = PartProg.GetVariableValue ("SAMPLE_NUMBER").StringValue
    Dim outputFn As String
    Set outputFn = PartProg.GetVariableValue ("OUTPUT_FILENAME").StringValue
    
    '  If the variables are empty, pop up a message box And don't Do anything
    If partNumber = vbNullString Then
      MsgBox "PART_NUMBER variable is empty.  This script is not going to do anything."
      GoTo endOfScript
    End If
    If lotNumber = vbNullString Then
      MsgBox "JOB_NUMBER variable is empty.  This script is not going to do anything."
      GoTo endOfScript
    End If
    If sampleNumber = vbNullString Then
      MsgBox "SAMPLE_NUMBER variable is empty.  This script is not going to do anything."
      GoTo endOfScript
    End If
    If outputFn = vbNullString Then
      MsgBox "OUTPUT_FILENAME variable is empty.  This script is not going to do anything."
      GoTo endOfScript
    End If
    
    ' Create filepaths For all possible scenarios
    Dim baseName As String
    baseName = outputFn & " - " & lotNumber & " - " & sampleNumber
    Dim desiredPdfPath As String
    desiredPdfPath = rootDirectory & "\" & partNumber & "\" & lotNumber & "\" & baseName & ".pdf"
    Dim directoryPath As String
    Set directoryPath = MyFSO.GetParentFolderName(desiredPdfPath)
    
    ' Check If destination folder exists, If Not make it.
    FolderCreate(directoryPath)
    
    '  Determines Filename, checks If desired filename exists And appends characters To it If it already exists
    Dim actualPdfPath As String
    If Not MyFSO.FileExists(desiredPdfPath) Then
      actualPdfPath = desiredPdfPath
    Else
      Dim I As Integer
      Dim CharString As String
      CharString = "abcdefghijklmnopqrstuvwxyz"
        
      For I = 1 To 26
        actualPdfPath = directoryPath & "\" & baseName & Mid(CharString, I, 1) & ".pdf"
        If Not MyFSO.FileExists(actualPdfPath) Then
            Exit For
        End If
      Next    
    End If
    
    ' MsgBox "About to Print New" & CHR(10) & actualPdfPath
    
    ' Sets Print options, namely Print To pdf (actualPdfPath)
    EditWin.ReportMode
    EditWin.SetPrintOptionsEx PCD_FILE, DMIS_OFF, PCD_NEWFILE, 0, actualPdfPath , PCD_PDF, False
    RepWin.PrintReport
    
    '  Apparently the above sets the printer To automatically Print at the End of the run, which duplicates the above Print commands, so this turns printing back off
    EditWin.SetPrintOptionsEx PCD_OFF, DMIS_OFF, PCD_NEWFILE, 0, desiredPdfPath, PCD_PDF, False
    
    If MyFSO.FileExists("C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe") Then
      shell("C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe" & " " & """" & actualPdfPath & """")
    Else
      If MyFSO.FileExists("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe") Then
        shell("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & " " & """" & actualPdfPath & """")
      End If
    End If
    
    '  End of Script Label For GoTo's
    endOfScript:
    End Sub
    
    
    Function FolderCreate(ByVal path As String) As Boolean
      Dim MyFSO As FileSystemObject
      Set MyFSO = CreateObject("Scripting.FileSystemObject")
      Dim parentPath As String
      Set parentPath = MyFSO.GetParentFolderName(path)
    
      If Not MyFSO.FolderExists(path) Then
        parentPath = MyFSO.getParentFolderName(path)
        If Not MyFSO.FolderExists(parentPath) Then
          FolderCreate(parentPath)
        End If  
        mkdir path  
      End If
    End Function
    
  • Not sure about C#. I just did what I had to in .BAS as that was what I copied from.
Reply Children
No Data