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

Reply Children
No Data