hexagon logo

VB Script, Using PutText to enter an expression

I want to write a script that can modify the THEO and TARG values of a feature to be an expression, such as "1+0" or "2*VX+4". So far I can change the values to other values but if I give it an expression it always seems to evaluate it first and insert the result rather than the expression itself.

Not sure if what I want to do is possible but after scouring the forum for any examples I am sure that if it is possible someone on here will know how to do it.
  • Hi Spikalus!

    Show me your code... I had a similar situation but managed to get around it.
  • A snippet from one of my scripts:

    Set PCDCommand = PCDCommands.Add (GENERIC_CONSTRUCTION, True)
      PCDCommand.Marked = True
      retval = PCDCommand.SetExpression ("0+0", THEO_X, 0)
      retval = PCDCommand.SetExpression ("0+" & DLG.CheckBox_1, THEO_Y, 0)
      retval = PCDCommand.SetExpression ("0+0", THEO_Z, 0)
      retval = PCDCommand.SetExpression ("0+0", THEO_I, 0)
      retval = PCDCommand.SetExpression ("0+0", THEO_J, 0)
      retval = PCDCommand.SetExpression ("0+1", THEO_K, 0)
      retval = PCDCommand.PutText (Diameter, THEO_DIAM, 0)
    
      retval = PCDCommand.SetExpression ("0+0", MEAS_X, 0)
      retval = PCDCommand.SetExpression ("0+" & DLG.CheckBox_1, MEAS_Y, 0)
      retval = PCDCommand.SetExpression ("0+0", MEAS_Z, 0)
      retval = PCDCommand.SetExpression ("0+0", MEAS_I, 0)
      retval = PCDCommand.SetExpression ("0+0", MEAS_J, 0)
      retval = PCDCommand.SetExpression ("0+1", MEAS_K, 0)
      retval = PCDCommand.PutText (Diameter, MEAS_DIAM, 0)
      retval = PCDCommand.PutText ("CDATA", ID, 0)
      retval = PCDCommand.SetToggleString (4, GEN_FEAT_TYPE, 0)


    SetExpression is what you want to be using, not PutText.
  • That's perfect! Thank you, for solving that for me. I am sure that you guys have run across the fact that if you try to loop a program using while loops PC-Dmis will decide that you obviously want your THEO and TARG values to update as you increment your alignments. A programmer once taught me that if you add "+0" or "*1" to the end of your values that PC-Dmis will see them as an expression and leave them alone. I am playing around with a script that will add "+0" to the end of all of the THEO and TARG values in my program since it is so tedious to do so myself, with the end goal of having a button in the wizards toolbar that I can access easily. I am simultaneously using this to try my hand out at automating PC-Dmis via Python since I am most comfortable with that language and want to build the next generation UI we use in it.

    Here is the base code I have for it now that it's working:

    from win32com.client import Dispatch
    from win32com.client import gencache
    from win32com.client import constants
    gencache.EnsureModule('{2A09476D-B502-4089-AA46-452D6754CB33}', 0, 9, 1)  
    
    DmisApp = Dispatch("pcdlrn.Application")
    DmisApp.Visible = 1  
    DmisAppEvents =
    DmisApp.ApplicationEvents  
    DmisApp.WaitUntilReady(40)
    DmisPart = DmisApp.ActivePartProgram  
    DmisCmds = DmisPart.Commands  
    
    FeatureTypes = [
         "Basic Hit",
        "CIRCLE (CONTACT)",
        "CONE (CONTACT)",
        "CYLINDER (CONTACT)",
        "Generic Feature",
        "LINE (CONTACT)",
        "Measured Circle",
        "Measured Cone",
        "Measured Cylinder",
        "Measured Line",
        "Measured Plane",
        "Measured Point",
        "Measured Round Slot",
        "Measured Sphere",
        "Measured Square Slot",
        "Measured Torus",
        "PLANE (CONTACT)",
        "SPHERE (CONTACT)",
        "VECTOR POINT (CONTACT)",
     ]
    
    DataTypes = [
         constants.THEO_X,
        constants.THEO_Y,
        constants.THEO_Z,
        constants.THEO_SX,
        constants.THEO_SY,
        constants.THEO_SZ,
        constants.THEO_EX,
        constants.THEO_EY,
        constants.THEO_EZ,
        constants.THEO_R,
        constants.THEO_A,
        constants.THEO_H,
        constants.THEO_SR,
        constants.THEO_SA,
        constants.THEO_SH,
        constants.THEO_ER,
        constants.THEO_EA,
        constants.THEO_EH,
        constants.THEO_I,
        constants.THEO_J,
        constants.THEO_K,
        constants.TARG_X,
        constants.TARG_Y,
        constants.TARG_Z,
        constants.TARG_SX,
        constants.TARG_SY,
        constants.TARG_SZ,
        constants.TARG_EX,
        constants.TARG_EY,
        constants.TARG_EZ,
        constants.TARG_R,
        constants.TARG_A,
        constants.TARG_H,
        constants.TARG_SR,
        constants.TARG_SA,
        constants.TARG_SH,
        constants.TARG_ER,
        constants.TARG_EA,
        constants.TARG_EH,
        constants.TARG_I,
        constants.TARG_J,
        constants.TARG_K,
        constants.MEAS_X,
        constants.MEAS_Y,
        constants.MEAS_Z,
        constants.MEAS_SX,
        constants.MEAS_SY,
        constants.MEAS_SZ,
        constants.MEAS_EX,
        constants.MEAS_EY,
        constants.MEAS_EZ,
        constants.MEAS_R,
        constants.MEAS_A,
        constants.MEAS_H,
        constants.MEAS_SR,
        constants.MEAS_SA,
        constants.MEAS_SH,
        constants.MEAS_ER,
        constants.MEAS_EA,
        constants.MEAS_EH,
        constants.MEAS_I,
        constants.MEAS_J,
        constants.MEAS_K,
    ]
    
    for cmd in DmisCmds:
        if cmd.TypeDescription in FeatureTypes:
            for dtype in DataTypes:
                old = cmd.GetText(dtype, 0)
                cmd.SetExpression(old + "+0", dtype, 0)
    
    


    I made another script that will remove all the "+0"s just to be prepared for the inevitability of it being useful now that I can so easily add the "+0"s

     from win32com.client import Dispatch
    from win32com.client import gencache
    from win32com.client import constants
    gencache.EnsureModule('{2A09476D-B502-4089-AA46-452D6754CB33}', 0, 9, 1)  
    
    DmisApp = Dispatch("pcdlrn.Application")
    DmisApp.Visible = 1  
    DmisAppEvents =
    DmisApp.ApplicationEvents  
    DmisApp.WaitUntilReady(40)
    DmisPart = DmisApp.ActivePartProgram  
    DmisCmds = DmisPart.Commands  
    
    FeatureTypes = [
         "Basic Hit",
        "CIRCLE (CONTACT)",
        "CONE (CONTACT)",
        "CYLINDER (CONTACT)",
        "Generic Feature",
        "LINE (CONTACT)",
        "Measured Circle",
        "Measured Cone",
        "Measured Cylinder",
        "Measured Line",
        "Measured Plane",
        "Measured Point",
        "Measured Round Slot",
        "Measured Sphere",
        "Measured Square Slot",
        "Measured Torus",
        "PLANE (CONTACT)",
        "SPHERE (CONTACT)",
        "VECTOR POINT (CONTACT)",
     ]
    
    DataTypes = [
         constants.THEO_X,
        constants.THEO_Y,
        constants.THEO_Z,
        constants.THEO_SX,
        constants.THEO_SY,
        constants.THEO_SZ,
        constants.THEO_EX,
        constants.THEO_EY,
        constants.THEO_EZ,
        constants.THEO_R,
        constants.THEO_A,
        constants.THEO_H,
        constants.THEO_SR,
        constants.THEO_SA,
        constants.THEO_SH,
        constants.THEO_ER,
        constants.THEO_EA,
        constants.THEO_EH,
        constants.THEO_I,
        constants.THEO_J,
        constants.THEO_K,
        constants.TARG_X,
        constants.TARG_Y,
        constants.TARG_Z,
        constants.TARG_SX,
        constants.TARG_SY,
        constants.TARG_SZ,
        constants.TARG_EX,
        constants.TARG_EY,
        constants.TARG_EZ,
        constants.TARG_R,
        constants.TARG_A,
        constants.TARG_H,
        constants.TARG_SR,
        constants.TARG_SA,
        constants.TARG_SH,
        constants.TARG_ER,
        constants.TARG_EA,
        constants.TARG_EH,
        constants.TARG_I,
        constants.TARG_J,
        constants.TARG_K,
        constants.MEAS_X,
        constants.MEAS_Y,
        constants.MEAS_Z,
        constants.MEAS_SX,
        constants.MEAS_SY,
        constants.MEAS_SZ,
        constants.MEAS_EX,
        constants.MEAS_EY,
        constants.MEAS_EZ,
        constants.MEAS_R,
        constants.MEAS_A,
        constants.MEAS_H,
        constants.MEAS_SR,
        constants.MEAS_SA,
        constants.MEAS_SH,
        constants.MEAS_ER,
        constants.MEAS_EA,
        constants.MEAS_EH,
        constants.MEAS_I,
        constants.MEAS_J,
        constants.MEAS_K,
    ]
    
    for cmd in DmisCmds:
        if cmd.TypeDescription in FeatureTypes:
            for dtype in DataTypes:
                old = cmd.GetText(dtype, 0)
                new = old[[emoticon:33306C418930400BAC28808410F8AC8B]ld.find("+0")]
                cmd.SetExpression(new, dtype, 0)
    
    


    The code could still be optimized a little bit but it works for now.

    Thanks again for the help!
  • No problem! Thanks for posting your Pythonflavored source!
  • I don't have a wild clue how all this Python stuff works but it has my attention. I have been going thru Mr. Al Sweigart's material "Automate the Boring Stuff with Python" so far I've learned to pull myself up to stand up but quickly fall back on VB.NET lol I hope to soon take baby steps to play on my own 2 feet with this new (to me) Python.
  • I can extract the THEO values, but they don’t seem to be in the correct coordinate (alignment) system. What advice can you give an old programmer. (Using the basic supplied with PCDMIS).

    Thanks
  • I can extract the THEO values, but they don’t seem to be in the correct coordinate


    How'd you that? I could probably help but by the time I type up my solution someone will have posted a better one hahaha! But I'll go ahead and try to help out someone who is constantly sharing their knowledge with others, Mr. dph51.

    Post your code if you don't mind. I'll guide you/correct it if needed.
  • how did you get "Dispatch" to work? I've pip'ed my win32com but I am having errors.


  • How'd you that? I could probably help but by the time I type up my solution someone will have posted a better one hahaha! But I'll go ahead and try to help out someone who is constantly sharing their knowledge with others, Mr. dph51.

    Post your code if you don't mind. I'll guide you/correct it if needed.


    code
    extracts theos and shows on screen


    Sub Main()
    Dim PCDApp, PCDPartPrograms, PCDPartProgram, PCDCommands, PCDCommand
    
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set PCDPARTPROGRAMS = PCDApp.PartPrograms
    Set PCDPartProgram = PCDApp.ActivePartProgram
    Set PCDCommands = PCDPartProgram.Commands
    
    Dim cmd As Object
    Dim fcntr As Integer
    Dim FeatureList$(99999)
    Dim TX, TY, TZ, NewTX, NewTY, NewTZ As Variant
    
    fcntr = 0
    
    For Each cmd in PCDCommands
    If cmd.IsDCCFeature or cmd.IsMeasuredFeature Then
    FeatureList(fcntr) = cmd.id
    
    MSG = "feature ID = " & FeatureList(fcntr)
    
    TX = cmd.GetText(THEO_X, 0)
    ​​​​​​​TY = cmd.GetText(THEO_Y, 0)
    ​​​​​​​TZ = cmd.GetText(THEO_Z, 0)
    NewTX = TX & "+0"
    NewTY = TY & "+0"
    NewTZ = TZ & "+0"
    
    MSG = MSG & chr(10) & chr(10) & "XTheo = " &TX
    MSG = MSG & chr(10) & chr(10) & "YTheo = " &TY
    MSG = MSG & chr(10) & chr(10) & "ZTheo = " &TZ
    MSG = MSG & chr(10) & chr(10) & "NewXTheo = " &NewTX
    MSG = MSG & chr(10) & chr(10) & "NewYTheo = " &NewTY
    MSG = MSG & chr(10) & chr(10) & "NewZTheo = " &NewTZ
    
    MsgBox MSG
    
    End If
    
    Next cmd
    
    Set PCDApp = nothing
    Set PCDPartPrograms = nothing
    Set PCDPartProgram = nothing