elemsel.sml

  Download

More scripts: Display Toolbar

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# ELEMSEL.SML
# View ToolScript
# Tool to allow selection of the nearest vector point or
# line or the enclosing polygon.
# Authors: Mark Smith and Randy Smith
# MicroImages, Inc.
# Version 6 May 2008
# Requires version 2007:73 of the TNT products
	#
	# The following symbols are predefined
	#    class GRE_VIEW View            {use to access the view the tool script is attached to}
	#    class GRE_GROUP Group          {use to access the group being viewed if the script is run from a group view}
	#    class GRE_LAYOUT Layout        {use to access the layout being viewed if the script is run from a layout view}
	#    numeric ToolIsActive           Will be 0 if tool is inactive or 1 if tool is active
	#
	# The following values are also predefined and are valid when the various On...()
	# functions are called which deal with pointer and keyboard events.
	#    numeric PointerX            Pointer X coordinate within view in pixels
	#    numeric PointerY            Pointer Y coordinate within view in pixels
	#    numeric ShiftPressed        1 if <shift> key being pressed or 0 if not
	#    numeric CtrlPressed         1 if <ctrl> key being pressed or 0 if not
	#    numeric LeftButtonPressed   1 if left pointer button pressed or 0 if not
	#    numeric RightButtonPressed  1 if right pointer button pressed or 0 if not
	#    numeric MiddleButtonPressed 1 if middle pointer button pressed or 0 if not
### GLOBAL VARIABLES
class GRE_GROUP activegroup;
class GRE_LAYER_VECTOR vectorLayer;
class RVC_VECTOR targetVector;
class RVC_GEOREFERENCE vecGeoref;
class STRING layerlabel$, mode$;
numeric toolOpen;
# variables for dialog controls
class GUI_DLG dlgwin;
class GUI_FORM_RADIOGROUP mode;
class GUI_CTRL_EDIT_STRING activeText;
###################################################################################
##############   USER-DEFINED FUNCTIONS AND PROCEDURES   ##########################
###################################################################################
# Checks layer to see if it is valid.
func checkLayer()
	{
	local numeric valid = true;
	# Get names layers if usable.  If not output error messages.
	# Get name of active layer if it is usable.  If not output an error message.
	if (activegroup.ActiveLayer.Type == "") {
		PopupMessage("Group has no layers!");
		valid = false;
		mode.SetEnabled(0);
		layerlabel$ = "";
		}
	else if (activegroup.ActiveLayer.Type == "Vector") {
		vectorLayer = activegroup.ActiveLayer;
		layerlabel$ = activegroup.ActiveLayer.Name;
		DispGetVectorFromLayer(targetVector, vectorLayer);
		targetVector.GetDefaultGeoref(vecGeoref);
		mode.SetEnabled(1);
		}
	else {
		PopupMessage("Active layer not a vector!");
		layerlabel$ = activegroup.ActiveLayer.Name;
		mode.SetEnabled(0);
		valid = false;
		}
	activeText.SetValueStr(layerlabel$);
	return valid;
	}
# Called when the active group changes.
proc cbGroup()
	{
	activegroup = Layout.ActiveGroup;
	}
# Called when active layer changes.
proc OnLayerChange()
	{
	if (toolOpen)
		checkLayer();
	}
# Called when user selects different vector selection mode.
proc OnModeChanged()
	{
	if ( mode.GetSelected() == "point" )
		{
		if (targetVector.$Info.NumPoints < 1) then
			PopupMessage("No points!");
		else mode$ = "point";
		}
	else if ( mode.GetSelected() == "line" )
		{
		if (targetVector.$Info.NumLines < 1) then
			PopupMessage("No lines!");
		else mode$ = "line";
		}
	else if ( mode.GetSelected() == "poly" )
		{
		if (targetVector.$Info.NumPolys < 1) then
			PopupMessage("No polygons!");
		else mode$ = "poly";
		}
		Group.ActiveLayer.UnhighlightAllElements();
	}
# Procedure for closing the tool dialog window.
proc OnClose()	{
	dlgwin.Close(0);
	toolOpen = false;
	View.SetDefaultTool();
	if (activegroup.ActiveLayer.Type == "Vector") then
		activegroup.ActiveLayer.UnhighlightAllElements();
	}
# The following script functions will be called (if used in the script) when
# the appropriate action or event occurs as described in the comments before each.
# To use a function, uncomment the lines containing the 'func' definition
# and ending brace '}' by removing the leftmost '#' on the line and add the
# function code between the two lines.
# Called the first time the tool is activated.
# If the tool implements a dialog it should be created (but not displayed) here.
proc OnInitialize ()
	{
	# variables for setting up the control dialog
	local string xml$;
	local numeric err;
	if (Layout) {
		WidgetAddCallback(Layout.GroupSelectedCallback, cbGroup);
		activegroup = Layout.ActiveGroup;
		}
	else {
		activegroup = Group;
		WidgetAddCallback(Group.LayerSelectedCallback, OnLayerChange);
		}
	# string with dialog specification
	xml$ = '<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE root SYSTEM "smlforms.dtd">
	<root>
		<dialog id="elemsel" Title="Select Element" Buttons="">
			<pane Orientation="horizontal">
				<label>Active Layer </label>
				<edittext id="activeText" Width="10" ReadOnly="true"/>
			</pane>
			<label>Select:</label>
			<radiogroup id="mode" Orientation="vertical" Enabled="false" OnSelection="OnModeChanged();">
				<item Value="point">Closest Point</item>
				<item Value="line">Closest Line</item>
				<item Value="poly">Enclosing Polygon</item>
			</radiogroup>
			<pane Orientation="horizontal">
				<label HorizResize="Expand"> </label>
				<pushbutton Name=" Close " HorizResize="Fixed" OnPressed="OnClose();"/>
			</pane>
		</dialog>
	</root>';
	### parse XML string; returns an error code (number < 0 ) if there are syntax errors
	local class XMLDOC doc;
	err = doc.Parse(xml$);
	if (err < 0)
		{
		PopupError(err); # Popup an error dialog. "Details" button shows syntax errors.
		Exit();
		}
	### declare class instance for the dialog element in the XML structure
	### and get the dialog handle from the XML structure.
	### Pop up an error dialog and exit if the dialog ID can't be found in the XML.
	class XMLNODE dlgnode;
	dlgnode = doc.GetElementByID("elemsel");
	if (dlgnode == 0)
		{
		PopupMessage("Could not find dialog node in XML document");
		Exit();
		}
	### set the XML structure in memory as the source for the dialog.
	dlgwin.SetXMLNode(dlgnode);
	dlgwin.CreateModeless();
	# get handles for dialog controls
	mode = dlgwin.GetCtrlByID("mode");
	activeText = dlgwin.GetCtrlByID("activeText");
	} # end of OnInitialize
# Called when tool is to be destroyed, will not be called if tool was never activated.
# If the tool implements a dialog it should be destroyed here.
# proc OnDestroy () {
# } # end of OnDestroy
# Called when tool is activated.
# If the tool implements a dialog it should be "managed" (displayed) here.
proc OnActivate ()
	{
	toolOpen = true;
	checkLayer();
	# Reset dialog window defaults
	mode.SetSelected("point");
	mode$ = "point";
	if (activegroup.ActiveLayer.Type == "Vector") then
		activegroup.ActiveLayer.UnhighlightAllElements();
	dlgwin.Open();
	}  # end of OnActivate
# Called when tool is deactivated (usually when switching to another tool).
# If the tool implements a dialog it should be "unmanaged" (hidden) here.
proc OnDeactivate ()
	{
	OnClose();
	}  # end of OnDeactivate
# Called when tool is to be 'suspended' during a redraw operation.
# proc OnSuspend () {
# }  # end of OnSuspend
# Called when tool is to be 'resumed' after a redraw operation.
# If the tool displays any graphics they should be updated by this function.
# proc OnResume () {
# }  # end of OnResume
# Called when user presses 'left' pointer/mouse button.
proc OnLeftButtonPress ()
	{
	# If the selected layer is not valid, don't do anything.
	if (checkLayer()) {
		# Set local variables
		local class POINT2D point;
		local numeric elementNum;
		local class TRANS2D_MAPGEN trans;		# coordinate transformation parameters
		# Find cursor position in screen coordinates and transform to
		# map coordinates for active layer.
		point.x = PointerX;
		point.y = PointerY;
		# convert point position from screen coordinates to map coordinates for the targetVector
		trans = View.GetTransViewToScreen(1);	# transformation from screen to view coordinates
		point = trans.ConvertPoint2DFwd(point);
		trans = View.GetTransMapToView(vecGeoref.GetCoordRefSys(), 1);	# transformation from view to layer map coordinates
		point = trans.ConvertPoint2DFwd(point);
		if (mode$ == "point") {
			elementNum = FindClosestPoint(targetVector, point.x, point.y, GetLastUsedGeorefObject(targetVector));
			if (elementNum > 0) then
				vectorLayer.Point.HighlightSingle(elementNum);
			}
		else if (mode$ == "line") {
			elementNum = FindClosestLine(targetVector, point.x, point.y, GetLastUsedGeorefObject(targetVector));
			if (elementNum > 0 ) then
				vectorLayer.Line.HighlightSingle(elementNum);
			}
		else if (mode$ == "poly") {
			elementNum = FindClosestPoly(targetVector, point.x, point.y, GetLastUsedGeorefObject(targetVector));
			if (elementNum > 0 ) then
				vectorLayer.Poly.HighlightSingle(elementNum);
			}
		}
	}  # end of OnLeftButtonPress
# Called when user presses 'right' pointer/mouse button.
# proc OnRightButtonPress () {
# }  # end of OnRightButtonPress
# Called when user presses 'middle' pointer/mouse button.
# proc OnMiddleButtonPress () {
# }  # end of OnMiddleButtonPress
# Called when user releases 'left' pointer/mouse button.
# proc OnLeftButtonRelease () {
# }  # end of OnLeftButtonRelease
# Called when user releases 'right' pointer/mouse button.
# proc OnRightButtonRelease () {
# }  # end of OnRightButtonRelease
# Called when user releases 'middle' pointer/mouse button.
# proc OnMiddleButtonRelease () {
# }  # end of OnMiddleButtonRelease
# Called when user moves cursor if no button being pressed
# proc OnPointerMoveNoButton () {
# }  # end of OnPointerMoveNoButton
# Called when user moves cursor while holding down button
# proc OnPointerMoveWithButton () {
# }  # end of OnPointerMoveWithButton
# Called when cursor enters window associated with view.
# proc OnEnterWindow () {
# }  # end of OnEnterWindow
# Called when cursor leaves window associated with view.
# proc OnLeaveWindow () {
# }  # end of OnLeaveWindow
# Called when user presses 'key' on keyboard.
# proc OnKeyPress (key) {
# }  # end of OnKeyPress