diff --git a/backend/src/cad_to_gcode.cpp b/backend/src/cad_to_gcode.cpp index 8477c73..fdf29c7 100644 --- a/backend/src/cad_to_gcode.cpp +++ b/backend/src/cad_to_gcode.cpp @@ -6,39 +6,48 @@ #include "iges_to_topo.hpp" #include "gcode_from_vectors.hpp" -// int main(int argc, char* argv[]) { -// // TopoDS_Shape shape = TopoDS_Shape(); -// // bool eq = shape.IsEqual(TopoDS_Shape()); -// // std::cout << eq << std::endl; -// } +TopoDS_Shape convert_file(const std::string filepath) { + std::__fs::filesystem::path p(filepath); + std::string extension = p.extension().string(); + for (auto& x : extension) { + x = tolower(x); + } + TopoDS_Shape shape = TopoDS_Shape(); + std::cout << "File extension: " << extension << std::endl; + if (extension == ".iges" || extension == ".igs") { + std::cout << "Reading IGES file." << std::endl; + TopoDS_Shape shape = read_iges(filepath.c_str()); + return shape; + } else if (extension == ".step" || extension == ".stp") { + std::cout << "Reading Step file." << std::endl; + TopoDS_Shape shape = read_step(filepath.c_str()); + return shape; + } else { + throw std::runtime_error("Error: File type not supported."); + } +} -int main() { +int main(int argc, char *argv[]) { try { - std::string step_filename = "RRH.STEP"; - std::string iges_filename = "RRH_test.IGS"; - - std::cout << "Reading files..." << step_filename << std::endl; - - TopoDS_Shape step_shape = read_step(step_filename); - TopoDS_Shape iges_shape = read_iges(iges_filename.c_str()); - - std::cout << "Creating B-Spline curves from shapes..." << std::endl; - - Handle(Geom_BSplineCurve) bsplineCurveStep = CreateBSplineFromShape(step_shape); - Handle(Geom_BSplineCurve) bsplineCurveIges = CreateBSplineFromShape(iges_shape); - - std::cout << "Calculating tangent vectors..." << std::endl; - - std::vector tangentVectorsStep = calculate_tangent_vectors(bsplineCurveStep); - std::vector tangentVectorsIges = calculate_tangent_vectors(bsplineCurveIges); - - std::cout << "Generating G-code..." << std::endl; - - generateGCode(tangentVectorsStep, "output_step.gcode"); - generateGCode(tangentVectorsIges, "output_iges.gcode"); + // Check if the correct number of arguments are passed + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; // Return an error code + } + + // Get file and output path from command line arguments + std::string filename = argv[1]; + std::string outputPath = argv[2]; + + // Assuming the rest of your code goes here + TopoDS_Shape shape = convert_file(filename); + Handle(Geom_BSplineCurve) bsplineCurve = CreateBSplineFromShape(shape); + std::vector tangentVectors = calculate_tangent_vectors(bsplineCurve); + generateGCode(tangentVectors, outputPath); } catch (const std::exception& e) { std::cerr << "Exception caught: " << e.what() << std::endl; + return 1; // Return an error code } return 0; diff --git a/frontend/public/electron.js b/frontend/public/electron.js index 5d8e1fd..2b59275 100644 --- a/frontend/public/electron.js +++ b/frontend/public/electron.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow } = require('electron'); +const { app, BrowserWindow, dialog } = require('electron'); const path = require('path'); const express = require('express'); const { spawnSync } = require('child_process'); @@ -24,11 +24,27 @@ function createWindow() { // Load the production build of the React application mainWindow.loadFile(path.join(__dirname, '..', 'build', 'index.html')); + // Open file dialog and handle the file path + ipcMain.on('open-file-dialog', (event) => { + dialog.showOpenDialog(mainWindow, { + properties: ['openFile'], + filters: [{ name: 'Design Files', extensions: ['iges', 'step', 'igs'] }], + }).then(result => { + if (!result.canceled && result.filePaths.length > 0) { + // Send the selected file path back to the renderer process + event.reply('selected-file', result.filePaths[0]); + } + }).catch(err => { + console.error('Error opening file dialog:', err); + }); + }); + global.executeBackendScriptSync = (fileName) => { try { - const scriptPath = path.join(__dirname, '../../backend/build/determine_file_type'); + const outputPath = path.join(app.getPath('downloads'), 'output_gcode'); + const scriptPath = path.join(__dirname, '../../build/cad_to_gcode'); console.log('Executing script at path:', scriptPath); - const scriptArguments = [fileName]; + const scriptArguments = [fileName, outputPath]; // Spawn the process with parameters const result = spawnSync(scriptPath, scriptArguments, { stdio: 'inherit' }); console.log('Result:', result) diff --git a/frontend/src/components/FileUploader.tsx b/frontend/src/components/FileUploader.tsx index dc6f765..cf2d0c5 100644 --- a/frontend/src/components/FileUploader.tsx +++ b/frontend/src/components/FileUploader.tsx @@ -1,29 +1,37 @@ -import React, { ChangeEvent, useState } from 'react'; +import React, { ChangeEvent, useState, useEffect } from 'react'; +import { IpcRendererEvent } from 'electron'; const { ipcRenderer } = window.require('electron'); const FileUploader: React.FC = () => { - const [selectedFile, setSelectedFile] = useState(null); - - const handleFileChange = (event: ChangeEvent) => { - const files = event.target.files; - if (files && files[0]) { - setSelectedFile(files[0]); - console.log('Selected file:', files[0].name); - ipcRenderer.send('file-upload', JSON.stringify(files[0].name)); - - } + useEffect(() => { + const handleSelectedFile = (event: IpcRendererEvent, path: string) => { + console.log('Selected file path:', path); + ipcRenderer.send('file-upload', path); + }; + + ipcRenderer.on('selected-file', handleSelectedFile); + + return () => { + ipcRenderer.removeListener('selected-file', handleSelectedFile); + }; + }, []); + + const handleOpenFileDialog = () => { + ipcRenderer.send('open-file-dialog'); }; return (
-
); };