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.
  • Yeah, give me more details and I'll try knock something up for you.

    I love this sort of stuff!

    Do you have excel on the pc? If so what version?

    What's the command in 2) ?

    Add the commands (dimensions) in 4) you want in one program, then export is as .bas - view it in notepad then delete everything above the commands you add. Post it here.

  • Hi Ninja,
    Thanks for the help. Yes, I do have excel 2016. I am currently using PCDMIS 2016 but could install any version for this application. Can also have any other common package that might be needed. No limits there really. Visual Studio, Python, whatever...

    The command in #2 could either be a comment or could be an alignment. I would guess alignment is a bit easier to target since it has a specific name. Also might be able to target using UUID. My only concern with that is that some of these programs are really old and not sure if UUID changes when other commands are added after the fact. If not then UUID will probably work,

    Unfortunately I can’t post any of the actual code here. I will be able to modify anything that you can give me to work on my specific application. For the sake of demonstration just have it insert two location dimensions with XYZ fields.

    I am quite comfortable with scripting so if you are able to provide even provide a framework with chunks of code here and there that need to be integrated I can very likely get it working from there. Any help you can provide is much appreciated. Just too overloaded right now to put the needed attention to this to develop from scratch.

    I’ll post my final working version, minus any application specific data, here when I’m done.
  • Cool.

    So I assume all the programs in the directory for the same part (i.e. it is the same program run on different parts)?

  • Yes, same program run on 1000+ parts over the course of several years. Just need to pull out some additional data that was not included in the original reports. There have been some changes to the program over the years but nothing dramatic. Feature names are consistent, alignments are consistent, etc. I have done this process manually on about 25 parts and it works fine. I just can't manage doing it manually on 1000+ parts.
  • 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
    
    
  • Second part of code

    You need to export a modified program (with new commands added) and remove everything so it's just the commands you want to add in there

    Below I'm adding two Loc commands with X Z and D lines to them.

    In notepad (or similar) replace DmisCommand with cmd, then replace my code below with your code.



    Sub DoShitWithDmis(fname As String)
    
    Dim progpath As String
    
    progpath = mypath & "\" & fname
    
    Dim pcpart As PCDLRN.PartProgram
    
    
    Debug.Print progpath
    
    Set pcpart = pcapp.PartPrograms.Open(progpath, "Offline")
    
    
    
    
    
    Dim cmds As PCDLRN.Commands
    Dim cmd As PCDLRN.Command
    
    Set cmds = pcpart.Commands
    
    
    Dim deletecommandflag As Boolean
    deletecommandflag = False
    
    
    'loop through commands deleting once delete command flag has bees set to true
    
    For Each cmd In cmds
    
    If cmd.ID = cmdID Then
    deletecommandflag = True
    End If
    
    If deletecommandflag = True Then
    cmd.Remove
    End If
    
    
    
    Next
    
    Set cmd = cmds.LastCommand
    
    cmds.InsertionPointAfter cmd
    
    
    
    
    '>>>>>>>>>>>>>>>>> COMMANDS TO ADD EXPORTED FROM PC-DMIS HERE
    'VIEW THE .BAS IN NOTEPAD AND REPLACE (CTRL+H) 'DmisCommand' WITH 'cmd'
    
      Set cmd = cmds.Add(DIMENSION_START_LOCATION, True)
        cmd.Marked = True
      ' Set Id  = LOC1
        retval = cmd.PutText("LOC1", ID, 0)
      ' Set Reference Id  = CIR2
        retval = cmd.PutText("CIR2", REF_ID, 0)
      ' Set Graphic Analysis  = OFF
        retval = cmd.SetToggleString(1, GRAPH_ANALYSIS, 0)
      ' Set Textual Analysis  = OFF
        retval = cmd.SetToggleString(1, TEXT_ANALYSIS, 0)
      ' Set Arrow Multiplier  = 10.000000
        retval = cmd.PutText("10.000000", ARROW_MULTIPLIER, 0)
      ' Set Output Type  = BOTH
        retval = cmd.SetToggleString(3, OUTPUT_TYPE, 0)
      ' Set Unit Type  = MM
        retval = cmd.SetToggleString(2, UNIT_TYPE, 0)
      ' Set Standard Deviation  = 0.000000
        retval = cmd.PutText("0.000000", STANDARD_DEVIATION, 0)
      ' Set Half Angle  = NO
        retval = cmd.SetToggleString(1, DIM_HALF_ANGLE, 0)
    
      Set cmd = cmds.Add(DIMENSION_X_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = X
        retval = cmd.PutText("X", Axis, 0)
      ' Set Nominal  =    0.000
        retval = cmd.PutText("   0.000", Nominal, 0)
      ' Set Measured  =    0.000
        retval = cmd.PutText("   0.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =   13.492
        retval = cmd.PutText("  13.492", DIM_MAX, 0)
      ' Set Min  =  -13.492
        retval = cmd.PutText(" -13.492", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_Z_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = Z
        retval = cmd.PutText("Z", Axis, 0)
      ' Set Nominal  =    0.000
        retval = cmd.PutText("   0.000", Nominal, 0)
      ' Set Measured  =    0.000
        retval = cmd.PutText("   0.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =   -0.471
        retval = cmd.PutText("  -0.471", DIM_MAX, 0)
      ' Set Min  =  -13.500
        retval = cmd.PutText(" -13.500", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_D_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = D
        retval = cmd.PutText("D", Axis, 0)
      ' Set Nominal  =   27.000
        retval = cmd.PutText("  27.000", Nominal, 0)
      ' Set Measured  =   27.000
        retval = cmd.PutText("  27.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =    0.000
        retval = cmd.PutText("   0.000", DIM_MAX, 0)
      ' Set Min  =    0.000
        retval = cmd.PutText("   0.000", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_END_LOCATION, True)
        cmd.Marked = True
    
      Set cmd = cmds.Add(DIMENSION_START_LOCATION, True)
        cmd.Marked = True
      ' Set Id  = LOC2
        retval = cmd.PutText("LOC2", ID, 0)
      ' Set Reference Id  = CIR3
        retval = cmd.PutText("CIR3", REF_ID, 0)
      ' Set Graphic Analysis  = OFF
        retval = cmd.SetToggleString(1, GRAPH_ANALYSIS, 0)
      ' Set Textual Analysis  = OFF
        retval = cmd.SetToggleString(1, TEXT_ANALYSIS, 0)
      ' Set Arrow Multiplier  = 10.000000
        retval = cmd.PutText("10.000000", ARROW_MULTIPLIER, 0)
      ' Set Output Type  = BOTH
        retval = cmd.SetToggleString(3, OUTPUT_TYPE, 0)
      ' Set Unit Type  = MM
        retval = cmd.SetToggleString(2, UNIT_TYPE, 0)
      ' Set Standard Deviation  = 0.000000
        retval = cmd.PutText("0.000000", STANDARD_DEVIATION, 0)
      ' Set Half Angle  = NO
        retval = cmd.SetToggleString(1, DIM_HALF_ANGLE, 0)
    
      Set cmd = cmds.Add(DIMENSION_X_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = X
        retval = cmd.PutText("X", Axis, 0)
      ' Set Nominal  =    0.000
        retval = cmd.PutText("   0.000", Nominal, 0)
      ' Set Measured  =    0.000
        retval = cmd.PutText("   0.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =   13.492
        retval = cmd.PutText("  13.492", DIM_MAX, 0)
      ' Set Min  =  -13.492
        retval = cmd.PutText(" -13.492", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_Z_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = Z
        retval = cmd.PutText("Z", Axis, 0)
      ' Set Nominal  =    0.000
        retval = cmd.PutText("   0.000", Nominal, 0)
      ' Set Measured  =    0.000
        retval = cmd.PutText("   0.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =   -0.471
        retval = cmd.PutText("  -0.471", DIM_MAX, 0)
      ' Set Min  =  -13.500
        retval = cmd.PutText(" -13.500", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_D_LOCATION, True)
        cmd.Marked = True
      ' Set Axis  = D
        retval = cmd.PutText("D", Axis, 0)
      ' Set Nominal  =   27.000
        retval = cmd.PutText("  27.000", Nominal, 0)
      ' Set Measured  =   27.000
        retval = cmd.PutText("  27.000", DIM_MEASURED, 0)
      ' Set Plus Tolerance  =    0.051
        retval = cmd.PutText("   0.051", F_PLUS_TOL, 0)
      ' Set Minus Tolerance  =   -0.051
        retval = cmd.PutText("  -0.051", F_MINUS_TOL, 0)
      ' Set Max  =    0.000
        retval = cmd.PutText("   0.000", DIM_MAX, 0)
      ' Set Min  =    0.000
        retval = cmd.PutText("   0.000", DIM_MIN, 0)
      ' Set Deviation  =    0.000
        retval = cmd.PutText("   0.000", DIM_DEVIATION, 0)
      ' Set Out Tol  = 0
        retval = cmd.PutText("0", DIM_OUTTOL, 0)
    
      Set cmd = cmds.Add(DIMENSION_END_LOCATION, True)
        cmd.Marked = True
    
    '>>>>>>>>>>>>>>>>> COMMANDS TO ADD EXPORTED FROM PC-DMIS ABOVE HERE
    
    
    
    pcpart.SaveAs (mypath & "\Processed files\" & fname)
    
    
    'Shell ("C:\Program Files (x86)\WAI\PC-DMIS 2012 MR1 (Release)\Wizards\PCD2EXCEL.exe /A")
    
    Debug.Print fname & " : file processed"
    
    
    pcpart.Quit
    
    
    
    End Sub
    
    
    
  • Thanks very much. I’ll work through it and post my updates back here. For the pcd2excel issue I’ll probably put the script into a loop checking the presence of the process and when the process disappears it will continue.
  • Hi Ninja,

    I almost have this working. Only issue I am running into is the call to PCD2Excel. It works fine but it doesn't run automatically. It is as if the /A parameter is being ignored. The configure window pops up, I click "Continue", it does its thing, then I have to click "Finish." Did it work automatically when you tested your code?

    This is the code I'm using for that section (I have my PCD2Excel located in a non-standard directory):
    'Launch PCD2Excel Wizard to generate .CSV
      Shell ("C:\BladeRunner\PCD2EXCEL.exe /A")
    
      'Wait for process to end
        'Wait for process to end
      While IsExeRunning("PCD2Excel.exe")
       Sleep (5000)
       'MsgBox "PCD2Excel is running", 0, "Debug"
      Wend
  • Nevermind, I got it working. Just needed to change the code for calling PCD2Excel a bit:
    'Launch PCD2Excel Wizard to generate .CSV
    Dim strProgramName As String
    Dim strArgument As String
    strProgramName = "C:\BladeRunner\PCD2EXCEL.exe"
    strArgument = "A"
    Call Shell(strProgramName & " " & strArgument, vbNormalFocus)
  • Cool - yeah, I think the /A is for external command in Pc-Dmis - I just forgot args for shell / command line don't require it.