16. Advanced Text Handling

 

  Example object for this post

The DEFINE STYLE{2} command is recommended to use with the PARAGRAPH, TEXTBLOCK and RICHTEXT command. In the following example the actual Area, Volume, Width, Depth and Height values are displayed of a block with m², m³ and m units. The values are left-aligned to the middle of the block (blockWidth/2).

GDLBasics_AdvancedTextHandling_Ex1

If the texts are needed to be used in model space, the fixed_height argument of the TEXTBLOCK command have to be set to 0. In this case the “samplesize” parameter, which is given in Paper size – mm by the user, have to be converted into Model size – m for the DEFINE STYLE{2} command.

! ------------------------------------------------------------------------------
! 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

GDLBasics_AdvancedTextHandling_Ex2

In the following example all of the values are displayed in the same format: The unit is “m”, two decimals are displayed and the zero decimals are hidden. For displaying other units, see the How to display values with units set in Project Preferences? post. The STR function creates a string from the numeric expressions using this format (_stValue). The unit string (_stUnit) is added to the formatted value with a space and gets the same text style. For the unit index (_stUnitIndex) the previously defined “SuperscriptValueStyle” is used. The area value unit gets an additional “2” superscript, the volume value unit gets a “3” superscript, in case of linear dimension the value of the _stUnitIndex remains an empty string. In order to avoid duplicated scripts, the PARAGRAPH and TEXTBLOCK creation and the RICHTEXT2 are in a subroutin.

GDLBasics_AdvancedTextHandling_Ex3

! ==============================================================================
! Setting the input variables for the subroutin
! ==============================================================================
_format 	= "%~.2m" 	! Unit: meter, Decimals: 2, Hide zero decimals
_stUnit 	= " m" 	 	! Displayed unit after the values

! ------------------------------------------------------------------------------
! Area Value
! ------------------------------------------------------------------------------
_stUnitIndex 		= "2"
_stValue		= str(_format, _areaValue)
_stParagraphName	= "areaPara"
_stTextblockName	= "areaTextblock"
_yPos			= 0
gosub "displayValues"

! ------------------------------------------------------------------------------
! Volume Value
! ------------------------------------------------------------------------------
_stUnitIndex 		= "3"
_stValue		= str(_format, _volumeValue)
_stParagraphName	= "volumePara"
_stTextblockName	= "volumeTextblock"
_yPos			= dy
gosub "displayValues"

! ------------------------------------------------------------------------------
! Width Value
! ------------------------------------------------------------------------------
_stUnitIndex 		= ""
_stValue 		= str(_format, blockWidth)
_stParagraphName	= "widthPara"
_stTextblockName	= "widthTextblock"
_yPos			= dy * 2
gosub "displayValues"

! ------------------------------------------------------------------------------
! Depth Value
! ------------------------------------------------------------------------------
_stUnitIndex 		= ""
_stValue 		= str(_format, blockDepth)
_stParagraphName	= "depthPara"
_stTextblockName	= "depthTextblock"
_yPos			= dy * 3
gosub "displayValues"

! ------------------------------------------------------------------------------
! Height Value
! ------------------------------------------------------------------------------
_stUnitIndex 		= ""
_stValue 		= str(_format, ZZYZX)
_stParagraphName	= "heightPara"
_stTextblockName	= "heightTextblock"
_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 textblock
    ! --------------------------------------------------------------------------
    ! 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
STR
GOSUB
PARAGRAPH
TEXTBLOCK
RICHTEXT2