Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/js/2d/floor2d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import paper from 'paper'
import { exportSceneToJson } from '../3d/scene3d.js'

const canvas2D = document.getElementById('canvas2D')
export { canvas2D }
Expand Down Expand Up @@ -437,6 +438,75 @@ function undo() {
}
}

function clearObjectRectangles() {
const itemsToRemove = []

paper.project.activeLayer.children.forEach((item) => {
if (item.data && item.data.isObjectRectangle) {
itemsToRemove.push(item)
}
})

itemsToRemove.forEach((item) => item.remove())

console.log(`Cleared ${itemsToRemove.length} object rectangles`)
}

export const drawObjectRectangle = () => {
clearObjectRectangles()

const data = exportSceneToJson()
if (!data || Object.keys(data).length === 0) {
console.log('No objects found in scene data')
return
}
const WORLD_TO_PX = 50
Object.entries(data).forEach(([key, objData]) => {
if (!objData.coords || !objData.dimensions) {
console.log(`Missing coords or dimensions for: ${key}`)
return
}
const { x, z } = objData.coords
const canvas2DX = x * WORLD_TO_PX + paper.view.bounds.width / 2
const canvas2DY = z * WORLD_TO_PX + paper.view.bounds.height / 2
const center = new paper.Point(canvas2DX, canvas2DY)
const rect = new paper.Path.Rectangle({
point: new paper.Point(center.x - 50 / 2, center.y - 50 / 2),
size: [50, 50],
fillColor: getColorForObject(key),
opacity: 0.5,
strokeColor: 'black',
strokeWidth: 2,
})
rect.data = {
type: 'object',
objectKey: key,
isObjectRectangle: true,
}
const label = new paper.PointText({
point: center,
content: key.split('_')[0],
fillColor: 'white',
fontSize: 12,
justification: 'center',
})
label.data = {
type: 'objectLabel',
parentKey: key,
isObjectRectangle: true,
}
})
paper.view.draw()
}

function getColorForObject(key) {
if (key.startsWith('table')) return 'brown'
if (key.startsWith('chair')) return 'blue'
if (key.startsWith('cooler')) return 'cyan'
if (key.startsWith('rack')) return 'gray'
return 'black'
}

document.addEventListener('keydown', function (event) {
if (
(event.ctrlKey || event.metaKey) &&
Expand Down
4 changes: 4 additions & 0 deletions src/js/3d/pathsTo3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function convertPathsTo3D() {

// Convert 2D paths to 3D walls
paper.project.activeLayer.children.forEach((item) => {
if (item.data && item.data.isObjectRectangle) {
return // Skip this item - it's an object, not a wall
}

if (
item instanceof paper.Path &&
item.segments.length >= 2 &&
Expand Down
3 changes: 2 additions & 1 deletion src/js/3d/scene3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function onMouseClick(event) {
}
}

function exportSceneToJson() {
export function exportSceneToJson() {
let localPosition = new THREE.Vector3(0, 0, 0)
let box = new THREE.Box3()
let size = new THREE.Vector3(0, 0, 0)
Expand Down Expand Up @@ -175,6 +175,7 @@ function exportSceneToJson() {
})

console.log(JSON.stringify(sceneData, null, 2)) // Pretty print JSON with 2-space indent
return sceneData
}

// Add event listener for keypress
Expand Down
2 changes: 2 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createGrid } from './2d/floor2d.js'
import { setupDxfUpload } from './2d/dxfLoader.js'
import { init3D, camera, renderer } from './3d/scene3d.js'
import { convertPathsTo3D } from './3d/pathsTo3d.js'
import { drawObjectRectangle } from './2d/floor2d.js'
// import parseDXF from 'dxf-parser'

createGrid()
Expand All @@ -23,6 +24,7 @@ switchButton.addEventListener('click', () => {
} else {
container2D.style.display = 'block'
container3D.style.display = 'none'
drawObjectRectangle()
switchButton.textContent = 'Switch to 3D'
}
})
Expand Down