hexagon logo

How to pass a variable to and from PCDMIS using Python?

I wanted to open a thread for only this issue. There is some simple, but cool stuff I'd like to do if I could simply pass a variable to and from PCDMIS and Python.

I'd prefer not having to use a notepad temporary txt file that is the workaround for passing variables.

Below is the code in python that seems like the correct syntax to pass the variables, but it isn't working. You're my hero if you have the solution ;)

import win32com.client as w32

# Connect to PCDMIS
dmisapp = w32.Dispatch('PCDLRN.Application')
dmispart = dmisapp.ActivePartProgram

# Retrieve the value of the variable "SHORTLOT"
# The VBA script to do this would be the following syntax:
# Dim Var As Object
# Set Var = dmispart.GetVariableValue ("SHORTLOT")

# The python syntax similar to the above approach does not work:
short_lot_value = dmispart.GetVariableValue("SHORTLOT")


# Set the value of the variable "SHORTLOT"
# The VBA Script to do this is:
# dmispart.SetVariableValue "SHORTLOT", short_lot_value

# The python method would seem to be this but doesn't work:
short_lot_value = "LOT-123456"
dmispart.SetVariableValue("SHORTLOT", short_lot_value)

Parents
  • I don't normally use python, so if I get some particular wrong, you'll have to excuse me. The reason it doesn't work is because you are treating an object as a string. The dmispart.GetVariableValue function returns an object of type variable. You have to define an object for that, e.g., dmisvar = dmispart.GetVariableValue("SHORTLOT"). Then you use that object's properties to get or set the value.

    import win32com.client as w32
    
    dmisapp = w32.Dispatch("PCDLRN.Application")
    dmispart = dmisapp.ActivePartProgram
    dmisvar = dmispart.GetVariableValue("SHORTLOT")
    
    short_lot_value = dmisvar.StringValue
    # OR
    dmisvar.StringValue = "LOT-123456"

  •  

    Thanks for the reply. I'll give that a shot. in the coming weeks and report back how it is working. Here is the code I'm going to try to pass back a variable to PCDMIS from python.

    import win32com.client as w32
    
    # Connect to PCDMIS
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmisvar = dmispart.GetVariableValue("SHORTLOT")
    
    dmisvar.StringValue = "LOT-123456"
    
    dmispart.SetVariableValue("SHORTLOT", dmisvar.StringValue)
    
    
    

Reply
  •  

    Thanks for the reply. I'll give that a shot. in the coming weeks and report back how it is working. Here is the code I'm going to try to pass back a variable to PCDMIS from python.

    import win32com.client as w32
    
    # Connect to PCDMIS
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmisvar = dmispart.GetVariableValue("SHORTLOT")
    
    dmisvar.StringValue = "LOT-123456"
    
    dmispart.SetVariableValue("SHORTLOT", dmisvar.StringValue)
    
    
    

Children
  • I think the SetVariableValue second argument has to be of an object type Variable. So, you'll have to leave off the ".StringValue" part of your argument and just pass "dmisvar". Setting the StringValue in line 8 is sufficient.

  • I had to create a python program to sort some csv data so I dug back into this issue. 

    You are correct that leaving off the .StringValue will cause the program to not error out, but for some reason it will not alter the variable inside of pcdmis. So where I am at now is I can get a variable from pcdmis, but I cannot pass a variable back into pcdmis for some reason.

    The code function is not working right now so here is the python code with error logging so you can see what is happening....

    import win32com.client as w32
    import logging
    import winsound

    # Set up logging
    log_file = r'C:\user\error.log'
    logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


    # Connect to PCDMIS
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmisvar = dmispart.GetVariableValue("V2") # get variable from pcdmis

    logging.info(f'The variable V2 from pcdmis is: {dmisvar.StringValue} ')  # success

    dmisvar.StringValue = dmisvar.StringValue + "_LOT-123456"

    logging.info(f'The modified variable is: {dmisvar.StringValue} ') # success

    try:
        dmispart.SetVariableValue("V2", dmisvar) # pass variable to pcdmis
    except Exception as e:
        logging.info(f'Error passing variable V2 to pcdmis: {e}')  # not kicking out an error msg

    winsound.Beep(500,500)

  • If your line of code containing the variable has not been executed yet, you will get errors when executing scripts. I have had this happen to me many times after opening up a program the next day working on a script, and the GetVariableValue gives an object reference error. After executing said variable the error stops. This may not be your issue, but just in case I thought I'd mention it.

    I am not too experienced with Python, but if your V2 original variable is a type of string, and you want the strings to append together, you will need to add quotes around the value here "_LOT-123456". I am not sure how you do it in python, but it essentially won't be able to concatenate within PC-DMIS into an assign statement without the quotes. 

    It would just become ASSIGN/V2="YourV2Variable" + _LOT-123456

    It needs to be ASSIGN/V2="YourV2Variable" + "_LOT-123456"
    which will cause an error I believe. I feel it would still allow you to pass this variable back in though, because I can pass back a variable without proper quotes around my string in basic script within PC-DMIS.

    You can use an alternative method to the GetVariableValue() and SetVariableValue().

    GetText(DEST_EXPR, 0) --> Gets Variable "ID" or name

    PutText(dmisvar, SRC_EXPR, 0) --> Sets variable's value

    Example below in Basic Script for modifying the variable value of "V2"

    Sub Main()
    
        Dim DmisApp As Object
        Dim DmisCmds As Object
        Dim DmisCmd As Object
        Dim DmisPart As Object    
        Dim DmisVar As String
    
        Set DmisApp = CreateObject("PCDLRN.Application")
        Set DmisPart = DmisApp.ActivePartProgram
        Set DmisCmds = DmisPart.Commands
    
        For Each DmisCmd In DmisCmds
            If DmisCmd.GetText(DEST_EXPR, 0) = "V2" Then
            DmisVar = DmisCmd.GetText(SRC_EXPR, 0) & "+" & """" & "_LOT-123456" & """"
            retval =  DmisCmd.PutText(DmisVar, SRC_EXPR, 0)
            End If
        Next DmisCmd
        
        Set DmisCmd = Nothing
        Set DmisCmds = Nothing
        Set DmisPart = Nothing
        Set DmisApp = Nothing
    End Sub

    In the mean time, I looked into how to convert/translate this into a Python script. I tested this and it works for me.

    import win32com.client as w32
    
    DmisApp = w32.Dispatch('PCDLRN.Application')
    DmisPart = DmisApp.ActivePartProgram
    DmisCmds = DmisPart.Commands
    
    for DmisCmd in DmisCmds:
        if DmisCmd.GetText(133, 0) == "V2":
            DmisVar = DmisCmd.GetText(134, 0)+' + "_LOT-123456"' 
            retval = DmisCmd.PutText(DmisVar, 134, 0)

  • Best to avoid the variable object unless you need it.

    import win32com.client as w32
    import win32api
    import ctypes

    # CONSTANTS
    DEST_EXPR = 133
    SRC_EXPR = 134

    dmisapp = w32.DispatchEx('PCDLRN.Application.14.2') # 14.2 = 2019 R2, 19.1 = 2024 R1
    dmispart = dmisapp.ActivePartProgram
    dmiscommands = dmispart.Commands

    for command in dmiscommands:
    if (command.Type==195):
    if (command.GetText(DEST_EXPR, 0)=="V2"):
    print(command.GetText(SRC_EXPR, 0)) # Current Value
    command.PutText(chr(34) + "NewValue" + chr(34), SRC_EXPR, 0) # New Value
    dmispart.RefreshPart

    dmisapp.Quit