# Amira-Script-Object V3.0 ######################################################################### # # Generate annotations from a cells selection in a spreadsheet. # author: Mickael Leslourdy # ######################################################################### # NO DEPRECATION WARNING ####################################################### # the constructor is called when a scro is created or restarted # and is used to create the user interface and initialize member variables ####################################################### "$this" proc constructor {} { global AMIRA_ROOT # hide the script file port, the user does not need it "$this" script hide "$this" setVar currentData "NODATA" # port information "$this" newPortInfo infoPort "$this" infoPort setLabel "Info:" "$this" infoPort setValue "Please select a source data" # port for column selection as axis "$this" newPortMultiMenu axisPort 1 1 1 "$this" axisPort setLabel "Axis:" "$this" axisPort setLabel 0 0 "NO SOURCE" "$this" axisPort setLabel 1 0 "NO SOURCE" "$this" axisPort setLabel 2 0 "NO SOURCE" $this setVar dataIs3D 1 # action port "$this" newPortDoIt action } ####################################################### # destructor is called when Annotation is destroyed ####################################################### "$this" proc destructor {} { } ####################################################### # Check if 'data' is a data supported by the script. ####################################################### proc isValidData { data } { if { $data == "" } { return 0 } elseif { [$data getTypeId] == "HxSpreadSheet" || [$data getTypeId] == "HxLabelAnalysis" || [$data getTypeId] == "HxImageAnalysis" } then { return 1 } return 0 } ####################################################### # Get the number of cols from a spreadsheet. ####################################################### proc getNumberOfCols { spreadsheet table } { if { $spreadsheet == "" || $table < 0 } { return -1 } elseif { [$spreadsheet getTypeId] == "HxSpreadSheet" } { return [$spreadsheet getNumCols $table] } elseif { [$spreadsheet getTypeId] == "HxLabelAnalysis" || [$spreadsheet getTypeId] == "HxImageAnalysis" } { return [$spreadsheet getNumOfCol $table] } return -1 } ####################################################### # Get the name of a col from a spreadsheet. ####################################################### proc getColumnName { spreadsheet table column } { if { $spreadsheet == "" || $table < 0 || $column < 0 } { return } elseif { [$spreadsheet getTypeId] == "HxSpreadSheet" } { return [$spreadsheet getColName $table $column] } elseif { [$spreadsheet getTypeId] == "HxLabelAnalysis" || [$spreadsheet getTypeId] == "HxImageAnalysis" } { return [$spreadsheet getColumName $table $column] } return } ####################################################### # Find if an HxMeasure module already exists ####################################################### proc findMeasurementModule {} { set objectList [all] foreach object $objectList { if { [$object getTypeId] == "HxMeasure" } { return $object } } return } ####################################################### # Fill the axisPort ####################################################### "$this" proc updateAxisPort { dataConnected } { "$this" setVar currentData $dataConnected if { [isValidData $dataConnected] == 0 } { "$this" axisPort setNum 0 1 "$this" axisPort setNum 1 1 "$this" axisPort setNum 2 1 "$this" axisPort setLabel 0 0 "NO SOURCE" "$this" axisPort setLabel 1 0 "NO SOURCE" "$this" axisPort setLabel 2 0 "NO SOURCE" "$this" infoPort setValue "Please select a valid source data" "$this" action setSensitivity 0 } else { # If the data is an HxSpreadSheet set nbTables [$dataConnected getNumTables] if { $nbTables == 1 } { set nbCols [getNumberOfCols $dataConnected 0] "$this" axisPort setNum 0 $nbCols "$this" axisPort setNum 1 $nbCols "$this" axisPort setNum 2 $nbCols for {set i 0} {$i < $nbCols} {incr i} { "$this" axisPort setLabel 0 $i [getColumnName $dataConnected 0 $i] "$this" axisPort setLabel 1 $i [getColumnName $dataConnected 0 $i] "$this" axisPort setLabel 2 $i [getColumnName $dataConnected 0 $i] } "$this" axisPort setValue 0 1 "$this" axisPort setValue 1 2 "$this" axisPort setValue 2 3 } else { for {set i 0} {$i < $nbTables} {incr i} { #set nbCols [$dataConnected getNumCols $i] } } "$this" action setSensitivity 1 "$this" axisPort touch } $this axisPort setMenuSensitivity 2 [$this getVar dataIs3D] } ####################################################### # the "update" method is called whenever a port is updated ####################################################### "$this" proc update {} { # If the input data changes, we need to update the axisPort if { ["$this" data isNew] && ["$this" data source] != ["$this" getVar currentData] } { set data [$this data source] if { [$data parameters hasParameter AnalyzedLabelImage] } { set labelData [$data parameters AnalyzedLabelImage getValue] set dims [$labelData getDims] $this setVar dataIs3D [expr [lindex $dims 2] != 1] } "$this" updateAxisPort ["$this" data source] } # If the axisPort changes, we need to update the information port if { ["$this" axisPort isNew] } { "$this" infoPort setValue "Press 'Apply' to generate annotations from a cell(s) selection in the table panel" } } ####################################################### # the "compute" method is called whenever a port has changed ####################################################### "$this" proc compute {} { # Apply was hit if {["$this" action wasHit ]} { # Check if there is a data set data ["$this" data source] if { $data == "" } { return } # Get the cell selection and catch tcl errors if needed. if { [catch {set selection [lindex ["$data" getSelection] 0]} error] } { "$this" infoPort setValue "The spreadsheet have to be displayed in the table panel and a cells selection have to be done." return } # Check if there is a selection set length [llength $selection] if { $length == 0 } { "$this" infoPort setValue "No selection done in the table panel." return } # Retrieve an HxMeasure module or create it if it doesn't exist set measureModule [findMeasurementModule] if { $measureModule == "" } { set measureModule [create HxMeasure] } # Generation of annotation texts set rowIndexList {} set descriptionList {} foreach cell $selection { if { [lindex $cell 0] == -1 } { continue } set colIndex [lindex $cell 0] set rowIndex [lindex $cell 1] set indexFind [lsearch $rowIndexList $rowIndex] if { $indexFind < 0 } { lappend rowIndexList $rowIndex lappend descriptionList [getColumnName $data 0 $colIndex]=[$data getValue $colIndex $rowIndex] } else { set concatString [lindex $descriptionList $indexFind] append concatString \n [getColumnName $data 0 $colIndex]=[$data getValue $colIndex $rowIndex] lset descriptionList $indexFind $concatString } } # Generation of annotation "All" setVisible 0 set cpt 0 foreach row $rowIndexList { set noteName Row[expr {$row + 1}] $measureModule GUI addMeasure Note "$noteName" "$noteName" startCreation "$noteName" setAllCalloutsVisible 1 "$noteName" textTitle setState $noteName "$noteName" textDescr setState [lindex $descriptionList $cpt] "$noteName" points setImmediate 0 "$noteName" points setOrtho 0 "$noteName" points showDragger 0 "$noteName" points showPoints 0 "$noteName" points setPointScale 1 "$noteName" points showOptionButton 0 "$noteName" points setNumPoints 1 1 100 set pointX [$data getValue ["$this" axisPort getValue 0] $row] set pointY [$data getValue ["$this" axisPort getValue 1] $row] if {[$this getVar dataIs3D]} { set pointZ [$data getValue ["$this" axisPort getValue 2] $row] } else { set pointZ 0 } "$noteName" points setValue $pointX $pointY $pointZ "$noteName" update "$noteName" finishCreation "$noteName" setCalloutVisible 0 1 "$noteName" setCalloutMinimized 0 0 incr cpt } "All" setVisible 1 "$this" infoPort setValue "Generation OK" } }