Changeset 2134 for trunk/lang/tcl/scripts/objects/number/number.rp
- Timestamp:
- Mar 14, 2011, 10:58:06 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/tcl/scripts/objects/number/number.rp
r2080 r2134 1 # ---------------------------------------------------------------------- 2 # RAPPTURE OBJECT: number 3 # 4 # A number is a real value with units of measure. It is usually 5 # used as an input, but it can also be an output from a simulation. 6 # 7 # ====================================================================== 8 # AUTHOR: Michael McLennan, Purdue University 9 # Copyright (c) 2004-2011 Purdue Research Foundation 10 # 11 # See the file "license.terms" for information on usage and 12 # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. 13 # ====================================================================== 14 1 15 object number -extends base { 2 palettes "Inputs" 16 palettes "Inputs" "Outputs" 17 3 18 help http://rappture.org/wiki/rp_xml_ele_number 19 4 20 attr default -title "Default Value" -type string:validate=number -path default 5 21 attr units -title "Units of Measurement" -type units -path units … … 7 23 attr max -title "Maximum Value" -type string:validate=number -path max 8 24 attr color -title "Color" -type color -path color 9 10 check label {11 if {[string length [string trim $attr(label)]] == 0} {12 return [list warning "Should set a label that describes this value"]13 }14 }15 check description {16 set desc [string trim $attr(description)]17 if {[string length $desc] == 0} {18 return [list warning "Should include a description of what this value represents, typical settings, etc."]19 } elseif {[string equal $desc $attr(label)]} {20 return [list warning "Description should be different from the label and give additional information about the value."]21 }22 }23 25 24 26 check default { … … 76 78 } 77 79 } 80 81 storage { 82 private variable _num 0.0 ;# real number value 83 private variable _units "" ;# physical units 84 } 85 86 import xml {xmlobj path} { 87 set units [getAttr units $xmlobj $path] 88 import_string [$xmlobj get $path.current] $units 89 } 90 91 export xml {xmlobj path} { 92 $xmlobj put $path "${_num}${_units}" 93 } 94 95 import string {val {defunits ""}} { 96 puts "IMPORTING: $val (number with units $defunits)" 97 if {[regexp {^([-+]?(?:[0-9]+\.|\.)?[0-9]+(?:[eE][-+]?[0-9]+)?)( *(?:[a-zA-Z]+[0-9]*)+(?:\/(?:[a-zA-Z]+[0-9]*)+)*)*$} $val match num units]} { 98 set sys [Rappture::Units::System::for $units] 99 set base [Rappture::Units::System::for $defunits] 100 if {$sys eq "" && $base ne ""} { 101 error "don't recognize value" 102 } 103 if {$sys ne $base} { 104 error "bad units \"$sys\": should be $base" 105 } 106 set _num $num 107 set _units $units 108 } elseif {[string is double -strict $val]} { 109 set _num $val 110 set _units $defunits 111 } else { 112 error "don't recognize value" 113 } 114 } 115 116 export string {var} { 117 upvar $var val 118 set val "${_num}${_units}" 119 } 120 121 compare { 122 set sys1 [Rappture::Units::System::for $_units] 123 set sys2 [Rappture::Units::System::for $_units2] 124 if {$sys1 ne $sys2} { 125 # different units systems -- order by physical quantities 126 return [string compare $sys1 $sys2] 127 } 128 129 # normalize the value of num1 130 if {$_units ne ""} { 131 set num1 [Rappture::Units::convert $_num \ 132 -context $_units -to $_units -units off] 133 } else { 134 set num1 $_num 135 } 136 137 # normalize the value of num2 138 if {$_units2 ne ""} { 139 set num2 [Rappture::Units::convert $_num2 \ 140 -context $_units2 -to $_units -units off] 141 } else { 142 set num2 $_num2 143 } 144 145 return [cmpdbl $num1 $num2] 146 } 78 147 }
Note: See TracChangeset
for help on using the changeset viewer.