Skip to content

Commit

Permalink
Merge pull request #25 from GenerateNU/refactor
Browse files Browse the repository at this point in the history
Get gcode from tangent vectors and refactor previous tickets
  • Loading branch information
muneerlalji authored Feb 21, 2024
2 parents 7c77d4b + 206c973 commit 2966fe4
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 53 deletions.
25 changes: 16 additions & 9 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ set(OpenCASCADE_DIR "/usr/local/Cellar/opencascade/7.7.2_2/lib/cmake/opencascade
find_package(OpenCASCADE REQUIRED)
include_directories(${OCE_INCLUDE_DIRS})

add_executable(cad_to_gcode src/cad_to_gcode.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)
add_executable(cad_to_gcode
src/cad_to_gcode.cpp
src/iges_to_topo.cpp
src/step_to_topo.cpp
src/shape_to_bspline.cpp
src/tangent_vectors_geom_bspline.cpp
src/gcode_from_vectors.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(iges_to_topo ${OpenCASCADE_LIBRARIES})
target_link_libraries(shape_to_bspline ${OpenCASCADE_LIBRARIES})
target_link_libraries(tangent_vectors_geom_bspline ${OpenCASCADE_LIBRARIES})
target_link_libraries(step_to_topo ${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})
# target_link_libraries(step_to_topo ${OpenCASCADE_LIBRARIES})
45 changes: 41 additions & 4 deletions backend/src/cad_to_gcode.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
#include <iostream>
#include <TopoDS_Shape.hxx>
#include "step_to_topo.hpp"
#include "shape_to_bspline.hpp"
#include "tangent_vectors_geom_bspline.hpp"
#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;
// int main(int argc, char* argv[]) {
// // TopoDS_Shape shape = TopoDS_Shape();
// // bool eq = shape.IsEqual(TopoDS_Shape());
// // std::cout << eq << std::endl;
// }

int main() {
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");

} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}

return 0;
}
68 changes: 68 additions & 0 deletions backend/src/gcode_from_vectors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <fstream>
#include <iostream>
#include <vector>
#include <Geom_Curve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <GeomLProp_SLProps.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <GeomAPI_PointsToBSpline.hxx>
#include "gcode_from_vectors.hpp"


void generateGCode(const std::vector<gp_Vec> &tangentVectors, const std::string &outputPath)
{
std::ofstream outFile(outputPath);
if (!outFile.is_open())
{
std::cerr << "Error opening output file." << std::endl;
return;
}
std::cout << "Starting G-code generation..." << std::endl;
// Initial position of the tool head, assuming it starts at (0, 0, 0)
gp_Pnt currentPosition(0.0, 0.0, 0.0);
// Constants
const double zIncrement = 1.0; // Constant Z movement for each step
const double feedRate = 100.0; // Constant feed rate
int vectorCount = 0; // For logging progress
// Iterate over each tangent vector
for (const auto &vec : tangentVectors)
{
// Calculate the new position by adding the tangent vector to the current position
gp_Pnt newPosition = currentPosition.Translated(vec);
// Generate the G01 command
outFile << "G01 "
<< "X" << newPosition.X() << " "
<< "Y" << newPosition.Y() << " "
<< "Z" << (currentPosition.Z() + zIncrement) << " " // Increment Z position
<< "F" << feedRate << std::endl;
// Update the current position to the new position
currentPosition = newPosition;
// Logging progress
vectorCount++;
if (vectorCount % 100 == 0) // Log every 100 vectors processed
{
std::cout << "Processed " << vectorCount << " vectors..." << std::endl;
}
}
outFile.close();
std::cout << "G-code generated successfully at " << outputPath << std::endl;
}

// int main(int argc, char *argv[])
// {
// std::cout << "Program started." << std::endl;
// // Create or obtain a BSplineCurve
// Handle(Geom_BSplineCurve) bsplineCurve = createExampleBSplineCurve();
// // Calculate tangent vectors
// std::vector<gp_Vec> tangentVectors = calculate_tangent_vectors(bsplineCurve);
// // Specify the output path for the G-code file
// std::string outputPath = "output.gcode";
// if (argc > 1)
// {
// outputPath = argv[1]; // Allow specifying output path as a command line argument
// }
// // Generate G-code
// generateGCode(tangentVectors, outputPath);
// std::cout << "Program completed." << std::endl;
// }
9 changes: 9 additions & 0 deletions backend/src/gcode_from_vectors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef GCODE_FROM_VECTORS
#define GCODE_FROM_VECTORS

#include <vector>
#include <gp_Vec.hxx>

void generateGCode(const std::vector<gp_Vec> &tangentVectors, const std::string &outputPath);

#endif
43 changes: 22 additions & 21 deletions backend/src/iges_to_topo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <IGESControl_Reader.hxx>
#include <TopoDS_Shape.hxx>
#include "iges_to_topo.hpp"

// takes in an IGES file and returns a TopoDS_Shape
TopoDS_Shape read_iges(const char* filename)
Expand All @@ -20,27 +21,27 @@ TopoDS_Shape read_iges(const char* filename)
}

// to see if it works
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <IGES file>" << std::endl;
return 1; // Return a non-zero value to indicate an error
}
// int main(int argc, char* argv[]) {
// if (argc != 2) {
// std::cerr << "Usage: " << argv[0] << " <IGES file>" << std::endl;
// return 1; // Return a non-zero value to indicate an error
// }

const char* igesFileName = argv[1];
std::cout <<igesFileName;
// const char* igesFileName = argv[1];
// std::cout <<igesFileName;

try {
// Call the function to read IGES file and get TopoDS_Shape
TopoDS_Shape shape = read_iges(igesFileName);
if (!shape.IsNull()) {
std::cout << "Shape is not null." << std::endl;
} else {
std::cout << "Shape is null." << std::endl;
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1; // Return 1 for error
}
// try {
// // Call the function to read IGES file and get TopoDS_Shape
// TopoDS_Shape shape = read_iges(igesFileName);
// if (!shape.IsNull()) {
// std::cout << "Shape is not null." << std::endl;
// } else {
// std::cout << "Shape is null." << std::endl;
// }
// } catch (const std::exception& e) {
// std::cerr << e.what() << std::endl;
// return 1; // Return 1 for error
// }

return 0; // Return 0 for success
}
// return 0; // Return 0 for success
// }
9 changes: 9 additions & 0 deletions backend/src/iges_to_topo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef IGES_READER_H
#define IGES_READER_H

#include <TopoDS_Shape.hxx>
#include <stdexcept>

TopoDS_Shape read_iges(const char* filename);

#endif
29 changes: 27 additions & 2 deletions backend/src/shape_to_bspline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <gp_Pnt.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include "shape_to_bspline.hpp"

bool VerticesMatch(const TopoDS_Vertex& v1, const TopoDS_Vertex& v2) {
gp_Pnt p1 = BRep_Tool::Pnt(v1);
Expand Down Expand Up @@ -82,6 +84,20 @@ std::vector<gp_Pnt> SamplePointsFromEdge(const TopoDS_Edge& edge, int numPoints)
return points;
}

Handle(Geom_BSplineCurve) CreateSimpleBSpline(const TopoDS_Shape& shape) {
TColgp_Array1OfPnt points(1, 6);

points.SetValue(1, gp_Pnt(0.0, 0.0, 0.0));
points.SetValue(2, gp_Pnt(1.0, 2.0, 0.0));
points.SetValue(3, gp_Pnt(2.0, 4.0, 1.0));
points.SetValue(4, gp_Pnt(3.0, 6.0, 1.0));
points.SetValue(5, gp_Pnt(4.0, 8.0, 0.0));
points.SetValue(6, gp_Pnt(5.0, 10.0, 0.0));

GeomAPI_PointsToBSpline splineBuilder(points);
return splineBuilder.Curve();
}

Handle(Geom_BSplineCurve) CreateBSplineFromShape(const TopoDS_Shape& shape) {
std::vector<TopoDS_Edge> orderedEdges = OrderEdges(shape);
std::vector<gp_Pnt> allPoints;
Expand All @@ -98,7 +114,16 @@ Handle(Geom_BSplineCurve) CreateBSplineFromShape(const TopoDS_Shape& shape) {
arrayPoints.SetValue(i + 1, allPoints[i]);
}

GeomAPI_PointsToBSpline splineBuilder(arrayPoints);
// // create simple bspline using CreateSimpleBSpline function
// Handle(Geom_BSplineCurve) simpleSpline = CreateSimpleBSpline();

// // make sure the simpleSpline is not null
// if (simpleSpline.IsNull()) {
// std::cout << "simpleSpline is null" << std::endl;
// } else {
// std::cout << "simpleSpline is not null" << std::endl;
// }

GeomAPI_PointsToBSpline splineBuilder(arrayPoints);
return splineBuilder.Curve();
}

9 changes: 9 additions & 0 deletions backend/src/shape_to_bspline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef BSPLINE_CREATOR_H
#define BSPLINE_CREATOR_H

#include <TopoDS_Shape.hxx>
#include <Geom_BSplineCurve.hxx>

Handle(Geom_BSplineCurve) CreateBSplineFromShape(const TopoDS_Shape& shape);

#endif
13 changes: 7 additions & 6 deletions backend/src/step_to_topo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <string>
using namespace std;
#include "step_to_topo.hpp"

TopoDS_Shape read_step(const string& filename) {
STEPControl_Reader reader;
Expand All @@ -29,10 +30,10 @@ TopoDS_Shape read_step(const string& filename) {
return result;
}

int main(int argc, char* argv[]) {
string full_frame_filename = "Full_Frame.STEP";
TopoDS_Shape full_frame_shape = read_step(full_frame_filename);
// int main(int argc, char* argv[]) {
// string full_frame_filename = "Full_Frame.STEP";
// TopoDS_Shape full_frame_shape = read_step(full_frame_filename);

string rrh_filename = "RRH.STEP";
TopoDS_Shape rrh_shape = read_step(rrh_filename);
}
// string rrh_filename = "RRH.STEP";
// TopoDS_Shape rrh_shape = read_step(rrh_filename);
// }
9 changes: 9 additions & 0 deletions backend/src/step_to_topo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef STEP_READER_H
#define STEP_READER_H

#include <string>
#include <TopoDS_Shape.hxx>

TopoDS_Shape read_step(const std::string& filename);

#endif
21 changes: 10 additions & 11 deletions backend/src/tangent_vectors_geom_bspline.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@

#include <Geom_Curve.hxx>
#include <Geom_BSplineCurve.hxx>
#include <GeomLProp_SLProps.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <vector>
#include <iostream>

// outputs a list of vectors containing the tangents to the knots on the curve
#include "tangent_vectors_geom_bspline.hpp"
// Outputs a list of vectors containing the tangents at various points along the curve
std::vector<gp_Vec> calculate_tangent_vectors(const Handle(Geom_BSplineCurve)& bsplineCurve) {
std::vector<gp_Vec> tangentVectors;
gp_Vec tangentVector;
gp_Pnt point;
int knots = bsplineCurve->NbKnots();
Standard_Real step_size = 1 / knots;
Standard_Real first_parameter = bsplineCurve->FirstParameter();
Standard_Real last_parameter = bsplineCurve->LastParameter();
for (int u = first_parameter; u < last_parameter; u += step_size) {
int numSamples = bsplineCurve->NbKnots() + 1; // For example, you might want samples at each knot and one in between each
Standard_Real step_size = (last_parameter - first_parameter) / (numSamples - 1);
for (Standard_Real u = first_parameter; u <= last_parameter; u += step_size) {
bsplineCurve->D1(u, point, tangentVector);
tangentVectors.push_back(tangentVector);
if (u + step_size > last_parameter && u != last_parameter) {
// Ensure the last parameter is included without going over
u = last_parameter - step_size;
}
}
return tangentVectors;
}

int main(int argc, char* argv[]) {
return 1;
}
}
10 changes: 10 additions & 0 deletions backend/src/tangent_vectors_geom_bspline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef TANGENT_CALCULATOR_H
#define TANGENT_CALCULATOR_H

#include <vector>
#include <Geom_BSplineCurve.hxx>
#include <gp_Vec.hxx>

std::vector<gp_Vec> calculate_tangent_vectors(const Handle(Geom_BSplineCurve)& bsplineCurve);

#endif

0 comments on commit 2966fe4

Please sign in to comment.