Maintaining Values regardless of Unit

I have started working on a plugin that I would like the values input into the inputbox to be in the current units.

Currently if I specify the unit
@radius = 1.mm
it works if the model is mm, but not if it is in.
same if I just have @radius = 1
works if the model is in inches but not mm.

Is there some sort of automatic conversion that I can take the value @radius and establish it as the current working units?

Class: Length
Class: Numeric

I appreciate the link to documentation, but I have already been over that and still don’t understand how to translate to the current unit.

Make sure you set the default inputbox dimension value(s) as a Sketchup length.

Then it will be converted automatically and saved as a length value (in inches) and displayed in model units, converted automatically.

See for example my plugin on Sketchucation Plugin Store called SU Draw Parametric Shapes. Or another called Wood Framing, written jointly with Steve Baumgartner (@slbaumgartner), which can use both imperial/US and metric dimensions.

Or for a simpler example, here’s part of a plugin I’ve written only for my own use, to draw rectangular stage flats.

    def default_parameters

      if !defined? @@dimension1  # then no previous values input
        defaults = {
          "height" => 14.feet,
          "customHeight" => 0.inch,
          "width" => 4.feet,
          "customWidth" => 0.inch,
          "thickness" => 1.inch
        }
# p defaults
      else
#         Reuse last inputs as defaults
      defaults = {
          "height" => @@dimension1,
          "customHeight" => @@dimension2,
          "width" => @@dimension3,
          "customWidth" => @@dimension4,
          "thickness" => @@dimension5
        }

This plugin allows me to pick height, width and thickness dimensions from a drop down list, or override the list value with a customHeight and/or customWidth typed into the inputBox.

Here’s the complete plugin for you to try out:
JWM_Scenery1.51.rbz (11.5 KB)

It puts a menu entry in the Draw menu named Scenery, and allows you to draw flats and rostra of selectable dimensions, and allows you to insert other items from components in the model (not included here, so only Flats and Rostra will work).

Ok, I think I see how you can do it with pulling the units out like you did

model = Sketchup.active_model
manager = model.options
if provider = manager["UnitsOptions"] # Check for nil value
  length_unit = provider["LengthUnit"] # Length unit value
  length_format = provider["LengthFormat"] # Length format value


and then using that to multiply by the value.
Let me try that out.

That just enables you to see what units the model is using, and isn’t strictly necessary, but it may help you to see what’s going on.

But please feel free to copy any of my/our code and modify it to fit your needs. It’s the way I learned to write plugins - but Steve is the really knowledgeable one in our partnerships! He did all the really tricky bits in Wood Framing - and some of those were REALLY tricky! Way beyond my pay grade.

The Scenery plugin is far and away the simplest, but it only has to operate in Imperial units.

When you’ve got a bit further, post your code here if you need further help, and describe what you are trying to achieve with it, if it isn’t commercially confidential.

I almost have it but when I try to modify the unit_length by a discrete value it fails.

I have broken it down to this:
if I have the following it does odd things.
@@var = 1.mm * 0.2
this output .007874
why doesn’t it output .2?
the .2 is not a length but a multiplier

Is there a way to specify a raw numeric value?

Ok, I found a way to do this

def self.l(v)
	case @@length_unit
	when 0 ## Imperial units
	  if @@length_format == 1 || @@length_format == 2
		# model is using Architectural (feet and inches)
		# or Engineering units (feet)
		return v.feet
	  else
		## model is using (decimal or fractional) inches
		 return v.inch
	  end # if
	when 1
	  ## Decimal feet
	  return v.feet
	when 2
	  ## model is using metric units - millimetres
	  return v.mm
	when 3
	  ## model is using metric units - centimetres
	  return v.cm
	when 4
	  ## model is using metric units - metres
	  return v.m 
	end #end case
end

I guess you can achieve the same if you convert it to string, then string to length:

def value_as_curent_unit(v)
  v.to_s.to_l
end
value_as_curent_unit(2)

Sharing my old bookmark: thomthom : dealing-with-units-in-sketchup


BTW I think it is introduced in later to SU, but the yard (case when 5) is missing from your method…

Because 1.mm is not equal to 1. It is the length in inches that equates to 1mm.

SketchUp uses inches internally, and it has an internal tolerance of 0.001".
You cannot place vertices closer than this (or have edge lengths smaller.)