diff --git a/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C b/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C index 99680f8bb6..ee260ea644 100644 --- a/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C +++ b/src/triSurface/triSurface/interfaces/OBJ/writeOBJ.C @@ -52,12 +52,8 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const // Print patch names as comment forAll(myPatches, patchI) { - const surfacePatch& patch = myPatches[patchI]; - - if (patch.size() > 0) - { - os << "# " << patchI << " " << patch.name() << nl; - } + os << "# " << patchI << " " + << myPatches[patchI].name() << nl; } os << "#" << nl; @@ -81,29 +77,25 @@ void triSurface::writeOBJ(const bool writeSorted, Ostream& os) const forAll(myPatches, patchI) { - const surfacePatch& patch = myPatches[patchI]; - // Print all faces belonging to this patch - if (patch.size() > 0) + + os << "g " << myPatches[patchI].name() << nl; + + for + ( + label patchFaceI = 0; + patchFaceI < myPatches[patchI].size(); + patchFaceI++ + ) { - os << "g " << patch.name() << nl; + const label faceI = faceMap[faceIndex++]; - for - ( - label patchFaceI = 0; - patchFaceI < patch.size(); - patchFaceI++ - ) - { - const label faceI = faceMap[faceIndex++]; - - os << "f " - << operator[](faceI)[0] + 1 << ' ' - << operator[](faceI)[1] + 1 << ' ' - << operator[](faceI)[2] + 1 - //<< " # " << operator[](faceI).region() - << nl; - } + os << "f " + << operator[](faceI)[0] + 1 << ' ' + << operator[](faceI)[1] + 1 << ' ' + << operator[](faceI)[2] + 1 + //<< " # " << operator[](faceI).region() + << nl; } } } diff --git a/src/triSurface/triSurface/interfaces/STL/writeSTL.C b/src/triSurface/triSurface/interfaces/STL/writeSTL.C index 63874b185e..ea3bc39845 100644 --- a/src/triSurface/triSurface/interfaces/STL/writeSTL.C +++ b/src/triSurface/triSurface/interfaces/STL/writeSTL.C @@ -31,20 +31,20 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::triSurface::writeSTLASCII(Ostream& os) const +void Foam::triSurface::writeSTLASCII(const bool writeSorted, Ostream& os) const { labelList faceMap; surfacePatchList myPatches(calcPatches(faceMap)); - label faceIndex = 0; - forAll(myPatches, patchI) + if (writeSorted) { - // Print all faces belonging to this region - const surfacePatch& patch = myPatches[patchI]; - - if (patch.size() > 0) + label faceIndex = 0; + forAll(myPatches, patchI) { + // Print all faces belonging to this region + const surfacePatch& patch = myPatches[patchI]; + os << "solid " << patch.name() << endl; for @@ -80,6 +80,63 @@ void Foam::triSurface::writeSTLASCII(Ostream& os) const os << "endsolid " << patch.name() << endl; } } + else + { + // Get patch (=compact region) per face + labelList patchIDs(size()); + forAll(myPatches, patchI) + { + label faceI = myPatches[patchI].start(); + + forAll(myPatches[patchI], i) + { + patchIDs[faceMap[faceI++]] = patchI; + } + } + + label currentPatchI = -1; + + forAll(*this, faceI) + { + if (currentPatchI != patchIDs[faceI]) + { + if (currentPatchI != -1) + { + // Have already valid patch. Close it. + os << "endsolid " << myPatches[currentPatchI].name() + << nl; + } + currentPatchI = patchIDs[faceI]; + os << "solid " << myPatches[currentPatchI].name() << nl; + } + + const vector& n = faceNormals()[faceI]; + + os << " facet normal " + << n.x() << ' ' << n.y() << ' ' << n.z() << nl + << " outer loop" << endl; + + const labelledTri& f = (*this)[faceI]; + const point& pa = points()[f[0]]; + const point& pb = points()[f[1]]; + const point& pc = points()[f[2]]; + + os << " vertex " + << pa.x() << ' ' << pa.y() << ' ' << pa.z() << nl + << " vertex " + << pb.x() << ' ' << pb.y() << ' ' << pb.z() << nl + << " vertex " + << pc.x() << ' ' << pc.y() << ' ' << pc.z() << nl + << " endloop" << nl + << " endfacet" << endl; + } + + if (currentPatchI != -1) + { + os << "endsolid " << myPatches[currentPatchI].name() + << nl; + } + } } diff --git a/src/triSurface/triSurface/triSurface.C b/src/triSurface/triSurface/triSurface.C index 258958ca69..8f80c21617 100644 --- a/src/triSurface/triSurface/triSurface.C +++ b/src/triSurface/triSurface/triSurface.C @@ -448,7 +448,7 @@ void Foam::triSurface::write } else if (ext == "stl") { - return writeSTLASCII(OFstream(name)()); + return writeSTLASCII(sort, OFstream(name)()); } else if (ext == "stlb") { diff --git a/src/triSurface/triSurface/triSurface.H b/src/triSurface/triSurface/triSurface.H index b32ca47947..cb0cf8c760 100644 --- a/src/triSurface/triSurface/triSurface.H +++ b/src/triSurface/triSurface/triSurface.H @@ -138,7 +138,7 @@ class triSurface //- Write to Ostream in ASCII STL format. // Each region becomes 'solid' 'endsolid' block. - void writeSTLASCII(Ostream&) const; + void writeSTLASCII(const bool writeSorted, Ostream&) const; //- Write to std::ostream in BINARY STL format void writeSTLBINARY(std::ostream&) const;