|
| 1 | +import {Point} from '@flatten-js/core'; |
| 2 | +import type {Entities as DxfEntities, Helper} from 'dxf'; |
| 3 | +import {isNil, uniqBy} from 'es-toolkit'; |
| 4 | +import {toast} from 'react-toastify'; |
| 5 | +import {CircleEntity} from '../../entities/CircleEntity.ts'; |
| 6 | +import type {Entity} from '../../entities/Entity.ts'; |
| 7 | +import {LineEntity} from '../../entities/LineEntity.ts'; |
| 8 | +import {getActiveLayerId, getActiveLineColor, getActiveLineWidth, getEntities, setEntities,} from '../../state'; |
| 9 | +import {toHex} from '../rgb-to-hex-color.ts'; |
| 10 | + |
| 11 | +function getDxfLineColor(dxfColor: [number, number, number] | undefined): string { |
| 12 | + if (isNil(dxfColor)) { |
| 13 | + return getActiveLineColor(); |
| 14 | + } |
| 15 | + return toHex(dxfColor[0], dxfColor[1], dxfColor[2], 1); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Imports entities from a DXF file. |
| 20 | + * @param file - The DXF file to import. |
| 21 | + */ |
| 22 | +export const importEntitiesFromDxfFile = async (file?: File): Promise<void> => { |
| 23 | + if (!file) { |
| 24 | + toast.warn('No DXF file selected.'); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + console.log(`Attempting to import DXF file: ${file.name}`); |
| 29 | + const reader = new FileReader(); |
| 30 | + |
| 31 | + reader.onload = async (event) => { |
| 32 | + if (!event.target?.result) { |
| 33 | + toast.error('Failed to read DXF file content.'); |
| 34 | + console.error('FileReader event.target.result is null or undefined.'); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const fileContent = event.target.result as string; |
| 39 | + |
| 40 | + try { |
| 41 | + console.log('DXF file content loaded, attempting to parse...'); |
| 42 | + // Dynamically import the dxf library |
| 43 | + const dxf = await import('dxf'); |
| 44 | + const parsedDxf = new dxf.Helper(fileContent) as Helper; |
| 45 | + |
| 46 | + if (!parsedDxf || !parsedDxf.denormalised) { |
| 47 | + toast.error('Failed to parse DXF file. No entities found or invalid format.'); |
| 48 | + console.error('Parsed DXF data is invalid or contains no entities:', parsedDxf); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + console.log(`Successfully parsed DXF. Found ${parsedDxf.denormalised.length} entities.`); |
| 53 | + const newEntities: Entity[] = []; |
| 54 | + const currentLayerId = getActiveLayerId(); |
| 55 | + const defaultWidth = getActiveLineWidth(); |
| 56 | + |
| 57 | + for (const entity of parsedDxf.denormalised) { |
| 58 | + // TODO: Coordinate transformation for Y-axis (DXF Y is usually up, canvas Y is down) |
| 59 | + // For now, we assume positive Y is down for simplicity matching canvas. |
| 60 | + // If DXF typically has Y up, then Y coordinates from DXF might need to be negated or subtracted from canvas height. |
| 61 | + if (entity.type === 'LINE') { |
| 62 | + const dxfLine = entity as DxfEntities.Line; |
| 63 | + if (dxfLine.start && dxfLine.end) { |
| 64 | + const startPoint: Point = new Point(dxfLine.start.x, dxfLine.start.y); |
| 65 | + const endPoint: Point = new Point(dxfLine.end.x, dxfLine.end.y); |
| 66 | + const line = new LineEntity(currentLayerId, startPoint, endPoint); |
| 67 | + line.lineColor = getDxfLineColor(dxfLine.colorNumber); |
| 68 | + line.lineWidth = dxfLine.thickness || defaultWidth; |
| 69 | + line.lineDash = undefined; |
| 70 | + newEntities.push(line); |
| 71 | + } else { |
| 72 | + console.warn('Skipping DXF LINE due to missing or insufficient vertices:', dxfLine); |
| 73 | + } |
| 74 | + } else if (entity.type === 'CIRCLE') { |
| 75 | + const dxfCircle = entity as DxfEntities.Circle; |
| 76 | + if (dxfCircle.x && dxfCircle.y && dxfCircle.r) { |
| 77 | + const centerPoint = new Point(dxfCircle.x, dxfCircle.y); |
| 78 | + const circle = new CircleEntity(currentLayerId, centerPoint, dxfCircle.r); |
| 79 | + circle.lineColor = getDxfLineColor(dxfCircle.colorNumber); |
| 80 | + circle.lineWidth = defaultWidth; |
| 81 | + circle.lineDash = undefined; |
| 82 | + newEntities.push(circle); |
| 83 | + } else { |
| 84 | + console.warn('Skipping DXF CIRCLE due to missing center or radius:', dxfCircle); |
| 85 | + } |
| 86 | + } else { |
| 87 | + console.log(`Unsupported DXF type: ${entity.type}. Skipping.`); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (newEntities.length > 0) { |
| 92 | + const uniqueEntities = uniqBy( |
| 93 | + newEntities, |
| 94 | + (entity) => |
| 95 | + `${JSON.stringify(entity.getShape())}|${entity.lineColor}|${entity.lineWidth}|${entity.lineDash}` |
| 96 | + ); |
| 97 | + setEntities([...getEntities(), ...uniqueEntities]); |
| 98 | + toast.success(`${uniqueEntities.length} entities imported successfully from DXF!`); |
| 99 | + } else { |
| 100 | + toast.info('No supported entities found in the DXF file.'); |
| 101 | + } |
| 102 | + } catch (error) { |
| 103 | + console.error('Error parsing DXF file:', error); |
| 104 | + toast.error('An error occurred while parsing the DXF file. See console for details.'); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + reader.onerror = () => { |
| 109 | + console.error('FileReader error:', reader.error); |
| 110 | + toast.error('Failed to read the DXF file.'); |
| 111 | + }; |
| 112 | + |
| 113 | + reader.readAsText(file); |
| 114 | +}; |
0 commit comments