Selection
Getting the current selection
Section titled “Getting the current selection”let model = await SketchUpApi.getActiveModel();let selection = await model.getSelection();
// print number of elementsconsole.log(selection.totalNumberOfElements);console.log(selection.drawingElements);Filtering the current selection
Section titled “Filtering the current selection”let model = await SketchUpApi.getActiveModel();
// Only grab the faceslet selection = await model.getSelection({filter:'typeIn', values: [SketchUpApi.EntityType.Face]});
// print number of elements in selection (ignoring the filter)console.log(selection.totalNumberOfElements);
// prints only the facesconsole.log(selection.drawingElements);Getting the current selection metadata
Section titled “Getting the current selection metadata”let model = await SketchUpApi.getActiveModel();let selection = await model.getSelectionMetadata();
// print number of elements (matchingDrawingElements) is the same as totalNumberOfElements when no filter is suppliedconsole.log(selection.totalNumberOfElements);console.log(selection.matchingDrawingElements);Filtering the current selection metadata
Section titled “Filtering the current selection metadata”let model = await SketchUpApi.getActiveModel();
// Only grab the faceslet selection = await model.getSelectionMetadata({filter:'typeIn', values: [SketchUpApi.EntityType.Face]});
// print number of elements in selection (ignoring the filter)console.log(selection.totalNumberOfElements);
// prints only the facesconsole.log(selection.matchingDrawingElements);Streaming the current selection metadata
Section titled “Streaming the current selection metadata”let model = await SketchUpApi.getActiveModel();
let handle = model.observeSelectionMetadata((metadata) => { console.log(`The user has selected ${metadata.totalNumberOfElements} entities`);});
// make sure to close the handle when you no longer need the streamsetTimeout(() => handle.stop(), 60 * 1000)Filtered streaming of the current selection metadata
Section titled “Filtered streaming of the current selection metadata”let model = await SketchUpApi.getActiveModel();
let handle = model.observeSelectionMetadata((metadata) => { console.log(`The user has selected ${metadata.matchingDrawingElements} faces`);}, {filter: 'typeIn', values: [SketchUpApi.EntityType.Face]});
// make sure to close the handle when you no longer need the streamsetTimeout(() => handle.stop(), 60 * 1000)Updating the selection
Section titled “Updating the selection”async function selectEntities(elements, model) { await model.updateSelection(elements, 'replace');}
async function appendEntities(elements, model) { await model.updateSelection(elements, 'add');}
async function removeEntities(elements, model) { await model.updateSelection(elements, 'remove');}
async function toggleEntities(elements, model) { await model.updateSelection(elements, 'toggle');}Inverting the selection
Section titled “Inverting the selection”let model = await SketchUpApi.getActiveModel();await model.invertSelection();