#============================================================================== # # Project: SharpImage # Module: Custom1.py # Language: IronPython # Author: Dan Mueller # Date: $Date$ # Revision: $Revision$ # # 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 the base script class import ImageToImageScript from ImageToImageScript import * # Add reference and import required libraries clr.AddReference("ManagedITK.Image.Cast") clr.AddReference("SharpImage.Extension.Custom1") from itk import * from SharpImage.Extension.Custom1 import * class Custom1Script(ImageToImageScriptObject): # ------------------------------------------------------------------------- Name = "Custom1" Help = """Insight Journal custom example. A form is displayed asking if the filter should be run.""" Parameters = """None""" Form = None RunFilter = False # ------------------------------------------------------------------------- def Run(self): """ Run the script. """ self.Initialise() def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ # ... # Init base ImageToImageScriptObject.Initialise( self ) # Set the output to always be a float self.Output = itkImage.New( itkPixelType.F, self.Input.Dimension ) # Show the form self.Form = siFormCustom1( ) self.Form.Continue += self.Continue self.Form.Cancel += self.Cancel self.ParentApplication.AddTool( self.Form ) # Allow user to interact with form self.StartedWork() self.ParentApplication.SetApplicationAsReady( 0 ) def Continue(self, sender, args): """ Continue the processing """ self.DoWork() def Cancel(self, sender, args): """ Cancel the script. """ self.WriteLineToConsole( "Script cancelled by user..." ) self.UnregisterEventHandlers() self.FinishedWork( True ) def ThreadedDoWork(self): """ Perform the main functions of the script on a background thread. """ try: # Setup self.ParentApplication.SetApplicationAsWorking() # Check if we are running the filter if (self.Form.RunFilter): # Run the filter # ... self.WriteInputName() filterCast = itkCastImageFilter.New( self.Input, self.Output ) self.AddEventHandlersToProcessObject( filterCast ) filterCast.SetInput( self.Input ) filterCast.Update() filterCast.GetOutput( self.Output ) self.DisconnectInputAndOutput() self.DisposeOfObject( filterCast ) self.WriteOutputName() else: # Don't run the filter #... self.WriteLineToConsole( "User selected to not run filter" ) # Finish self.FinishedWork( True ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( ) def Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ self.UnregisterEventHandlers() ImageToImageScriptObject.Finalise(self) def UnregisterEventHandlers(self): """ Unregister all event handlers. """ if (self.Form != None): self.Form.Continue -= self.Continue self.Form.Cancel -= self.Cancel self.Form = None