Skip to content

Commit

Permalink
chore: remove 'nodeIds' support and add type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Jul 15, 2024
1 parent 2c6b780 commit b657d38
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
16 changes: 14 additions & 2 deletions react-components/src/components/CadModelContainer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ export type TreeIndexStylingGroup = {
style?: NodeAppearance;
};

export type CadStyleGroup = NodeStylingGroup | TreeIndexStylingGroup;
export type CadStylingGroup = NodeStylingGroup | TreeIndexStylingGroup;

export type CadModelStyling = {
defaultStyle?: NodeAppearance;
groups?: CadStyleGroup[];
groups?: CadStylingGroup[];
};

export function isNodeStylingGroup(
stylingGroup: CadStylingGroup
): stylingGroup is NodeStylingGroup {
return 'nodeIds' in stylingGroup;
}

export function isTreeIndexStylingGroup(
stylingGroup: CadStylingGroup
): stylingGroup is TreeIndexStylingGroup {
return 'treeIndexSet' in stylingGroup;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import { type CogniteClient } from '@cognite/sdk';
import { isEqual } from 'lodash';
import { useReveal } from '../RevealCanvas/ViewerContext';
import { modelExists } from '../../utilities/modelExists';
import { type CadModelStyling, type NodeStylingGroup, type TreeIndexStylingGroup } from './types';
import {
isNodeStylingGroup,
isTreeIndexStylingGroup,
type CadModelStyling,
type NodeStylingGroup,
type TreeIndexStylingGroup
} from './types';
import { assertNever } from '../../utilities/assertNever';

export const useApplyCadModelStyling = (
model?: CogniteCadModel,
Expand Down Expand Up @@ -57,15 +64,15 @@ async function applyStyling(

if (stylingGroup.style === undefined) continue;

if ('treeIndexSet' in stylingGroup) {
if (isTreeIndexStylingGroup(stylingGroup)) {
const nodes = new TreeIndexNodeCollection(stylingGroup.treeIndexSet);
model.assignStyledNodeCollection(nodes, stylingGroup.style);
}

if ('nodeIds' in stylingGroup) {
} else if (isNodeStylingGroup(stylingGroup)) {
const nodes = new NodeIdNodeCollection(sdk, model);
await nodes.executeFilter(stylingGroup.nodeIds);
model.assignStyledNodeCollection(nodes, stylingGroup.style);
} else {
assertNever(stylingGroup);
}
}

Expand Down
8 changes: 6 additions & 2 deletions react-components/src/components/Reveal3DResources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { type Matrix4 } from 'three';
import { type DmsUniqueIdentifier, type Source } from '../../utilities/FdmSDK';
import { type CogniteInternalId, type Node3D } from '@cognite/sdk';
import { type CadStyleGroup } from '../CadModelContainer/types';
import { type TreeIndexStylingGroup } from '../CadModelContainer/types';

export type AddImage360CollectionOptions =
| AddImage360CollectionEventsOptions
Expand Down Expand Up @@ -68,7 +68,11 @@ export type AddPointCloudResourceOptions = Add3dResourceOptions & {
};

export type AddCadResourceOptions = AddModelOptions & { transform?: Matrix4 } & {
styling?: { default?: NodeAppearance; mapped?: NodeAppearance; nodeGroups?: CadStyleGroup[] };
styling?: {
default?: NodeAppearance;
mapped?: NodeAppearance;
nodeGroups?: TreeIndexStylingGroup[];
};
};

export type TypedReveal3DModel = CadModelOptions | PointCloudModelOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
type ModelRevisionAssetNodesResult
} from '../CacheProvider/types';
import {
type CadStyleGroup,
type CadStylingGroup,
type NodeStylingGroup,
type TreeIndexStylingGroup
} from '../CadModelContainer/types';
Expand Down Expand Up @@ -55,7 +55,7 @@ type StyledModelWithMappingsFetched = {

export type StyledModel = {
model: CadModelOptions;
styleGroups: CadStyleGroup[];
styleGroups: CadStylingGroup[];
};

export const useCalculateCadStyling = (
Expand Down
2 changes: 1 addition & 1 deletion react-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export {
export { type CogniteCadModelProps } from './components/CadModelContainer/CadModelContainer';
export {
type CadModelStyling,
type CadStyleGroup,
type CadStylingGroup,
type TreeIndexStylingGroup,
type NodeStylingGroup
} from './components/CadModelContainer/types';
Expand Down
3 changes: 1 addition & 2 deletions react-components/stories/Reveal3DResources.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const Main: Story = {
transform: new Matrix4().makeTranslation(40, 0, 0),
styling: {
nodeGroups: [
{ treeIndexSet: new IndexSet([2, 4, 6, 8]), style: { color: new Color('blue') } },
{ nodeIds: [7152264879809192, 7899489067916664], style: { visible: false } }
{ treeIndexSet: new IndexSet([2, 4, 6, 8]), style: { color: new Color('blue') } }
]
}
},
Expand Down

0 comments on commit b657d38

Please sign in to comment.