From 9e7d7d3101daf5c0bbe23e7bad5cdbb7c6913e8f Mon Sep 17 00:00:00 2001 From: Joachim Mairboeck Date: Thu, 10 Mar 2022 19:11:17 +0100 Subject: [PATCH 1/4] add parameter for setting the output unit fixes #4 --- src/STEPToMesh.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/STEPToMesh.cpp b/src/STEPToMesh.cpp index 42cddf2..cd4d63f 100644 --- a/src/STEPToMesh.cpp +++ b/src/STEPToMesh.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -76,12 +77,13 @@ void getNamedSolids(const TopLoc_Location& location, const std::string& prefix, } } -void read(const std::string& inFile, std::vector& namedSolids) { +void read(const std::string& inFile, const std::string& unit, std::vector& namedSolids) { Handle(TDocStd_Document) document; Handle(XCAFApp_Application) application = XCAFApp_Application::GetApplication(); application->NewDocument(inFile.c_str(), document); STEPCAFControl_Reader reader; reader.SetNameMode(true); + if (!Interface_Static::SetCVal("xstep.cascade.unit", unit.c_str())) throw std::logic_error{ std::string{"Could not set unit '"} + unit + "'" }; IFSelect_ReturnStatus stat = reader.ReadFile(inFile.c_str()); if (stat != IFSelect_RetDone || !reader.Transfer(document)) throw std::logic_error{std::string{"Could not read '"} + inFile + "'"}; Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(document->Main()); @@ -128,6 +130,11 @@ void write(const std::string& outFile, const std::vector& namedSolid if (!writer.Write(compound, outFile.c_str())) throw std::logic_error{std::string{"Could not write '"} + outFile + "'"}; } +std::string str_toupper(std::string s) { + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); } ); + return s; +} + int main(int argc, char* argv[]) { cxxopts::Options options{"STEPToMesh", "STEP to triangle mesh conversion"}; options.add_options() @@ -138,6 +145,7 @@ int main(int argc, char* argv[]) { ("s,select", "Select solids by name or index (comma seperated list, index starts with 1)", cxxopts::value>()) ("l,linear", "Linear deflection", cxxopts::value()) ("a,angular", "Angular deflection (degrees)", cxxopts::value()) + ("u,unit", "Output unit (default mm)", cxxopts::value()->default_value("MM")) ("h,help", "Print usage"); try { const auto result = options.parse(argc, argv); @@ -145,7 +153,7 @@ int main(int argc, char* argv[]) { if (result.count("in")) { const std::string inFile = result["in"].as(); std::vector namedSolids; - read(inFile, namedSolids); + read(inFile, "MM", namedSolids); for (const auto& namedSolid : namedSolids) std::cout << namedSolid.name << std::endl; } else throw std::logic_error{std::string{"Missing option 'in'"}}; @@ -160,7 +168,8 @@ int main(int argc, char* argv[]) { std::vector select; if (result.count("select")) select = result["select"].as>(); std::vector namedSolids; - read(inFile, namedSolids); + const auto unit{str_toupper(result["unit"].as())}; + read(inFile, unit, namedSolids); write(outFile, namedSolids, select, linearDeflection, angularDeflection, format); } else std::cout << options.help() << std::endl; From cd328bcf520b8a29675313620bdfa71680017fab Mon Sep 17 00:00:00 2001 From: Joachim Mairboeck Date: Thu, 10 Mar 2022 19:19:42 +0100 Subject: [PATCH 2/4] formatting --- src/STEPToMesh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/STEPToMesh.cpp b/src/STEPToMesh.cpp index cd4d63f..0d6f96e 100644 --- a/src/STEPToMesh.cpp +++ b/src/STEPToMesh.cpp @@ -83,7 +83,7 @@ void read(const std::string& inFile, const std::string& unit, std::vectorNewDocument(inFile.c_str(), document); STEPCAFControl_Reader reader; reader.SetNameMode(true); - if (!Interface_Static::SetCVal("xstep.cascade.unit", unit.c_str())) throw std::logic_error{ std::string{"Could not set unit '"} + unit + "'" }; + if (!Interface_Static::SetCVal("xstep.cascade.unit", unit.c_str())) throw std::logic_error{std::string{"Could not set unit '"} + unit + "'"}; IFSelect_ReturnStatus stat = reader.ReadFile(inFile.c_str()); if (stat != IFSelect_RetDone || !reader.Transfer(document)) throw std::logic_error{std::string{"Could not read '"} + inFile + "'"}; Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(document->Main()); From 85b66c7246fc32c1b7b651ceec83a5ee525f25b3 Mon Sep 17 00:00:00 2001 From: Joachim Mairboeck Date: Mon, 14 Mar 2022 12:15:55 +0100 Subject: [PATCH 3/4] * add supported units to the help string for the --unit option * initialize units at the start and set it globally and not in read --- src/STEPToMesh.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/STEPToMesh.cpp b/src/STEPToMesh.cpp index 0d6f96e..586076d 100644 --- a/src/STEPToMesh.cpp +++ b/src/STEPToMesh.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -77,13 +78,12 @@ void getNamedSolids(const TopLoc_Location& location, const std::string& prefix, } } -void read(const std::string& inFile, const std::string& unit, std::vector& namedSolids) { +void read(const std::string& inFile, std::vector& namedSolids) { Handle(TDocStd_Document) document; Handle(XCAFApp_Application) application = XCAFApp_Application::GetApplication(); application->NewDocument(inFile.c_str(), document); STEPCAFControl_Reader reader; reader.SetNameMode(true); - if (!Interface_Static::SetCVal("xstep.cascade.unit", unit.c_str())) throw std::logic_error{std::string{"Could not set unit '"} + unit + "'"}; IFSelect_ReturnStatus stat = reader.ReadFile(inFile.c_str()); if (stat != IFSelect_RetDone || !reader.Transfer(document)) throw std::logic_error{std::string{"Could not read '"} + inFile + "'"}; Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(document->Main()); @@ -136,24 +136,39 @@ std::string str_toupper(std::string s) { } int main(int argc, char* argv[]) { + XSAlgo::Init(); + Standard_Integer unitsStart, unitsEnd; + Standard_Boolean unitsMatch; + auto units{Interface_Static::Static("xstep.cascade.unit")}; + units->EnumDef(unitsStart, unitsEnd, unitsMatch); + std::string unitDesc{"Output unit (one of "}; + for (auto i = unitsStart; i <= unitsEnd; i++) { + if (i > unitsStart) unitDesc += ", "; + unitDesc += units->EnumVal(i); + } + unitDesc += ")"; cxxopts::Options options{"STEPToMesh", "STEP to triangle mesh conversion"}; options.add_options() ("i,in", "Input file", cxxopts::value()) ("o,out", "Output file", cxxopts::value()) ("f,format", "Output file format (stl_bin or stl_ascii)", cxxopts::value()->default_value("stl_bin")) ("c,content", "List content (solids)") - ("s,select", "Select solids by name or index (comma seperated list, index starts with 1)", cxxopts::value>()) + ("s,select", "Select solids by name or index (comma separated list, index starts with 1)", cxxopts::value>()) ("l,linear", "Linear deflection", cxxopts::value()) ("a,angular", "Angular deflection (degrees)", cxxopts::value()) - ("u,unit", "Output unit (default mm)", cxxopts::value()->default_value("MM")) + ("u,unit", unitDesc, cxxopts::value()->default_value("MM")) ("h,help", "Print usage"); try { const auto result = options.parse(argc, argv); + if (result.count("unit")) { + const auto unit{str_toupper(result["unit"].as())}; + if (!units->SetCStringValue(unit.c_str())) throw std::logic_error{std::string{"Could not set unit '"} + unit + "'"}; + } if (result.count("content")) { if (result.count("in")) { const std::string inFile = result["in"].as(); std::vector namedSolids; - read(inFile, "MM", namedSolids); + read(inFile, namedSolids); for (const auto& namedSolid : namedSolids) std::cout << namedSolid.name << std::endl; } else throw std::logic_error{std::string{"Missing option 'in'"}}; @@ -168,8 +183,7 @@ int main(int argc, char* argv[]) { std::vector select; if (result.count("select")) select = result["select"].as>(); std::vector namedSolids; - const auto unit{str_toupper(result["unit"].as())}; - read(inFile, unit, namedSolids); + read(inFile, namedSolids); write(outFile, namedSolids, select, linearDeflection, angularDeflection, format); } else std::cout << options.help() << std::endl; From d39496e62a0da339cb304abbd6d7607a5b3e0c79 Mon Sep 17 00:00:00 2001 From: Joachim Mairboeck Date: Tue, 15 Mar 2022 09:56:25 +0100 Subject: [PATCH 4/4] clarify that the linear deflection is given in the specified unit --- src/STEPToMesh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/STEPToMesh.cpp b/src/STEPToMesh.cpp index 586076d..74705e8 100644 --- a/src/STEPToMesh.cpp +++ b/src/STEPToMesh.cpp @@ -154,7 +154,7 @@ int main(int argc, char* argv[]) { ("f,format", "Output file format (stl_bin or stl_ascii)", cxxopts::value()->default_value("stl_bin")) ("c,content", "List content (solids)") ("s,select", "Select solids by name or index (comma separated list, index starts with 1)", cxxopts::value>()) - ("l,linear", "Linear deflection", cxxopts::value()) + ("l,linear", "Linear deflection (in the unit given by --unit)", cxxopts::value()) ("a,angular", "Angular deflection (degrees)", cxxopts::value()) ("u,unit", unitDesc, cxxopts::value()->default_value("MM")) ("h,help", "Print usage");