Text
This example demonstrates how to create and modify Text entities in SketchUp using the JavaScript SDK.
Prerequisites
Section titled “Prerequisites”- The protocol version supports text entities (SDK 2.22.0 / Protocol 1.19.0 or later).
Example
Section titled “Example”Creating a Screen Text (no attachment)
Section titled “Creating a Screen Text (no attachment)”let model = await SketchUpApi.getActiveModel();await model.performOperation(async operation => { const text = operation.createText(model, 'Hello, SketchUp!', { point: [0, 0, 0], }); console.log(text);}, 'Create screen text');Updating Text Content
Section titled “Updating Text Content”let model = await SketchUpApi.getActiveModel();let [text] = await model.entities();
await model.performOperation(async operation => { operation.textSetText(text, 'Updated content'); const sectionPlane = await text.refresh(); console.log('New text:', sectionPlane.text);}, 'Update text content');Attaching Text to Geometry at Creation
Section titled “Attaching Text to Geometry at Creation”A text can be attached to geometry by providing an instancePath — an ordered list of entities
leading to the attachment point (e.g. a ComponentInstance followed by an Edge inside it).
let model = await SketchUpApi.getActiveModel();await model.performOperation(async operation => { // Create a component with an edge inside it const componentRef = operation.createDefinition('my component'); const [edgeRef] = operation.createEdge(componentRef, [[0, 0, 0], [10, 0, 0]]); const edge = await operation.entityForRef(edgeRef);
// Place an instance of the component in the model const instanceRef = operation.createInstance( operation.model, componentRef, SketchUpApi.Transformation.identity, ); const instance = await operation.entityForRef(instanceRef);
// Create a text attached to the midpoint of the edge through the instance const text = operation.createText( operation.model, 'Attached Text', { instancePath: [instance, edge], point: [5, 0, 0], }, [0, 5, 0], // leader vector );
const resolved = await operation.entityForRef(text); console.log('Attached to:', resolved.attachedTo);}, 'Create text with attachment');Adjusting the Leader Appearance
Section titled “Adjusting the Leader Appearance”let { SketchUpTextArrowTypeEnum } = SketchUpApi;
let model = await SketchUpApi.getActiveModel();let [text] = await model.entities();
await model.performOperation(async operation => { operation.textSetDisplayLeader(text, true); operation.textSetLineWeight(text, 2); operation.textSetArrowType(text, SketchUpApi.TextArrowType.Dot); const sectionPlane = await text.refresh(); console.log('Arrow type:', sectionPlane.arrowType);}, 'Adjust leader appearance');