hexagon logo

Script to modify a series of .PRG files

I have a need to automate the following process:

Loop through a set of .PRG files that are in a common directory. For each file do the following:
1) Open the file
2) Locate a specific command in the program
3) Delete all commands in the program after the command located in step 2
4) Add a set of dimension commands after the command identified in step 2
5) Launch the PCD2EXCEL wizard to output a .CSV file containing the data from the commands added in step 4
6) Close the file

Has anyone had to do something similar? Is anyone willing to share their code for any or all of these actions?

Thanks for any help.
Parents
  • Open excel
    Hit Alt+F11 to get to the vba editor
    Insert> Module

    Tools > References (select your version of pc-dmis object library)

    Copy the code below into the module

    Save the workbook as DaSalo(ver1).xlsm (assuming later versions of excel where you need .xlsm for macro workbooks)


    Place the path where the files are in cell A1
    Place the ID for the command you want to start removing from in A2 (This command is removed, it's easy enough if you want to make it delete all below this command)


    Notes and limitations...

    Before starting backup your original files, no liability accepted yada yada yada...

    1) I wouldn't run this on 1000 files at once. Start with 10 or so and work your way up
    2) Processed files are saved in a 'Processed Files' sub-directory - but you need to manually create this.

    3) I've commented out the Shell command which runs the Excel Wizard - the problem doing it this way is that this will get fired and then execution will continue (which will close the part program, leaving the Wizard nothing to work with.

    A solution might be to add an external command with 'wait' set as part of your PC-Dmis basic export 'template', and after adding commands execute the last command before quitting the program. The issue will be if you do this online the CMM will probably want to load the active probe etc. Off line this should work fine.

    
    'decalre top level object variables (pcdmis / file scripting / excel)
    
    Dim pcapp As PCDLRN.Application
    
    
    Dim oxl As Excel.Application
    
    
    
    'declare global variables (public/shared project wide)
    
    Dim mypath As String
    Dim cmdID As String
    
    
    
    Sub main()
    
    
    'get the path from cell A1
    
    Set oxl = CreateObject("Excel.Application")
    
    Dim wrkbooks As Excel.Workbooks
    
    Set wrkbooks = Excel.Application.Workbooks
    
    Dim wrkbook As Excel.Workbook
    
    For Each wrkbook In wrkbooks
    
    If wrkbook.Name = "DaSalo(ver1).xlsm" Then
    
    
    Dim wrksheet As Excel.Worksheet
    
    Set wrksheet = wrkbook.Worksheets(1)
    
    mypath = wrksheet.Range("A1")
    cmdID = wrksheet.Range("A2")
    
    Debug.Print mypath
    
    End If
    
    
    Next
    
    
    'open pc-dmis
    
    
    Set pcapp = CreateObject("pcdlrn.application")
    
    Dim fname As String
    
    
    fname = Dir(mypath & "\*.prg")
    
    
    Do While fname <> ""
    Debug.Print fname
    
    
    
    
    ''Call pc-dmis subroutine here
    
    DoShitWithDmis (fname)
    
    
    fname = Dir()
    Loop
    
    
    End Sub
    
    
Reply
  • Open excel
    Hit Alt+F11 to get to the vba editor
    Insert> Module

    Tools > References (select your version of pc-dmis object library)

    Copy the code below into the module

    Save the workbook as DaSalo(ver1).xlsm (assuming later versions of excel where you need .xlsm for macro workbooks)


    Place the path where the files are in cell A1
    Place the ID for the command you want to start removing from in A2 (This command is removed, it's easy enough if you want to make it delete all below this command)


    Notes and limitations...

    Before starting backup your original files, no liability accepted yada yada yada...

    1) I wouldn't run this on 1000 files at once. Start with 10 or so and work your way up
    2) Processed files are saved in a 'Processed Files' sub-directory - but you need to manually create this.

    3) I've commented out the Shell command which runs the Excel Wizard - the problem doing it this way is that this will get fired and then execution will continue (which will close the part program, leaving the Wizard nothing to work with.

    A solution might be to add an external command with 'wait' set as part of your PC-Dmis basic export 'template', and after adding commands execute the last command before quitting the program. The issue will be if you do this online the CMM will probably want to load the active probe etc. Off line this should work fine.

    
    'decalre top level object variables (pcdmis / file scripting / excel)
    
    Dim pcapp As PCDLRN.Application
    
    
    Dim oxl As Excel.Application
    
    
    
    'declare global variables (public/shared project wide)
    
    Dim mypath As String
    Dim cmdID As String
    
    
    
    Sub main()
    
    
    'get the path from cell A1
    
    Set oxl = CreateObject("Excel.Application")
    
    Dim wrkbooks As Excel.Workbooks
    
    Set wrkbooks = Excel.Application.Workbooks
    
    Dim wrkbook As Excel.Workbook
    
    For Each wrkbook In wrkbooks
    
    If wrkbook.Name = "DaSalo(ver1).xlsm" Then
    
    
    Dim wrksheet As Excel.Worksheet
    
    Set wrksheet = wrkbook.Worksheets(1)
    
    mypath = wrksheet.Range("A1")
    cmdID = wrksheet.Range("A2")
    
    Debug.Print mypath
    
    End If
    
    
    Next
    
    
    'open pc-dmis
    
    
    Set pcapp = CreateObject("pcdlrn.application")
    
    Dim fname As String
    
    
    fname = Dir(mypath & "\*.prg")
    
    
    Do While fname <> ""
    Debug.Print fname
    
    
    
    
    ''Call pc-dmis subroutine here
    
    DoShitWithDmis (fname)
    
    
    fname = Dir()
    Loop
    
    
    End Sub
    
    
Children
No Data