How to display values with units set in Project Preferences?

Example objects for this post can be downloaded here.

The DEFINE STYLE{2} command is recommended to use with the PARAGRAPH, TEXTBLOCK and RICHTEXT2 command. The usage of these commands is presented on the Advanced Text Handling page. In the following example the actual Area, Volume, Width, Depth and Height values are displayed of a block with units set in Options/Project Preferences/Dimensions and Options/Project Preferences/Calculation & Rules. The values are left-aligned to the middle of the block (blockWidth/2).

! ------------------------------------------------------------------------------
! Input parameters controlled by the user:
! ------------------------------------------------------------------------------
! sampleSize 		- (RealNum) indicates the height of the font from the
! 		 	  baseline to the cap height, given in Paper size - mm
! fontType 		- (String) font family of the displayed text
! stAreaName 		- (String) the name of the block area value
! stVolumeName 		- (String) the name of the block volume value
! stWidthName 		- (String) the name of the block width value
! stDepthName 		- (String) the name of the block depth value
! stHeightName 		- (String) the name of the block height value
! ------------------------------------------------------------------------------
! Draw the block in 2D
! ------------------------------------------------------------------------------
rect2 0, 0, blockWidth, blockDepth

! ------------------------------------------------------------------------------
! Set input data for the text drawing
! ------------------------------------------------------------------------------

! Set the multiplier of the paper scale
PAPER_TO_MODEL = GLOB_SCALE / 1000

! Calculate the values to display
_areaValue 	= blockWidth * blockDepth
_volumeValue 	= blockWidth * blockDepth * ZZYZX

! Set text coordinate variable:
! samplesize is given in Paper Size - mm, convert it into Model size  - m
! calculate the row height from it, 
! therefore the row height will not depend on the font family
dy = - sampleSize * 1.5 * PAPER_TO_MODEL

! ------------------------------------------------------------------------------
! Define the text styles
! ------------------------------------------------------------------------------
! Not all of the following variables are used in this example, but the kind of
! definition showed below is the GRAPHISOFT standard constant definition

! Text styles for define style and define style{2}
NORMAL		= 0
BOLD 		= 1
ITALIC 		= 2
UNDERLINE 	= 4
SUPERSCRIPT	= 32
SUBSCRIPT	= 64
STRIKETHROUGH	= 128


! Anchor positions for define style
LEFT_UPPER	= 1
MIDDLE_UPPER	= 2
RIGHT_UPPER	= 3
LEFT_MIDDLE	= 4
MIDDLE		= 5
RIGHT_MIDDLE	= 6
LEFT_LOWER	= 7
MIDDLE_LOWER	= 8
RIGHT_LOWER	= 9

! Define the different text styles
define style "ItalicNameStyle" fontType, sampleSize, LEFT_UPPER, ITALIC
define style "NormalNameStyle" fontType, sampleSize, LEFT_UPPER, NORMAL

! The textblock is in model size (fixed_height = 0), 
! therefore the samplesize parameter should be converted to Model Size - m
define style{2} "NormalValueStyle" 	fontType,\
		sampleSize * PAPER_TO_MODEL, NORMAL
define style{2} "SuperscriptValueStyle" fontType,\
		sampleSize * PAPER_TO_MODEL, SUPERSCRIPT

! ------------------------------------------------------------------------------
! Display the texts in the first column
! ------------------------------------------------------------------------------
style "ItalicNameStyle"		! Set the previously defined style

text2 0, 0, 		stAreaName
text2 0, dy, 		stVolumeName

style "NormalNameStyle"		! Set the previously defined style

text2 0, dy * 2, 	stWidthName
text2 0, dy * 3, 	stDepthName
text2 0, dy * 4, 	stHeightName

The format of the linear dimensions can be known by using the “Linear_Dimension” request. It returns the format set in Options/Project Preferences/Dimensions/Linear Dimensions. The Width, Depth and Height of the block is displayed in this format. For the format of the Area and Volume values the Options/Project Preferences/Calculation Units settings are used by the “Calc_area_unit” and “Calc_volume_unit” requests.

TipsAndTricks_ValueUnits_Ex1

After getting the formats into the proper variables (_stLinearFormat, _stAreaFormat, _stVolumeFormat), the unit set on Project Preferences can be known by searching through the format string by the STRSTR function for the possible conversion specifiers. Note that conversion specifiers as strings could contain other conversion specifiers. To avoid overwriting the unit string variables, first those conversion specifiers have to be found which contain other ones. If those have been found, the unit string variables have to maintain their given values. For example the “sqm” conversion specifier can be found in a format string containing the “sqm” and also the “sqmm” conversion specifiers. Therefore first the “sqmm” conversion specifier has to be searched, and if it is found, the _stUnit and _stUnitIndex variables should not be reset. If a conversion specifier has been found, the _bUnitIsFound variable is set to 1, and the actual value of the _bUnitIsFound variable is always checked before setting the _stUnit and the _stUnitIndex variable.

Before each value display the _stUnit and the _stUnitIndex variables are initialized as empty string. When the actual conversion specifier has been found, the _stUnit gets the corresponding unit starting with a space character in order to separate the unit from the numeric value. If there is no supescript character in the corresponding unit, the value of the _stUnitIndex variable remains an empty string.

! ------------------------------------------------------------------------------
! Get the dimension formats
! ------------------------------------------------------------------------------
! Initialize string variables
_stLinearFormat 	= ""
_stAreaFormat 		= ""
_stVolumeFormat 	= ""

! Get the dimension formats set in the 
! Options/Project Preferences/Dimensions and Calculation Units dialog boxes
! For the block Width, Depth and Height the linear dimension request is used

r = request ("Linear_Dimension",  "", _stLinearFormat)
r = request ("Calc_area_unit",	  "", _stAreaFormat)
r = request ("Calc_volume_unit",  "", _stVolumeFormat)

! ==============================================================================
! Setting the input variables for the subroutin
! ==============================================================================

! ------------------------------------------------------------------------------
! Area Value
! ------------------------------------------------------------------------------
! Set a variable to check if a unit has been found, because
! "sqmm" contains "sqm", therefore it would reset the _stUnit
_bUnitIsFound 		= 0

! Initialize the Unit and Unit Index variables
_stUnit 		= ""
_stUnitIndex 		= ""

if strstr (_stAreaFormat, "sqcm") & not(_bUnitIsFound) then

	! If the proper conversion specifier is found, set the unit string
	! variables and also the _bUnitIsFound variable to avoid overwriting them
	_stUnit 	= " cm"	
	_stUnitIndex 	= "2"
	_bUnitIsFound  	= 1
endif

! First the "mm" unit types should be checked, because it contains
! the "m" unit, otherwise wrong unit could be found
if strstr (_stAreaFormat, "sqmm") & not(_bUnitIsFound) then
	_stUnit 	= " mm"	
	_stUnitIndex 	= "2"
	_bUnitIsFound  	= 1
endif
if strstr (_stAreaFormat, "sqm") & not(_bUnitIsFound)  then
	_stUnit 	= " m"  
	_stUnitIndex 	= "2"
	_bUnitIsFound  	= 1
endif
if strstr (_stAreaFormat, "sqf") & not(_bUnitIsFound) then
	_stUnit 	= " sq ft"
	_bUnitIsFound  	= 1
endif
if strstr (_stAreaFormat, "sqi") & not(_bUnitIsFound) then
	_stUnit 	= " sq inch"
	_bUnitIsFound  	= 1 
endif

After setting the _stUnit and the _stUnitIndex variables, the actual numeric value have to be converted into string type with the format returned by the actual request. For this conversion the STR function is used. The formatted value string, the unit string and if it exists, the superscipt unit string are the main data needed for displaying the value. For the PARAGRAPH and the TEXTBLOCK commands a unique name is required, for the RICHTEXT2 command the vertical position has to be set (in this example the horizontal position is permanent).

! Set the input variables for the subroutin
! Format the Area value with the _stAreaFormat, 
! which has got from the  "Calc_area_unit" request
_stFormattedArea 	= str(_stAreaFormat, _areaValue)
_stParagraphName	= "areaPara"
_stTextblockName	= "textblockArea"
_stValue 		= _stFormattedArea
_yPos			= 0
gosub "displayValues"

! ------------------------------------------------------------------------------
! Volume Value
! ------------------------------------------------------------------------------
_bUnitIsFound 		= 0
_stUnit 		= ""
_stUnitIndex 		= ""

if strstr (_stVolumeFormat, "cucm") & not(_bUnitIsFound) then
	_stUnit 	= " cm"	
	_stUnitIndex 	= "3"
	_bUnitIsFound  	= 1	
endif
if strstr (_stVolumeFormat, "cumm") & not(_bUnitIsFound) then
	_stUnit 	= " mm"	
	_stUnitIndex 	= "3"
	_bUnitIsFound  	= 1	
endif
if strstr (_stVolumeFormat, "cum") & not(_bUnitIsFound)  then
	_stUnit 	= " m"  
	_stUnitIndex 	= "3"
	_bUnitIsFound 	= 1
endif
if strstr (_stVolumeFormat, "cuf") & not(_bUnitIsFound) then
	_stUnit 	= " cu ft"
	_bUnitIsFound  	= 1
endif
if strstr (_stVolumeFormat, "cui") & not(_bUnitIsFound) then
	_stUnit 	= " cu inch"
	_bUnitIsFound  	= 1
endif
if strstr (_stVolumeFormat, "cuy") & not(_bUnitIsFound) then
	_stUnit 	= " cu yards"
	_bUnitIsFound  	= 1
endif

! Set the input variables for the subroutin
_stFormattedVolume 	= str(_stVolumeFormat, _volumeValue)
_stParagraphName	= "volumePara"
_stTextblockName	= "textblockVolume"
_stValue 		= _stFormattedVolume
_yPos			= dy
gosub "displayValues"

! ------------------------------------------------------------------------------
! Width, Depth, Height Values
! ------------------------------------------------------------------------------
! For all the three values _stLinearFormat format is used
_bUnitIsFound 		= 0
_stUnit 		= ""
_stUnitIndex 		= ""

if strstr (_stLinearFormat, "mm") & not(_bUnitIsFound) then
	_stUnit 	= " mm"	
	_bUnitIsFound  	= 1
endif
if strstr (_stLinearFormat, "cm") & not(_bUnitIsFound) then
	_stUnit 	= " cm"	
	_bUnitIsFound  	= 1	
endif
if strstr (_stLinearFormat, "m") & not(_bUnitIsFound)  then
	_stUnit 	= " m"  
	_bUnitIsFound  	= 1
endif
if 	strstr (_stLinearFormat, "f") |  strstr (_stLinearFormat, "i") &\
	not(_bUnitIsFound) then

	_stUnit 	= ""
	_bUnitIsFound  	= 1
endif

! Set the input variables for the subroutin
_stValue 		= str(_stLinearFormat, blockWidth)
_stParagraphName	= "widthPara"
_stTextblockName	= "textblockWidth"
_yPos			= dy * 2
gosub "displayValues"

_stValue 		= str(_stLinearFormat, blockDepth)
_stParagraphName	= "depthPara"
_stTextblockName	= "textblockDepth"
_yPos			= dy * 3
gosub "displayValues"

_stValue 		= str(_stLinearFormat, ZZYZX)
_stParagraphName	= "heightPara"
_stTextblockName	= "textblockHeight"
_yPos			= dy * 4
gosub "displayValues"


! ==============================================================================
end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! end ! en
! ==============================================================================

! ==============================================================================
! Create paragraphs, textblock 
! and display texts in the second column with richtext2
!-------------------------------------------------------------------------------
! Subroutin input variables:
! _stValue	   - (String) The displayed value without unit as a string
! _stUnit	   - (String) The unit of the actual displayed value 
!		     without index
! _stUnitIndex	   - (String) Superscript index of the unit
! _stParagraphName - (String) Name of the paragraph for the 
!		     actual displayed value
! _stTextblockName - (String) Name of the textblock for the 
!		     actual displayed value
!_yPos		   - (Length) Vertical position of the actual displayed value
!		     in Model Size (m)
! ==============================================================================
"displayValues":
    ! --------------------------------------------------------------------------
    ! Create paragraph
    ! --------------------------------------------------------------------------
    ! name: 		input variable
    ! alignment: 	1 - left aligned
    ! firstline_indent:	0 - no indention
    ! left_indent: 	0 - no indention
    ! right_indent: 	0 - no indention
    ! line_spacing:	1 - automatic line space

    paragraph _stParagraphName 1,
		0, 0, 0,
		1
		style "NormalValueStyle"
		_stValue + _stUnit
		style "SuperscriptValueStyle"
		_stUnitIndex
    endparagraph

    ! --------------------------------------------------------------------------
    ! Create texblock
    ! --------------------------------------------------------------------------
    ! name: 		input variable
    ! width: 		blockWidth/2 - Model Size, m
    ! anchor: 		1 - left upper
    ! angle: 		0 - no rotation
    ! width_factor: 	1 - automatic lineheight
    ! charspace_factor:	1 - automatic character space
    ! fixed_height:	0 - scale dependent (Model Size, m)
    ! string_expri:	input variable

    textblock _stTextblockName blockWidth/2,
		1, 0, 1, 1, 0,
		_stParagraphName

    ! --------------------------------------------------------------------------
    ! Display values with richtext2
    ! --------------------------------------------------------------------------
    richtext2 blockWidth/2, _yPos, _stTextblockName
return

Command and function pool:
RECT2
GLOB_SCALE
DEFINE STYLE
DEFINE STYLE{2}
[SET] STYLE
TEXT2
“Linear_Dimension”
“Calc_area_unit”
“Calc_volume_unit”
STRSTR
STR
GOSUB
PARAGRAPH
TEXTBLOCK
RICHTEXT2