Skip to content

Commit

Permalink
fix cad file upload and gcode output download
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkim218 committed Feb 26, 2024
1 parent af3bb4f commit 2b76798
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
65 changes: 37 additions & 28 deletions backend/src/cad_to_gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gp_Vec> tangentVectorsStep = calculate_tangent_vectors(bsplineCurveStep);
std::vector<gp_Vec> 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] << " <input_file_path> <output_file_path>" << 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<gp_Vec> 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;
Expand Down
22 changes: 19 additions & 3 deletions frontend/public/electron.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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)
Expand Down
36 changes: 22 additions & 14 deletions frontend/src/components/FileUploader.tsx
Original file line number Diff line number Diff line change
@@ -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<File | null>(null);

const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
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 (
<div>
<label htmlFor="fileInput" className="w-52 h-12 flex justify-center items-center rounded-xl bg-brand-blue text-brand-white cursor-pointer">
<button
onClick={handleOpenFileDialog}
className="w-52 h-12 flex justify-center items-center rounded-xl bg-brand-blue text-brand-white cursor-pointer"
>
<h3 className="text-center font-semibold text-sm">Upload Bend Design</h3>
<input type="file" id="fileInput" accept=".iges,.step,.igs" onChange={handleFileChange} style={{ display: 'none' }} />
</label>
</button>
</div>
);
};
Expand Down

0 comments on commit 2b76798

Please sign in to comment.