Skip to content

Length

Represents a length in the standard SketchUp unit (inches).

Usage of this class is highly encouraged when dealing with non-default units in SketchUp, as it reduces the chance of mistakes and ambiguity.

Provides utilities for parsing and formatting a length based on the context of a model.

let model = await SketchUpApi.getActiveModel();
let length = SketchUpApi.Length.meters(1);
let formatted = length.format(model);
console.log(formatted);
console.log(SketchUpApi.Length.parse(formatted, model).meters);

SDK 2.38.0

new Length(inches): Length

Parameter Type Description

inches

number

The number of inches that this length represents (the natural unit that SketchUp supports)

Length

readonly centimeters: number

Get the length in millimeters

SDK 2.38.0


readonly feet: number

Get the length in feet

SDK 2.38.0


readonly inches: number

The number of inches that this length represents (the natural unit that SketchUp supports)


readonly kilometers: number

Get the length in kilometers

SDK 2.38.0


readonly meters: number

Get the length in meters

SDK 2.38.0


readonly miles: number

Get the length in miles

SDK 2.38.0


readonly millimeters: number

Get the length in millimeters

SDK 2.38.0


readonly type: "length" = 'length'


readonly yards: number

Get the length in yards

SDK 2.38.0

add(other): Length

Returns a new Length with this length plus the given value

Parameter Type Description

other

Numeric

the number in inches (or Length) to add to this Length.

Length

let length = new SketchUpApi.Length(12);
console.log(length.add(12));
console.log(length.add(length.scale(3)));

SDK 2.38.1


format(options?): string

Formats this length according to supplied length options

Parameter Type Description

options?

Model | { forceInchDisplay?: boolean; forceShowFraction?: boolean; lengthFormat?: LengthFormat; lengthPrecision?: number; lengthUnit: LengthUnit; suppressUnitsDisplay?: boolean; }

the options

string

let { Length, LengthFormat, LengthUnit } = SketchUpApi;
let length = Length.feet(5.5);
// Decimal, in a chosen unit
console.log(length.format({
lengthFormat: LengthFormat.Decimal,
lengthUnit: LengthUnit.Meter,
lengthPrecision: 3,
}));
// Feet and inches, to the nearest 1/16 inch
console.log(length.format({
lengthFormat: LengthFormat.Architectural,
lengthPrecision: 16,
}));
// Decimal feet
console.log(length.format({
lengthFormat: LengthFormat.Engineering,
}));
// Inches with a fraction
console.log(length.format({
lengthFormat: LengthFormat.Fractional,
}));
// Or use the units configured in the model
let model = await SketchUpApi.getActiveModel();
console.log(length.format(model));

SDK 2.38.0


scale(scale): Length

Returns a new Length with this length multiplied by the given scale

Parameter Type Description

scale

number

the amount to scale by

Length

let length = new SketchUpApi.Length(12);
console.log(length.scale(-1));

SDK 2.38.1


toString(): string

string


valueOf(): number

Utility that converts this Length to a number (in inches), this allows built-in operators to work on a Length.

NOTE: This will ALWAYS result in a number value, you must wrap it back into a Length if that is desirable.

number

let length = new SketchUpApi.Length(12);
console.log(length + 3);
console.log(length - 3);
console.log(length * 3);
console.log(length / 3);

Typescript, you need to opt in to the conversion by including a `+` before your length variable.

let length = new SketchUpApi.Length(12);
console.log(+length + 3);
console.log(+length - 3);
console.log(+length * 3);
console.log(+length / 3);

SDK 2.38.1


static centimeters(length): Length

Construct a length instance from centimeters

Parameter Type

length

number

Length

SDK 2.38.0


static feet(length): Length

Construct a length instance from feet

Parameter Type

length

number

Length

SDK 2.38.0


static inches(length): Length

Construct a length instance from inches

Parameter Type

length

number

Length

SDK 2.38.0


static kilometers(length): Length

Construct a length instance from kilometers

Parameter Type

length

number

Length

SDK 2.38.0


static meters(length): Length

Construct a length instance from meters

Parameter Type

length

number

Length

SDK 2.38.0


static miles(length): Length

Construct a length instance from miles

Parameter Type

length

number

Length

SDK 2.38.0


static millimeters(length): Length

Construct a length instance from millimeters

Parameter Type

length

number

Length

SDK 2.38.0


static parse(string, options?): Length | undefined

Parses a length from a formatted length string.

On an invalid input this returns undefined

Parameter Type Description

string

string

the length string

options?

Model | { lengthFormat?: LengthFormat; lengthUnit?: LengthUnit; }

setup defaults if the string is unit-less.

Length | undefined

let { Length, LengthUnit } = SketchUpApi;
// Feet and inches are understood
let a = Length.parse("5' 6\"");
console.log(a.inches);
// Metric units are understood
let b = Length.parse("2.5m");
console.log(b.millimeters);
// Unit-less input uses the supplied default unit
let c = Length.parse("12", {
lengthUnit: LengthUnit.Centimeter,
});
console.log(c.centimeters);
// Invalid input returns undefined
console.log(Length.parse("not a length"));
// Or defer to the units configured in the model
let model = await SketchUpApi.getActiveModel();
console.log(Length.parse("10", model).inches);

SDK 2.38.0 If not supplied inches are assumed


static yards(length): Length

Construct a length instance from yards

Parameter Type

length

number

Length

SDK 2.38.0

readonly data: number