What's the right way to set unit of model with C API?

Hey, there.
Just described as the title. I wanna set the units as in model info panel, and all the settings are as shown in below image:

7fbcea27e20e4afcaa85d5593dd7f034558c1270%20-%20%E5%89%AF%E6%9C%AC

And I have completed part of the code, but it couldn’t work well:

	SULengthFormatterRef formatter = SU_INVALID;
	SU_CALL(SULengthFormatterCreate(&formatter));
	SU_CALL(SUModelGetLengthFormatter(model, &formatter));
	SU_CALL(SULengthFormatterSetFormat(formatter, SULengthFormatType::SU_LFORMAT_DECIMAL));
	SU_CALL(SULengthFormatterSetUnits(formatter, SULengthUnitType::SU_LUNIT_CENTIMETER));
	SU_CALL(SULengthFormatterSetPrecision(formatter, 6));

Could someone give me a sample or code segment about my issue? Thanks in advance.

Can you elaborate on what didn’t work well? What did you observe?

Yep. In my mind, when I set format, unit, and precision of model’s LengthFormatter just shown as my code segment, the units part in model info panel of the opening .skp file will be amended as the above image. However, after I execute the code with .skp file, the units part in model info panel is not changed, and the model info panel always automatically popup…

My goal:
Format: Decimal
UnitType: cm
Precision:0.000000cm
Enable length snapping: Checked 0.000001cm
Display units format: Checked

Original:
Format: Architecture
UnitType: feet
Precision:1/16"
Enable length snapping: Checked 1/16"
Display units format: Checked

This is not what the documentation says that LengthFormatter objects do …

The doc says:

Length formatters are used to generate formatted strings (optionally with units) from length, area, and volume values. Additionally length formatters can be used to translate a formatted length/area/volume string into a value. Accessors and setters are exposed for some of the key formatting properties, facilitating customization of the formater. In cases when users want the formatter to reflect the properties of a model, SUModelGetLengthFormatter should be used to more efficiently extract/copy the relevant properties from the model to the formatter.


In order to set units, which are part of the model’s options, you’ll need to access the model’s OptionsManager and OptionsProvider collections.

See:

The keyname for the units options provider is: "UnitsOptions".

Within SketchUp, you can paste this Ruby snippet into the Ruby Console to see the current values assigned to the "UnitsOptions" provider’s key value pairs …

model = Sketchup.active_model
manager = model.options
provider = manager["UnitsOptions"]
provider.each { |key, value| puts "#{key} = #{value}" }

You’ll see that the keys are …

# Ordinal values (0 based position in dropdown list)
LengthFormat = 1
LengthUnit = 0
LengthPrecision = 4
AnglePrecision = 1

# Boolean values
LengthSnapEnabled = true
AngleSnapEnabled = true
SuppressUnitsDisplay = false
ForceInchDisplay = false

# Float values
LengthSnapLength = 0.0625
SnapAngle = 15.0

Also you must save the SKP file after setting Units properties of the model object.

1 Like

P.S. - The C++ API is retired. This applies to the newer C API.

Thanks Dan. I have completed the settings with C API according to your suggestion :slight_smile:

And I find the breakdown of options and value types for each options provider in following page: http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/options__provider_8h.html#a0374fb272f9f2acf0959eeabac3da6f3

Anyway, appreciate for your time.:gift_heart:

1 Like

That sounds strange… are you saying the Model Info dialog automatically opens without user interaction? After opening a file you created with the C API?