hexagon logo

python development instances for pc-dmis.

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()
Parents
  • 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.

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

    # 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.
Children
No Data