hexagon logo

help, auto save .prg after run, in a specific location variable.

I have been looking through all the past topics about this. but I am having trouble getting them working. I tried copy and pasting a few of them. and saving them as a .bas file and running the script. but i cant get one to work for me.


i currently already have a script that auto creates a folder from an operator input and saves the cmm report in that folder.

so ideally i want to follow that same train of thought and save a .prg & .cad in that same folder already created.


please advice.

  • Credit goes to pcdmisforum.com username vpt.se

    Hopefully your CMM computer has the correct permission settings to run BAS scripts.

    1. At beginning of program:
    'TOTALLY FREE OPEN SOURCE CODE NO TRADEMARK OR COPYRIGHT WHATSOEVER
    'NO WARRANTY OR GUARANTEE IMPLIED OR SPECIFIED
    'WORKS WITH WITH PC-DMIS SOFTWARE
    
    ' Displays an inputbox telling the user To enter a serialnumber
    ' Or other information that will be concatenated To the partprogram
    ' Part Name from program header And saved In the current partprogram folder.
    ' If the file already exist, the user will be prompted and told
    ' to enter a new serial.
    '
    ' vpt.se 2011
    
    '1) Requires variable assignment for SERNO to be in PC-DMIS program
    
    '2) Insert at beginning of program.
    
    '3) Additionally, use companion "Auto Save" script at end of program.
    
    '4) For PCDMIS v2011 MR1 and up, Registy Setting "DocumentRecovery" needs to be set to False
    
    '--------------------------------------------------------------------------
    
    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Dim Cmds As Object
    Dim Cmd As Object
    Set Cmds = PCDPartProgram.Commands
    
    ser$ = InputBox$("Enter serial number:", "Serial", "", 200, 175)
    If ser$ <> "" Then
      newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & ser$ & ".PRG"
      retval = PCDPartProgram.SaveAs(newname)
    End If
    
    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + ser$+ """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub
    


    and

    2. At end of program:
    'TOTALLY FREE OPEN SOURCE CODE NO TRADEMARK OR COPYRIGHT WHATSOEVER
    'NO WARRANTY OR GUARANTEE IMPLIED OR SPECIFIED
    'WORKS WITH WITH PC-DMIS SOFTWARE
    
    '     1) Insert at end of program.
    
    '     2) Additionally, use companion "Auto Save With Serial Number" script at beginning of program.
    
    
    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram 
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    
      retval = PCDPartProgram.Save
    
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub
    

  • I wouldn't recommend saving the .CAD file though, as you will quickly fill up hard drive or network space that way. The program files are fairly small so just save them. make sure that you take note within the program which version of CAD was used and if and when you need it you can add the cad back in very easily.
  • Here's mine, feel free to use.

    Note - it doesn't save the CAD (if it did my archive would be MASSIVE)

    If I want to review the program with CAD in it I simply import the .CAD file from the original program

    '
    JON WOOD - AUTOMETTECH LTD (UK) www.automettech.com
    
    Sub main()
    
    
    '========= SETTINGS =================
    
    'Root save directory For archive
    Dim archivePath
    archivePath = "C:\CMM\Archive\AutoArchive"
    
    'This is the Name of a PC-Dmis ASSIGNMENT - If it exists the value it holds will be used In the file Name
    'of the archived program (i.e. can be a serial number, Or works order number etc)
    Dim myUID
    myUID = "UID"
    
    'NOTE! - File Name will be In the following format...
    'SaveDirectory\Program Name\ProgramName_UniqueID_DateTime.prg
    
    'i.e. C:\CMM Files\Part Program Run Instances Archive\123456-01\123456-01_abc123_20140824104232.prg
    
    'Where...
    'archivePath = "C:\CMM Files\Part Program Run Instances Archive"
    'Part program = 123456-01.prg
    'Serial Number / Unique Id = abc123
    'DateTime = 24/08/2014 10:42:32
    
    
    '===========End OF SETTINGS==========
    
    'Create objects
    Dim pcapp As Object
    Set pcapp = createobject("pcdlrn.application")
    
    Dim pcpart As Object
    Set pcpart = pcapp.activepartprogram
    
    'Save existing program
    pcpart.save
    
    'Get the current program path
    Dim source_path
    source_path = pcpart.fullname
    
    'Get program Name (without file extension)
    Dim progname
    progname = left(pcpart.Name,len(pcpart.Name)-4)
    
    'Create File System Object For file operations 
    Dim fso As Object
    Set fso = createobject("scripting.filesystemobject")
    
    'Check main archive directory And program specific directories exist
    Dim ofolder As Object
    
    If Not fso.folderexists(archivepath) Then
    Set ofolder = fso.createfolder(archivepath)
    End If
    
    If Not fso.folderexists(archivepath & "\" & progname) Then
    Set ofolder = fso.createfolder(archivepath & "\" & progname)
    End If
    
    'Get a UID If present
    Dim uid
    uid=""
    
    Dim myvar As Object
    Set myvar = pcpart.getvariablevalue(myUID)
    
    If Not myVar is Nothing Then
    uid = myvar.stringvalue
    'MsgBox(uid)
    End If
    
    'Genrate the destination (save) path
    dest_path = archivepath & "\" & progname & "\" & progname & "_" & uid & "_" &  format(now(),"YYYYMMDDHHNNSS") & ".prg"
    'MsgBox(dest_path)
    
    
    'Save the file
    fso.copyfile source_path, dest_path
    
    
    'Tidy up
    Set fso = Nothing
    Set pcpart = Nothing
    Set pcapp = Nothing
    
    
    End Sub
  • And mine, from the old thread - http://www.pcdmisforum.com/forum/pc-dmis-enterprise-metrology-software/pc-dmis-code-samples/28447-saving-pcdmis-file-at-end-of-program?p=301873#post301873

    What version of PC-DMIS are you running? 32- or 64-bit? Do note that 64 bit PC-DMIS versions has had quite a lot of bugs in the BASIC department, among other things my script didn't work. I believe those BASIC bugs are fixed now, but I don't remember exactly from which version...

    My script definitely works in 2016.0 SP7 (I don't have anything older immediately at hand).
  • Thanks for the credits, it's rare thing in our times.
  • I have everything set up now., after the program is complete it will create a SN-(folder) with comment input. Then it dumps the CMM report in that folder, along with .prg & .CAD(for now) and creates an .IGES file of all the measure data. The script has me assign which alignment to use for creating the .iges file, and the first and last feature. And creates a .MEA file of scan files.


    So excited i got everything completed this week.
  • If possible could you share the completed code so others can learn from it?
  • I'd like to echo Zeros' request... I've been trying to accomplish the folder creation aspect as well as auto saving the program and data in said folder. So far I've had little success, so if you wouldn't mind sharing the completed code that would be fantastic Slight smile
  • By any chance would anyone else be able to offer a suggestion as to what I'm doing wrong here? I'm attempting to check for an existing folder in Q:\Quality\In Process Inspection Plans\11127-MACHINE\CMM REPORTS for a folder named for the Job Number. If the folder exists, I'm wanting to save both the readout and the program in this folder with the serial number for the part attached to the end of each to differentiate each one from the readout and program saved for the other parts with differing serial number but are under the same job(work order).
    I had the code working to save the program in the CMM REPORTS folder, but when I attempted to modify it to put it in the folder that was created using the Job number, it gave me an "OLE Automation method exception" error on the line beginning with "newname". I'm not sure if I'm not setting the file path up correctly or what exactly I'm doing wrong, but any help would be appreciated...

    [SIZE=14px][FONT=times new roman][COLOR=#0000ff][COLOR=#0000ff]Sub[/COLOR][/COLOR] Main()
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] PCDApp,PCDPartProgram, PCDCommands, PCDCommand, retval
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] objFSO
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] objFSO = CreateObject ("Scripting.FileSystemObject")
    
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDApp = CreateObject("PCDLRN.Application")
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDPartPrograms = PCDApp.PartPrograms
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDPartProgram = PCDApp.ActivePartProgram
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] Cmds [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Object[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] Cmd [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Object[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] Cmds = PCDPartProgram.Commands
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] setCrntName [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]String[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] PartNo [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Object[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] JobNo [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Object[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] SerNo [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Object[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] SerialNo [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]String[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] Results [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]String[/COLOR][/COLOR]
    
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PartNo = PCDPartProgram.GetVariableValue ("V5")
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] JobNo = PCDPartProgram.GetVariableValue ("V1")
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] SerNo = PCDPartProgram.GetVariableValue ("V2")
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] Serial = PCDPartProgram.GetVariableValue ("V2")
    
    strPath = "Q:\Quality\In Process Inspection Plans\PartNo.StringValue\CMM REPORTS\"
    strFolderName = JobNo.StringValue & "_" & SerNo.StringValue
    strFolder = strPath & "\RESULTS\" & strFolderName
    
    [COLOR=#0000ff][COLOR=#0000ff]If[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Not[/COLOR][/COLOR] objFSO.FolderExists(strFolder) [COLOR=#0000ff][COLOR=#0000ff]Then[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] objFolder = objFSO.CreateFolder(strFolder)
    
    [COLOR=#0000ff][COLOR=#0000ff]End[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]If[/COLOR][/COLOR]
    
    SerialNo = Serial.StringValue
    setCrntName = PCDPartProgram.FullName
    newname = "Q:\Quality\In Process Inspection Plans\" & PartNo.StringValue & "\CMM REPORTS\ObjFolder\" & PCDPartProgram.PartName & " - " & SerialNo & ".PRG"
    
    retval = PCDPartProgram.SaveAs(newname)
    retval = PCDPartProgram.SaveAs(setCrntName)
    
    [COLOR=#007f00][COLOR=#007f00]' Cleanup [/COLOR][/COLOR]
    
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDPartProgram = [COLOR=#0000ff][COLOR=#0000ff]Nothing[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDPartPrograms = [COLOR=#0000ff][COLOR=#0000ff]Nothing[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]Set[/COLOR][/COLOR] PCDApp = [COLOR=#0000ff][COLOR=#0000ff]Nothing[/COLOR][/COLOR]
    [COLOR=#0000ff][COLOR=#0000ff]End[/COLOR][/COLOR] [COLOR=#0000ff][COLOR=#0000ff]Sub[/COLOR][/COLOR][/FONT][/SIZE]