I have a Modeless form, and I want to change the contents of it to something else depending on what button is pushed.
I want this section over here to change to other things
lines (52-60)
# Create a group box for atrButtons
self.m_groupboxAtr = forms.GroupBox()
self.m_groupboxAtr.Padding = drawing.Padding(5)
groupLayoutAtr = forms.DynamicLayout()
groupLayoutAtr.Spacing = drawing.Size(5, 5)
groupLayoutAtr.AddRow(self.CreateAtrButtons())
self.m_groupboxAtr.Content = groupLayoutAtr
it’s pulled by line 84
layout.AddRow(groupLayoutPartNum, groupLayoutAtr)
When one of the buttons gets clicked I want to remove the groupLayoutAtr and push something else to display in that area.
I know what most of my work is going to happen in OnAtrButtonClick line 180, or AssignAttributes line 184
I just don’t know how to “unload” one group set so I can load something else in there…
I’ve changed everything to one icon file attached here. In case some needs to try and run the script.
But here is what it looks like on my end.
import System
import Rhino
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
import scriptcontext as sc
import rhinoscriptsyntax as rs
import os
import time
dir = os.path.dirname(__file__)
__commandname__ = "viAttributeTEMP2"
class viAttributeForm(forms.Form):
def __init__(self):
self.Initialize()
self.previousHighlight = set()
# Basic form initialization
def Initialize(self):
self.Title = 'Attribute Form'
self.Padding = drawing.Padding(5)
self.Resizable = False
self.Maximizable = False
self.Minimizable = False
self.ShowInTaskbar = True
self.MinimumSize = drawing.Size(650, 400)
# Create a group box for PartNum
self.m_groupboxPtNum = forms.GroupBox()
self.m_groupboxPtNum.Padding = drawing.Padding(5)
groupLayoutPartNum = forms.DynamicLayout()
groupLayoutPartNum.Spacing = drawing.Size(5, 5)
groupLayoutPartNum.AddRow(self.CreateListBox())
self.m_groupboxPtNum.Content = groupLayoutPartNum
# Create a group box for Highlite button
self.m_groupboxHighlight = forms.GroupBox()
self.m_groupboxHighlight.Padding = drawing.Padding(5)
groupLayoutHighlight = forms.DynamicLayout()
groupLayoutHighlight.Spacing = drawing.Size(5, 5)
groupLayoutHighlight.AddRow(self.CreateHighlightButton())
self.m_groupboxHighlight.Content = groupLayoutHighlight
# Create a group box for atrButtons
self.m_groupboxAtr = forms.GroupBox()
self.m_groupboxAtr.Padding = drawing.Padding(5)
groupLayoutAtr = forms.DynamicLayout()
groupLayoutAtr.Spacing = drawing.Size(5, 5)
groupLayoutAtr.AddRow(self.CreateAtrButtons())
self.m_groupboxAtr.Content = groupLayoutAtr
# Create a group box for Default Buttons
self.m_groupboxDefaultButtons = forms.GroupBox()
self.m_groupboxDefaultButtons.Padding = drawing.Padding(5)
groupDefaultButtons = forms.DynamicLayout()
groupDefaultButtons.Spacing = drawing.Size(5, 5)
groupDefaultButtons.AddRow(self.CreateDefaultButtons())
self.m_groupboxDefaultButtons.Content = groupDefaultButtons
# Create a label control
labelParts = forms.Label()
labelParts.Text = 'Parts'
lableAtr = forms.Label()
lableAtr.Text = 'Attributes'
# Create dynamic layout control
layout = forms.DynamicLayout()
layout.Padding = drawing.Padding(5)
layout.Spacing = drawing.Size(5, 5)
# Add controls to layout
layout.AddRow(labelParts, lableAtr)
layout.AddRow(groupLayoutPartNum, groupLayoutAtr)
layout.AddRow(groupLayoutHighlight, groupDefaultButtons)
# Set the dialog content
self.Content = layout
# FormClosed event handler
self.Closed += self.OnFormClosed
def CreateListBox(self):
#Create ListBox
listbox = forms.ListBox()
listbox.DataStore = self.organizedKeys()
listbox.SelectedIndex = 0
listbox.Size = drawing.Size(100,300)
self.m_listbox = listbox
return self.m_listbox
def GetSelectedPartsGUIDs(self):
### Need a way to check if there is anything in the scea, or if anything got selected
### Otherwise a lot of stuff will be crashing
### As it relies on this selection
# Grab the list box data
listBoxData = self.m_listbox.DataStore
# Grab selected index
selectedIndex = self.m_listbox.SelectedIndex
# Get part number from list box based on selection index
objTofind = listBoxData[selectedIndex]
# Find objects in dictionary based on part number
foundObj = self.mainDictionary.get(str(objTofind))
return foundObj, objTofind
def OnHighlightButtonClick(self, sender, e):
foundObj = self.GetSelectedPartsGUIDs()[0]
currentHighlight = set(foundObj)
# If something was found
if foundObj != None:
# if previousHighlight set is not empty and currentHighlight is not same as previousHighlight
if not not self.previousHighlight and currentHighlight != self.previousHighlight:
# Set all objects to color by layer
for priv_obj in self.previousHighlight:
rs.ObjectColorSource(priv_obj, source=0)
# Clear out previousHighlight set
self.previousHighlight.clear()
# Change color to highlight and add them to previousHighlight set
for obj in foundObj:
rs.ObjectColor(obj, color=(117,255,127))
self.previousHighlight.add(obj)
else:
# For every object found run loop
for obj in foundObj:
ColorSource = rs.ObjectColorSource(obj)
# If color source is 0 (by layer), change to green
# If color source is something else, change to layer source
if ColorSource == 0:
rs.ObjectColor(obj, color=(117,255,127))
self.previousHighlight.add(obj)
else:
rs.ObjectColorSource(obj, source=0)
self.previousHighlight.discard(obj)
# OK button click handler
def OnOkButtonClick(self, sender, e):
self.m_selected_index = self.m_listbox.SelectedIndex
self.Close()
# Cancel button click handler
def OnCancelButtonClick(self, sender, e):
self.Close()
# Create button controls
def CreateDefaultButtons(self):
# 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.OnCancelButtonClick
# Create button layout
button_layout = forms.DynamicLayout()
button_layout.Spacing = drawing.Size(5, 5)
button_layout.AddRow(None, self.DefaultButton, self.AbortButton, None)
return button_layout
# Create button controls
def CreateHighlightButton(self):
# Create the highlight button
self.HighlightButton = forms.Button(Text = 'Highlight')
self.HighlightButton.Click += self.OnHighlightButtonClick
# Create button layout
button_layout = forms.DynamicLayout()
button_layout.Spacing = drawing.Size(5, 5)
button_layout.AddRow(None, self.HighlightButton, None)
return button_layout
# Atr button click handler
def OnAtrButtonClick(self, sender, e):
atrClass = sender.Tag
self.AssignAttributes(atrClass)
def AssignAttributes(self, atrClass):
print 'Class: {}'.format(atrClass)
PartNum = self.GetSelectedPartsGUIDs()[1]
print 'PartNum',PartNum
GUIDs = self.GetSelectedPartsGUIDs()[0]
print 'GUIDs',GUIDs
# Create button controls
def CreateAtrButtons(self):
# wood image icon
image_btn_wood = forms.ImageView()
image_btn_wood.Size = drawing.Size(50, 50)
image_btn_wood.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# angle image icon
image_btn_angle = forms.ImageView()
image_btn_angle.Size = drawing.Size(50, 50)
image_btn_angle.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# flatStock image icon
image_btn_flatStock = forms.ImageView()
image_btn_flatStock.Size = drawing.Size(50, 50)
image_btn_flatStock.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# tubeRect image icon
image_btn_tubeRect = forms.ImageView()
image_btn_tubeRect.Size = drawing.Size(50, 50)
image_btn_tubeRect.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# tubeSquare image icon
image_btn_tubeSquare = forms.ImageView()
image_btn_tubeSquare.Size = drawing.Size(50, 50)
image_btn_tubeSquare.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# tubeRound image icon
image_btn_tubeRound = forms.ImageView()
image_btn_tubeRound.Size = drawing.Size(50, 50)
image_btn_tubeRound.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# solidSquare image icon
image_btn_solidSquare = forms.ImageView()
image_btn_solidSquare.Size = drawing.Size(50, 50)
image_btn_solidSquare.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# solidRound image icon
image_btn_solidRound = forms.ImageView()
image_btn_solidRound.Size = drawing.Size(50, 50)
image_btn_solidRound.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# solidHalf image icon
image_btn_solidHalf = forms.ImageView()
image_btn_solidHalf.Size = drawing.Size(50, 50)
image_btn_solidHalf.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# mesh image icon
image_btn_mesh = forms.ImageView()
image_btn_mesh.Size = drawing.Size(50, 50)
image_btn_mesh.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# pipe image icon
image_btn_pipe = forms.ImageView()
image_btn_pipe.Size = drawing.Size(50, 50)
image_btn_pipe.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# tBar image icon
image_btn_tBar = forms.ImageView()
image_btn_tBar.Size = drawing.Size(50, 50)
image_btn_tBar.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# iBeam image icon
image_btn_iBeam = forms.ImageView()
image_btn_iBeam.Size = drawing.Size(50, 50)
image_btn_iBeam.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# cChan image icon
image_btn_cChan = forms.ImageView()
image_btn_cChan.Size = drawing.Size(50, 50)
image_btn_cChan.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# plate image icon
image_btn_plate = forms.ImageView()
image_btn_plate.Size = drawing.Size(50, 50)
image_btn_plate.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# cust image icon
image_btn_cust = forms.ImageView()
image_btn_cust.Size = drawing.Size(50, 50)
image_btn_cust.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# stock image icon
image_btn_stock = forms.ImageView()
image_btn_stock.Size = drawing.Size(50, 50)
image_btn_stock.Image = drawing.Bitmap(os.path.join(dir, 'icons','btn_test.png'))
# Create button
self.WoodButton = forms.Button(Text = 'Wood', Tag='000')
self.WoodButton.Click += self.OnAtrButtonClick
# Create button
self.AngleButton = forms.Button(Text = 'Angle', Tag='100')
self.AngleButton.Click += self.OnAtrButtonClick
# Create button
self.FlatStockButton = forms.Button(Text = 'Flat Stock', Tag='101')
self.FlatStockButton.Click += self.OnAtrButtonClick
# Create button
self.TubeRectButton = forms.Button(Text = 'Tube Rect', Tag='102')
self.TubeRectButton.Click += self.OnAtrButtonClick
# Create button
self.TubeSquareButton = forms.Button(Text = 'Tube Square', Tag='103')
self.TubeSquareButton.Click += self.OnAtrButtonClick
# Create button
self.TubeRoundButton = forms.Button(Text = 'Tube Round', Tag='104')
self.TubeRoundButton.Click += self.OnAtrButtonClick
# Create button
self.SolidSquareButton = forms.Button(Text = 'Solid Square', Tag='105')
self.SolidSquareButton.Click += self.OnAtrButtonClick
# Create button
self.SolidRoundButton = forms.Button(Text = 'Solid Round', Tag='106')
self.SolidRoundButton.Click += self.OnAtrButtonClick
# Create button
self.SolidHalfButton = forms.Button(Text = 'Half Round', Tag='107')
self.SolidHalfButton.Click += self.OnAtrButtonClick
# Create button
self.MeshButton = forms.Button(Text = 'Mesh', Tag='108')
self.MeshButton.Click += self.OnAtrButtonClick
# Create button
self.PipeButton = forms.Button(Text = 'Pipe', Tag='109')
self.PipeButton.Click += self.OnAtrButtonClick
# Create button
self.TBarButton = forms.Button(Text = 'T-Bar', Tag='110')
self.TBarButton.Click += self.OnAtrButtonClick
# Create button
self.IBeamButton = forms.Button(Text = 'I-Beam', Tag='111')
self.IBeamButton.Click += self.OnAtrButtonClick
# Create button
self.CChanButton = forms.Button(Text = 'C-Channel', Tag='112')
self.CChanButton.Click += self.OnAtrButtonClick
# Create button
self.PlateButton = forms.Button(Text = 'Plate', Tag='200')
self.PlateButton.Click += self.OnAtrButtonClick
# Create button
self.CustButton = forms.Button(Text = 'Custom', Tag='300')
self.CustButton.Click += self.OnAtrButtonClick
# Create button
self.StockButton = forms.Button(Text = 'Stock', Tag='400')
self.StockButton.Click += self.OnAtrButtonClick
# Create button layout
atr_button_layout = forms.DynamicLayout()
atr_button_layout.Spacing = drawing.Size(5, 5)
atr_button_layout.Size = drawing.Size(550,300)
atr_button_layout.AddRow(image_btn_wood, image_btn_angle, image_btn_flatStock, image_btn_tubeRect, image_btn_tubeSquare, image_btn_tubeRound)
atr_button_layout.AddRow(self.WoodButton, self.AngleButton, self.FlatStockButton, self.TubeRectButton, self.TubeSquareButton, self.TubeRoundButton)
atr_button_layout.AddRow(None, None, None, None, None, None)
atr_button_layout.AddRow(None, None, None, None, None, None)
atr_button_layout.AddRow(image_btn_solidSquare, image_btn_solidRound, image_btn_solidHalf, image_btn_mesh, image_btn_pipe, image_btn_tBar)
atr_button_layout.AddRow(self.SolidSquareButton, self.SolidRoundButton, self.SolidHalfButton, self.MeshButton, self.PipeButton, self.TBarButton)
atr_button_layout.AddRow(None, None, None, None, None, None)
atr_button_layout.AddRow(None, None, None, None, None, None)
atr_button_layout.AddRow(image_btn_iBeam, image_btn_cChan, image_btn_plate, image_btn_cust, image_btn_stock, None)
atr_button_layout.AddRow(self.IBeamButton, self.CChanButton, self.PlateButton, self.CustButton, self.StockButton, None)
atr_button_layout.AddRow(None, None, None, None, None, None)
atr_button_layout.AddRow(None, None, None, None, None, None)
return atr_button_layout
# Find all objects with a beetle_PartNum partNum_key
def findObjects(self):
# Get all objects in file
ids = rs.AllObjects(include_lights=False, include_grips=False)
# Set some variables
partNum_key = 'beetle_PartNum'
PartNumAndGUIDs = {}
# for every id given run part finding
for id in ids:
# Get partNum from every object
partNumFound = rs.GetUserText(id, partNum_key, False)
# If part number is not empty make a dictionary
if partNumFound != None:
# Make a dictionary with part Number as Key and GUIDs as a list of values
PartNumAndGUIDs.setdefault(partNumFound, []).append(id)
# Return dictionary once loop is over
return PartNumAndGUIDs
def organizedKeys(self):
self.mainDictionary = self.findObjects()
# Get all keys from dictionary and organize them by numerical value
organizedKeys = sorted(list(self.mainDictionary), key=int)
return organizedKeys
# Form Closed event handler
def OnFormClosed(self, sender, e):
# Remove the events added in the initializer
#self.RemoveEvents()
# Dispose of the form and remove it from the sticky dictionary
if sc.sticky.has_key('attribute_form'):
form = sc.sticky['attribute_form']
if form:
#form.Dispose()
form = None
sc.sticky.Remove('attribute_form')
################################################################################
# Creating a dialog instance and displaying the dialog.
################################################################################
def TestViAttributeForm():
# See if the form is already visible
if sc.sticky.has_key('attribute_form'):
return
# Create and show form
form = viAttributeForm()
form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
form.Show()
# Add the form to the sticky dictionary so it
# survives when the main function ends.
sc.sticky['attribute_form'] = form
# RunCommand is the called when the user enters the command name in Rhino.
# The command name is defined by the filname minus "_cmd.py"
def RunCommand( is_interactive ):
print "Running", __commandname__
# For every organized key print guids
#for key in organizedKeys:
# print 'Part #: {} | Qty: {} | GUIDs: {}'.format(key, len(mainDictionary.get(key)), mainDictionary.get(key))
#return sorted(removeDup, key=int)
TestViAttributeForm()
if __name__ == "__main__":
RunCommand(True)