Dwinzo_dev/app/src/components/temporary/SelectFloorPlan.tsx

122 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-05-26 05:03:27 +00:00
import React, { useEffect, useState } from "react";
import useLayoutStore from "../../store/builder/uselayoutStore";
2025-05-26 05:03:27 +00:00
import { useDfxUpload } from "../../store/builder/store";
import DxfParser from "dxf-parser";
import { getWallPointsFromBlueprint } from "../../modules/builder/dfx/functions/getWallPointsFromBlueprint";
import { convertDXFToThree } from "../../modules/builder/dfx/functions/convertDxfToThree";
const SelectFloorPlan: React.FC = () => {
2025-05-28 06:37:31 +00:00
// Access layout state and state setters
const { currentLayout, setLayout } = useLayoutStore();
2025-05-28 06:37:31 +00:00
// Access DXF-related states and setters
const { setDfxUploaded, setDfxGenerate, setObjValue, objValue } = useDfxUpload();
// Local state to store the parsed DXF file
const [parsedFile, setParsedFile] = useState<DXFData | undefined>(undefined);
2025-05-26 05:03:27 +00:00
2025-05-28 06:37:31 +00:00
// Flag to trigger generation after file upload
2025-05-26 05:03:27 +00:00
const [generate, setGenerate] = useState<boolean>(false);
2025-05-28 06:37:31 +00:00
// Handles file upload and DXF parsing
2025-05-26 05:03:27 +00:00
const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
2025-05-28 06:37:31 +00:00
setLayout(null); // Reset current layout
setParsedFile(undefined); // Clear any previously parsed file
2025-05-26 05:03:27 +00:00
const file = event.target.files?.[0];
if (!file || !file.name.endsWith(".dxf")) {
alert("Please upload a valid .dxf file.");
return;
}
const reader = new FileReader();
reader.onload = async (e) => {
const dxfContent = e.target?.result as string;
try {
const parser = new DxfParser();
2025-05-28 06:37:31 +00:00
const parsedDatas = parser.parse(dxfContent) as DXFData;
const geometries = convertDXFToThree(parsedDatas);
setParsedFile(parsedDatas);
setObjValue({ x: 0, y: 0, z: 0 });
2025-05-26 05:03:27 +00:00
setDfxUploaded(geometries);
} catch (error) {
2025-05-28 06:37:31 +00:00
console.error("Failed to import your .dxf file", error);
} finally {
// ✅ Reset input AFTER processing
event.target.value = "";
2025-05-26 05:03:27 +00:00
}
};
2025-05-28 06:37:31 +00:00
reader.readAsText(file); // Read the uploaded file as text
2025-05-26 05:03:27 +00:00
};
2025-05-28 06:37:31 +00:00
// Trigger wall point generation when `generate` flag changes
2025-05-26 05:03:27 +00:00
useEffect(() => {
2025-05-28 06:37:31 +00:00
if (parsedFile !== undefined) {
getWallPointsFromBlueprint({ parsedData: parsedFile, setDfxGenerate, objValue });
}
}, [generate]);
2025-05-26 05:03:27 +00:00
return (
<div className="select-floorplane-wrapper">
Preset Layouts
2025-05-28 06:37:31 +00:00
<div className="presets-container">
<button
className={`preset ${currentLayout === "layout1" ? "active" : ""}`}
onClick={() => {
setLayout("layout1");
2025-05-28 06:37:31 +00:00
setDfxUploaded([]);
setGenerate(false);
}}
>
Preset 1
</button>
2025-05-28 06:37:31 +00:00
<button
className={`preset ${currentLayout === "layout2" ? "active" : ""}`}
onClick={() => {
setLayout("layout2");
2025-05-28 06:37:31 +00:00
setDfxUploaded([]);
setGenerate(false);
}}
>
Preset 2
</button>
2025-05-26 05:03:27 +00:00
<input
type="file"
id="file-upload"
accept=".dxf"
style={{ display: "none", width: "10px" }}
onChange={handleFileUpload}
/>
2025-05-28 06:37:31 +00:00
2025-05-26 05:03:27 +00:00
<label
htmlFor="file-upload"
className="preset"
style={{
cursor: "pointer",
padding: "10px 48px",
}}
>
Upload
</label>
2025-05-28 06:37:31 +00:00
2025-05-26 05:03:27 +00:00
<button
type="button"
id="generate-upload"
onClick={() => {
2025-05-28 06:37:31 +00:00
setDfxUploaded([]);
setLayout(null);
setGenerate(!generate);
2025-05-26 05:03:27 +00:00
}}
>
Generate
</button>
</div>
2025-05-28 06:37:31 +00:00
</div>
);
};
2025-05-28 06:37:31 +00:00
export default SelectFloorPlan;