Text Box Values

Hi

Can someone please explain how I Get the value of the Text boxes and do I have to Set the value when I get it. I have only inserted the code for the first Text box I do appreciate the rest need to be added when I understand what is going on.

# Imports
import Rhino
import Rhino.UI
import scriptcontext
import System
import Eto.Drawing as drawing
import Eto.Forms as forms

# ImportExportPoints dialog class
class GeoPointVectors(forms.Dialog[bool]):
    
    # Dialog box Class initializer
    def __init__(self):
        # Initialize dialog box
        self.Title = "Geo Point Vectors"
        self.Padding = drawing.Padding(10)
        self.Resizable = False
        
        # Create controls for the dialog
        self.m_label1 = forms.Label(Text = 'Job Number:')
        self.m_JobNum_textbox = forms.TextBox(Text = "SC-000")
        
        self.m_label2 = forms.Label(Text = 'New Layer:')
        self.m_NewLayer_textbox = forms.TextBox(Text = "Point Vectors")
        
        self.m_label3 = forms.Label(Text = 'Range mm:')
        #MaskedTextBox()
        self.m_Range_textbox = forms.TextBox(Text = "50")
        
        self.m_label4 = forms.Label(Text = 'Font Size mm:')
        self.m_Font_textbox = forms.TextBox(Text = "5")
        
        self.m_label5 = forms.Label(Text = 'Vector Scale:')
        self.m_VectScale_textbox = forms.TextBox(Text = "4")
        
        self.m_label6 = forms.Label(Text = 'Tol X mm:')
        self.m_TolX_textbox = forms.TextBox(Text = "4")        
        
        self.m_label7 = forms.Label(Text = 'Tol Y mm:')
        self.m_TolY_textbox = forms.TextBox(Text = "4")       
        
        self.m_label8 = forms.Label(Text = 'Tol Z mm:')
        self.m_TolZ_textbox = forms.TextBox(Text = "4") 
        
        self.m_sign_checkbox = forms.CheckBox(Text = 'Sign convention')
        self.m_sign_checkbox.Checked = False
        
        
        #self.m_export_checkbox = forms.CheckBox(Text = 'Select if export points')
        #self.m_export_checkbox.Checked = False
        
        
        self.m_NumDp_combobox = forms.ComboBox()
        self.m_NumDp_combobox.DataStore = ['NumDP0', 'NumDP1', 'NumDP2', 'NumDP3']
        self.m_NumDp_combobox.SelectedIndex = 0
        
        self.m_label = forms.Label(Text = 'Decimal Places:')
        
        # Create the default button
        self.DefaultButton = forms.Button(Text = 'OK')
        self.DefaultButton.Click += self.OnOKButtonClick
        
        # Create the abort button
        self.AbortButton = forms.Button(Text = 'Cancel')
        self.AbortButton.Click += self.OnCloseButtonClick
        
        # Create a table layout and add all the controls
        layout = forms.DynamicLayout()
        layout.Spacing = drawing.Size(5, 5)
        #layout.AddRow(self.m_JobNum_textbox)
        layout.AddRow(self.m_label1, self.m_JobNum_textbox)
        #layout.AddRow(self.m_NewLayer_textbox)
        layout.AddRow(self.m_label2, self.m_NewLayer_textbox)
        
        layout.AddRow(self.m_label3, self.m_Range_textbox)
        
        layout.AddRow(self.m_label4, self.m_Font_textbox)
        
        layout.AddRow(self.m_label5, self.m_VectScale_textbox)
        
        layout.AddRow(self.m_label6, self.m_TolX_textbox)
        
        layout.AddRow(self.m_label7, self.m_TolY_textbox)
        
        layout.AddRow(self.m_label8, self.m_TolZ_textbox)
        
        layout.AddRow(self.m_sign_checkbox)
        #layout.AddRow(self.m_export_checkbox)
        layout.AddRow(self.m_label, self.m_NumDp_combobox)
        layout.AddRow(None) # spacer
        layout.AddRow(self.DefaultButton, self.AbortButton)
        
        # Set the dialog content
        self.Content = layout
    
    # Start of the class functions
    
    
    
        # Get the value of the textbox
        def GetText(self):
            return self.m_JobNum_textbox.Text
        def SetText(self):
            #self.m_JobNum_textbox.Text
            print self.m_JobNum_textbox.Text
    
    
    
    
     #Get/set the value of the first checkbox
        def GetSign(self):
            return self.m_sign_checkbox.Checked
        def SetSign(self, set):
            self.m_sign_checkbox.Checked = set
    
    # Get/set the value of the second checkbox
    ##def GetExport(self):
        ##return self.m_export_checkbox.Checked
    ##def SetExport(self, set):
        ##self.m_export_checkbox.Checked = set
        
    # Get/set the value of the combobox
    def GetNumDp(self):
      return self.m_NumDp_combobox.SelectedIndex
    def SetNumDp(self, index):
      self.m_NumDp_combobox.SelectedIndex = Rhino.RhinoMath.Clamp(index, 0, 2)
    
    # Close button click handler
    def OnCloseButtonClick(self, sender, e):
        self.Close(False)
    
    # OK button click handler
    def OnOKButtonClick(self, sender, e):
        self.Close(True)
    ## End of Dialog Class ##

# Utility function to show the dialog box
def ShowGeoPointVectors( setNumDp = 0):
    dialog = GeoPointVectors()
    #dialog.SetSign(setsign)
    #dialog.SetExport(setExport)
    dialog.SetNumDp(setNumDp)
    rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
    if rc:
        return dialog.GetNumDp()
    return None
    
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if __name__ == "__main__":
    #
    # Show dialog with default settings
    rc0 = ShowGeoPointVectors()

Hi @RogerD,

you can set values outside the dialog instance after it has been initialized, right before you open it eg:

# init the dialog
dialog = GeoPointVectors()
# set a value
dialog.m_NewLayer_textbox.Text = "Hello World"
# open the dialog
rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)

After the dialog has been closed, you can access the text boxes the same way eg:

if rc:
    print "New Layer: ".format(dialog.m_NewLayer_textbox.Text)

does this help ?

_
c.

Hi Clement many thanks guess I was jumping the gun a little bit, these little technicalities are sometimes hard to find when learning a new language.

Hi Clement, would you mind if I sent you my script and asked one more question about it ?

Hi @RogerD, just send it via PM if you do not want to post it in public.

_
c.

Hi Clement,

After years of VB Script I am having a go at ETO and Python so slow going, I wrote the maths separate from the front end and am now trying to re allocate the values from the text boxes etc to the variables I have used, please see rows 200 and 201 where I show what I am trying to do, would have thought it was easy but I must be overlooking something basic.

Not a lot of documentation on ETO forms and what I have read I don’t always understand or appreciate as yet.

I will change the extn name to txt so outlook doesn’t stop the file, if you change it back to py you should be ok.

Not sure where in the world you are I’m in the UK and will be having to head off shortly so no rush , any advice appreciated.

Geo Point Vectors A.txt (19.2 KB)

Hi @RogerD, some tips which may help:

Since your dialog uses text boxes for the numbers, their content is of type String. So you need to set the proper type when setting values before showing the dialog. Same goes after you’ve clicked OK and want to assign the textbox contents to variables. Below is a possible snippet to to set values before showing the dialog:

    # init dialog
    dialog = GeoPointVectors()
    
    # set values before showing the dialog
    dialog.m_JobNum_textbox.Text = "SC-000"
    dialog.m_NewLayer_textbox.Text = "Point Vector"
    dialog.m_Range_textbox.Text = "50"
    dialog.m_Font_textbox.Text = "5"
    dialog.m_VectScale_textbox.Text = "4"
    dialog.m_TolX_textbox.Text = "4"
    dialog.m_TolY_textbox.Text = "4"
    dialog.m_TolZ_textbox.Text = "4"
    dialog.m_sign_checkbox.Checked = True
    dialog.m_NumDp_combobox.SelectedIndex = 0
   

If you write your code into functions instead of putting it all in the main scope, you can quickly return out of a function, eg. if someone presses the Cancel button in the dialog. Then the variable rc is False:

    # show dialog, exit if True is not returned
    rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
    if not rc: return

To get the textbox values usable in your variables, you need to cast again to the proper type like below:

    # get values and convert to required types used in your variables
    JobNum = dialog.m_JobNum_textbox.Text
    NewLayer = dialog.m_NewLayer_textbox.Text
    Range = float(dialog.m_Range_textbox.Text)
    FontSize = float(dialog.m_Font_textbox.Text)
    VecScale = float(dialog.m_VectScale_textbox.Text)
    TolX = float(dialog.m_TolX_textbox.Text)
    TolY = float(dialog.m_TolY_textbox.Text)
    TolZ = float(dialog.m_TolZ_textbox.Text)
    Sign = bool(dialog.m_sign_checkbox.Checked)
    decimal_places = dialog.m_NumDp_combobox.SelectedIndex

You may get rid of many type conversions by using proper controls in the Eto dialog. Instead of using text inputs for numbers, replace the Eto.Forms.TextBox controls with Eto.Forms.NumericStepper. This prevents that the fields can be left empty or with letters instead of numbers (which would cause errors in the type conversion). The Eto.Forms.NumericStepper supports other useful things eg:

self.m_numeric1 = Eto.Forms.NumericStepper(
                                           Value = 0.9,
                                           MinValue = 0.5,
                                           MaxValue = 1.0,
                                           DecimalPlaces = 2,
                                           MaximumDecimalPlaces = 2, 
                                           Increment = 0.05                                           
                                           )

_
c.

1 Like

Many thanks Clement I will work my way through your suggestions and see how I go. I had realised about the type as string and had posted on the forum how to input an integer or if I just put in text and then converted as I wasn’t sure. I did see the Numeric stepper but thought is was overkill for what I needed but will re visit this control function.

Onwards and upwards as they say.