From 050298cb8a51c5d44310dc71ab0c3cdb28198ea3 Mon Sep 17 00:00:00 2001 From: michelleli04 Date: Thu, 22 Feb 2024 17:57:56 -0500 Subject: [PATCH] file send child process --- backend/CMakeLists.txt | 3 +++ backend/src/determine_file_type.cpp | 33 ++++++++++++++++++++++++ frontend/electron.js | 27 +++++++++++++++++-- frontend/src/components/FileUploader.tsx | 3 +++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 backend/src/determine_file_type.cpp diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 90ae5cd..1f0bc1b 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -14,11 +14,14 @@ add_executable(cad_to_gcode src/tangent_vectors_geom_bspline.cpp src/gcode_from_vectors.cpp ) +add_executable(determine_file_type src/determine_file_type.cpp) # add_executable(iges_to_topo src/iges_to_topo.cpp) # add_executable(step_to_topo src/step_to_topo.cpp) # add_executable(shape_to_bspline src/shape_to_bspline.cpp) # add_executable(tangent_vectors_geom_bspline src/tangent_vectors_geom_bspline.cpp) target_link_libraries(cad_to_gcode ${OpenCASCADE_LIBRARIES}) +target_link_libraries(determine_file_type ${OpenCASCADE_LIBRARIES}) + # target_link_libraries(iges_to_topo ${OpenCASCADE_LIBRARIES}) # target_link_libraries(shape_to_bspline ${OpenCASCADE_LIBRARIES}) # target_link_libraries(tangent_vectors_geom_bspline ${OpenCASCADE_LIBRARIES}) diff --git a/backend/src/determine_file_type.cpp b/backend/src/determine_file_type.cpp new file mode 100644 index 0000000..1ce931d --- /dev/null +++ b/backend/src/determine_file_type.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +// #include "step_to_topo.cpp" +// #include "iges_to_topo.cpp" +using namespace std; +TopoDS_Shape convert_file(const string filepath) { + std::__fs::filesystem::path p(filepath); + string extension = p.extension().string(); + for (auto& x : extension) { + x = tolower(x); + } + TopoDS_Shape shape = TopoDS_Shape(); + cout << "File extension: " << extension << endl; + if (extension == ".iges" || extension == ".igs") { + cout << "Reading IGES file." << endl; + // TopoDS_Shape shape = read_iges(filepath.c_str()); + return shape; + } else if (extension == ".step" || extension == ".stp") { + cout << "Reading Step file." << endl; + // TopoDS_Shape shape = read_step(filepath.c_str()); + return shape; + } else { + throw std::runtime_error("Error: File type not supported."); + } +} +int main(int argc, char* argv[]) { + string step_file = argv[1]; + TopoDS_Shape shape = convert_file(step_file); + return 0; +} diff --git a/frontend/electron.js b/frontend/electron.js index 420530f..f77c2a1 100644 --- a/frontend/electron.js +++ b/frontend/electron.js @@ -1,6 +1,10 @@ const { app, BrowserWindow } = require('electron'); const path = require('path'); const express = require('express'); +const { spawnSync } = require('child_process'); +const { ipcMain } = require('electron'); + + // Implement NSApplicationDelegate.applicationSupportsSecureRestorableState app.applicationSupportsSecureRestorableState = () => true; @@ -12,14 +16,33 @@ function createWindow() { width: 800, height: 600, webPreferences: { - nodeIntegration: false, + nodeIntegration: true, contextIsolation: true, }, }); // Load the production build of the React application mainWindow.loadFile(path.join(__dirname, 'build', 'index.html')); - + global.executeBackendScriptSync = (fileName) => { + try { + const scriptPath = path.join(__dirname, '../backend/build/determine_file_type'); + console.log('Executing script at path:', scriptPath); + const scriptArguments = [fileName]; + // Spawn the process with parameters + const result = spawnSync(scriptPath, scriptArguments, { stdio: 'inherit' }); + console.log('Result:', result) + if (result.error) { + throw result.error; + } + } catch (error) { + console.error('Error executing backend script:', error); + return 'Error executing backend script'; + } + }; + ipcMain.on('file-upload', (event, arg) => { + console.log(arg); + global.executeBackendScriptSync(arg); + }); mainWindow.on('closed', () => (mainWindow = null)); } diff --git a/frontend/src/components/FileUploader.tsx b/frontend/src/components/FileUploader.tsx index 2e5d8cd..d0995e6 100644 --- a/frontend/src/components/FileUploader.tsx +++ b/frontend/src/components/FileUploader.tsx @@ -9,6 +9,9 @@ const FileUploader: React.FC = () => { if (files && files[0]) { setSelectedFile(files[0]); console.log('Selected file:', files[0].name); + const { ipcRenderer } = window.require('electron'); + ipcRenderer.send('file-upload', JSON.stringify(files[0].name)); + } };