hexagon logo

Using input comment from program in basic script

I'm trying to use a script I pulled from the forum to autosave my program to a different location, but I can't figure out how to pull the serial number that's input at the beginning of the program into the script to use in the new program name. Does anyone have any insight on this issue?

Edit: Below is a copy of the code I'm trying to use. I'm getting the error "ole automation object does not have a default value" on line 12. I do not have much scripting experience, so I've been piecing together things from the forum.

Sub Main()
Dim PCDApp, PCDPartPrograms, PCDPartProgram

Set PCDApp = CreateObject("PCDLRN.Application")
Set PCDPartPrograms = PCDApp.PartPrograms
Set PCDPartProgram = PCDApp.ActivePartProgram
Dim Cmds As Object
Dim Cmd As Object
Set Cmds = PCDPartProgram.Commands

Dim sernum As String	
Set sernum = PCDPartProgram.GetVariableValue("V1")
If sernum <> "" Then
  newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & sernum & ".PRG"
  retval = PCDPartProgram.SaveAs(newname)
End If

For Each Cmd In Cmds
If Cmd.Type = ASSIGNMENT Then
   If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
      bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
      Cmd.ReDraw
   End If
End If
Next Cmd

' Cleanup
Set PCDPartProgram = Nothing
Set PCDPartPrograms = Nothing
Set PCDApp = Nothing
End Sub

Parents
  • You can refer to this script below that I made for another user yesterday, and see if this will help you. Let me know if you need help.

    Dim <your variable> As String for the Serial

    Set <your variable> = DmisPart.SerialNumber

    Then append it to the filepath/filename  "D:\temp\" & PartName & "_" & PartSerial & "_" & currentDateTime & ".xml"

    There is also a method of just DmisPart.Name that copies the exact program file name if you want the entire name to be the same, but I believe it also includes the .PRG at the end. Basically the name on the top left of the edit window is what gets used.

    Sub Main
    
    Dim DmisApp As Object
    Dim DmisPart As Object  
    Dim PartSerial As String
    Dim PartName As String
    
    Set DmisApp = CreateObject("PCDLRN.Application")
    Set DmisPart = DmisApp.ActivePartProgram
    Set PartSerial = DmisPart.SerialNumber
    Set PartName = DmisPart.PartName
    
    Dim currentDateTime
        currentDateTime = Format(Now, "yyyyMMdd_HHmmss")
    
    Dim exportPath As String
        exportPath  = "D:\temp\" & PartName & "_" & PartSerial & "_" & currentDateTime & ".xml"
    
    DmisPart.ExportToXML ExportPath

    For an actual comment input to be pulled from your program

    'In the Edit Window code
    C1         =COMMENT/INPUT,NO,FULL SCREEN=NO,
                Enter Serial Number
                ASSIGN/V1=C1.INPUT
    
    'Basic Script portion
    Dim DmisApp As Object
    Set DmisApp = CreateObject("PCDLRN.Application")
    Dim DmisPart As Object
    Set DmisPart = DmisApp.ActivePartProgram
    Dim PartSerial As String
    Set PartSerial = DmisPart.GetVariableValue("V1")
    
    Dim SavePath As String
        SavePath = "D:\YourPath\" & "Filename" & PartSerial & "

  • I'm getting closer to it working. Here is an example of the script I'm using. I'm getting the error "ole automation object does not have a default value" on line 12. Any ideas? I'm very new to scripting and have been basically piecing together forum scripts to get what I need.

    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Dim Cmds As Object
    Dim Cmd As Object
    Set Cmds = PCDPartProgram.Commands
    
    Dim sernum As String	
    Set sernum = PCDPartProgram.GetVariableValue("V1")
    If sernum <> "" Then
      newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & sernum & ".PRG"
      retval = PCDPartProgram.SaveAs(newname)
    End If
    
    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub

  • You are dimensioning sernum as string, however "PCDPartProgram.GetVariableValue("V1")" does not return a string. It returns an object of type "variable". You need to dimension sernum as object. 

    ex.

    Dim sernum as Object
    Set sernum = PCDPartProgram.GetVariableValue("V1")
    If sernum.StringValue <> "" Then
        '
        '
    End If

  • I'm now getting the same error, but on line 14

    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Dim Cmds As Object
    Dim Cmd As Object
    Set Cmds = PCDPartProgram.Commands
    
    Dim sernum As Object  
    Set sernum = PCDPartProgram.GetVariableValue("V1")
    If sernum.StringValue <> "" Then  
    newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & sernum & ".PRG"
      retval = PCDPartProgram.SaveAs(newname)
    End If
    
    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub

  • I just ran this script and got no errors, but it did not create a program for me. Can you tell me if anything else stands out as far as errors?

    This works for me but I changed the variable statement to SerialNumber. So I know what we need to work on now.

    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Dim Cmds As Object
    Dim Cmd As Object
    Set Cmds = PCDPartProgram.Commands
    Dim PartProgramPath As String
    Set PartProgramPath = PCDPartProgram.Path
    
    Dim sernum As String	
    Set sernum = PCDPartProgram.SerialNumber
    If sernum <> "" Then
    newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & sernum & ".PRG"
      retval = PCDPartProgram.SaveAs(newname)
    End If
    
    
    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub

  • No, that's the only error I get when running. I don't have much experience with scripts, so I can't really help other than what errors PC-DMIS gives me. I appreciate your help though.

Reply Children
  • This works. The issue was that I had to execute the program before I ran the script to generate the variable value. That may be what was happening to you.

    And using sernum in the file name needs to be sernum.StringValue, since the type is still an object, it'll produce nothing.

    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPartPrograms = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Dim Cmds As Object
    Dim Cmd As Object
    Set Cmds = PCDPartProgram.Commands
    
    Dim sernum As Object
    Set sernum = PCDPartProgram.GetVariableValue("V1")
    If sernum.StringValue <> "" Then
    newname = PCDPartProgram.Path & PCDPartProgram.PartName & "_" & sernum.StringValue & ".PRG"
      retval = PCDPartProgram.SaveAs(newname)
    End If
    
    
    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd
    
    ' Cleanup
    Set PCDPartProgram = Nothing
    Set PCDPartPrograms = Nothing
    Set PCDApp = Nothing
    End Sub

  • FYI. The error message is because the object of type "Variable" does not have a default value assigned to it. If you were to output the PCDApp object it does and would return the string value "PCDLRN". With no default value, trying to output it or check the value of it will return an error. The documentation for the COM model for PC-DMIS doesn't always provide all the information one would like. A lot of it will be trial and error. I like to create a reference to the library in excel and browse through the object model (F2). That may help.

  • Hello,
    what exactly do you want to do with this section of the script?
    Looks like he writes the serial number that he extracted from the ASSIGNMENT "V1" into the ASSIGNMENT "SERNO" if it exists.

    Furthermore, this should be called sernum.StringValue, which shows me that this block isn't running anyway. I think you can just remove the whole block

    For Each Cmd In Cmds
    If Cmd.Type = ASSIGNMENT Then
       If Cmd.GetText(DEST_EXPR, 0) = "SERNO" Then
          bln = Cmd.PutText("""" + sernum + """", SRC_EXPR, 0)
          Cmd.ReDraw
       End If
    End If
    Next Cmd