hexagon logo

How can I save past runs as individual files

Hello,

I am relatively new to CMM programing. I was wondering if there was any way to set up a system where when a measurement routine ends, the program automatically saves the routine with the measurement points for that specific serial number to a set file directory. It's been a hassle for when I forget to print out a report at the end of a routine, go to run the next part and I lose my information for the previous one forever. I would like to have it set up as to where, when a measurement routine ends, the program asks me what i could like to name the file, and where to save it. Would someone with more knowledge of the program be able to assist me with some code that I can put in my programs so that this can be done, please?

Thanks
  • If you want to save a copy of the program itself you can use a basic script to save the current measuring routine, then save a copy of the measuring routine in some sort of archive folder.

    shared a nice script for that a while back. Here is a link to that post: https://www.pcdmisforum.com/forum/pc-dmis-enterprise-metrology-software/pc-dmis-code-samples/390021-auto-archive-freebie

    I created some of my own scripts based on what he shared to save copies of the measuring routine each time we measure a part where I work. This one below can be called at the end of a measuring routine and it will automatically look in the program for operator inputs such as Operator Initials or Serial Number. It will then prompt the operator if they want to save the measuring routine and save a copy of the measuring routine in a separate folder in the same directory as the measuring routine is being executed from. If the operator clicks OK, It will make a folder with the same name as the measuring routine file name with an '(Archive)' suffix. A copy of the .prg and .cad file will be put in that folder with suffixes added to the file names. The suffixes will be the operator inputs separated by dashes "-" and the date/time the file was created. Those files are then made to be 'read-only' so the data is not lost if someone executes one of the archived programs.

    ' Save_Copy_And_Write_Protect
    '
    ' By Cris_C 9-12-2019
    '
    ' Credit to NinjaBadger for the original program from which this was created.
    ' Post Link: https://www.pcdmisforum.com/forum/pc-dmis-enterprise-metrology-software/pc-dmis-code-samples/390021-auto-archive-freebie
    '
    ' This Basic Script it to be called at the end of a measuring routine.
    ' It Saves the current measuring routine and then copies the .prg and the .cad file
    ' to a folder in the same directory as the measuring routine. The folder has the same
    ' name as the measuring routine .prg name with an "(Archive)" suffix.
    ' The copies are given a suffix made from operator inputs in the program (such as Serial number) and
    ' The date/time the copy was made.
    ' The copy of the .prg file and .cad file are then made Read-Only so data is not lost if someone tries
    ' to re-execute an archived program.
    
    Sub main()
    
    'Create objects------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Dim pcapp As Object
    Set pcapp = createobject("pcdlrn.application")
    Dim pcpart As Object
    Set pcpart = pcapp.activepartprogram
    Dim Cmd As Object
    Dim Cmds As Object
    Dim ComCmd As Object
    Set Cmds = pcpart.Commands
    
    'Figure out where To copy To/from-----------------------------------------------------------------------------------------------------------------------------------------------------------
    '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    'Get the current prg And cad file path
    Dim prg_source_path
    Dim cad_source_path
    prg_source_path = pcpart.fullname
    cad_source_path = left(prg_source_path,len(prg_source_path)-4) & ".cad"
    
    'Get program Name (without file extension)
    Dim progname
    progname = left(pcpart.Name,len(pcpart.Name)-4)
    
    'Define the Archive Path
    Dim archivepath
    archivepath = left(pcpart.FullName,len(pcpart.FullName)-len(pcpart.Name)) & progname & "(Archive)"
    
    'Get Additional Information from the CMM Program User inputs. --------------------------------------------------------------------------------------------------------------------
    '---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    'Go Through Each of the Commands In the program And Find all the comments. If a comment is an Input comment Then Get the Object And retrieve the user Input.
    'take the user Input And concatinate it into one variable. Essentially making a chain of all the user inputs In the order they appear In the program.
    Dim userinputs
    For Each Cmd In Cmds
    If Cmd.IsComment Then
    Set ComCmd = Cmd.CommentCommand
    If ComCmd.CommentType = 2 Then
    userinputs = userinputs & ComCmd.Input & "-"
    End If
    End If
    Next Cmd
    
    'Replace Any characters that will mess up windows If you use them In a file Name. Also some that would be fine but I just don't like To see In a file Name.
    Dim revisedinputs
    For count=1 To Len(userinputs)
    If mid(userinputs,count,1) = "/" Or mid(userinputs,count,1) = "\" Or mid(userinputs,count,1) = "<" Or mid(userinputs,count,1) = ">" Or mid(userinputs,count,1) = ":" Or mid(userinputs,count,1) = "." Or _
    mid(userinputs,count,1) = ";" Or mid(userinputs,count,1) = "?" Or mid(userinputs,count,1) = "*" Or mid(userinputs,count,1) = "|" Or mid(userinputs,count,1) = "," Or mid(userinputs,count,1) = """" Then
    newchar = "#"
    Else
    newchar = mid(userinputs,count,1)
    End If
    revisedinputs = revisedinputs & newchar
    Next
    
    'Make sure the inputs are Not too Long For a file Name, Limit it To an arbitrary 30 characters. It leaves lots of space For Long CMM Program names And date And time.
    revisedinputs = left(revisedinputs,30)
    
    'Complete the saving Name/path And Get ready To propt the user -----------------------------------------------------------------------------------------------------------------
    '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    'Genrate the destination (save) paths
    prg_dest_path = archivepath & "\" & progname & "(" & revisedinputs & ")_" & format(now(),"YYMMDDHHNNSS") & ".prg"
    cad_dest_path = archivepath & "\" & progname & "(" & revisedinputs & ")_" & format(now(),"YYMMDDHHNNSS") & ".cad"
    
    'Create the Long String of text that will be used In the OK/Cancel Message box
    Dim Msg
    Msg = "The CMM measuring is complete." & chr(13)
    Msg = Msg & chr(13)
    Msg = Msg & "Would you like to save the current CMM program and also create a backup with the following name and location?" & chr(13)
    Msg = Msg & chr(13)
    Msg = Msg & prg_dest_path & chr(13)
    Msg = Msg & chr(13)
    Msg = Msg & "Click OK to save CMM program and make a backup." & chr(13)
    Msg = Msg & "Click CANCEL to skip the saving/backup operation." & chr(13)
    
    'Create title For mesasge box
    MsgTitle = "CMM Program Backup Utility"
    
    
    'Create File System Object For file operations
    'Note: This Object is Not documented In the PC-DMIS help file. More details On it can be found On the Microsoft wesite. Try this link:
    'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filesystemobject-object
    Dim fso As Object
    Dim fo As Object
    Set fso = createobject("scripting.filesystemobject")
    
    'Display the OK/Cancel Message box And save/copy If user clicks OK. If the user clicks OK it will return a 1. -----------------------------------------------------------
    '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    If MsgBox(Msg,1,MsgTitle) = 1 Then
    'Save existing program
    pcpart.save
    'Check If the destination folder exists, If Not Then create one
    If Not fso.folderexists(archivepath) Then
    Set ofolder = fso.createfolder(archivepath)
    End If
    'Copy the program And the cad file
    fso.copyfile prg_source_path, prg_dest_path
    fso.copyfile cad_source_path, cad_dest_path
    'Create a file Object pointing To the coppied program And cad file. Then make the program Read-Only.
    Set fo = fso.GetFile(prg_dest_path)
    fo.Attributes = 1
    Set fo = fso.GetFile(cad_dest_path)
    fo.Attributes = 1
    End If
    
    'Tidy up -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    Set fso = Nothing
    Set pcpart = Nothing
    Set pcapp = Nothing
    Set Cmd = Nothing
    Set Cmds = Nothing
    Set ComCmd = Nothing
    
    End Sub
    
    


    I am trying to insert the script into my program. I start to type script and it gives me this line of code:
    CS1 =SCRIPT/FILENAME= C:\USERS\CMM\DESKTOP\AUTOARCHIVE.BAS
    FUNCTION/Main,SHOW=YES,,,
    STARTSCRIPT/


    I noticed that in ninja badger's code he has "ENDSCRIPT/" inserted after his start script but when I try and type "ENDSCRIPT" nothing pops up. Am I doing something wrong here? When I try and run my program, the file doesn't get routed to the selected folder I have in the script unless I press F9 on the line of code and hit the run button to the far right of the screen. Is there a way that I can insert this script code into a program and have it run automatically and send it to my requested directory without having to press F9 to run it each time?


  • I am trying to insert the script into my program. I start to type script and it gives me this line of code:
    CS1 =SCRIPT/FILENAME= C:\USERS\CMM\DESKTOP\AUTOARCHIVE.BAS
    FUNCTION/Main,SHOW=YES,,,
    STARTSCRIPT/
    Hmmm. This is the same way it appears when I use the script. I don't get an ENDSCRIPT/ line in my edit window. I don't know why NinjaBadger's example has that. At any rate, it still executes properly for me. It might have been something that has changed in the newer software versions.


    I noticed that in ninja badger's code he has "ENDSCRIPT/" inserted after his start script but when I try and type "ENDSCRIPT" nothing pops up. Am I doing something wrong here? When I try and run my program, the file doesn't get routed to the selected folder I have in the script unless I press F9 on the line of code and hit the run button to the far right of the screen. Is there a way that I can insert this script code into a program and have it run automatically and send it to my requested directory without having to press F9 to run it each time?
    I don't know why it wouldn't run automatically when that command is executed in the measuring routine. The only thing I can think of is that when you insert a Basic Script command, PC-DMIS will automatically un-mark it (Skip it during execution). Make sure you have the command Marked. You can toggle it by putting the cursor on the command and hit F3.


    Try that

  • Try that


    Awesome, yes this worked for me, thanks!. But it created a new problem lol. I'm now getting an error code that says "error on line:81 - OLE Automation method exception" Do you have any clue what this means or how I can fix it? The line in question would be:
    'Save the file
    fso.copyfile source_path, dest_path
  • Hmmm odd. What's your program name? Does it contain characters not allowed in file names \?/! etc?
  • I am having a similar problem. I would like to save .pdf files created from reports to network folders. Recently we updated to pc-dmis 2020 r2, and we are using windows 10. Old programs work, but new programs give me a popup asking me where I would like to save the file. The code at the top of the program looks like this:

    PART NAME : 0035_6
    REV NUMBER : C
    SER NUMBER :
    STATS COUNT : 22

    STARTUP =ALIGNMENT/START,RECALL:USE_PART_SETUP,LIST=YES
    ALIGNMENT/END
    STATS/ON,DATAPAGE,$
    DIRECTORY=,$
    READ=0,WRITE=0,MEMPAGES=0,FEATURE NAME,CONTROLCALC ON,$
    STATS/END
    FORMAT/TEXT,OPTIONS,ID,HEADINGS,SYMBOLS, ;NOM,TOL,MEAS,DEV,OUTTOL, ,
    $$ NO,
    .
    .
    PROGRAMER : DARRIN S
    .
    PROVEOUT DATE :
    PROVEOUT SIGNOFF :
    .
    .
    PROGRAM NOTES / REVISIONS :
    .
    .LAST EDITED BY DARRIN S. 6/9/2021

    C1 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'MO #'
    C2 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'EMPLOYEE #'
    C3 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'WORK CENTER #'
    C4 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'WORK OFFSET #'
    C5 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'MO STATUS'
    C6 =COMMENT/INPUT,YES,FULL SCREEN=NO,
    'INSPECTION GAGE #'
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=MO # ; MO # : C1.INPUT
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=EMPLOYEE # ; EMPLOYEE # : C2.INPUT
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=WORK CENTER # ; WORK CENTER # : C3.INPUT
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=OFFSET # ; OFFSET # : C4.INPUT
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=MO STATUS ; MO STATUS : C5.INPUT
    TRACEFIELD/DISPLAY=NO,REPORT=NO,DISPLAY MESSAGE=INSPECTION GAGE # ; INSPECTION GAGE # : C6.INPUT

    Then the save to network code at the bottom of the program looks like this:

    ASSIGN/PART_NUMBER="0035"
    ASSIGN/JOB_NUMBER=C1.INPUT
    ASSIGN/FILESTORELOC="Q:\\APM\\Quality\\Public\\CMM REPORTS\\CURRENT YEAR\\BRAVO\\0035\"
    ASSIGN/FILENAME=PART_NUMBER+"_"+JOB_NUMBER+"_"+SYSTEMDATE ("MMMM dd,yyyy")+"_"+SYSTEMTIME("hh:mm:ss")+".PDF"
    ASSIGN/VAR_FILE=FILESTORELOC+FILENAME
    PRINT/REPORT,EXEC MODE=END,$
    TO_FILE=ON,APPEND=VAR_FILE.PDF,AUTO OPEN=OFF,$
    TO_PRINTER=OFF,COPIES=1,$
    TO_DMIS_REPORT=OFF,FILE_OPTION=OVERWRITE,FILENAME= VAR_FILE,$
    REPORT_THEORETICALS=NONE,REPORT_FEATURE_WITH_DIMEN SIONS=NO,$
    TO_EXCEL=OFF,$
    PREVIOUS_RUNS=DELETE_INSTANCES
    STATS/TRANSFER,DIRECTORY=C:\SPCDATA
    ROUTINE/END

    I even tried using code from old programs to send the new report to the wrong folder, but I still get the popup. JOB_NUMBER comes from the C1 comment. I know they are different, but that still works on old programs. What am I missing?
  • It's a window update issue which has affected PDF printing.

    Best solution is to run a repair on the PC-Dmis install - it will redo the pdf drivers. Make sure you run the repair/install with Admin rights.
  • NinjaBadger,
    I have an issue with your archive script not working with file names using Parenthesis the other day. I have not had a chance to look into why yet. Here is a file name example: 7PP00100 (Casting). Thank you for creating this script.
  • Oh! I will ask our IT department for help. Thank you!