hexagon logo

Saving Subroutine Math with External Script

Good morning,

I hope someone can help me. First of all I apologize for my English, but I'm using google translator.

My question is:   I have an external script in Basic that, if called at the end of a measurement routine, is used to save the mathematics of the prg it has just run.

The script in question is as follows

Option Explicit

Dim App As Object
Dim PP As Object

Sub Main (FN As String,Suffix As String)
  Dim retval As Boolean
  Dim FNOri As String

  Set App = CreateObject("PCDLRN.Application")
  Set Part = App.ActivePartProgram

  If FN="" Then
      FN= Part.Path & Part.Name
  Else
      FN=FN
  End If

  FNOri=FN

  If Suffix="" Then 
      FN=FN
  Else
      If ucase(right(FN,4))=".PRG" Then 
         FN=left(FN,len(FN)-4) & "_" & Suffix & ".prg"
      Else
         FN=FN & "_" & Suffix & ".prg"
      End If
  End If

  retval= Part.SaveAs (FN)
  If FN<>FNOri Then
     retval= Part.SaveAs (FNOri)
  End If

End Sub

The script works great if I put it at the end of every single measurement routine.

The problem arose when I wanted to create a main measurement routine that was intended to invoke other subroutines. I prepared each subroutine so that it was enclosed by the create and end subroutine commands (giving it a name). And I put in each subroutine, before the end of subroutine command, the aforementioned script. In the main measurement routine I have only included the recall of the necessary subroutines. Now, when I run the main routine, everything works as expected except for the script, which instead of saving me the math of the subroutines that have run, saves me the math of the main routine (which is actually just a subroutine container). What I guess is that surely the script was created to save the math of the main routine. Since I don't know the Basic language, I don't know how to modify it so that it saves the math of the invoked subroutines. I have also tried using AI such as Copilot and ChatGPT but with poor results. Is there anyone in the forum who knows the Basic language who can help me modify the script so that it saves the math of the invoked subroutines?

Parents
  • I have no experience with subroutines, but I will take a guess here. If it is saving the math from the original program that the subroutine is created from.. I believe that the variables "FN" / "FNOri" are pulling the active program name from that original program, and that's why it keeps saving the math from that subroutine. I do not know how subroutines are called. Do you truly need it to be a subroutine? Or can you just continue putting your script at the end of your programs?

  • Thank you for replying. To explain myself better, I can say that I was asked by my superiors to make sure that CMM operators could create a list of routines to be run in unmanned work shifts. I immediately thought of using a free supervisor (EasyRun) that can manage the lists of routines to be run, but it doesn't always work properly (sometimes between one routine and another it freezes without giving any error message). I have seen that there is also Inspect, but for the management of lists it requires the purchase of a license (at the moment the company where I work cannot incur expenses unless strictly necessary). So I tried to create a program called "routine_list" where CMM operators can choose to run the subroutine called "Sub_A", the one called "Sub_B", the "Sub_C" and so on. As a final result I should find a pdf file with the measurement report and a prg file with the mathematics of the measured part. Answering your question: yes I need them to be subroutines. Could you kindly explain to me better what you were recommending about the "FN"/"FNOri" variables?

  • Sub Main (FN As String,Suffix As String)
      Dim retval As Boolean
      Dim FNOri As String
    
      Set App = CreateObject("PCDLRN.Application")
      Set Part = App.ActivePartProgram
    
      If FN="" Then
          FN= Part.Path & Part.Name

    From this snippet of your code above. FN = The Active Part Program in the PC-DMIS App. That variable is being pulled, and stored from your subroutine's base program, not your actual part program, so you will need to declare the variable FN.
    Since it has an IF statement which is IF "FN" = empty string or ""

    See the reply below me too. Dismas is good with this kind of stuff.

    If you want to split a program into 3 options, you can always an input type comment to send them to the correct portion of the code 

Reply
  • Sub Main (FN As String,Suffix As String)
      Dim retval As Boolean
      Dim FNOri As String
    
      Set App = CreateObject("PCDLRN.Application")
      Set Part = App.ActivePartProgram
    
      If FN="" Then
          FN= Part.Path & Part.Name

    From this snippet of your code above. FN = The Active Part Program in the PC-DMIS App. That variable is being pulled, and stored from your subroutine's base program, not your actual part program, so you will need to declare the variable FN.
    Since it has an IF statement which is IF "FN" = empty string or ""

    See the reply below me too. Dismas is good with this kind of stuff.

    If you want to split a program into 3 options, you can always an input type comment to send them to the correct portion of the code 

Children
No Data