program MeasureToSheet ; { Illustrates how to create a spreadsheet and transfer data into it } { By Digital Optics, July 2008 } { How to use: - Set two flags to indicate a distance or size on an image - Press Measure to calculate the distance (in pixels) and insert it into a sheet - Next Column moves the selected cell to the start of the next column to the right - Reset Cell sets the next cell position to the currently selected cell - New Sheet starts a new spreadsheet - The next cell position is shown on the status bar - The spreadsheet will automatically grow to accomodate new measurements } toolbar 'Measure' ; const { Spreadsheet initial size } nRows = 10 ; nCols = 5 ; { Starting row and column numbers } R0 = 1 ; C0 = 1 ; var { global variables } Sheet ; Row,Col ; procedure ShowCurrentCell ; { Display the current cell position on the status bar } begin WriteStatus( 'Next cell: ',CellName( Col,Row ) ) ; end; { ShowCurrentCell } procedure CheckSheet ; { Make sure the spreadsheet exists and create a new one if not } var xs,ys ; begin GetSpreadsheetSize( Sheet,xs,ys ) ; if ( xs = 0 ) or ( ys = 0 ) then begin Sheet := CreateSpreadsheet( '',nCols,nRows ) ; Row := R0 ; Col := C0 ; SetSelectedCells( Sheet,Col,Row ) ; ShowCurrentCell ; end; end; { CheckSheet } procedure ResizeSheet ; { Check the spreadsheet size and increase it if required } var xs,ys ; begin GetSpreadsheetSize( Sheet,xs,ys ) ; if Col > xs then xs := Col ; if Row > ys then ys := Row ; SetSpreadsheetSize( Sheet,xs,ys ) ; SetSelectedCells( Sheet,Col,Row ) ; ShowCurrentCell ; end; { ResizeSheet } procedure MeasureAndSend ; button btn_Text,'Measure|Measure and record flagged distance' ; { Measure the distance between two flags and store it in the spreadsheet } var Image ; Flags ; x1,y1,x2,y2 ; d ; begin { get image } GetActiveImage( Image ) ; if IsImage( Image ) then begin { get list of flags } Flags := GetFlags( Image ) ; if IsArray( Flags ) then begin if GetYSize( Flags ) >= 2 then begin { get flag coordinates } x1 := Flags[0,0] ; y1 := Flags[1,0] ; x2 := Flags[0,1] ; y2 := Flags[1,1] ; { calculate and record distance } d := sqrt( sqr( x2-x1 ) + sqr( y2-y1 ) ) ; CheckSheet ; SetCellValue( Sheet,Col,Row,d ) ; { next row } Row := Row + 1 ; ResizeSheet ; { remove flags } DeleteFlags( Image ) ; end else WriteInfo( 'At least 2 flags are required' ) ; end else WriteInfo( 'Indicate the end-points using flags' ) ; end else WriteInfo( 'Select an image' ) ; end; procedure NextColumn ; button btn_Text,'Next Column|Move to next column' ; { Move the current position to the top of the next column to the right } var xs,ys ; begin Row := R0 ; Col := Col + 1 ; ResizeSheet ; end; procedure ResetCell ; button btn_Text,'Reset Cell|Reset to the selected cell' ; { Move the current position to the selected cell } var x,y ; begin GetSelectedCells( Sheet,x,y ) ; if ( x > 0 ) and ( y > 0 ) then begin Col := x ; Row := y ; ShowCurrentCell ; end; end; procedure NewSheet ; button btn_Text,'-New Sheet|Start a new spreadsheet' ; { Reset the current position and start a new set of measurements } begin Sheet := CreateSpreadsheet( '',nCols,nRows ) ; Row := R0 ; Col := C0 ; SetSelectedCells( Sheet,Col,Row ) ; ShowCurrentCell ; end; { Initialization } begin Sheet := -1 ; Row := R0 ; Col := C0 ; end.