-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from GenerateNU/refactor
Get gcode from tangent vectors and refactor previous tickets
- Loading branch information
Showing
12 changed files
with
237 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |