forked from kovacsv/occt-import-js
-
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.
Add output interface for step reader.
- Loading branch information
Showing
5 changed files
with
207 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
#include <iostream> | ||
|
||
#include "occt-import-js.hpp" | ||
#include "importer.hpp" | ||
|
||
class ConsoleOutput : public Output | ||
{ | ||
public: | ||
ConsoleOutput () | ||
{ | ||
|
||
} | ||
|
||
virtual void OnShape (const Shape& shape) override | ||
{ | ||
std::cout << "Shape Start" << std::endl; | ||
shape.EnumerateFaces ([] (const Face& face) { | ||
std::cout << " Face Start" << std::endl; | ||
face.EnumerateVertices ([] (double x, double y, double z) { | ||
std::cout << " Vertex: " << x << ", " << y << ", " << z << std::endl; | ||
}); | ||
face.EnumerateTriangles ([] (int v0, int v1, int v2) { | ||
std::cout << " Trianlge: " << v0 << ", " << v1 << ", " << v2 << std::endl; | ||
}); | ||
std::cout << " Face End" << std::endl; | ||
}); | ||
std::cout << "Shape End" << std::endl; | ||
} | ||
}; | ||
|
||
int main (int argc, const char* argv[]) | ||
{ | ||
if (argc < 2) { | ||
return 1; | ||
} | ||
|
||
StepToJson (argv[1]); | ||
ConsoleOutput output; | ||
ReadStepFile (argv[1], output); | ||
|
||
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,124 @@ | ||
#include "importer.hpp" | ||
|
||
#include <STEPControl_Reader.hxx> | ||
#include <TopExp_Explorer.hxx> | ||
#include <TopoDS.hxx> | ||
#include <BRep_Tool.hxx> | ||
#include <BRepMesh_IncrementalMesh.hxx> | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
class VectorBuffer : public std::streambuf | ||
{ | ||
public: | ||
VectorBuffer (const std::vector<uint8_t>& v) | ||
{ | ||
setg ((char*) v.data (), (char*) v.data (), (char*) (v.data () + v.size ())); | ||
} | ||
|
||
~VectorBuffer () | ||
{ | ||
|
||
} | ||
}; | ||
|
||
class OcctFace : public Face | ||
{ | ||
public: | ||
OcctFace (const opencascade::handle<Poly_Triangulation>& triangulation) : | ||
triangulation (triangulation) | ||
{ | ||
|
||
} | ||
|
||
virtual void EnumerateVertices (const std::function<void (double, double, double)>& onVertex) const override | ||
{ | ||
// TODO: get transformation from location | ||
for (Standard_Integer nodeIndex = 1; nodeIndex <= triangulation->NbNodes (); nodeIndex++) { | ||
gp_Pnt vertex = triangulation->Node (nodeIndex); | ||
onVertex (vertex.X (), vertex.Y (), vertex.Z ()); | ||
} | ||
} | ||
|
||
virtual void EnumerateTriangles (const std::function<void (int, int, int)>& onTriangle) const override | ||
{ | ||
for (Standard_Integer triangleIndex = 1; triangleIndex <= triangulation->NbTriangles (); triangleIndex++) { | ||
Poly_Triangle triangle = triangulation->Triangle (triangleIndex); | ||
onTriangle (triangle (1), triangle (2), triangle (3)); | ||
} | ||
} | ||
|
||
const opencascade::handle<Poly_Triangulation>& triangulation; | ||
}; | ||
|
||
class OcctShape : public Shape | ||
{ | ||
public: | ||
OcctShape (TopoDS_Shape& shape) : | ||
shape (shape) | ||
{ | ||
|
||
} | ||
|
||
virtual void EnumerateFaces (const std::function<void (const Face& face)>& onFace) const override | ||
{ | ||
for (TopExp_Explorer explorer (shape, TopAbs_FACE); explorer.More (); explorer.Next ()) { | ||
const TopoDS_Face& face = TopoDS::Face (explorer.Current ()); | ||
TopLoc_Location location; | ||
Handle (Poly_Triangulation) triangulation = BRep_Tool::Triangulation (face, location); | ||
if (triangulation.IsNull () || triangulation->NbNodes () == 0 || triangulation->NbTriangles () == 0) { | ||
continue; | ||
} | ||
OcctFace outputFace (triangulation); | ||
onFace (outputFace); | ||
} | ||
} | ||
|
||
TopoDS_Shape& shape; | ||
}; | ||
|
||
static Result ReadStepFile (std::istream& inputStream, Output& output) | ||
{ | ||
STEPControl_Reader stepReader; | ||
std::string dummyFileName = "stp"; | ||
IFSelect_ReturnStatus readStatus = stepReader.ReadStream (dummyFileName.c_str (), inputStream); | ||
if (readStatus != IFSelect_RetDone) { | ||
return Result::ImportFailed; | ||
} | ||
|
||
stepReader.TransferRoots (); | ||
|
||
for (Standard_Integer rank = 1; rank <= stepReader.NbShapes (); rank++) { | ||
TopoDS_Shape shape = stepReader.Shape (rank); | ||
|
||
// TODO: calculate accuracy based on bounding box | ||
double accuracy = 1.0; | ||
|
||
// Calculate triangulation | ||
BRepMesh_IncrementalMesh mesh (shape, accuracy); | ||
|
||
OcctShape outputShape (shape); | ||
output.OnShape (outputShape); | ||
} | ||
|
||
return Result::Success; | ||
} | ||
|
||
Result ReadStepFile (const std::string& filePath, Output& output) | ||
{ | ||
std::ifstream inputStream (filePath, std::ios::binary); | ||
if (!inputStream.is_open ()) { | ||
return Result::FileNotFound; | ||
} | ||
Result result = ReadStepFile (inputStream, output); | ||
inputStream.close (); | ||
return result; | ||
} | ||
|
||
Result ReadStepFile (const std::vector<std::uint8_t>& fileContent, Output& output) | ||
{ | ||
VectorBuffer fileBuffer (fileContent); | ||
std::istream fileStream (&fileBuffer); | ||
return ReadStepFile (fileStream, output); | ||
} |
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,37 @@ | ||
#ifndef IMPORTER_HPP | ||
#define IMPORTER_HPP | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <functional> | ||
|
||
class Face | ||
{ | ||
public: | ||
virtual void EnumerateVertices (const std::function<void (double, double, double)>& onVertex) const = 0; | ||
virtual void EnumerateTriangles (const std::function<void (int, int, int)>& onTriangle) const = 0; | ||
}; | ||
|
||
class Shape | ||
{ | ||
public: | ||
virtual void EnumerateFaces (const std::function<void (const Face& face)>& onFace) const = 0; | ||
}; | ||
|
||
class Output | ||
{ | ||
public: | ||
virtual void OnShape (const Shape& shape) = 0; | ||
}; | ||
|
||
enum class Result | ||
{ | ||
Success = 0, | ||
FileNotFound = 1, | ||
ImportFailed = 2 | ||
}; | ||
|
||
Result ReadStepFile (const std::string& filePath, Output& output); | ||
Result ReadStepFile (const std::vector<std::uint8_t>& fileContent, Output& output); | ||
|
||
#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,101 +1,43 @@ | ||
#include "occt-import-js.hpp" | ||
|
||
#include <STEPControl_Reader.hxx> | ||
#include <TopExp_Explorer.hxx> | ||
#include <TopoDS.hxx> | ||
#include <BRep_Tool.hxx> | ||
#include <BRepMesh_IncrementalMesh.hxx> | ||
#include "importer.hpp" | ||
|
||
#ifdef EMSCRIPTEN | ||
#include <emscripten/bind.h> | ||
#endif | ||
|
||
static std::string StepToJson (std::istream& inputStream) | ||
{ | ||
STEPControl_Reader reader; | ||
std::string dummyFileName = "stp"; | ||
IFSelect_ReturnStatus readStatus = reader.ReadStream (dummyFileName.c_str (), inputStream); | ||
if (readStatus != IFSelect_RetDone) { | ||
return ""; // TODO | ||
} | ||
|
||
reader.TransferRoots (); | ||
|
||
for (Standard_Integer rank = 1; rank <= reader.NbShapes (); rank++) { | ||
TopoDS_Shape shape = reader.Shape (rank); | ||
|
||
// TODO: calculate accuracy based on bounding box | ||
double accuracy = 1.0; | ||
|
||
// Calculate triangulation | ||
BRepMesh_IncrementalMesh mesh (shape, accuracy); | ||
|
||
for (TopExp_Explorer explorer (shape, TopAbs_FACE); explorer.More (); explorer.Next ()) { | ||
const TopoDS_Face& face = TopoDS::Face (explorer.Current ()); | ||
|
||
TopLoc_Location location; | ||
Handle (Poly_Triangulation) triangulation = BRep_Tool::Triangulation (face, location); | ||
if (triangulation.IsNull () || triangulation->NbNodes () == 0 || triangulation->NbTriangles () == 0) { | ||
continue; | ||
} | ||
// TODO: get transformation from location | ||
for (Standard_Integer nodeIndex = 1; nodeIndex <= triangulation->NbNodes (); nodeIndex++) { | ||
gp_Pnt aPnt = triangulation->Node (nodeIndex); | ||
continue; | ||
} | ||
for (Standard_Integer triangleIndex = 1; triangleIndex <= triangulation->NbTriangles (); triangleIndex++) { | ||
Poly_Triangle aTri = triangulation->Triangle (triangleIndex); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
std::string StepToJson (const std::string& filePath) | ||
{ | ||
std::ifstream inputStream (filePath, std::ios::binary); | ||
if (!inputStream.is_open ()) { | ||
return ""; // TODO | ||
} | ||
std::string result = StepToJson (inputStream); | ||
inputStream.close (); | ||
return result; | ||
} | ||
|
||
class VectorBuffer : public std::streambuf | ||
class EmscriptenOutput : public Output | ||
{ | ||
public: | ||
VectorBuffer (const std::vector<uint8_t>& v) | ||
EmscriptenOutput () | ||
{ | ||
setg ((char*) v.data (), (char*) v.data (), (char*) (v.data () + v.size ())); | ||
} | ||
} | ||
|
||
~VectorBuffer () | ||
virtual void OnShape (const Shape& shape) override | ||
{ | ||
|
||
shape.EnumerateFaces ([] (const Face& face) { | ||
face.EnumerateVertices ([] (double x, double y, double z) { | ||
return; | ||
}); | ||
face.EnumerateTriangles ([] (int v0, int v1, int v2) { | ||
return; | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
std::string StepToJson (const std::vector<std::uint8_t>& fileContent) | ||
{ | ||
VectorBuffer fileBuffer (fileContent); | ||
std::istream fileStream (&fileBuffer); | ||
return StepToJson (fileStream); | ||
} | ||
|
||
#ifdef EMSCRIPTEN | ||
|
||
std::string StepToJsonEmscripten (const emscripten::val& content) | ||
int ReadStepFile (const emscripten::val& content) | ||
{ | ||
const std::vector<uint8_t>& contentArr = emscripten::vecFromJSArray<std::uint8_t> (content); | ||
return StepToJson (contentArr); | ||
EmscriptenOutput output; | ||
return ReadStepFile (contentArr, output); | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS (assimpjs) | ||
{ | ||
emscripten::function<std::string, const emscripten::val&> ("StepToJson", &StepToJsonEmscripten); | ||
emscripten::function<int, const emscripten::val&> ("ReadStepFile", &ReadStepFile); | ||
} | ||
|
||
#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,10 +1,4 @@ | ||
#ifndef OCCTIMPORTJS_HPP | ||
#define OCCTIMPORTJS_HPP | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::string StepToJson (const std::string& filePath); | ||
std::string StepToJson (const std::vector<std::uint8_t>& fileContent); | ||
|
||
#endif |