Tools
This example demonstrates how to work with SketchUp tools using the JavaScript SDK.
Prerequisites
Section titled “Prerequisites”- The protocol version supports tools (SDK 2.19.0 / Protocol 1.16.0 or later).
Example
Section titled “Example”Getting the Active Tool
Section titled “Getting the Active Tool”let model = await SketchUpApi.getActiveModel();let tool = await model.tools.getActive();console.log(`Active tool: ${tool.name} (ID: ${tool.id})`);Clearing the Active Tool
Section titled “Clearing the Active Tool”let model = await SketchUpApi.getActiveModel();// Return to the selection toollet selectTool = await model.tools.clearActiveTool();console.log(`Cleared to: ${selectTool.name}`);Observing Tool Changes
Section titled “Observing Tool Changes”let model = await SketchUpApi.getActiveModel();
let handle = model.tools.observeToolChanges((event) => { if (event.event === 'Initial') { console.log(`Initial tool: ${event.tool.name}`); } else if (event.event === 'Activation') { console.log(`Tool activated: ${event.tool.name}`); } else if (event.event === 'StateChange') { console.log(`Tool state changed: ${event.state.tool.name}, state: ${event.state.state}`); }});
// Make sure to close the handle when you no longer need the observersetTimeout(() => handle.stop(), 60 * 1000);Placing a Component Once
Section titled “Placing a Component Once”let model = await SketchUpApi.getActiveModel();
// Get a component to placelet components = await model.getDefinitions();if (components.length === 0) { console.log('No components available'); return;}
let component = components[0];
// Place the component once - returns the placed instance or undefined if cancelledlet instance = await model.tools.placeComponentOnce(component);if (instance) { console.log('Component placed:', instance.name);} else { console.log('Component placement cancelled');}Placing a Component Repeatedly
Section titled “Placing a Component Repeatedly”let model = await SketchUpApi.getActiveModel();
// Get a component to placelet components = await model.getDefinitions();if (components.length === 0) { console.log('No components available'); return;}
let component = components[0];
// Place multiple instances with callback for each placementlet instances = await model.tools.placeComponentRepeatedly( component, (addedInstances) => { console.log(`Placed ${addedInstances.length} instances`); });
console.log(`Total instances placed: ${instances.length}`);