#============================================================================== # # Project: SharpImage # Module: Script.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ # Revision: $Revision: 2 $ # # Copyright (c) Queensland University of Technology (QUT) 2007. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the above copyright notices for more information. # #============================================================================== import System, clr, sys # Add references to the desired assemblies clr.AddReference("IronPython") clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms") clr.AddReference("ManagedITK.Common") clr.AddReference("SharpImage") # Import symbols from desired assemblies from IronPython.Runtime.Calls import * from System import * from System.IO import * from System.Math import * from System.Collections import * from System.Collections.Generic import * from System.Drawing import * from System.Drawing.Imaging import * from System.Windows.Forms import * from System.Diagnostics import * from itk import * from SharpImage import * from SharpImage.Main import * from SharpImage.Forms import * from SharpImage.Script import * from SharpImage.Rendering import * from SharpImage.SpatialObject import * # Setup for using arrays array = System.Array class ScriptObject: Name = "Script" Help = "No help available for this script." Parameters = "None" ParentApplication = None FilePath = None Handlers = None Success = True def Run(self): """ The entry-point for this script. """ try: self.Initialise() self.DoWork() self.Finalise() except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise() def Initialise(self): """ Initialise the environment for running this script. """ self.ParentApplication.SetApplicationAsWorking() self.Handlers["ScriptRegisterName"]( self.Name ) def StartedWork(self): """ Inform listeners that the work has started. """ self.Handlers["ScriptStarted"]() def FinishedWork(self, success): """ Inform listeners that the work has finished. """ self.Success = success; self.Handlers["ScriptCompleted"]( success ) def DoWork(self): """ Perform the main functions of the script. """ # Subclasses to override def Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ self.ParentApplication.SetApplicationAsReady( 0 ) self.Handlers["ScriptFinalised"]() def WriteHelp(self): """ Write the help information associated with this script to the console. """ header = "===== Help: @self.Name@ =====" params = "---- Parameters ---- \r @self.Parameters@" header = header.replace( "@self.Name@", self.Name ) params = params.replace( "@self.Parameters@", self.Parameters ) self.WriteLineToConsole( header + "\r" + self.Help + "\r" + params + "\r" ) self.ParentApplication.SetApplicationAsReady( 0 ) def AddEventHandlersToProcessObject(self, process): """ Add Started, Ended and Progress event handlers to the ProcessObject. """ process.Started += self.Handlers["ScriptFilterStarted"] process.Ended += self.Handlers["ScriptFilterEnded"] process.Progress += self.Handlers["ScriptFilterProgress"] def WriteImageName(self, header, image): """ Write the given header and image name to the console. """ self.WriteLineToConsole( header + Path.GetFileName(image.Name) ) def WriteLineToConsole(self, value): """ Print the given message to the console. """ self.Handlers["ScriptWriteLineToConsole"]( value ) def WriteTask(self, task): """ Print the given task message to the console and the parent app status label. """ self.WriteLineToConsole( task ) self.ParentApplication.SetApplicationStatusLabel( task ) def HandleException(self, ex): """ Pass an exception to the application for handling. """ self.Handlers["ScriptException"]( ex ) def DisposeOfObject(self, object): """ Dispose and nullify the given disposable object. """ if (object != None): object.Dispose() object = None def GetOpenImageMatchingPattern(self, pattern): """ Search through the first inputs of all Renderers and returns the the image containing the given pattern in it's Name property. Return None if there is no image path matching pattern or if an exception is raised. """ try: if (pattern == None): return None for renderer in self.ParentApplication.Renderers: if (renderer != None and renderer.Inputs.Count > 0): if (renderer.Inputs[0].Name.ToLower().Contains( pattern.ToLower() )): return renderer.Inputs[0] return None except Exception, ex: return None def GetAllRenderersMatchingPattern(self, pattern): """ Return all the renderers with first images matching the given pattern. """ result = List[siRenderer]() for renderer in self.ParentApplication.Renderers: if (renderer != None and renderer.Inputs.Count > 0): if (renderer.Inputs[0].Name.ToLower().Contains( pattern.ToLower() )): result.Add( renderer ) return result def ArrayToString(self, array): """ Convert an array to a string in the format [XX,XX,...]. """ result = "[" for i in range( len(array) ): result += array[i].ToString() + ", " return ( result.TrimEnd(' ', ',') + "]" )