From dc28b75f6c276ffc4fbf3c1ffeec988577b2f7c0 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Wed, 24 Sep 2025 15:01:31 +0530 Subject: [PATCH] simulation event data code optimization --- .../eventProperties/EventProperties.tsx | 2 +- .../sidebarRight/simulation/Simulations.tsx | 2 +- .../builder/asset/functions/addAssetModel.ts | 284 +----------------- .../assetControls/cutCopyPasteControls3D.tsx | 254 ++-------------- .../assetControls/duplicationControls3D.tsx | 257 ++-------------- .../controls/assetControls/moveControls3D.tsx | 12 +- .../triggerConnections/triggerConnector.tsx | 2 +- .../simulation/functions/createEventData.ts | 248 +++++++++++++++ .../functions/handleAddEventToProduct.ts | 76 ++--- .../points => }/functions/pointsCalculator.ts | 0 10 files changed, 353 insertions(+), 784 deletions(-) create mode 100644 app/src/modules/simulation/functions/createEventData.ts rename app/src/modules/simulation/{events/points => }/functions/handleAddEventToProduct.ts (86%) rename app/src/modules/simulation/{events/points => }/functions/pointsCalculator.ts (100%) diff --git a/app/src/components/layout/sidebarRight/properties/eventProperties/EventProperties.tsx b/app/src/components/layout/sidebarRight/properties/eventProperties/EventProperties.tsx index 402c2a0..a07d039 100644 --- a/app/src/components/layout/sidebarRight/properties/eventProperties/EventProperties.tsx +++ b/app/src/components/layout/sidebarRight/properties/eventProperties/EventProperties.tsx @@ -7,7 +7,7 @@ import MachineMechanics from "./mechanics/machineMechanics"; import StorageMechanics from "./mechanics/storageMechanics"; import HumanMechanics from "./mechanics/humanMechanics"; import { AddIcon } from "../../../../icons/ExportCommonIcons"; -import { handleAddEventToProduct } from "../../../../../modules/simulation/events/points/functions/handleAddEventToProduct"; +import { handleAddEventToProduct } from "../../../../../modules/simulation/functions/handleAddEventToProduct"; import { useParams } from "react-router-dom"; import { useSceneContext } from "../../../../../modules/scene/sceneContext"; import CraneMechanics from "./mechanics/craneMechanics"; diff --git a/app/src/components/layout/sidebarRight/simulation/Simulations.tsx b/app/src/components/layout/sidebarRight/simulation/Simulations.tsx index ac46c2f..ebfb866 100644 --- a/app/src/components/layout/sidebarRight/simulation/Simulations.tsx +++ b/app/src/components/layout/sidebarRight/simulation/Simulations.tsx @@ -6,7 +6,7 @@ import { useSimulationState, useSelectedAsset } from "../../../../store/simulati import { generateUUID } from "three/src/math/MathUtils"; import RenderOverlay from "../../../templates/Overlay"; import EditWidgetOption from "../../../ui/menu/EditWidgetOption"; -import { handleAddEventToProduct } from "../../../../modules/simulation/events/points/functions/handleAddEventToProduct"; +import { handleAddEventToProduct } from "../../../../modules/simulation/functions/handleAddEventToProduct"; import { deleteEventDataApi } from "../../../../services/simulation/products/deleteEventDataApi"; import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { deleteProductApi } from "../../../../services/simulation/products/deleteProductApi"; diff --git a/app/src/modules/builder/asset/functions/addAssetModel.ts b/app/src/modules/builder/asset/functions/addAssetModel.ts index 497aa61..675a1ee 100644 --- a/app/src/modules/builder/asset/functions/addAssetModel.ts +++ b/app/src/modules/builder/asset/functions/addAssetModel.ts @@ -3,7 +3,8 @@ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"; import { retrieveGLTF, storeGLTF } from "../../../../utils/indexDB/idbUtils"; import { Socket } from "socket.io-client"; import * as CONSTANTS from "../../../../types/world/worldConstants"; -import PointsCalculator from "../../../simulation/events/points/functions/pointsCalculator"; +import PointsCalculator from "../../../simulation/functions/pointsCalculator"; +import createEventData from "../../../simulation/functions/createEventData"; import { getUserData } from "../../../../functions/getUserData"; import { setAssetsApi } from "../../../../services/factoryBuilder/asset/floorAsset/setAssetsApi"; @@ -119,13 +120,6 @@ async function handleModelLoad( model.position.set(intersectPoint.x, intersectPoint.y, intersectPoint.z); model.scale.set(...CONSTANTS.assetConfig.defaultScaleAfterGsap); - model.traverse((child: any) => { - if (child) { - child.castShadow = true; - child.receiveShadow = true; - } - }); - const newFloorItem: Asset = { modelUuid: model.uuid, modelName: selectedItem.name, @@ -146,277 +140,23 @@ async function handleModelLoad( const eventData: any = { type: selectedItem.type, subType: selectedItem.subType }; - if (selectedItem.type === "Conveyor") { - const ConveyorEvent: ConveyorEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "transfer", - subType: selectedItem.subType || "", - speed: 1, - points: data.points.map((point: THREE.Vector3, index: number) => { - const triggers: TriggerSchema[] = []; + const assetEvent = createEventData(newFloorItem, selectedItem.type, selectedItem.subType, data.points); + if (!assetEvent) return; - if (data.points && index < data.points.length - 1) { - triggers.push({ - triggerUuid: THREE.MathUtils.generateUUID(), - triggerName: `Trigger 1`, - triggerType: "onComplete", - delay: 0, - triggeredAsset: { - triggeredModel: { - modelName: newFloorItem.modelName, - modelUuid: newFloorItem.modelUuid, - }, - triggeredPoint: { - pointName: `Point`, - pointUuid: "", - }, - triggeredAction: { - actionName: `Action 1`, - actionUuid: "", - }, - }, - }); - } + addEvent(assetEvent); - return { - uuid: THREE.MathUtils.generateUUID(), - position: [point.x, point.y, point.z], - rotation: [0, 0, 0], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: `Action 1`, - actionType: "default", - material: "Default Material", - delay: 0, - spawnInterval: 5, - spawnCount: 1, - triggers: triggers, - }, - }; - }), + if ("point" in assetEvent) { + eventData.point = { + uuid: assetEvent.point.uuid, + position: assetEvent.point.position, + rotation: assetEvent.point.rotation, }; - - for (let i = 0; i < ConveyorEvent.points.length - 1; i++) { - const currentPoint = ConveyorEvent.points[i]; - const nextPoint = ConveyorEvent.points[i + 1]; - - if (currentPoint.action.triggers.length > 0) { - currentPoint.action.triggers[0].triggeredAsset!.triggeredPoint!.pointUuid = nextPoint.uuid; - currentPoint.action.triggers[0].triggeredAsset!.triggeredAction!.actionUuid = nextPoint.action.actionUuid; - } - } - addEvent(ConveyorEvent); - eventData.points = ConveyorEvent.points.map((point) => ({ + } else if ("points" in assetEvent) { + eventData.points = assetEvent.points.map((point) => ({ uuid: point.uuid, position: point.position, rotation: point.rotation, })); - } else if (selectedItem.type === "Vehicle") { - const vehicleEvent: VehicleEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "vehicle", - subType: selectedItem.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "travel", - unLoadDuration: 5, - loadCapacity: 1, - steeringAngle: 0, - pickUpPoint: null, - unLoadPoint: null, - paths: { - initPickup: [], - pickupDrop: [], - dropPickup: [], - }, - triggers: [], - }, - }, - }; - addEvent(vehicleEvent); - eventData.point = { - uuid: vehicleEvent.point.uuid, - position: vehicleEvent.point.position, - rotation: vehicleEvent.point.rotation, - }; - } else if (selectedItem.type === "ArmBot") { - const roboticArmEvent: RoboticArmEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "roboticArm", - subType: selectedItem.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - ], - }, - }; - addEvent(roboticArmEvent); - eventData.point = { - uuid: roboticArmEvent.point.uuid, - position: roboticArmEvent.point.position, - rotation: roboticArmEvent.point.rotation, - }; - } else if (selectedItem.type === "StaticMachine") { - const machineEvent: MachineEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "machine", - subType: selectedItem.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "process", - processTime: 10, - swapMaterial: "Default Material", - triggers: [], - }, - }, - }; - addEvent(machineEvent); - eventData.point = { - uuid: machineEvent.point.uuid, - position: machineEvent.point.position, - rotation: machineEvent.point.rotation, - }; - } else if (selectedItem.type === "Storage") { - const storageEvent: StorageEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "storageUnit", - storageCapacity: 10, - storageCount: 10, - materialType: "Default material", - subType: selectedItem.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "store", - triggers: [], - }, - ], - }, - }; - addEvent(storageEvent); - eventData.point = { - uuid: storageEvent.point.uuid, - position: storageEvent.point.position, - rotation: storageEvent.point.rotation, - }; - } else if (selectedItem.type === "Human") { - const humanEvent: HumanEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "human", - subType: selectedItem.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "worker", - loadCount: 1, - assemblyCount: 1, - assemblyCondition: { - conditionType: "material", - materialType: "Default material", - }, - manufactureCount: 1, - loadCapacity: 1, - processTime: 10, - triggers: [], - }, - ], - }, - }; - addEvent(humanEvent); - eventData.point = { - uuid: humanEvent.point.uuid, - position: humanEvent.point.position, - rotation: humanEvent.point.rotation, - }; - } else if (selectedItem.type === "Crane") { - const craneEvent: CraneEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "crane", - subType: selectedItem.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [data.points[0].x, data.points[0].y, data.points[0].z], - rotation: [0, 0, 0], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndDrop", - maxPickUpCount: 1, - triggers: [], - }, - ], - }, - }; - addEvent(craneEvent); - eventData.point = { - uuid: craneEvent.point.uuid, - position: craneEvent.point.position, - rotation: craneEvent.point.rotation, - }; } const completeData = { diff --git a/app/src/modules/scene/controls/assetControls/cutCopyPasteControls3D.tsx b/app/src/modules/scene/controls/assetControls/cutCopyPasteControls3D.tsx index f30a659..c7e2364 100644 --- a/app/src/modules/scene/controls/assetControls/cutCopyPasteControls3D.tsx +++ b/app/src/modules/scene/controls/assetControls/cutCopyPasteControls3D.tsx @@ -10,6 +10,7 @@ import { detectModifierKeys } from "../../../../utils/shortcutkeys/detectModifie import { getUserData } from "../../../../functions/getUserData"; import useAssetResponseHandler from "../../../collaboration/responseHandler/useAssetResponseHandler"; +import createEventData from "../../../simulation/functions/createEventData"; import { setAssetsApi } from "../../../../services/factoryBuilder/asset/floorAsset/setAssetsApi"; import { deleteFloorAssetApi } from "../../../../services/factoryBuilder/asset/floorAsset/deleteFloorAssetApi"; import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; @@ -363,10 +364,8 @@ const CutCopyPasteControls3D = () => { isVisible: true, }; - let updatedEventData = null; - if (pastedAsset.userData.eventData) { - updatedEventData = JSON.parse(JSON.stringify(pastedAsset.userData.eventData)); + let updatedEventData = JSON.parse(JSON.stringify(pastedAsset.userData.eventData)) as EventsSchema; updatedEventData.modelUuid = newFloorItem.modelUuid; const eventData: any = { @@ -374,240 +373,31 @@ const CutCopyPasteControls3D = () => { subType: pastedAsset.userData.eventData.subType, }; - if (pastedAsset.userData.eventData.type === "Conveyor") { - const ConveyorEvent: ConveyorEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "transfer", - subType: pastedAsset.userData.eventData.subType || "", - speed: 1, - points: updatedEventData.points.map((point: any, index: number) => ({ - uuid: THREE.MathUtils.generateUUID(), - position: [point.position[0], point.position[1], point.position[2]], - rotation: [point.rotation[0], point.rotation[1], point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: `Action 1`, - actionType: "default", - material: "Default Material", - delay: 0, - spawnInterval: 5, - spawnCount: 1, - triggers: [], - }, - })), + let points: THREE.Vector3[] = []; + + if ("point" in updatedEventData) { + points = [new THREE.Vector3(...updatedEventData.point.position)]; + } else if ("points" in updatedEventData) { + points = updatedEventData.points.map((point) => new THREE.Vector3(...point.position)); + } + + const assetEvent = createEventData(newFloorItem, updatedEventData.type, updatedEventData.subType, points); + if (!assetEvent) return; + + addEvent(assetEvent); + + if ("point" in assetEvent) { + eventData.point = { + uuid: assetEvent.point.uuid, + position: assetEvent.point.position, + rotation: assetEvent.point.rotation, }; - addEvent(ConveyorEvent); - eventData.points = ConveyorEvent.points.map((point) => ({ + } else if ("points" in assetEvent) { + eventData.points = assetEvent.points.map((point) => ({ uuid: point.uuid, position: point.position, rotation: point.rotation, })); - } else if (pastedAsset.userData.eventData.type === "Vehicle") { - const vehicleEvent: VehicleEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "vehicle", - subType: pastedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "travel", - unLoadDuration: 5, - loadCapacity: 1, - steeringAngle: 0, - pickUpPoint: null, - unLoadPoint: null, - paths: { - initPickup: [], - pickupDrop: [], - dropPickup: [], - }, - triggers: [], - }, - }, - }; - addEvent(vehicleEvent); - eventData.point = { - uuid: vehicleEvent.point.uuid, - position: vehicleEvent.point.position, - rotation: vehicleEvent.point.rotation, - }; - } else if (pastedAsset.userData.eventData.type === "ArmBot") { - const roboticArmEvent: RoboticArmEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "roboticArm", - subType: pastedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - ], - }, - }; - addEvent(roboticArmEvent); - eventData.point = { - uuid: roboticArmEvent.point.uuid, - position: roboticArmEvent.point.position, - rotation: roboticArmEvent.point.rotation, - }; - } else if (pastedAsset.userData.eventData.type === "StaticMachine") { - const machineEvent: MachineEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "machine", - subType: pastedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "process", - processTime: 10, - swapMaterial: "Default Material", - triggers: [], - }, - }, - }; - addEvent(machineEvent); - eventData.point = { - uuid: machineEvent.point.uuid, - position: machineEvent.point.position, - rotation: machineEvent.point.rotation, - }; - } else if (pastedAsset.userData.eventData.type === "Storage") { - const storageEvent: StorageEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "storageUnit", - storageCapacity: 10, - storageCount: 10, - materialType: "Default material", - subType: pastedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "store", - triggers: [], - }, - ], - }, - }; - addEvent(storageEvent); - eventData.point = { - uuid: storageEvent.point.uuid, - position: storageEvent.point.position, - rotation: storageEvent.point.rotation, - }; - } else if (pastedAsset.userData.eventData.type === "Human") { - const humanEvent: HumanEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "human", - subType: pastedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "worker", - loadCapacity: 1, - assemblyCount: 1, - assemblyCondition: { - conditionType: "material", - materialType: "Default material", - }, - manufactureCount: 1, - loadCount: 1, - processTime: 10, - triggers: [], - }, - ], - }, - }; - addEvent(humanEvent); - eventData.point = { - uuid: humanEvent.point.uuid, - position: humanEvent.point.position, - rotation: humanEvent.point.rotation, - }; - } else if (pastedAsset.userData.eventData.type === "Crane") { - const craneEvent: CraneEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "crane", - subType: pastedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndDrop", - maxPickUpCount: 1, - triggers: [], - }, - ], - }, - }; - addEvent(craneEvent); - eventData.point = { - uuid: craneEvent.point.uuid, - position: craneEvent.point.position, - rotation: craneEvent.point.rotation, - }; } newFloorItem.eventData = eventData; diff --git a/app/src/modules/scene/controls/assetControls/duplicationControls3D.tsx b/app/src/modules/scene/controls/assetControls/duplicationControls3D.tsx index f787ee0..da01750 100644 --- a/app/src/modules/scene/controls/assetControls/duplicationControls3D.tsx +++ b/app/src/modules/scene/controls/assetControls/duplicationControls3D.tsx @@ -11,6 +11,7 @@ import { getUserData } from "../../../../functions/getUserData"; import { handleAssetPositionSnap } from "../selectionControls/selection3D/functions/handleAssetPositionSnap"; import useAssetResponseHandler from "../../../collaboration/responseHandler/useAssetResponseHandler"; +import createEventData from "../../../simulation/functions/createEventData"; import { setAssetsApi } from "../../../../services/factoryBuilder/asset/floorAsset/setAssetsApi"; const DuplicationControls3D = () => { @@ -304,10 +305,8 @@ const DuplicationControls3D = () => { isVisible: true, }; - let updatedEventData = null; - if (duplicatedAsset.userData.eventData) { - updatedEventData = JSON.parse(JSON.stringify(duplicatedAsset.userData.eventData)); + let updatedEventData = JSON.parse(JSON.stringify(duplicatedAsset.userData.eventData)) as EventsSchema; updatedEventData.modelUuid = newFloorItem.modelUuid; const eventData: any = { @@ -315,240 +314,31 @@ const DuplicationControls3D = () => { subType: duplicatedAsset.userData.eventData.subType, }; - if (duplicatedAsset.userData.eventData.type === "Conveyor") { - const ConveyorEvent: ConveyorEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "transfer", - subType: duplicatedAsset.userData.eventData.subType || "", - speed: 1, - points: updatedEventData.points.map((point: any, index: number) => ({ - uuid: THREE.MathUtils.generateUUID(), - position: [point.position[0], point.position[1], point.position[2]], - rotation: [point.rotation[0], point.rotation[1], point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: `Action 1`, - actionType: "default", - material: "Default Material", - delay: 0, - spawnInterval: 5, - spawnCount: 1, - triggers: [], - }, - })), + let points: THREE.Vector3[] = []; + + if ("point" in updatedEventData) { + points = [new THREE.Vector3(...updatedEventData.point.position)]; + } else if ("points" in updatedEventData) { + points = updatedEventData.points.map((point) => new THREE.Vector3(...point.position)); + } + + const assetEvent = createEventData(newFloorItem, updatedEventData.type, updatedEventData.subType, points); + if (!assetEvent) return; + + addEvent(assetEvent); + + if ("point" in assetEvent) { + eventData.point = { + uuid: assetEvent.point.uuid, + position: assetEvent.point.position, + rotation: assetEvent.point.rotation, }; - addEvent(ConveyorEvent); - eventData.points = ConveyorEvent.points.map((point) => ({ + } else if ("points" in assetEvent) { + eventData.points = assetEvent.points.map((point) => ({ uuid: point.uuid, position: point.position, rotation: point.rotation, })); - } else if (duplicatedAsset.userData.eventData.type === "Vehicle") { - const vehicleEvent: VehicleEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "vehicle", - subType: duplicatedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "travel", - unLoadDuration: 5, - loadCapacity: 1, - steeringAngle: 0, - pickUpPoint: null, - unLoadPoint: null, - paths: { - initPickup: [], - pickupDrop: [], - dropPickup: [], - }, - triggers: [], - }, - }, - }; - addEvent(vehicleEvent); - eventData.point = { - uuid: vehicleEvent.point.uuid, - position: vehicleEvent.point.position, - rotation: vehicleEvent.point.rotation, - }; - } else if (duplicatedAsset.userData.eventData.type === "ArmBot") { - const roboticArmEvent: RoboticArmEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "roboticArm", - subType: duplicatedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndPlace", - process: { - startPoint: null, - endPoint: null, - }, - triggers: [], - }, - ], - }, - }; - addEvent(roboticArmEvent); - eventData.point = { - uuid: roboticArmEvent.point.uuid, - position: roboticArmEvent.point.position, - rotation: roboticArmEvent.point.rotation, - }; - } else if (duplicatedAsset.userData.eventData.type === "StaticMachine") { - const machineEvent: MachineEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "machine", - subType: duplicatedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - action: { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "process", - processTime: 10, - swapMaterial: "Default Material", - triggers: [], - }, - }, - }; - addEvent(machineEvent); - eventData.point = { - uuid: machineEvent.point.uuid, - position: machineEvent.point.position, - rotation: machineEvent.point.rotation, - }; - } else if (duplicatedAsset.userData.eventData.type === "Storage") { - const storageEvent: StorageEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "storageUnit", - storageCapacity: 10, - storageCount: 10, - materialType: "Default material", - subType: duplicatedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "store", - triggers: [], - }, - ], - }, - }; - addEvent(storageEvent); - eventData.point = { - uuid: storageEvent.point.uuid, - position: storageEvent.point.position, - rotation: storageEvent.point.rotation, - }; - } else if (duplicatedAsset.userData.eventData.type === "Human") { - const humanEvent: HumanEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "human", - subType: duplicatedAsset.userData.eventData.subType || "", - speed: 1, - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "worker", - loadCapacity: 1, - assemblyCount: 1, - assemblyCondition: { - conditionType: "material", - materialType: "Default material", - }, - manufactureCount: 1, - loadCount: 1, - processTime: 10, - triggers: [], - }, - ], - }, - }; - addEvent(humanEvent); - eventData.point = { - uuid: humanEvent.point.uuid, - position: humanEvent.point.position, - rotation: humanEvent.point.rotation, - }; - } else if (duplicatedAsset.userData.eventData.type === "Crane") { - const craneEvent: CraneEventSchema = { - modelUuid: newFloorItem.modelUuid, - modelName: newFloorItem.modelName, - position: newFloorItem.position, - rotation: newFloorItem.rotation, - state: "idle", - type: "crane", - subType: duplicatedAsset.userData.eventData.subType || "", - point: { - uuid: THREE.MathUtils.generateUUID(), - position: [updatedEventData.point.position[0], updatedEventData.point.position[1], updatedEventData.point.position[2]], - rotation: [updatedEventData.point.rotation[0], updatedEventData.point.rotation[1], updatedEventData.point.rotation[2]], - actions: [ - { - actionUuid: THREE.MathUtils.generateUUID(), - actionName: "Action 1", - actionType: "pickAndDrop", - maxPickUpCount: 1, - triggers: [], - }, - ], - }, - }; - addEvent(craneEvent); - eventData.point = { - uuid: craneEvent.point.uuid, - position: craneEvent.point.position, - rotation: craneEvent.point.rotation, - }; } newFloorItem.eventData = eventData; @@ -631,7 +421,8 @@ const DuplicationControls3D = () => { modelName: newFloorItem.modelName, assetId: newFloorItem.assetId, position: [position.x, position.y, position.z], - rotation: [duplicatedAsset.rotation.x, duplicatedAsset.rotation.y, duplicatedAsset.rotation.z], + rotation: newFloorItem.rotation, + scale: newFloorItem.scale, isLocked: false, isVisible: true, socketId: builderSocket?.id, diff --git a/app/src/modules/scene/controls/assetControls/moveControls3D.tsx b/app/src/modules/scene/controls/assetControls/moveControls3D.tsx index e464d3b..e4499e2 100644 --- a/app/src/modules/scene/controls/assetControls/moveControls3D.tsx +++ b/app/src/modules/scene/controls/assetControls/moveControls3D.tsx @@ -393,12 +393,12 @@ function MoveControls3D() { const eventData = eventStore.getState().getEventByModelUuid(movedAsset.userData.modelUuid); const productData = productStore.getState().getEventByModelUuid(productStore.getState().selectedProduct.productUuid, movedAsset.userData.modelUuid); - if (eventData) { - eventStore.getState().updateEvent(movedAsset.userData.modelUuid, { - position: [position.x, position.y, position.z], - rotation: [movedAsset.rotation.x, movedAsset.rotation.y, movedAsset.rotation.z], - }); - } + // if (eventData) { + // eventStore.getState().updateEvent(movedAsset.userData.modelUuid, { + // position: [position.x, position.y, position.z], + // rotation: [movedAsset.rotation.x, movedAsset.rotation.y, movedAsset.rotation.z], + // }); + // } if (productData) { const event = productStore.getState().updateEvent(productStore.getState().selectedProduct.productUuid, movedAsset.userData.modelUuid, { diff --git a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx index e53a1df..3d45970 100644 --- a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx +++ b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx @@ -4,7 +4,7 @@ import { useEffect, useRef, useState } from "react"; import { useFrame, useThree } from "@react-three/fiber"; import { useSubModuleStore } from "../../../../store/ui/useModuleStore"; import { useSelectedAction, useSelectedAsset, useSelectedEventData } from "../../../../store/simulation/useSimulationStore"; -import { handleAddEventToProduct } from "../points/functions/handleAddEventToProduct"; +import { handleAddEventToProduct } from "../../functions/handleAddEventToProduct"; import { QuadraticBezierLine } from "@react-three/drei"; import { upsertProductOrEventApi } from "../../../../services/simulation/products/UpsertProductOrEventApi"; import { usePlayButtonStore } from "../../../../store/ui/usePlayButtonStore"; diff --git a/app/src/modules/simulation/functions/createEventData.ts b/app/src/modules/simulation/functions/createEventData.ts new file mode 100644 index 0000000..7e002d3 --- /dev/null +++ b/app/src/modules/simulation/functions/createEventData.ts @@ -0,0 +1,248 @@ +import { MathUtils, Vector3 } from "three"; + +export default function createEventData(asset: Asset, type: string, subType: string, points: Vector3[]): EventsSchema | null { + if (type === "Conveyor") { + const ConveyorEvent: ConveyorEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "transfer", + subType: subType || "", + speed: 1, + points: points.map((point: Vector3, index: number) => { + const triggers: TriggerSchema[] = []; + + if (points && index < points.length - 1) { + triggers.push({ + triggerUuid: MathUtils.generateUUID(), + triggerName: `Trigger 1`, + triggerType: "onComplete", + delay: 0, + triggeredAsset: { + triggeredModel: { + modelName: asset.modelName, + modelUuid: asset.modelUuid, + }, + triggeredPoint: { + pointName: `Point`, + pointUuid: "", + }, + triggeredAction: { + actionName: `Action 1`, + actionUuid: "", + }, + }, + }); + } + + return { + uuid: MathUtils.generateUUID(), + position: [point.x, point.y, point.z], + rotation: [0, 0, 0], + action: { + actionUuid: MathUtils.generateUUID(), + actionName: `Action 1`, + actionType: "default", + material: "Default Material", + delay: 0, + spawnInterval: 5, + spawnCount: 1, + triggers: triggers, + }, + }; + }), + }; + + for (let i = 0; i < ConveyorEvent.points.length - 1; i++) { + const currentPoint = ConveyorEvent.points[i]; + const nextPoint = ConveyorEvent.points[i + 1]; + + if (currentPoint.action.triggers.length > 0) { + currentPoint.action.triggers[0].triggeredAsset!.triggeredPoint!.pointUuid = nextPoint.uuid; + currentPoint.action.triggers[0].triggeredAsset!.triggeredAction!.actionUuid = nextPoint.action.actionUuid; + } + } + + return ConveyorEvent; + } else if (type === "Vehicle") { + const vehicleEvent: VehicleEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "vehicle", + subType: (subType as "" | "manual" | "automatic" | "semiAutomatic") || "", + speed: 1, + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + action: { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "travel", + unLoadDuration: 5, + loadCapacity: 1, + steeringAngle: 0, + pickUpPoint: null, + unLoadPoint: null, + paths: { + initPickup: [], + pickupDrop: [], + dropPickup: [], + }, + triggers: [], + }, + }, + }; + + return vehicleEvent; + } else if (type === "ArmBot") { + const roboticArmEvent: RoboticArmEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "roboticArm", + subType: subType || "", + speed: 1, + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + actions: [ + { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "pickAndPlace", + process: { + startPoint: null, + endPoint: null, + }, + triggers: [], + }, + ], + }, + }; + } else if (type === "StaticMachine") { + const machineEvent: MachineEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "machine", + subType: subType || "", + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + action: { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "process", + processTime: 10, + swapMaterial: "Default Material", + triggers: [], + }, + }, + }; + + return machineEvent; + } else if (type === "Storage") { + const storageEvent: StorageEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "storageUnit", + storageCapacity: 10, + storageCount: 10, + materialType: "Default material", + subType: subType || "", + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + actions: [ + { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "store", + triggers: [], + }, + ], + }, + }; + + return storageEvent; + } else if (type === "Human") { + const humanEvent: HumanEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "human", + subType: subType || "", + speed: 1, + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + actions: [ + { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "worker", + loadCount: 1, + assemblyCount: 1, + assemblyCondition: { + conditionType: "material", + materialType: "Default material", + }, + manufactureCount: 1, + loadCapacity: 1, + processTime: 10, + triggers: [], + }, + ], + }, + }; + + return humanEvent; + } else if (type === "Crane") { + const craneEvent: CraneEventSchema = { + modelUuid: asset.modelUuid, + modelName: asset.modelName, + position: asset.position, + rotation: asset.rotation, + state: "idle", + type: "crane", + subType: (subType as "" | "pillarJib") || "", + point: { + uuid: MathUtils.generateUUID(), + position: [points[0].x, points[0].y, points[0].z], + rotation: [0, 0, 0], + actions: [ + { + actionUuid: MathUtils.generateUUID(), + actionName: "Action 1", + actionType: "pickAndDrop", + maxPickUpCount: 1, + triggers: [], + }, + ], + }, + }; + + return craneEvent; + } + + return null; +} diff --git a/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts b/app/src/modules/simulation/functions/handleAddEventToProduct.ts similarity index 86% rename from app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts rename to app/src/modules/simulation/functions/handleAddEventToProduct.ts index 7cafc1d..e909768 100644 --- a/app/src/modules/simulation/events/points/functions/handleAddEventToProduct.ts +++ b/app/src/modules/simulation/functions/handleAddEventToProduct.ts @@ -1,38 +1,38 @@ -import { upsertProductOrEventApi } from "../../../../../services/simulation/products/UpsertProductOrEventApi"; - -interface HandleAddEventToProductParams { - event: EventsSchema | undefined; - addEvent: (productUuid: string, event: EventsSchema) => void; - selectedProduct: { - productUuid: string; - productName: string; - } - clearSelectedAsset?: () => void; - versionId: string; - projectId: string; -} - -export const handleAddEventToProduct = ({ - event, - addEvent, - selectedProduct, - clearSelectedAsset, - projectId, - versionId -}: HandleAddEventToProductParams) => { - if (event && selectedProduct.productUuid) { - addEvent(selectedProduct.productUuid, event); - - upsertProductOrEventApi({ - productName: selectedProduct.productName, - productUuid: selectedProduct.productUuid, - versionId, - projectId: projectId || '', - eventDatas: event - }) - - if (clearSelectedAsset) { - clearSelectedAsset(); - } - } -}; +import { upsertProductOrEventApi } from "../../../services/simulation/products/UpsertProductOrEventApi"; + +interface HandleAddEventToProductParams { + event: EventsSchema | undefined; + addEvent: (productUuid: string, event: EventsSchema) => void; + selectedProduct: { + productUuid: string; + productName: string; + } + clearSelectedAsset?: () => void; + versionId: string; + projectId: string; +} + +export const handleAddEventToProduct = ({ + event, + addEvent, + selectedProduct, + clearSelectedAsset, + projectId, + versionId +}: HandleAddEventToProductParams) => { + if (event && selectedProduct.productUuid) { + addEvent(selectedProduct.productUuid, event); + + upsertProductOrEventApi({ + productName: selectedProduct.productName, + productUuid: selectedProduct.productUuid, + versionId, + projectId: projectId || '', + eventDatas: event + }) + + if (clearSelectedAsset) { + clearSelectedAsset(); + } + } +}; diff --git a/app/src/modules/simulation/events/points/functions/pointsCalculator.ts b/app/src/modules/simulation/functions/pointsCalculator.ts similarity index 100% rename from app/src/modules/simulation/events/points/functions/pointsCalculator.ts rename to app/src/modules/simulation/functions/pointsCalculator.ts