hexagon logo

How to open a python script in pcdmis?

I've got a couple basic scripts running on our programs and you simply go to INSERT > BASIC SCRIPT and chose the .bas file. But it won't let me choose a python file .py.
I tried external command and that doesn't work and I've tried INSERT > FILE I/O COMMAND > FILE OPEN, but when I run the program it runs the program without doing anything inside the python script.

How were those running some python scripts here actually getting pcdmis to open the python script?

Thanks.

  • I think you can run a python script from VB...
  • Try execute external program, select your .PY and when run, the app selected in your system should run and do the python script. Just like you would doubleclick it.
  • Have you tried to shell the application?

    Here is an example of C# application being shell using the Insert Basic Script.

    The C# Administrator application will check if PC-DMIS is running as administrator. I haven't tried this with python and probably won't because I find it much simpler to use C# with the PC-DMIS library.

    Here is the PC-DMIS code.
    $$ NO,
    <Administration Check. Some directories are not available while running as administrator.>​
    ADMIN_CHECK_CS1 =SCRIPT/FILENAME= C:\USERS\PUBLIC\DOCUMENTS\HEXAGON\ROUTINE BUILDER\ROUTINE BASIC SCRIPTS\AMINISTRATOR_NOTIFICATION.BAS
    FUNCTION/Main,SHOW=YES,,
    STARTSCRIPT/
    ENDSCRIPT/​
    


    Here is the AMINISTRATOR_NOTIFICATION.BAS script for the example.

    Sub Main
    
    Dim shellpath
    
    shellpath = "C:\Users\Public\Documents\Hexagon\Routine Builder\Routine Exe Scripts\AministratorNotificationConsoleApp1.exe"
    
    Close shellpath
    
    'Launch application & Open file
    Shell shellpath
    
    End Sub
    


    Here is the C# application.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Security.Principal;
    using System.Runtime.InteropServices;
    namespace AministratorNotificationConsoleApp1
    {
        internal class Program
        {
            [DllImport("User32.dll", CharSet = CharSet.Unicode)]
            public static extern int MessageBox(IntPtr h, string m, string c, int type);
            static void Main(string[] args)
            {
                bool isRunningAsAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
                // found this syntax executes faster
                if (isRunningAsAdmin)
                {
                    MessageBox((IntPtr)0, "This application is running as administrator.", "Admin Warning", 0);
                    Console.WriteLine("This application is running as administrator.");
                    Console.ReadKey();
                }
            }
        }
    }
    /*
     Using .NET 4.8 FrameWork
    To switch this app from using the Console. Select AministratorNotificationConsoleApp1 within the
    Solution Explorer right mouse click and select properties.
    Change the OutPut Type to Windows Application.​
    */
    


    For more information on the Shell function.

    PC-DMIS Help

    Shell Function
    1. Shell ( app [, style])
    Runs an executable program.
    The shell function has two parameters. The first one, app is the name of the program to be executed. The name of the program in app must include a .PIF, .COM, .BAT, or .EXE file extension or an error will occur. The second argument, style is the number corresponding to the style of the window . It is also optional and if omitted the program is opened minimized with focus.
    Window styles:
    1. Normal with focus 1,5,9
      Minimized with focus (default) 2
      Maximized with focus 3
      normal without focus 4,8
      minimized without focus 6,7
    Return value: ID, the task ID of the started program.



    Link: https://docs.hexagonmi.com/pcdmis/2021.1/en/helpcenter/index.htm?rhcsh=1&rhnewwnd=0#t=mergedProjects%2Fba sic%2FShell_Function.htm&rhsearch=shell&rhsyns=%20 &rhhlterm=shell
  • Appreciate the replies...I believe I was onto something last night, but my problem is at work we have One Drive and my python is installed somewhere else so I'm getting python errors. I need to have our IT look at how I'm calling the cmd.exe to run the python script.

    I don't think the external program works b/c it needs to be called with python.exe for example if you want to run a python program from cmd you need to enter 'py program.py'

    Because my work setup is on the strange side, I just tried to get a python program running from inside Excel VBA. There are many methods to do this by googling, but I found this example to be the shortest:

    Sub Main()
    
    Dim vbaShell As Object
    Set vbaShell = VBA.CreateObject("Wscript.Shell")
    vbaShell.Run """C:\Users\Owner\AppData\Local\Programs\Python\Py thon310\python.exe""" & """C:\Users\Owner\Desktop\PCDMIS.PY"""
    
    End Sub
    
    ​
    


    The python code I tested below, I simply dumped into Excel VBA and ran it from there and it worked. For anyone, unfamiliar with win32api it is a library you must install after you install python on your computer. To get win32api library open a command prompt and type in 'pip install pywin32' they have different names for some reason.

    My goal is to not learn a new language as I enjoy pythons elegance and libraries and don't have the need to learn a new language. I'd like to write programs that I can do already in 20-30 minutes instead of struggling thru vba syntax.

    import win32com.client as w32
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmiscommands = dmispart.Commands
    
    print(dmisapp.CurrentFolder) # Location of .PRG
    input('Press any key to continue...')
    
    dmispart.RefreshPart()​
    


    I'll post my code and what I'm doing once I get this up and running. I'm sure what I'm doing can be done in VBA, but I'd much prefer using python.
  • I agree. I would rather use python instead of VBA. I was hoping to use python for PC-DMIS and I just found it much easier to use Visual Studio and C#. With Visual Studio it's all there for me to develop GUI, Console apps and executables all within one install. Right now I am learning the Go or golang using VS code for other projects.
  • When I post the below code into Excel VBA my python script runs fine.
    But when I call the test.bas script in pcdmis 2021 or 2023 I get the below error:

    Runtime Error on line: 4 - Type mismatch



    The error occurs at the line 'Set vbaShell......"

    Sub Main
    
      Dim vbaShell As Object
      Set vbaShell = VBA.CreateObject("Wscript.Shell")
      vbaShell.Run """C:\Users\PNT31\AppData\Local\Programs\Python\Python311\python.exe""" & """C:\Users\PNT31\OneDrive - PNT31\Desktop\test.py"""
    
    End Sub​
    


    Any ideas? I'm going to keep trying...once I can get this to work I am golden.
  • OKAY, I GOT IT!!!

    The problem was :
    Set Set vbaShell = VBA.CreateObject("Wscript.Shell") needed to remove the 'VBA.'

    So this code is working now to call my python file...I'm going to try and call my python code now to do some more involved CSV editing...I'll report back.

    I thought the pause line was needed to slow pcdmis down, but it starts the python program without regard for the pause command. PCdmis looks like it allows the python code to continue to run even after the program is finished, which is a good thing.

    Sub Main
    
      Dim vbaShell As Object
      Set vbaShell = CreateObject("Wscript.Shell")
      vbaShell.Run """C:\Users\PNT31\AppData\Local\Programs\Python\Python311\python.exe""" & """C:\Users\PNT31\OneDrive - PNT31\Desktop\test.py"""
      Application.Wait (Now + TimeValue("0:00:10"))
    
    End Sub​
    
  • Here is my latest issue...I'd like to get a variable from the CMM program so I can manipulate the data.

    The typical VBA code that does this isn't working with python:

    import win32com.client as w32
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmiscommands = dmispart.Commands
    
    print(dmisapp.CurrentFolder) # Location of .PRG
    
    
    x = cmdcount = dmiscommands.Count
    print(x)
    
    y = dmispart.GetVariableValue ("CMM")
    print(y)
    
    input('Press any key to continue...')
    
    dmispart.RefreshPart()​
    


    Everything above is working except:
    y = dmispart.GetVariableValue ("CMM")

    any ideas how to get a specific variable from the CMM into python. I tried y = dmispart.GetVariableValue(CMM) and y = dmispart.GetVariableValue.CMM

    Thanks.
  • Did not a cmd call work?
    python script.py
    


    make sure your path to script is fully defined and make sure python is on your PATH variable.