python development instances for pc-dmis.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • python development instances for pc-dmis.

    Code:
    import win32com.client as w32
    dmisapp = w32.Dispatch('PCDLRN.Application')
    dmispart = dmisapp.ActivePartProgram
    dmiscommands = dmispart.Commands
    
    def pc_dmis_report():
        for cmd in dmiscommands:
            if cmd.IsDimension and int(cmd.DimensionCommand.OutTol)==0:
                cmd.SetToggleString(3,165,0)
            else:
                cmd.SetToggleString(4,165,0)
    
    pc_dmis_report()
    dmispart.RefreshPart()

  • #2
    good job,xujie

    Comment


    • arttk
      arttk commented
      Editing a comment
      Who are you?

  • #3
    arttk,
    Have you seen this error message before?
    import win32com.client as w32
    ModuleNotFoundError: No module named 'win32'
    Last edited by davehocum; 04-10-2018, 10:42 AM.

    Comment


  • #4
    AlliumPorrum,
    I am now past that error. Installed the pywin32-221.win-amd64-py3.6 and now I am receiving this error.
    Any ideas.
    Thank you.

    pywintypes.com_error.PNG

    Additional Info:
    I installed the Python 3.6 64-bit and pywin32-221.win-amd64-py3.6 on a Windows 10 OS.
    Last edited by davehocum; 04-17-2018, 01:39 PM.

    Comment


    • arttk
      arttk commented
      Editing a comment
      Run win32com -->makepy.py

    • davehocum
      davehocum commented
      Editing a comment
      arttk, can you explain more in depth with "Run win32com-->makepy.py".
      Thank you

  • #5
    Originally posted by davehocum View Post
    AlliumPorrum,
    I am now past that error. Installed the pywin32-221.win-amd64-py3.6 and now I am receiving this error.
    Any ideas.
    Thank you.

    pywintypes.com_error.PNG

    Additional Info:
    I installed the Python 3.6 64-bit and pywin32-221.win-amd64-py3.6 on a Windows 10 OS.
    Chances are you actually have the 32bit version of python installed even though it's a 64bit OS. Open cmd and type 'Python"
    It should show you what version you're running. I prefer running 32 bit, personally. Less headaches.

    chrome_2018-04-17_11-46-24.png

    Comment


    • #6
      I've also been working on an example for working with PC-DMIS and Python both to learn for myself and to help other co-workers who might be interested. This is a WIP and will be updated as I add more examples for working with different types of objects. Basically all it does is print out info about objects and their attributes, but it might help.

      Code:
      # Written with Python 3.5 by Frank Bohan
      # 3-2-2018
      import win32com.client as w32 # Imports required library to call the win32 API. This must be downloaded and installed beforehand. tinyurl.com/yaqtjehc  
      import os # In python, the os library gives you access to a lot of useful file manipulation methods. Another great alternative is shutil.
      dmisapp = w32.Dispatch('PCDLRN.Application')
      dmispart = dmisapp.ActivePartProgram # Attaches to currently open/active program. Other dmisapp options: ClosePartProgram(), SavePartProgram() and OpenPartProgram().
      dmiscommands = dmispart.Commands # Returns a series of objects representing every command in the edit window.
      cmdcount = dmiscommands.Count # Gets total number of objects in program.
      
      dmisapp.Caption = 'You can set the titlebar to whatever you want!'
      
      
      print(dmisapp.CurrentFolder) # Location of .PRG
      print(dmisapp.CurrentUserDirectory) # Current user's AppData folder path to DMIS related files.
      
      print("Program Name: {}".format(dmispart.Name))
      print("Full path to current program is: {}".format(os.path.join(dmisapp.CurrentFolder, dmispart.Name)))
      dmispart.ExportDataToXML("C:\some_folder\\") # Exports XML to folder. Will overwrite itself every run. Writing an XML parser for PC-DMIS is next on my to-do list.
      # dmispart.Export('dmi') # Opens dialog to export dmi.
      
      dmisapp.Visible = False # Toggle visibility of PC-DMIS window to False before running through any loop in dmiscommands for greatly improved execution time.
      current_count = 0 # Creates a variable to keep track of the current loop number in dmiscommands.
      for cmd in dmiscommands: # Iterates through collection of objects in the program.
        current_count += 1 # Increments by 1 to keep track of current position in loop.
        dmisapp.StatusBar = "{} of {}".format(current_count, cmdcount) # If the "dmisapp.Visible = False" line was taken out, you could see the statusbar update. eg: 1 of 500
        print("Type Desc is: {}".format(cmd.TypeDescription))
        print("Command type is: {}".format(cmd.Type))
        if cmd.ID != '':
          print("Command ID: {}".format(cmd.ID))
        if cmd.IsFeature:
          print("Command is Feature.")
      
        elif cmd.IsDimension:
          print("Command is Dimension.")
          cmd.DimensionCommand.Evaluate()
          if cmd.ID != '':
            dimname = cmd.ID # Saves the name of the Dimension if it's not empty.
            dimname1 = cmd.DimensionCommand.Feat1 # Will pull the name of the 1st referenced feature of a dimension.
          if cmd.DimensionCommand.Feat2 != '':
            dimname2 = cmd.DimensionCommand.Feat2 # If the dimension uses a 2nd feature, it will be assigned to this.
          axis_letter = cmd.DimensionCommand.AxisLetter
          nominal = cmd.DimensionCommand.Nominal
          measured = cmd.DimensionCommand.Measured
          plus_tol = cmd.DimensionCommand.Plus
          minus_tol = cmd.DimensionCommand.Minus
          out_tol = cmd.DimensionCommand.OutTol
          print(
            "Dimension Name: {}\n\
            Name of First Reference Feature: {}\n\
            Axis: {}\n\
            Nominal: {}\n\
            Measured: {}\n\
            Plus Tol: {}\n\
            Minus Tol: {}\n\
            Out Tol: {}".format(
            dimname,
            dimname1,
            axis_letter,
            nominal,
            measured,
            plus_tol,
            minus_tol,
            out_tol))
      dmisapp.Visible = True # Shows PC-DMIS window again. If your script crashes before this is executed, the process will still be running in the background.
      
      
      # cmd = dmiscommands('P13.1_PTOL_ABC') # Alternatively to the 'for' loop above, you can set the cursor position to a specific command like such. This way you don't have to loop through the entire program.
      # print("Current position in program is now: {}".format(cmd.ID))
      # cmd.Dialog # Will open the edit dialog for the current feature. I Haven't figured out how to interact with the buttons without using a Python library.
      
      # dmisapp.Minimize() # Pretty self explanatory.
      # dmisapp.Maximize()  
      
      
      if dmispart.MessageBox("This is some message body asking a question.", "", 4) == 6: # Calls builtin PC-DMIS message box. This example is configured for flow control.
        print('User clicked "Yes"')
      else:
        print('User clicked "No"')
      
      dmispart.RefreshPart() # Refreshes part to show changes.
      If you're new to Python, I'd also highly recommend using Sublime Text 3 as your editor. With a plugin, it has great syntax highlighting and allows you to run the script with a key-combo.
      sublime_text_2018-04-17_11-59-47.png
      Last edited by AlliumPorrum; 04-17-2018, 02:07 PM.

      Comment


      • #7
        Originally posted by AlliumPorrum View Post

        Chances are you actually have the 32bit version of python installed even though it's a 64bit OS. Open cmd and type 'Python"
        It should show you what version you're running. I prefer running 32 bit, personally. Less headaches.

        chrome_2018-04-17_11-46-24.png
        This indicates a 64 bit python on my system. I could try to install the 32 bit python instead.

        Python 3.6 64bit.PNG

        Thank you for the additional info (WIP)

        Comment


        • #8
          Originally posted by davehocum View Post

          This indicates a 64 bit python on my system. I could try to install the 32 bit python instead.

          Python 3.6 64bit.PNG

          Thank you for the additional info (WIP)
          You could just go ahead and install the same version I'm running. https://www.python.org/downloads/release/python-351/
          pywin32-220.win32-py3.5

          Running the newest version of Python before libraries have been updated can create headaches.

          If it's something enough people would be interested in, I could write a tutorial on setting up everything from scratch too.
          Last edited by AlliumPorrum; 04-17-2018, 03:40 PM.

          Comment


          • Kp61dude!
            Kp61dude! commented
            Editing a comment
            write the tutorial!

        • #9
          No luck with python 3.5 either. This could be user privilege or just a Windows 10 security again.
          Also, as you stated that this could be an install issue on my part.


          Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
          Type "copyright", "credits" or "license()" for more information.
          >>>
          RESTART: C:\Users\davehocum\AppData\Local\Programs\Python\P ython35-32\PC-DMIS_Test1.py
          Traceback (most recent call last):
          File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
          IDispatch = pythoncom.connect(IDispatch)
          pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
          During handling of the above exception, another exception occurred:
          Traceback (most recent call last):
          File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\PC-DMIS_Test1.py", line 2, in <module>
          dmisapp = w32.Dispatch('PCDLRN.Application')
          File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
          dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userN ame,clsctx)
          File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
          return (_GetGoodDispatch(IDispatch, clsctx), userName)
          File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
          IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
          pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
          >>>

          Comment


          • #10
            Originally posted by AlliumPorrum View Post

            You could just go ahead and install the same version I'm running. https://www.python.org/downloads/release/python-351/
            pywin32-220.win32-py3.5

            Running the newest version of Python before libraries have been updated can create headaches.

            If it's something enough people would be interested in, I could write a tutorial on setting up everything from scratch too.
            A setup tutorial would be very helpful for anyone that wants to use python with PC-DMIS.

            Comment


            • #11
              Would be an interesting use case for a Raspberry Pi controller for the CMM :P

              Comment


              • #12
                Originally posted by InspectorJester View Post
                Would be an interesting use case for a Raspberry Pi controller for the CMM :P
                Yes, that would give the ability for button customization of the hand held controller.

                Comment


                • #13
                  Originally posted by davehocum View Post

                  Yes, that would give the ability for button customization of the hand held controller.
                  This idea is quite sexy. The ability to have/create/fenaggle together a custom controller for PC-DMIS?
                  Heck, a whole box!
                  I've always dreamt of having a PC-DMIS 'operating system' where I've just got the controller, the software, and a GUI...
                  Is Python the answer???
                  Maybe. Maybe not. Would be a very cool project to play around with...

                  Comment


                  • davehocum
                    davehocum commented
                    Editing a comment
                    I always wonder if there was a way to use FreeCAD to program a CNC and then use some parts of that program to measure the part using a CMM. Also add the Step AP242 with MBD to the software to create the programs.

                  • InspectorJester
                    InspectorJester commented
                    Editing a comment
                    I don't see why not. Going the RPi route, it's got Linux, which is extremely versatile. It could easily (for someone who knows exactly what they're doing) be used to accept inputs/control outputs to/from a CMM.
                    If only I had one I could test on... :P

                  • davehocum
                    davehocum commented
                    Editing a comment
                    Yeah me too. Also, if I had the knowledge to be able to do this would be a big plus.

                • #14
                  Originally posted by davehocum View Post
                  No luck with python 3.5 either. This could be user privilege or just a Windows 10 security again.
                  Also, as you stated that this could be an install issue on my part.


                  Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
                  Type "copyright", "credits" or "license()" for more information.
                  >>>
                  RESTART: C:\Users\davehocum\AppData\Local\Programs\Python\P ython35-32\PC-DMIS_Test1.py
                  Traceback (most recent call last):
                  File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
                  IDispatch = pythoncom.connect(IDispatch)
                  pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
                  During handling of the above exception, another exception occurred:
                  Traceback (most recent call last):
                  File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\PC-DMIS_Test1.py", line 2, in <module>
                  dmisapp = w32.Dispatch('PCDLRN.Application')
                  File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
                  dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userN ame,clsctx)
                  File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
                  return (_GetGoodDispatch(IDispatch, clsctx), userName)
                  File "C:\Users\davehocum\AppData\Local\Programs\Pyt hon\ Python35-32\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
                  IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
                  pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
                  >>>
                  I'm thinking that arttk is right about it being an issue with the type library not being registered. I remember having lots of issues with that a while ago, but can't remember the solution for the life of me.

                  Can you try changing "dmisapp = w32.Dispatch" to "dmisapp = w32.GetActiveObject" and then running it after you've opened a test PC-DMIS program?

                  ETA: I think this might have been the solution.


                  For me, it was:
                  Code:
                  import sys
                  from win32com.client import makepy
                  
                  sys.argv = ["makepy", r"C:\Program Files\Hexagon\PC-DMIS 2017 R2 64-bit\Pcdlrn.tlb"]
                  makepy.main ()
                  Last edited by AlliumPorrum; 04-18-2018, 10:06 AM.

                  Comment


                  • davehocum
                    davehocum commented
                    Editing a comment
                    So far, I got this. "Importing module"

                    I did try changing "dmisapp = w32.Dispatch" to "dmisapp = w32.GetActiveObject" and received the same as before.

                    Code:
                    import sys
                    from win32com.client import makepy

                    sys.argv = ["makepy", r"C:\Program Files\Hexagon\PC-DMIS 2018 R1 64-bit\Pcdlrn.tlb"]
                    makepy.main ()
                    Generating to C:\Users\davehocum\AppData\Local\Programs\Python\P ython35-32\lib\site-packages\win32com\gen_py\7B1E293F-219C-4F48-81FC-1DB24F7DC527x0x13x1.py
                    Building definitions from type library...
                    Generating...
                    Importing module

                  • arttk
                    arttk commented
                    Editing a comment
                    davehocum:
                    Do you succeed?

                  • davehocum
                    davehocum commented
                    Editing a comment
                    arttk,
                    Was not able to get past the errors.
                    All I could get out of this was "Building definitions from type library..., Generating..., Importing module". Nothing happens after that.
                    Then try to Run:
                    Code:
                    import win32com.client as w32
                    dmisapp = w32.Dispatch('PCDLRN.Application')
                    dmispart = dmisapp.ActivePartProgram
                    dmiscommands = dmispart.Commands

                    def pc_dmis_report():
                    for cmd in dmiscommands:
                    if cmd.IsDimension and int(cmd.DimensionCommand.OutTol)==0:
                    cmd.SetToggleString(3,165,0)
                    else:
                    cmd.SetToggleString(4,165,0)

                    pc_dmis_report()
                    dmispart.RefreshPart()

                    and then received the same error again.

                • #15
                  Originally posted by AlliumPorrum View Post

                  I'm thinking that arttk is right about it being an issue with the type library not being registered. I remember having lots of issues with that a while ago, but can't remember the solution for the life of me.

                  Can you try changing "dmisapp = w32.Dispatch" to "dmisapp = w32.GetActiveObject" and then running it after you've opened a test PC-DMIS program?

                  ETA: I think this might have been the solution.


                  For me, it was:
                  Code:
                  import sys
                  from win32com.client import makepy
                  
                  sys.argv = ["makepy", r"C:\Program Files\Hexagon\PC-DMIS 2017 R2 64-bit\Pcdlrn.tlb"]
                  makepy.main ()
                  Googling the error message turns up this: https://stackoverflow.com/questions/...ecution-failed
                  Do you have multiple versions of PC-DMIS installed?

                  Also, to double check, do you have PC-DMIS open with a PRG loaded when you run this? I get the same error if I try running the script without a program open.

                  Comment

                  Related Topics

                  Collapse

                  Working...
                  X