LayerSelectTool.sml

  Download

More scripts: Display Toolbar

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# LayerSelectToolDemo.sml
# Started:  7 June 2005
# Updated:  9 June 2005
# Author: Dave Breitwisch, MicroImages
#################################################################################################################
# Functionality of ToolScript:
# 
# The purpose of this toolscript is to allow the user to use a rectangle tool to select layers in the view.  This
# can be done in the following ways:
#
#  Left click and drag the tool to create a rectangle over the layers you to be selected
#
#  Then ...
#
#    Right click - All layers that that overlap the rectangle that was drawn are displayed.  All other 
#                  layers are turned off.
#
#    Shift + Right click - All layers that overlap the drawn rectangle are displayed.  All other   
#                  layers remain in their previous state (on or off).
#
#    Ctrl + Right click - All layers that are completely enclosed in the rectangle that was drawn are turned
#                  off.  All other layers remain in their previous state (on or off).
#
#    Shift + Ctrl + Right click - All layers are turned on.
#
#################################################################################################################
#################################################################################################################
# View ToolScript
#
# 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
#
# 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.
#################################################################################################################
# Global Variables
numeric setDefaultWhenClose;
class MdispRegionTool tool;
numeric enclosed, overlapping;
#################################################################################################################
# Processes specific to this ToolScript
#################################################################################################################
proc compareRegions(class REGION2D selR, class REGION2D layR) {
# This procedure compares two regions and sets the values of overlapping and enclosed to true or false based
# on the results of the comparison
	if ((layR.Extents.x1 < selR.Extents.x2) && (layR.Extents.x2 > selR.Extents.x1)) {
		if ((layR.Extents.y1 < selR.Extents.y2) && (layR.Extents.y2 > selR.Extents.y1)) {
			overlapping = true;
		} 
		else {
			overlapping = false;
		}
	} 
	else {
		overlapping = false;
	}
	if ((layR.Extents.x1 > selR.Extents.x1) && (layR.Extents.x2 < selR.Extents.x2)) {
		if ((layR.Extents.y1 > selR.Extents.y1) && (layR.Extents.y2 < selR.Extents.y2)) {
			enclosed = true;
		} 
		else {
			enclosed = false;
		}
	} 
	else {
		enclosed = false;
	}
}
proc checkOverlap(class GRE_LAYER testLayer) {
# This process transforms the extents of the tool and the layer to View coordinates and then calls
# compareRegions() to compare them.
	local class REGION2D testRegion = tool.RegionData;
	local class REGION2D testLayerReg = testLayer.MapRegion;
	local class TRANSPARM tempTrans; 
	tempTrans = ViewGetTransViewToScreen(View, true);
	testRegion = RegionTrans(testRegion, tempTrans);
	tempTrans = ViewGetTransMapToView(View, testLayer.MapRegion.CoordRefSys);
	testLayerReg = RegionTrans(testLayerReg, tempTrans);
	compareRegions(testRegion, testLayerReg);
}
func checkLayer(class GRE_LAYER checkLayer) {
# This function returns whether or not the current layer matches the specifications the user wants.
# In it's current form, it only makes sure the layer has a type.  The commented-out section is an 
# example of how to specify what type of layers the user wants the script to work with.
	if (checkLayer.Type == "") {
		PopupMessage("Layer has no Type");
		return (false);
	}
#	if (checkLayer.Type == "Raster") {
#		if (checkLayer.Name.indexOf("Tile_r", 0) != -1 ) {   #####################
#			return (true);
#		}
#	}
#	return (false);
	return true;
}
proc cbClose()
# This process starts to deactive the tool if called
{
	tool.Managed = 0;
	if (setDefaultWhenClose)
	{
		setDefaultWhenClose = false;
		View.SetDefaultTool();
	}
}
proc cbToolApply(class MdispRegionTool tool) {
# Procedure called when the Right Mouse button is clicked after using the tool to 
# create a rectangle.  It calls the appropriate procedures to make the desired geographic 
# selection of layers.
	local numeric shiftWasPressed = ShiftPressed;
	local numeric ctrlWasPressed = CtrlPressed;
	local class GRE_GROUP currentGroup;
	local class GRE_LAYER currentLayer;
	if (Layout) {
		currentGroup = Layout.FirstGroup;
		currentLayer = Layout.FirstGroup.FirstLayer;
	} 
	else currentLayer = Group.FirstLayer;
	View.DisableRedraw = 1;
	# Cycles through all layers
	while (currentLayer != 0) {
		# See if layer matches criteria
		if (checkLayer(currentLayer)) {
			checkOverlap(currentLayer);
			local numeric visible;
			# Shift + Right Click
			if (shiftWasPressed && !ctrlWasPressed) {
				currentLayer.SetVisibleInView(View.GetViewNum(), overlapping || currentLayer.IsVisibleInView(View.GetViewNum()));
			}
			# Ctrl + Right Click
			 else if (!shiftWasPressed && ctrlWasPressed) {
				currentLayer.SetVisibleInView(View.GetViewNum(), !enclosed && currentLayer.IsVisibleInView(View.GetViewNum()));
			}
			# Shift + Ctrl + RightClick
			 else if (shiftWasPressed && ctrlWasPressed) {
				currentLayer.SetVisibleInView(View.GetViewNum(), true);
			}
			# Right Click
			 else {
				currentLayer.SetVisibleInView(View.GetViewNum(), overlapping);
			}
		}
		currentLayer = currentLayer.NextLayer;
		if (Layout) {
			if ((currentLayer == 0) && (currentGroup.NextGroup != 0)) {
				currentGroup = currentGroup.NextGroup;
				currentLayer = currentGroup.FirstLayer;
			}
		}
	}
	View.DisableRedraw = 0;
	View.RedrawIfNeeded();
	tool.HasPosition = 0;
	# Uncomment the following line to have tool deactivate after each use.
	#cbClose();
}
#################################################################################################################
# ToolScript Basic Processes
#################################################################################################################
# Called the first time the tool is activated.
# If the tool implements a dialog it should be created (but not displayed) here.
proc OnInitialize ()
{
	tool = ViewCreateRectangleTool(View);
	ToolAddCallback(tool.ActivateCallback, cbToolApply);
} # 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 ()
{
	tool.Managed = 0;
} # end of OnDestroy
# Called when tool is activated.
# If the tool implements a dialog it should be "managed" (displayed) here.
proc OnActivate ()
{
	tool.Managed = 1;
	setDefaultWhenClose = true;
}  # 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 ()
{
	setDefaultWhenClose = false;
	cbClose();
}  # end of OnDeactivate
#################################################################################################################
#################################################################################################################