Skip to content

Commit

Permalink
Fix unit handling in an edge case.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacsv committed Jul 17, 2023
1 parent 5c5f2d9 commit f953274
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
Binary file modified dist/occt-import-js.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions occt-import-js/example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ int main (int argc, const char* argv[])
}

ImportParams params;
params.linearUnit = ImportParams::LinearUnit::Meter;
Importer::Result result = importer->LoadFile (argv[1], params);
if (result != Importer::Result::Success) {
return 1;
Expand Down
27 changes: 26 additions & 1 deletion occt-import-js/src/importer-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <UnitsMethods.hxx>

VectorBuffer::VectorBuffer (const std::vector<uint8_t>& v)
{
Expand Down Expand Up @@ -95,6 +96,25 @@ bool OcctFace::HasTriangulation () const
return true;
}

UnitsMethods_LengthUnit LinearUnitToLengthUnit (ImportParams::LinearUnit linearUnit)
{
UnitsMethods_LengthUnit lengthUnit = UnitsMethods_LengthUnit_Millimeter;
switch (linearUnit) {
case ImportParams::LinearUnit::Millimeter:
return UnitsMethods_LengthUnit_Millimeter;
case ImportParams::LinearUnit::Centimeter:
return UnitsMethods_LengthUnit_Centimeter;
case ImportParams::LinearUnit::Meter:
return UnitsMethods_LengthUnit_Meter;
case ImportParams::LinearUnit::Inch:
return UnitsMethods_LengthUnit_Inch;
case ImportParams::LinearUnit::Foot:
return UnitsMethods_LengthUnit_Foot;
default:
return UnitsMethods_LengthUnit_Millimeter;
}
}

bool TriangulateShape (TopoDS_Shape& shape, const ImportParams& params)
{
Standard_Real linDeflection = params.linearDeflection;
Expand All @@ -112,7 +132,12 @@ bool TriangulateShape (TopoDS_Shape& shape, const ImportParams& params)
Standard_Real avgSize = ((xMax - xMin) + (yMax - yMin) + (zMax - zMin)) / 3.0;
linDeflection = avgSize * params.linearDeflection;
if (linDeflection < Precision::Confusion ()) {
linDeflection = 0.1;
// use 1mm in the current unit
double mmToUnit = UnitsMethods::GetLengthUnitScale (
UnitsMethods_LengthUnit_Millimeter,
LinearUnitToLengthUnit (params.linearUnit)
);
linDeflection = 1.0 * mmToUnit;
}
}

Expand Down
2 changes: 2 additions & 0 deletions occt-import-js/src/importer-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <TopoDS_Face.hxx>
#include <TopLoc_Location.hxx>
#include <Poly_Triangulation.hxx>
#include <UnitsMethods_LengthUnit.hxx>

#include <vector>
#include <fstream>
Expand Down Expand Up @@ -36,4 +37,5 @@ class OcctFace : public Face
TopLoc_Location location;
};

UnitsMethods_LengthUnit LinearUnitToLengthUnit (ImportParams::LinearUnit linearUnit);
bool TriangulateShape (TopoDS_Shape& shape, const ImportParams& params);
21 changes: 1 addition & 20 deletions occt-import-js/src/importer-xcaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,26 +371,7 @@ Importer::Result ImporterXcaf::LoadFile (const std::vector<std::uint8_t>& fileCo
{
document = new TDocStd_Document ("XmlXCAF");

UnitsMethods_LengthUnit lengthUnit = UnitsMethods_LengthUnit_Millimeter;
switch (params.linearUnit) {
case ImportParams::LinearUnit::Millimeter:
lengthUnit = UnitsMethods_LengthUnit_Millimeter;
break;
case ImportParams::LinearUnit::Centimeter:
lengthUnit = UnitsMethods_LengthUnit_Centimeter;
break;
case ImportParams::LinearUnit::Meter:
lengthUnit = UnitsMethods_LengthUnit_Meter;
break;
case ImportParams::LinearUnit::Inch:
lengthUnit = UnitsMethods_LengthUnit_Inch;
break;
case ImportParams::LinearUnit::Foot:
lengthUnit = UnitsMethods_LengthUnit_Foot;
break;
default:
return Importer::Result::ImportFailed;
}
UnitsMethods_LengthUnit lengthUnit = LinearUnitToLengthUnit (params.linearUnit);
XCAFDoc_DocumentTool::SetLengthUnit (document, 1.0, lengthUnit);

if (!TransferToDocument (fileContent)) {
Expand Down

0 comments on commit f953274

Please sign in to comment.