Script to modify a series of .PRG files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.
    2013MR1 SP6
    Global Frames, Tesastar-M Heads, LSP-X1M/H Probes

  • #2
    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.

    Automettech - Automated Metrology Technology

    Comment


    • #3
      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.
      2013MR1 SP6
      Global Frames, Tesastar-M Heads, LSP-X1M/H Probes

      Comment


      • #4
        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)?

        Automettech - Automated Metrology Technology

        Comment


        • #5
          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.
          2013MR1 SP6
          Global Frames, Tesastar-M Heads, LSP-X1M/H Probes

          Comment


          • #6
            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.

            Code:
            '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
            Last edited by NinjaBadger; 08-02-2019, 07:59 AM.
            Automettech - Automated Metrology Technology

            Comment


            • #7
              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.



              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
              Last edited by NinjaBadger; 08-02-2019, 07:47 AM.
              Automettech - Automated Metrology Technology

              Comment


              • Kp61dude!
                Kp61dude! commented
                Editing a comment
                I recommend Notepad++ if you don't already have it.

              • NinjaBadger
                NinjaBadger commented
                Editing a comment
                Kp61dude! - That was the 'or similar' I was referring to!

                I use xml files instead of txt files for storing data in my applications. I find find it invaluable too when viewing structured data.

                Also I have a compare plug in for it which makes me do a 5ex wee every time I use it!

            • #8
              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.
              2013MR1 SP6
              Global Frames, Tesastar-M Heads, LSP-X1M/H Probes

              Comment


              • #9
                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):
                Code:
                  '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
                2013MR1 SP6
                Global Frames, Tesastar-M Heads, LSP-X1M/H Probes

                Comment


                • DaSalo
                  DaSalo commented
                  Editing a comment
                  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)

                • AndersI
                  AndersI commented
                  Editing a comment
                  The parameter should be just A, not /A

              • #10
                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.
                Automettech - Automated Metrology Technology

                Comment


                • #11
                  Wow - this is some top-level work NinjaBadger - well done!

                  Comment


                  • NinjaBadger
                    NinjaBadger commented
                    Editing a comment
                    Thanks Ego Murphy - but like most stuff it's easy when you know how!

                • #12
                  Code:
                  '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
                  First day after vacation, so bear with me...
                  What if replacing the red marked text with (pseudocode):

                  Code:
                  SendKeys(SHIFT + CTRL + END) ' selects the text from deleteflag to program end
                  Wait(250) ' lets give it time to select the text
                  SendKeys(DELETE) ' delete the selected text
                  PC-DMIS CAD++ 2o19 R1 SP10

                  Comment

                  Related Topics

                  Collapse

                  Working...
                  X