Skip to content

Commit

Permalink
Fix view failing to apply when there is no default view (#70)
Browse files Browse the repository at this point in the history
No changelog entry because the fix is in undocumented experimental API.

Also fix view overrides not being cleared when applying a new saved view
in the test app.
  • Loading branch information
roluk authored May 17, 2024
1 parent 3ccd6c1 commit 8bd5b17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/
import { IModelReadRpcInterface, type ViewQueryParams, type ViewStateProps } from "@itwin/core-common";
import {
DisplayStyle3dState, DrawingViewState, EmphasizeElements, SheetViewState, SpatialViewState, type BlankConnection,
type IModelConnection, type ViewState, type Viewport,
DisplayStyle3dState, DrawingViewState, EmphasizeElements, SheetViewState, SpatialViewState, type IModelConnection,
type ViewState, type Viewport,
} from "@itwin/core-frontend";
import {
isViewDataITwin3d, isViewDataITwinDrawing, isViewDataITwinSheet, type SavedViewRepresentation,
Expand Down Expand Up @@ -255,10 +255,14 @@ async function fetchIModelViewData(iModel: IModelConnection, viewClassName: View
}

const viewId = await getDefaultViewIdFromClassName(iModel, viewClassName);
if (viewId === "") {
return manufactureEmptyViewState(iModel, viewClassName);
}

return iModel.views.load(viewId);
}

function manufactureEmptyViewState(iModel: BlankConnection, viewClassName: ViewTypes): ViewState {
function manufactureEmptyViewState(iModel: IModelConnection, viewClassName: ViewTypes): ViewState {
const blankViewState = SpatialViewState.createBlank(
iModel,
{ x: 0, y: 0, z: 0 },
Expand Down
18 changes: 14 additions & 4 deletions packages/test-app-frontend/src/App/ITwinJsApp/ITwinJsApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { FrontendIModelsAccess } from "@itwin/imodels-access-frontend";
import { IModelsClient } from "@itwin/imodels-client-management";
import { PageLayout } from "@itwin/itwinui-layouts-react";
import { useToaster } from "@itwin/itwinui-react";
import { applyExtensionsToViewport, type LegacySavedViewBase } from "@itwin/saved-views-react/experimental";
import { ModelCategoryOverrideProvider, applyExtensionsToViewport, type LegacySavedViewBase } from "@itwin/saved-views-react/experimental";
import { useEffect, useRef, useState, type ReactElement } from "react";

import { applyUrlPrefix } from "../../environment.js";
Expand Down Expand Up @@ -73,10 +73,11 @@ export function ITwinJsApp(props: ITwinJsAppProps): ReactElement | null {
);

const viewportRef = useRef<ScreenViewport>();
const handleSavedViewSelect = (savedView: LegacySavedViewBase, viewState: ViewState) => {
const handleSavedViewSelect = async (savedView: LegacySavedViewBase, viewState: ViewState) => {
setViewState(viewState);
if (viewportRef.current) {
applyExtensionsToViewport(viewportRef.current, savedView);
if (iModel && viewportRef.current) {
await clearAllOverrides(iModel, viewportRef.current);
await applyExtensionsToViewport(viewportRef.current, savedView);
}
};

Expand Down Expand Up @@ -206,3 +207,12 @@ async function getStoredViewState(iModel: IModelConnection): Promise<ViewState |

return viewId ? iModel.views.load(viewId) : undefined;
}

async function clearAllOverrides(iModel: IModelConnection, viewport: Viewport): Promise<void> {
const subcatProvider = viewport.findFeatureOverrideProviderOfType(ModelCategoryOverrideProvider);
if (subcatProvider) {
viewport.dropFeatureOverrideProvider(subcatProvider);
}

iModel.selectionSet.emptyAll();
}

0 comments on commit 8bd5b17

Please sign in to comment.