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
  • Hi,

    In this example the file path is hardcoded with some pcDMIS functions (such as Date() or GETPROGRAMINFO("PARTNAME")).

    You can also ignore this functionality and assign a new path to the pcDMIS variable "REPORT_NAME" directly from C++ with the ".PutText" command.


    something like this:
    ASSIGN/REPORT_NAME="a Path set by C++ programm"
    
    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
    


    make sure you set the path before reading the pcDMIS print command otherwise, if the path doesn't exist, the user will be prompted with a save pdf dialog
  • This solution will still involve actually adding this PC Dmis code to every prg file. Can I use the PutText function in C# to add this whole chunk of text? I am a software engineer trying to help the Quality Inspectors avoid having to do more work and to make less mistakes so I would prefer to not have to add extra manual steps in our process.
  • yes you can add this pcDmis-code by an external programm.
    You just have to keep in mind that you can't do this while a measurement routine is running.

    with an open pcDMIS routine you can click "file->export->basic". There you get a whole measurement routine as basic code,
    means a general idea how to add pcDMIS-commands with an external programm.

    the help file for the pcDMIS Object Library is also certainly helpful:
    https://docs.hexagonmi.com/pcdmis/2021.1/en/helpcenter/mergedProjects/automationobjects/webframe.html
  • "file->export->basic" whoa!! That's a really useful trick. Thank you so much! I have use the object hierarchy before but sometimes it's not clear to me how to actually write code using it. I have already figure out how to add Tracefields and values before the program runs so I am hoping it will be something similar and I can add this code before the program runs. Thus far my use cases are adding metadata before the program runs and knowing at runtime where the file is going to be saved based on that metadata. Hopefully, I won't have to mess with anything while the program is running. Does the print command need to be the last thing to avoid interfering with the program run?


  • I am able to get it to print pdfs without prompting the user but it won't save it in the file location that I am giving it. The only thing I have noticed is that I can't figure out how to change this feature
    DELETE_INSTANCES


    C#

    var reportPath = $@"{REPORTS_DIRECTORY_PATH}\{programData.Customer}\{programData.InternalPartName}-{programData.Revision}\{programData.RawMaterialId}\#{programData.UnitNumber}";
    
                //reportPath = reportPath.Replace(@"\", @"\\");
    
                var commands = _pcdPartProgram.Commands;
                commands.InsertionPointAfter(commands.LastCommand);
    
                //var command = commands.Add(ASSIGNMENT, true);
                //command.PutText("PDF_REPORT_NAME", DEST_EXPR, 0);
                //command.PutText($"\"{reportPath}.PDF\"", SRC_EXPR, 0);
    
                var command = commands.Add(PRINT_REPORT, true);
                command.Marked = true;
    
                // Set Print To File  = ON
                command.SetToggleString(2, PRINT_TO_FILE, 0);
    
                command.PutText($"{reportPath}", FILE_NAME, 1);
                //// index start
                //command.PutText("1", INDEX_START, 0);
    
                // Set File Print Method  = AUTO
                command.SetToggleString(2, FILE_COMMAND_TYPE, 0);
                // Set Auto Open  = OFF
                command.SetToggleString(1, ONOFF_TYPE, 0);
                // Set Print To Printer  = OFF
                command.SetToggleString(1, PRINT_TO_PRINTER, 0);
                // Set Number Of Copies  = 1
                command.PutText("1", INDEX_END, 0);
                // Set Previous Runs  = DELETE_INSTANCES
                command.SetToggleString(1, PRINT_DELETE_RUNS, 0);
                // Set DMIS Output  = OFF
                command.SetToggleString(1, OUTPUT_DMIS_REPORT, 0);
                // Set Overwrite  = OVERWRITE
                command.SetToggleString(2, OVERWRITE, 0);
                // Set Output Theoretical Values  = NONE
                command.SetToggleString(1, OUTPUT_FEATURE_NOMS, 0);
                // Set Output Features with Dimensions  = NO
                command.SetToggleString(1, OUTPUT_FEAT_W_DIMENS, 0);
                // Set Mode  = END
                command.SetToggleString(2, MODE_TYPE, 0);
                // Set Output To Excel  = ON
                command.SetToggleString(1, OUTPUT_TO_REPORT, 0);
                // Set File Print Method  = OVERWRITE
                command.SetToggleString(2, UNIT_TYPE, 0);
                // Set Auto Open  = OFF
                command.SetToggleString(1, OUTPUT_TYPE, 0);
    


    SCRIPT:
    PRINT/REPORT,EXEC MODE=END,$
                  TO_FILE=ON,APPEND=G:\My Drive\My Folder\Customer Inspections\Reports\Customer\9-03\RM-51\#1.PDF,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=DELETE_INSTANCES
    


    I have literally spent all day tinkering with this code and cannot get it to work. This feels insanely complicated for something that seems like it should be easy. Can anyone see what the heck I'm doing wrong?



  • SCRIPT:
    PRINT/REPORT,EXEC MODE=END,$
    TO_FILE=ON,APPEND=G:\My Drive\My Folder\Customer Inspections\Reports\Customer\9-03\RM-51\#1.PDF,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=DELETE_INSTANCES
    


    I have literally spent all day tinkering with this code and cannot get it to work. This feels insanely complicated for something that seems like it should be easy. Can anyone see what the heck I'm doing wrong?


    Are you writing to the G drive location?

    Are you using the "REPORT_NAME" function in FILENAME?

    If you're trying to use both, that could be the combatting factor.

    the can be removed. I think Dan tried bolding those, but that functionality doesn't work in the code function.
  • Is 'REPORT_NAME' a function? I think I was assuming it was just a variable name. I think I renamed it to be PDF_REPORT_NAME. Maybe that's the issue.
  • Hello,


    "TO_FILE=" Line for PDF output
    -> "APPEND=" means he is trying to append the measurement data to an existing PDF file, is that intentional? If the path is wrong or the file doesn't exist, it simply doesn't do anything

    "TO_PRINTER=" he is trying to print to a real printer.

    "TO_DMIS_REPORT=" Line for *.dmo files -> "FILENAME=REPORT_NAME" in this line does nothing, because "TO_DMIS_REPORT=OFF"
    -> REPORT_NAME is a pcDMIS-Variable
    -> B is forum formating and can be deleted

    "TO_EXCEL=" Line for Excel and CSV output files

    ################################################## ####################################

    in your example it ONLY tries to attach the measurement data to a PDF called #1.pdf
    all other lines are =OFF

    if you keep assigning a new file (a new Number) name via your external program, then "OVERWRITE" is the correct procedure.
    File extension ".PDF" is not required
    File extension ".CSV" is required in Excel Line

    Code for PDF ONLY
    Set DmisCommand = DmisCommands.Add(PRINT_REPORT, TRUE)
    DmisCommand.Marked = TRUE
    
      ' Line for PDF-File output
        retval = DmisCommand.SetToggleString (2, PRINT_TO_FILE, 0) ' Set Print To File  = ON
        retval = DmisCommand.PutText ("C:\Users\QS\Desktop\MigrationsTools\test123", FILE_NAME, 1) '<- change PDF path here, ("test123" is indeed the filename)
        retval = DmisCommand.SetToggleString (2, FILE_COMMAND_TYPE, 0) ' Set File Print Method  = OVERWRITE
        retval = DmisCommand.SetToggleString (1, ONOFF_TYPE, 0)  ' Set Auto Open  = OFF
    
      ' Line for Printer output
        retval = DmisCommand.SetToggleString (1, PRINT_TO_PRINTER, 0) ' Set Print To Printer  = OFF
        retval = DmisCommand.PutText ("1", INDEX_END, 0)  ' Set Number Of Copies  = 1
        retval = DmisCommand.SetToggleString (1, PRINT_DELETE_RUNS, 0) ' Set Previous Runs  = DELETE_INSTANCES
    
      ' Line for *.dmo file output
        retval = DmisCommand.SetToggleString (1, OUTPUT_DMIS_REPORT, 0) ' Set DMIS Output  = OFF
        retval = DmisCommand.SetToggleString (1, OVERWRITE, 0)   ' Set Overwrite  = INDEX
        retval = DmisCommand.SetToggleString (1, OUTPUT_FEATURE_NOMS, 0)  ' Set Output Theoretical Values  = NONE
        retval = DmisCommand.SetToggleString (1, OUTPUT_FEAT_W_DIMENS, 0) ' Set Output Features with Dimensions  = NO
        retval = DmisCommand.SetToggleString (2, MODE_TYPE, 0)  ' Set Mode  = END
    
      ' Line for Excel / CSV file output
      ' note that not all excel settings can be set via basic/C++
        retval = DmisCommand.SetToggleString (1, OUTPUT_TO_REPORT, 0) ' Set Output To Excel  = OFF
        retval = DmisCommand.SetToggleString (3, UNIT_TYPE, 0)  ' Set File Print Method  = AUTO
        retval = DmisCommand.PutText ("1", ROW_ID, 0) ' Set Index Start  = 1
        retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0) ' Set Auto Open  = ON
    
    DmisPart.RefreshPart
    


    Code for CSV ONLY
    Set DmisCommand = DmisCommands.Add(PRINT_REPORT, TRUE)
    DmisCommand.Marked = TRUE
    
      ' Line for PDF file output
        retval = DmisCommand.SetToggleString (1, PRINT_TO_FILE, 0)   ' Set Print To File  = OFF
        retval = DmisCommand.PutText ("", FILE_NAME, 1) ' Set Filename Item 1 = ""
        retval = DmisCommand.SetToggleString (2, FILE_COMMAND_TYPE, 0)  ' Set File Print Method  = OVERWRITE
        retval = DmisCommand.SetToggleString (1, ONOFF_TYPE, 0) ' Set Auto Open  = OFF
    
      ' Line for Printer output
        retval = DmisCommand.SetToggleString (1, PRINT_TO_PRINTER, 0) ' Set Print To Printer  = OFF
        retval = DmisCommand.PutText ("1", INDEX_END, 0) ' Set Number Of Copies  = 1
        retval = DmisCommand.SetToggleString (1, PRINT_DELETE_RUNS, 0) ' Set Previous Runs  = DELETE_INSTANCES
    
      ' Line for *.dmo file output
        retval = DmisCommand.SetToggleString (1, OUTPUT_DMIS_REPORT, 0)  ' Set DMIS Output  = OFF
        retval = DmisCommand.SetToggleString (1, OVERWRITE, 0) ' Set Overwrite  = INDEX
        retval = DmisCommand.SetToggleString (1, OUTPUT_FEATURE_NOMS, 0) ' Set Output Theoretical Values  = NONE
        retval = DmisCommand.SetToggleString (1, OUTPUT_FEAT_W_DIMENS, 0) ' Set Output Features with Dimensions  = NO
        retval = DmisCommand.SetToggleString (2, MODE_TYPE, 0) ' Set Mode  = END
     
      ' Line for Excel / CSV file output
      ' note that not all excel settings can be set via basic/C++
        retval = DmisCommand.SetToggleString (2, OUTPUT_TO_REPORT, 0)  ' Set Output To Excel  = ON
        retval = DmisCommand.SetToggleString (3, UNIT_TYPE, 0) ' Set File Print Method  = AUTO
        retval = DmisCommand.PutText ("1", ROW_ID, 0) ' Set Index Start  = 1
        retval = DmisCommand.PutText ("C:\Users\QS\Desktop\MigrationsTools\test123.CSV", SUB_NAME, 1) <- Change CSV path here
        retval = DmisCommand.SetToggleString (2, OUTPUT_TYPE, 0) ' Set Auto Open  = ON
    
    DmisPart.RefreshPart
    


    CSV and PDF output can be combined in a single pcDMIS-print-command
    CSV and Excel output can not be combined, but aditional print commands maybe possible
  • I don't know what I'm doing wrong still but this code isn't doing what I want it to. PCDMIS is still prompting me to change the file name and it's not even starting with the value I gave it. It's just using what was there last time. It's adding the print commands but they don't work as expected. I'm kind of pulling my hair out over here.

    This C#:
    var reportPath = $@"{REPORTS_DIRECTORY_PATH}\{programData.Customer}\{programData.InternalPartName}-{programData.Revision}\{programData.RawMaterialId}\#{programData.UnitNumber}";
    
                var commands = _pcdPartProgram.Commands;
                commands.InsertionPointAfter(commands.LastCommand);
    
                var command = commands.Add(PRINT_REPORT, true);
                command.Marked = true;
    
                // Line for PDF-File output
                command.SetToggleString(2, PRINT_TO_FILE, 0);// Set Print To File  = ON
                command.PutText(reportPath, FILE_NAME, 1); // change PDF path here
                command.SetToggleString(2, FILE_COMMAND_TYPE, 0);// Set File Print Method  = OVERWRITE
                command.SetToggleString(1, ONOFF_TYPE, 0); // Set Auto Open  = OFF
    
                // Line for Printer output
                command.SetToggleString(1, PRINT_TO_PRINTER, 0);// Set Print To Printer  = OFF
                command.PutText("1", INDEX_END, 0); // Set Number Of Copies  = 1
                command.SetToggleString(1, PRINT_DELETE_RUNS, 0);// Set Previous Runs  = DELETE_INSTANCES
    
                // Line for *.dmo file output
                command.SetToggleString(1, OUTPUT_DMIS_REPORT, 0);// Set DMIS Output  = OFF
                command.SetToggleString(1, OVERWRITE, 0);  // Set Overwrite  = INDEX
                command.SetToggleString(1, OUTPUT_FEATURE_NOMS, 0); // Set Output Theoretical Values  = NONE
                command.SetToggleString(1, OUTPUT_FEAT_W_DIMENS, 0);// Set Output Features with Dimensions  = NO
                command.SetToggleString(2, MODE_TYPE, 0); // Set Mode  = END
    
                // Line for Excel / CSV file output
                // note that not all excel settings can be set via basic/C++
                command.SetToggleString(2, OUTPUT_TO_REPORT, 0);  // Set Output To Excel  = ON
                command.SetToggleString(3, UNIT_TYPE, 0); // Set File Print Method  = AUTO
                command.PutText($"{reportPath}.CSV", SUB_NAME, 1); // <- Change CSV path here
                command.SetToggleString(1, OUTPUT_TYPE, 0); // Set Auto Open  = ON
    
                _pcdPartProgram.RefreshPart();
                _pcdPartProgram.Save();
    


    Produces this program snippet:
    PRINT/REPORT,EXEC MODE=END,$
                  TO_FILE=ON,OVERWRITE=G:\My Drive\My Folder\Customer Inspections\Reports\Customer\9-03\RM-51\#1.PDF,AUTO OPEN=OFF,$
                  TO_PRINTER=OFF,COPIES=1,$
                  TO_DMIS_REPORT=OFF,FILE_OPTION=INDEX,FILENAME=[B]REPORT_NAME[/B],$
                  REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMENSIONS=NO,$
                  TO_EXCEL=ON,AUTO=1,FILENAME=G:\My Drive\My Folder\Customer Inspections\Reports\Customer\9-03\RM-51\#1.CSV,AUTO OPEN=OFF,$
                  PREVIOUS_RUNS=DELETE_INSTANCES
    

  • hi

    maybe the global print option is not toggled off?


    File -> Printing -> Report window print setup
    this setup is the same like the print command, but it will be fired after the execution of the measurement routine.
    check if it is switched on in your measurement program.

    this global print dialog can be altered too with an external program like this:
    but be careful, to this dialog all information are passed as parameters


    I think in your example it should simply be switched off because the print command from before already takes over the printing.
    means setting the first two parameters to 0
    Part.EditWindow.SetPrintOptionsEx 0, 0, 5, 1, vPath, 1, False
    



    docu is here for this SetPrintOptionsEx
    https://docs.hexagonmi.com/pcdmis/20...OptionsEx.html