Scripting

 From:  Michael Gibson
7238.46 In reply to 7238.45 
Hi dune1982 - the UI for a command is handled by a .htm document that can contain various controls in it, and your command script can retrieve values from controls in the .htm file.

If you look in the \commands sub-folder you'll see many .htm files in addition to .js files - each of those .htm files is one of these files that is handling the UI for a particular command, look into some of them to see how the UI is set up.

You just give your own .htm file the same base filename as your .js script file, then the command infrastructure will make sure to load that .htm file and show it in the command options area in the upper right corner of the main window before your .js script is launched.

Here's an example .htm file that will have a control in it for putting in a diameter:

code:
<html>
	<body class="commandbody">
		<div class="commandheader">
			<div class="commandprompt">Command options</div>
		</div>
		
		<div class="commandoptions">
			<table>
				<tr>
					<td>Diameter:</td>
					
					<td>
						<moi:DistanceInput id="distanceinput" style="persist:false"/>
					</td>
				</tr>
			</table>
		</div>

		<moi:CommandCancel />
	</body>
</html>




Your .js command script can get access to the .htm document by using moi.ui.commandUI - that returns the global object of the HTML document. If your control has been given an id="" value then you'll be able to access it using that id, like for example to get the value property of the control above with id="distanceinput", your script would call: moi.ui.commandUI.distanceinput.value; - that will return the number of whatever value has been typed in there.

Typically a command that shows UI will wait in an event loop for UI events to be triggered, that way they can pause themselves until some controls are filled in and then the script can continue.

An event loop means the script will call waitForEvent() on a waitable object, some examples of waitable objects are a pointpicker which will trigger events when the point is finished being picked, or an objectpicker which triggers an event when selection is finished, or also another one is moi.ui.commandDialog which can be used to wait on UI events triggered by controls in the command option area. Any control that has an id="" value set on it will trigger a UI event with that id value when the control is activated, like for a button control when it's pushed or for an input control when the user has typed some value in it and pushed enter.

So here's how your script could pause itself and wait until the user typed in a value for the distance input control:

code:
	var commandDialog = moi.ui.commandDialog;
	
	while ( 1 )
	{
		// Wait for user input.

		if ( !commandDialog.waitForEvent() )
			return false; // if waitForEvent returns false the command is being canceled.
			
		if ( commandDialog.event == 'distanceinput' )
			break; // User entered a value in the id="distanceinput" control.
	}

        // Get the value from the control.
	var diameter = moi.ui.commandUI.distanceinput.value;