Combine two dataset
To show the programming in another small example, the task is to combine (merge) two polydata sets (i.e. points and lines) into one. The CO example is part of the installation and is found under polydata > Utilities > Combine two datasets.
The functionality can at its simplest level be written as one statement and a few comments. It utilizes the append functionality and the result is saved in active data.
# dataset1 is in active, dataset2 is in workspace. mlo dataset2 app ; # move low dataset2 to active and append to active.
Using only shell commands the task is straightforward. The user must place one dataset in workspace dataset2 and the other in active before executing the command.
In a menu oriented environment those preparations are part of the menu. The dataset to be placed in workspace must be browsed in from the project and the result dataset must be placed in the project through another browsing operation.
- List the first dataset in the menu. The one that this CO is run upon.
- Browse and list the second dataset.
- Specify where to save the result data by browsing the project.
- Control functionality for dataset operations
- Pragma statements to put the CO into its right group and context.
- Help text.
The menu example saves the new data either in the project or in workspace if blank in the result browsing field.
The menu for combining two datasets
Program listing of script
The example programmed with a menu is listed below.
In essence, the menu script is simple and self explaning. In practical programming one copy a similar command object and rewrite it to fit its use. The Geocap installation has many scripted command objects and its rational to copy and paste and look at examples.
Geocap::pragma commandtype {Combine two dataset} Geocap::pragma commandname {Combine two dataset} Geocap::pragma executionmode interactive Geocap::pragma namespacemode protected Geocap::pragma schema vtk_poly_data "Utilities" Geocap::pragma schemainpopup 1 Geocap::pragma operationmode "Default" active Geocap::pragma sharedcommand 1 Editing Geocap::pragma filterexecution 1 Geocap::pragma filtervisibility 1 Geocap::pragma systemfilter types category_generic vtk_poly_data Geocap::pragma version 1.0 "Generated menu. August 2007" Geocap::pragma version 2.0 "Updated operation mode. May 2009" Geocap::pragma documentation { <b>PURPOSE:</b> To combine two poly dataset into one poly dataset. <br><br>Combining two poly dataset into one means two combine their topology and cell structure and scalars if both have scalar values. <br><br>The order is according to which one is activated as the first dataset. <br><br>The result is saved in the project if a folder is browsed in." } Geocap::pragma prestep {} Geocap::pragma mainstep { # CO: Combine two dataset variable inputdataPm2 "EMPTY" ; # input data from PM variable combinedData "" ; # output data active variable combinedDataPm "EMPTY" ; # output data active proc InputFileBrowse2 {} { variable inputdataPm2 set file [project browse category_generic category_collection vtk_poly_data] if {$file == ""} return set inputdataPm2 $file } proc ReadInputFile2 {} { variable inputdataPm2 set file $inputdataPm2 if {$file == "" || $file == "EMPTY"} { qinformation "Error" "Could not read second file" "OK" return 0 } if {$file == "active"} {return 1} project setpath "$file" project getdata set ft [gvar filetype] if {$ft != 0} { qinformation "Error" "Incorrect dataset type for second file" "OK" return 0 } return 1 } proc combinedDataFileBrowse { } { variable combinedDataPm set file [project browse category_generic category_collection vtk_structured_points] if {$file == ""} return set combinedDataPm $file } namespace eval input { variable inputname variable space 3 variable margin 3 variable resultinfo "The result data is default saved as combinedData" } # Update procedure. # called after script and menu initialization # and before closing the menu proc update { what } { if { $what == "menu" } { # Update menu from script variables } elseif { $what == "vars" } { # Update variables from menu } } proc execute {} { variable combinedDataPm mhi ^inputdata set read [ReadInputFile2] if {$read == 0} return mhi ^inputdata2 mlo ^inputdata mlo ^inputdata2 app if {$combinedDataPm == "EMPTY" || $combinedDataPm == ""} { mhi combinedData set input::resultinfo "The dataset was saved in workspace as combinedData" } else { project setcreatedata $combinedDataPm combinedData set input::resultinfo "The dataset was saved into the project in $combinedDataPm" } dhi ^inputdata* } proc testproject { } { # projectobject is the activated dataset and it is checked. variable projectobject set pnames [project projectnames] if {$pnames == ""} {return 0} if {[info exist projectobject] == 0} {return 0} if {$projectobject == "{}" || $projectobject == "" } {return 0} return 1 } proc menu {} { variable projectobject variable inputdataPm2 variable combinedDataPm variable resultinfo set path "" if {[testproject] == 1} { set input::inputname $projectobject project setpath $projectobject set path [project path] } qvboxlayout { qsetspacing $input::space qsetmargin $input::margin qwidget qhboxlayout { qlabel "<b>Combining two poly dataset into one</b>" } qgroupbox "Project Data" qhboxlayout { qlabel "1. dataset" qlineedit qsettext $path qsettooltip "Selected data from the project" } qgroupbox "Dataset to be combined (merged)" qhboxlayout { qlabel "2. dataset" qlineedit qblockedtrace inputdataPm2 qsettooltip "Select data from the project" qpushbutton "Browse" {InputFileBrowse2} qsettooltip "Select a file from the project" } qgroupbox "Specify where to save the result data" qvboxlayout { qsetspacing $input::space qsetmargin $input::margin qwidget qhboxlayout { qlineedit qblockedtrace combinedDataPm qpushbutton "Browse" {combinedDataFileBrowse} qsettooltip "Select a file from the project" } qaddhboxlayout { qlabel qblockedtrace input::resultinfo } } qvspacer qlabel " Press <b>Execute</b> to perform combining" } } } Geocap::pragma poststep {} Geocap::pragma prototype 1 Geocap::pragma iconfile {Combine two dataset.png}