Jump to content

Selection element list-user defined script function


---
 Share

Recommended Posts

Hello everyone,

I want to create a dialog window in which there will be selection list. First, i created some variables and then want user to select the name of one of  the created  variable in the dialog. Since i did not create an element and only create a  variable in workspace, i could not create selection list which feed from  names of the created variables.

Is there a way to do that through scripting before creating an actual  element like plane, point etc.?

Many thanks,

Canset

Link to comment
Share on other sites

If it is not possible, could you please provide an example code about the usage of user-defined script function?

Many thanks,

Canset

Link to comment
Share on other sites

Here is a dialog that you can pass a list of items to (and a prompt if desired), and it will have the user to choose one of them. The return value is a string of whichever one was selected. 

It also divides the items into columns of 20 (if you have more than 20 items) to keep it from becoming too long

 

def selection(prompt='', item_list=[]):
	
	if not isinstance(item_list, list):
		
		return
	
	header = '<dialog>' \
			'<title>*</title>' \
			'<control id="Empty"/>' \
			'<position>center</position>' \
			'<sizemode>fixed</sizemode>' \
			'<size width="200" height="100"/>' \
			'<content rows="{}" columns="{}">' \
			'<widget row="0" column="0" type="label">' \
			'<name>prompt</name>' \
			'<text>{}</text>' \
			'</widget>'
	
	button = '<widget column="{}" row="{}" type="button::pushbutton">' \
			'<name>{}</name>' \
			'<text>{}</text>' \
			'<type>toggle</type>' \
			'<focus>True</focus>' \
			'</widget>'
	
	def build_dialog():
		
		num_columns = int(len(item_list) / 20) + 1
		
		dialog = header.format(len(item_list) + 1, num_columns, prompt)
		
		index = 0
		
		for i in range(1, len(item_list) + 1):
			
			if i <= 20:
				
				row = i
				
				column = index
				
				if i == 20:
					
					index += 1
			else:
				
				row = abs(index * 20 - i)
				
				column = index
				
				if i % 20 == 0:
					
					index += 1
			
			dialog += button.format(column, row, i, item_list[i - 1])
		
		return dialog + '</content></dialog>'
	
	def dialog_event_handler(widget):
		
		global selected
		
		if isinstance(widget, gom.Widget):
			
			selected = widget.text
			
			gom.script.sys.close_user_defined_dialog(dialog=dialog)
	
	dialog = gom.script.sys.create_user_defined_dialog(content=build_dialog())
	
	dialog.handler = dialog_event_handler
	
	gom.script.sys.show_user_defined_dialog(dialog=dialog)
	
	return selected

 

Link to comment
Share on other sites

Hi Robert,

Many thanks for the quick response.  I implemented the code into my program and it works fine for me.

Thanks,

Canset

Link to comment
Share on other sites

 Share

×
×
  • Create New...