How to create entities with unit length as setting, not in inch

I am trying to create entities in Sketchup but a problem is whenever I create them the unit of the value that I use to create is always in inch.

mod = Sketchup.active_model
ents = mod.entities

#Radius
r = 5

ents.add_circle([0,0,0],[0,0,1],r)

The created circle always have radius in inch which convert from 5.

I wonder if there’s anyway to create them to have a unit as setting, like having 5 meters in this case.

SketchUp extends the Ruby Numeric class with methods for units conversion. Among these are ones that inform the Ruby interpreter that a numeric value in your code is to be taken to have particular units. For example,

r = 5.m

converts the value 5 in meters into the equivalent internal inches (196.8503937007874) that SketchUp always uses for lengths.

In case that I don’t have a particular unit but I want them to change along each template, like I want to have 5 as radius but in mm template they will create 5.mm, in m template they will create 5 m. Does it have any possible ways to do that?

You can retrieve model units first

Sketchup.active_model.options['UnitsOptions']

I think I can use

Sketchup.active_model.options['UnitsOptions']['LengthUnit']

to retrieve unit and check what it is then change the number afterward but is there any way which more efficient than this idea since it have to check on every single numbers I have to create all the time.

I usually force the input length to the desired one. For example:

Radius (cm)?

That way you always make the same conversion

You can use too:

length = Sketchup.format_length(5)

But you receive a string and you should transform to float

See this …

1 Like

This doc really help! Thank you a lot.

1 Like