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
  • It's because when you call a subroutine in a different program file, the script that runs in that file is referencing 'activepartprogram'.  The 'activepartprogram' is the program calling the script. If you use an external program file for subroutines, make sure you pass the file name as an argument to the subroutine and always explicitly set 'FN' as an argument passed to the script. Leaving it blank and letting it default, this routine will always set the variable equal to the calling program file name, ie. the subroutine program.

  • Thank you for your reply. Assuming my main program is called "Routine_list" and is located in the "C:prg" folder and the subroutines are located in the same folder and are called "Sub_A", "Sub_B", "Sub_C" etc. How should I edit the script?

  • Instead of setting part to equal 'activepartprogram', you can instead reference the 'partprograms' collection.

    ' Replace
    
    Set App = CreateObject("PCDLRN.Application")
    Set Part = App.ActivePartProgram
    
    If FN="" Then
        FN= Part.Path & Part.Name
    Else
        FN=FN
    End If
    
    FNOri=FN
    
    ' With 
    
    Set App = CreateObject("PCDLRN.Application")
    Set Part = App.PartPrograms(2)
    
    FN = Part.Path & Part.Name
    FNOri=FN
    

Reply Children