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.
Example
Section titled “Example”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
Constructor
Section titled “Constructor”Constructor
Section titled “Constructor”new Length(
inches):Length
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
The number of inches that this length represents (the natural unit that SketchUp supports) |
Returns
Section titled “Returns”Length
Properties
Section titled “Properties”centimeters
Section titled “centimeters”
readonlycentimeters:number
Get the length in millimeters
SDK 2.38.0
readonlyfeet:number
Get the length in feet
SDK 2.38.0
inches
Section titled “inches”
readonlyinches:number
The number of inches that this length represents (the natural unit that SketchUp supports)
kilometers
Section titled “kilometers”
readonlykilometers:number
Get the length in kilometers
SDK 2.38.0
meters
Section titled “meters”
readonlymeters:number
Get the length in meters
SDK 2.38.0
readonlymiles:number
Get the length in miles
SDK 2.38.0
millimeters
Section titled “millimeters”
readonlymillimeters:number
Get the length in millimeters
SDK 2.38.0
readonlytype:"length"='length'
readonlyyards:number
Get the length in yards
SDK 2.38.0
Methods
Section titled “Methods”add(
other):Length
Returns a new Length with this length plus the given value
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
the number in inches (or Length) to add to this Length. |
Returns
Section titled “Returns”Length
Example
Section titled “Example”let length = new SketchUpApi.Length(12);console.log(length.add(12));console.log(length.add(length.scale(3)));SDK 2.38.1
format()
Section titled “format()”format(
options?):string
Formats this length according to supplied length options
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
the options |
Returns
Section titled “Returns”string
Example
Section titled “Example”let { Length, LengthFormat, LengthUnit } = SketchUpApi;let length = Length.feet(5.5);
// Decimal, in a chosen unitconsole.log(length.format({ lengthFormat: LengthFormat.Decimal, lengthUnit: LengthUnit.Meter, lengthPrecision: 3,}));
// Feet and inches, to the nearest 1/16 inchconsole.log(length.format({ lengthFormat: LengthFormat.Architectural, lengthPrecision: 16,}));
// Decimal feetconsole.log(length.format({ lengthFormat: LengthFormat.Engineering,}));
// Inches with a fractionconsole.log(length.format({ lengthFormat: LengthFormat.Fractional,}));
// Or use the units configured in the modellet model = await SketchUpApi.getActiveModel();console.log(length.format(model));SDK 2.38.0
scale()
Section titled “scale()”scale(
scale):Length
Returns a new Length with this length multiplied by the given scale
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
the amount to scale by |
Returns
Section titled “Returns”Length
Example
Section titled “Example”let length = new SketchUpApi.Length(12);console.log(length.scale(-1));SDK 2.38.1
toString()
Section titled “toString()”toString():
string
Returns
Section titled “Returns”string
valueOf()
Section titled “valueOf()”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.
Returns
Section titled “Returns”number
Examples
Section titled “Examples”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
centimeters()
Section titled “centimeters()”
staticcentimeters(length):Length
Construct a length instance from centimeters
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
feet()
Section titled “feet()”
staticfeet(length):Length
Construct a length instance from feet
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
inches()
Section titled “inches()”
staticinches(length):Length
Construct a length instance from inches
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
kilometers()
Section titled “kilometers()”
statickilometers(length):Length
Construct a length instance from kilometers
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
meters()
Section titled “meters()”
staticmeters(length):Length
Construct a length instance from meters
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
miles()
Section titled “miles()”
staticmiles(length):Length
Construct a length instance from miles
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
millimeters()
Section titled “millimeters()”
staticmillimeters(length):Length
Construct a length instance from millimeters
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0
parse()
Section titled “parse()”
staticparse(string,options?):Length|undefined
Parses a length from a formatted length string.
On an invalid input this returns undefined
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
the length string |
|
|
|
setup defaults if the string is unit-less. |
Returns
Section titled “Returns”Length | undefined
Example
Section titled “Example”let { Length, LengthUnit } = SketchUpApi;
// Feet and inches are understoodlet a = Length.parse("5' 6\"");console.log(a.inches);
// Metric units are understoodlet b = Length.parse("2.5m");console.log(b.millimeters);
// Unit-less input uses the supplied default unitlet c = Length.parse("12", { lengthUnit: LengthUnit.Centimeter,});console.log(c.centimeters);
// Invalid input returns undefinedconsole.log(Length.parse("not a length"));
// Or defer to the units configured in the modellet model = await SketchUpApi.getActiveModel();console.log(Length.parse("10", model).inches);SDK 2.38.0 If not supplied inches are assumed
yards()
Section titled “yards()”
staticyards(length):Length
Construct a length instance from yards
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
|
|
|
Returns
Section titled “Returns”Length
SDK 2.38.0