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)

  • What exactly happens when you try your script? I don't actually have PCDMIS but use a similar program...

    There is also environment variables, which you could set in python and and use a VBA script to pull into PCDMIS, not sure how impractical that is though:

    import os
    
    os.environ["MYVARIABLE"] = "hello"
    print(os.environ["MYVARIABLE"])
    
    
    

    VBA to read the variable is Environ("MYVARIABLE")

  • I don't think they environment variable is saved once the python program is closed. If I were to do this I would use a workaround notepad txt file like below.  I really don't want to do this and want to find the simple solution to directly pass variables to and from pcdmis.  Not to mention I can't stand the vba syntax.

    LOC1 = 0.100
    LOC2 = 0.200
    
    variables_path = r'C:\Users\user\Desktop\variables.txt'
    
    # Write the values to a file
    with open(variables_path, "w") as file:
        file.write(f"{LOC1}\n")
        file.write(f"{LOC2}\n")
    
    # Read and print the values from the file
    with open(variables_path, "r") as file:
        for line in file:
            print(line.strip())

  • The environment variable is not lost when the python script ends. It is part of the set of variables the windows OS uses. You just have to make sure the variable is named in a very very specific fashion. There are other ways to read them as well. Like I said I can't test it for your specific application.

  • I realized you are correct about that. I've found a way to do it that is to my liking at least.

    My software has a built-in command: Clipboard.SetText / GetText

    I'm sure you can figure out your favorite way to access the clipboard from PC-DMIS.

    import subprocess
    
    txt = subprocess.getoutput("powershell.exe -Command Get-Clipboard")
    print(txt)

    there are many other ways to access the clipboard in python: https://stackoverflow.com/questions/101128/how-do-i-read-text-from-the-windows-clipboard-in-python

    Its not as sophisticated as I would like but saves you the trouble of having a million .txt files that your programs require 

  • 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)
    
    
    

  • 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.