Continuing the discussion from Since SU2017 exploding a group replaces all entities:
Scenario: Given a numeric value of cubic inches, … get a formatted string of volume in model units.
(Note, Version 1, previously posted in another thread, was a “cutsie” one-liner that failed.)
VERSION 2
Barebones but might fail if the "SuppressUnitsDisplay"
switch is true
.
(See discussion and Version 3, below.)
(Click to show Version 2) ...
# format_volume_in_model_units( volume, spacing, precision )
#
# @param volume [Numeric] The volume in cubic inches.
# @param spacing [Integer] The spaces before unit symbol. (Default=0)
# @param precision [Integer] The numbers shown after decimal point.
# Defaults to the current model units precision (Range 0..6). Allows higher.
# @return [String] The volume in cubic model units.
def format_volume_in_model_units(
volume,
spacing = 0,
precision = Sketchup::active_model.options['UnitsOptions']['LengthPrecision']
)
inch_side = Math::cbrt(volume)
unit_side = Sketchup::format_length(inch_side).to_f
unit_vol = unit_side**3
unit = Sketchup::format_length(1.0).split(/~?\s*\d+\.?\d*/).last
unit = 'in' if unit == '"'
unit = 'ft' if unit == "'"
"%.#{precision}f%s%s³" % [ unit_vol, ' '*spacing, unit ]
end
VERSION 3
Incorporating some modifications discussed below by Christina …
(Click to show Version 3) ...
# format_volume_in_model_units( volume, spacing, precision )
#
# @param volume [Numeric] The volume in cubic inches.
# @param hideunit [Boolean] Whether to hide the unit symbols, or not.
# Defaults to the current model units "SuppressUnitsDisplay" switch.
# @param spacing [Integer] The spaces before unit symbol. (Default=0)
# @param precision [Integer] The numbers shown after decimal point.
# Defaults to the current model units precision (Range 0..6). Allows higher.
# @return [String] The volume in cubic model units.
def format_volume_in_model_units(
volume,
hideunit = Sketchup::active_model.options['UnitsOptions']['SuppressUnitsDisplay'],
spacing = 0,
precision = Sketchup::active_model.options['UnitsOptions']['LengthPrecision']
)
inch_side = Math::cbrt(volume)
unit_side = Sketchup::format_length(inch_side).to_f
unit_vol = unit_side**3
if hideunit
"%.#{precision}f" % unit_vol
else
i = Sketchup::active_model.options['UnitsOptions']['LengthUnit']
unit = ['in','ft','mm','cm','m'][i]
"%.#{precision}f%s%s³" % [ unit_vol, ' '*spacing, unit ]
end
end
VERSION 4
-
Fixed the
"~ "
prefix bug that foolsString#to_f
.
(Core Ruby method that fails silently if string does not begin with numeral characters.) -
Moved
precision
to be 2nd argument (as it’s likely to be the most used optional parameter.) -
Simplified
spacing
argument to expect Boolean. (If a number is passed0
will be taken asfalse
, anything else will be evaluated in the Ruby conditional way, so any other number will betrue
.)
!
There are now only no space (SketchUp’s mode) or one space choices.
I decided simplicity is best, and that a report column output format can be done in a subsequent call.
The output from this method (using 1 space,) can besplit
into an array of two strings, and this array used as the argument toString#%
, ie:
# To right-justify the volume into a 12 character column, # followed by the unit right-justified in a 7 wide column: str = format_volume_in_model_units( volume, 6, false, true ) "%12s%7s" % str.split
In actual use, report values would be stored in an array of arrays, etc. and output in a formatting loop.
Here is version 4 …
# format_volume_in_model_units( volume, precision, hideunit, spacing )
#
# @param volume [Numeric] The volume in cubic inches.
# @param precision [Integer] The numbers shown after decimal point.
# Defaults to the current model units precision (Range 0..6). Allows higher.
# @param hideunit [Boolean] Whether to hide the unit symbols, or not.
# Defaults to the current model units "SuppressUnitsDisplay" switch.
# @param spacing [Boolean] Whether a space before unit symbol (Default=false).
#
# @return [String] The volume in cubic model units.
def format_volume_in_model_units(
volume,
precision = Sketchup::active_model.options['UnitsOptions']['LengthPrecision'],
hideunit = Sketchup::active_model.options['UnitsOptions']['SuppressUnitsDisplay'],
spacing = false
)
inch_side = Math::cbrt(volume)
unit_side = Sketchup::format_length(inch_side).delete('~').lstrip.to_f
unit_vol = unit_side**3
if hideunit
"%.#{precision}f" % unit_vol
else
spacing = false if spacing == 0
i = Sketchup::active_model.options['UnitsOptions']['LengthUnit']
unit = ['in','ft','mm','cm','m'][i]
if spacing
"%.#{precision}f %s³" % [ unit_vol, unit ]
else
"%.#{precision}f%s³" % [ unit_vol, unit ]
end
end
end