Skip to main content

Structure

Types​

ObjectPath​

type ObjectPath = ObjectID[];

The ids of an object's ancestors and of the object itself, from the drawing's root down: where the object stands in the structure tree. It is what tells one occurrence of a product apart from another, and gives the object's whole transformation. An InteractionInfo carries it as objectPath where it is known; buildObjectPath makes it, and ScgTreeWalker hands it to every node it renders.


ScgTreeWalkerNode​

type ScgTreeWalkerNode = {
childrenNode: React.ReactNode;
level: number;
obj: ScgObject;
objectPath: ObjectPath;
};

What ScgTreeWalker hands its children for a node of the tree it walks.

Properties​

PropertyTypeDescription
childrenNodeReact.ReactNodeIts children, already rendered.
levelnumberHow deep it is: 0 for the walk's root, or for the children of a root that is flattened.
objScgObjectThe node, from the drawing's structure tree.
objectPathObjectPathIts path: the ids from the walk's root down to it (ObjectPath).

ScgTreeWalkerProps​

type ScgTreeWalkerProps = {
children: (node) => React.ReactNode;
drawingId: DrawingID;
filter?: (obj) => boolean;
flatten?: (obj) => boolean;
level?: number;
objectId: ObjectID;
objectPath?: ObjectPath;
};

Props of ScgTreeWalker.

Properties​

PropertyTypeDescription
children(node) => React.ReactNodeRenders a node; its children are already rendered and passed in.
drawingIdDrawingIDThe drawing.
filter?(obj) => booleanWhich nodes take part: a node it refuses is left out with everything below it. All, unless given.
flatten?(obj) => booleanWhich nodes are passed through: a flattened node is not rendered, its children stand in its place.
level?numberThe level of the start node; 0 unless given.
objectIdObjectIDThe node the walk starts at.
objectPath?ObjectPathThe path of the nodes above objectId, where the walk starts below the drawing's root.

Advanced​

Most apps do not need these directly: types they meet through the exports above, and what is there for special cases or for building on the SDK itself.

buildObjectPath()​

function buildObjectPath(drawingId, objectId): ObjectPath;

The path of an object in the structure tree: the ids from the drawing's root (AllObjects, where the drawing has it) down to the object, by the objects' parent links.

Parameters​

ParameterTypeDescription
drawingIdDrawingIDThe drawing.
objectIdnumberThe object the path leads to.

Returns​

ObjectPath

The path, the object's id last; empty when the drawing or the object is not there.

Example​

import { getDrawing } from '@classcad/sdkjs'
import type { DrawingID, ObjectID } from '@classcad/sdkjs'
import { buildObjectPath } from '@classcad/react'

// the names of the objects an instance is nested in, outermost first, the instance's own last
function nesting(drawingId: DrawingID, instanceId: ObjectID) {
const tree = getDrawing(drawingId).structure.tree
return buildObjectPath(drawingId, instanceId).map(id => tree[id]?.name)
}

ScgTreeWalker​

const ScgTreeWalker: React.FC<ScgTreeWalkerProps>;

Walks the structure tree from a node down and renders every node with the function it is given.

Each node subscribes to its own object, so a change to one node renders only that node and its parent again. The filter says which nodes take part, and flatten which are passed through: for an assembly's tree, the instances take part and the current instance is flattened, so that its children are the rows. Every node knows its ObjectPath, the ids from the walk's root down to it, which is what tells one occurrence of a product apart from another. The model tree of @classcad/react-cad walks an assembly with it.

Example​

import type { DrawingID, ObjectID } from '@classcad/sdkjs'
import { ScgTreeWalker, useDrawing } from '@classcad/react'

// the instances below the current one, as nested lists
function Instances({ drawingId }: { drawingId: DrawingID }) {
const current = useDrawing(drawingId, d => d.structure.currentInstance) as ObjectID
return (
<ul>
<ScgTreeWalker
drawingId={drawingId}
objectId={current}
filter={obj => obj.id === current || obj.class.endsWith('ProductReference')}
flatten={obj => obj.id === current}>
{({ obj, childrenNode }) => (
<li>
{obj.name}
<ul>{childrenNode}</ul>
</li>
)}
</ScgTreeWalker>
</ul>
)
}