Skip to content

Commit

Permalink
file send child process
Browse files Browse the repository at this point in the history
  • Loading branch information
michelleli04 committed Feb 22, 2024
1 parent 2966fe4 commit 050298c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
33 changes: 33 additions & 0 deletions backend/src/determine_file_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <string>
#include <TopoDS_Shape.hxx>
#include <iostream>
#include <filesystem>
// #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;
}
27 changes: 25 additions & 2 deletions frontend/electron.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}

Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}
};

Expand Down

0 comments on commit 050298c

Please sign in to comment.