import { AsyncMethodsOf , CompressedId64Set , Id64 , Id64String , OrderedId64Array , PromiseReturnType } from "@itwin/core-bentley" ; import { LineSegment3d , Point3d , Transform , YawPitchRollAngles } from "@itwin/core-geometry" ; import { BisCodeSpec , Code , CodeProps , GeometryStreamBuilder , PhysicalElementProps } from "@itwin/core-common" ; import { BriefcaseConnection , IModelConnection , IpcApp } from "@itwin/core-frontend" ; import { EditTools } from "@itwin/editor-frontend" ; import { BasicManipulationCommandIpc , editorBuiltInCmdIds } from "@itwin/editor-common" ; import { fullstackIpcChannel , FullStackTestIpc } from "../common/FullStackTestIpc" ; async function startCommand ( imodel ) { return EditTools . startCommand < string > ( editorBuiltInCmdIds . cmdBasicManipulation , imodel . key ) ; } function callCommand < T extends keyof BasicManipulationCommandIpc > ( method , ... args ) { return EditTools . callCommand ( method , ... args ) as ReturnType < BasicManipulationCommandIpc [ T ] > ; } function orderIds ( elementIds ) { const ids = new OrderedId64Array ( ) ; elementIds . forEach ( ( id ) => ids . insert ( id ) ) ; return ids ; } function compressIds ( elementIds ) { const ids = orderIds ( elementIds ) ; return CompressedId64Set . compressIds ( ids ) ; } export function makeLineSegment ( p1 ? , p2 ? ) { return LineSegment3d . create ( p1 || new Point3d ( 0 , 0 , 0 ) , p2 || new Point3d ( 1 , 1 , 0 ) ) ; } export async function insertLineElement ( imodel , model , category , line ? ) { await startCommand ( imodel ) ; line = line ?? makeLineSegment ( ) ; const origin = line . point0Ref ; const angles = new YawPitchRollAngles ( ) ; const builder = new GeometryStreamBuilder ( ) ; builder . setLocalToWorld3d ( origin , angles ) ; if ( ! builder . appendGeometry ( line ) ) return Id64 . invalid ; const elemProps = { classFullName : "Generic:PhysicalObject" , model , category , code : Code . createEmpty ( ) , placement : { origin , angles } , geom : builder . geometryStream } ; return callCommand ( "insertGeometricElement" , elemProps ) ; } export async function transformElements ( imodel , ids , transform ) { await startCommand ( imodel ) ; await callCommand ( "transformPlacement" , compressIds ( ids ) , transform . toJSON ( ) ) ; } export async function deleteElements ( imodel , ids ) { await startCommand ( imodel ) ; return callCommand ( "deleteElements" , compressIds ( ids ) ) ; } export async function initializeEditTools ( ) { return EditTools . initialize ( ) ; } export async function makeCode ( iModel , specName , scope , value ) { const modelCodeSpec = await iModel . codeSpecs . getByName ( specName ) ; return { scope , spec : modelCodeSpec . id , value } ; } export async function makeModelCode ( iModel , scope , value ) { return makeCode ( iModel , BisCodeSpec . informationPartitionElement , scope , value ) ; } export async function callFullStackTestIpc < T extends AsyncMethodsOf < FullStackTestIpc > > ( methodName , ... args ) { return IpcApp . callIpcChannel ( fullstackIpcChannel , methodName , ... args ) as PromiseReturnType < FullStackTestIpc [ T ] > ; }