hexagon logo

VB.NET .GetVariableValue("NAME")

How do you pull variable values from PCD program using VB.NET?

I know how to do it with VBA and BASIC etc.... but how do you do it with VB.NET, I can get variable name but no values!

Any help, thanks.

***Edit***

My code works I just hadn't ran PcD to 'load' the variables with values....HAHAHAHA!!!!!!!!!!!!!!!
  • Learning awesome new things I didn't know were possible...

    Made a little .exe that creates a CSV with measured values and things utilizing old VB.NET, highly recommend playing with this on your free time it's FUN!


  • Don't forget to share your code/sources as it WILL help others.
  • Don't forget to share your code/sources as it WILL help others.


    Ahhhh but its my humble beginnings code! Eh I'm sure it could be written far better but here it is anyways.

    '************************************************************
    ' Author: Kp61dude!
    ' Date: 6/21/2017
    ' Desc: Using PCD template "TEMPLATE-AUTOMATED_A_20170613.PRG", pulling measured data to CSV. Using VB.NET
    '************************************************************
    Public Class Form1
        Private Sub CSV_Out_Click(sender As Object, e As EventArgs)
    
            Dim app As PCDLRN.Application 'PCDLRN.Application
            Dim cmds As Object 'PCDLRN.Commands
            Dim cmd As PCDLRN.Command 'PCDLRN.Command
            Dim part As PCDLRN.PartProgram 'PCDLRN.PartProgram
            Dim s As String = "" 'string to hold dimensions ID
            Dim r As Integer = 1 'row
    
            app = GetObject("", "PCDLRN.Application.10.1") 'Using PCD version 2015.1, change to yours or if one PCD version installed use "PCDLRN.Application"
            app.Visible = True
            part = app.ActivePartProgram
            cmds = part.Commands
    
            'Set up the progress bar
            Dim nCommands As String = part.Commands.Count.ToString
            ProgressBar1.Minimum = 0
            If nCommands <> "" Then
                Dim result As Boolean = Int32.TryParse(nCommands, nCommands)
                If result Then
                    ProgressBar1.Maximum = nCommands
                Else
                    ProgressBar1.Maximum = 100
                End If
            End If
    
            'Pulled PCD variables. For Header build.
            Dim serNum, woNum, pNumber, pRev, pcdDate, pcdTime, inCSV As String
            Dim pcdFilepath As String = part.GetVariableValue("FILEPATH").StringValue
    
            'If for whatever reason variable is empty save elsewhere.
            If pcdFilepath Is "" Then
                pcdFilepath = "C:\Users\Public\Documents\WAI\PC-DMIS\2015.1\Output2CSV.CSV"
            End If
    
            'CSV starage location
            Dim dotPosition As Integer = pcdFilepath.LastIndexOf(".")
            Dim csvFile As String = pcdFilepath.Substring(0, dotPosition) & ".CSV"
    
            'Open CSV for writing
            Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, False)
    
            'Loop counter
            Dim c As Integer = 0
    
            'Scan thru all variables in PCD program and write to CVS file.
            For Each cmd In cmds
                If cmd.TypeDescription = "Assignment" Then
                    Dim assignName As Object = cmd.GetText(PCDLRN.ENUM_FIELD_TYPES.DEST_EXPR, 0)
    
                    Select Case assignName
                        Case "SERIAL"
                            serNum = part.GetVariableValue(assignName).StringValue
                            serNum = serNum.Substring(serNum.LastIndexOf("-") + 1)
                            If serNum <> "" Then
                                serNum = Convert.ToDouble(serNum)
                            End If
                            outFile.WriteLine("Serial Number:," & serNum)
    
                        Case "WO"
                            woNum = part.GetVariableValue(assignName).StringValue
                            outFile.WriteLine("Work Order:," & woNum)
    
                        Case "PART"
                            pNumber = part.GetVariableValue(assignName).StringValue
                            outFile.WriteLine("Part Number:," & pNumber)
    
                        Case "REV"
                            If c = 36 Then 'pull second instance of "REV"
                                pRev = part.GetVariableValue(assignName).StringValue
                                outFile.WriteLine("Part Revision:," & pRev)
                            End If
    
                        Case "SYSTEM_DATE"
                            pcdDate = part.GetVariableValue(assignName).StringValue
                            outFile.WriteLine("Date:," & pcdDate)
    
                        Case "SYSTEM_TIME"
                            pcdTime = part.GetVariableValue(assignName).StringValue
                            outFile.WriteLine("Time:," & pcdTime)
    
                        Case "CSV_FILEPATH"
                            inCSV = part.GetVariableValue(assignName).StringValue
                    End Select
                End If
    
                If pcdDate <> "" Then
                    outFile.WriteLine(",")
                    outFile.WriteLine(",")
                    outFile.WriteLine(",")
                    outFile.WriteLine("Dimension, Feature, Axis, Segment, Nom, Meas, UTOL, LTOL")
                End If
    
                pcdDate = ""
    
                'Pull dimension data from PCD program.
                If cmd.ID <> "" Then s = cmd.ID 'capture the name of the dimension
                If cmd.IsDimension Then ' if the command is a dimension
                    If cmd.DimensionCommand.AxisLetter <> "" Then 'if there is an axis letter
    
                        Dim aXL As String = cmd.DimensionCommand.AxisLetter
                        Dim Nom As Double = Math.Round(cmd.DimensionCommand.NOMINAL, 4)
                        Dim Meas As Double = Math.Round(cmd.DimensionCommand.Measured, 5)
                        Dim UTOL As Double = Math.Round(cmd.DimensionCommand.Plus, 5)
                        Dim LTOL As Double = Math.Round(cmd.DimensionCommand.Minus, 5)
    
                        outFile.WriteLine(s & ",," & aXL & ",," & Nom & "," & Meas & "," & UTOL & "," & LTOL)
    
                    End If 'if cmd.DimensionCommand.Axisletter
                End If 'if cmd.IsDimenion
                c = c + 1
                ProgressBar1.Increment(1)
            Next cmd
    
            outFile.Close()
    
            Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))
    
            releaseObject(app)
            releaseObject(cmds)
            releaseObject(part)
    
            'Move file to PCD variable value.
            If Dir("Filename Here") <> "" Then 'if file exists on network
                If inCSV <> "" Then
                    Dim destNetFile As String = inCSV & csvFile.Substring(csvFile.LastIndexOf("\") + 1)
                    My.Computer.FileSystem.MoveFile(csvFile, destNetFile, overwrite:=True)
                End If
            End If
    
            ProgressBar1.Value = nCommands - 1
    
        End Sub
        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Visible = True
            CSV_Out_Click(Nothing, Nothing)
            Me.Close()
        End Sub
    End Class
    
    
  • You're missing all FCF dimensions, as they are not .IsDimension (obviously!), but maybe you knew that already?

    Edit: And probably missing the new SIZE, too.
  • You're missing all FCF dimensions, as they are not .IsDimension (obviously!), but maybe you knew that already?

    Edit: And probably missing the new SIZE, too.


    Oops forgot to mention that! This works with my current style and setup, I did not think about anything else as I do not use it.

    Though I should NOT assume that things will stay that way for us forever... legacy could possibly be extinct one day, another future programmer could come in and program differently, our engineers could change their GD&T technique that may require us to use something different, etc.... I will go back and bullet proof things up a bit in the future.

    Thanks for pointing that out !!!!!!!
  • There are also some 'brackets' through the command.Type DIMENSION_TRUE_START_POSITION, DIMENSION_TRUE_END_POSITION, DIMENSION_START_LOCATION and DIMENSION_END_LOCATION. The START part is a good place to grab the dimension ID, and the END part the place to forget the ID again.

    A warning for the future - my FEATUREEXPORT.BAS (which is used commercially) is 479 lines long. It includes handling of FCF and [most] composite dimensions, but not simultaneous or SIZE, yet...
    (And it surely contains some bugs...)