From 196caf7b678c4a66ba6f325da61609579744a978 Mon Sep 17 00:00:00 2001 From: Philippose Rajan Date: Tue, 17 Aug 2010 20:59:18 +0000 Subject: [PATCH] * Initial version of an extended STL export format for splitting up mesh faces into separate "solid" entities based on the boundary condition number * This extended format has been added in addition to the normal STL export format of Netgen * The extended format can also be read by Netgen without any changes to the STL reader code * ToDo: The code needs to be optimised and sped up.... current implementation is not sufficiently elegant! --- libsrc/interface/writeuser.cpp | 86 ++++++++++++++++++++++++++++++++++ libsrc/interface/writeuser.hpp | 10 ++++ 2 files changed, 96 insertions(+) diff --git a/libsrc/interface/writeuser.cpp b/libsrc/interface/writeuser.cpp index 71c91afb..60d7f44e 100644 --- a/libsrc/interface/writeuser.cpp +++ b/libsrc/interface/writeuser.cpp @@ -32,6 +32,7 @@ namespace netgen "FEAP Format", ".mesh", "Elmer Format", "*", "STL Format", ".stl", + "STL Extended Format", ".stl", "VRML Format", ".*", "Gmsh Format", ".gmsh", "Gmsh2 Format", ".gmsh2", @@ -93,6 +94,13 @@ bool WriteUserFormat (const string & format, else if (format == "STL Format") WriteSTLFormat (mesh, filename); + // Philippose - 16 August 2010 + // Added additional STL Export in which + // each face of the geometry is treated + // as a separate "solid" entity + else if (format == "STL Extended Format") + WriteSTLExtFormat (mesh, filename); + else if (format == "VRML Format") WriteVRMLFormat (mesh, 1, filename); @@ -332,6 +340,84 @@ void WriteSTLFormat (const Mesh & mesh, +/* + * Philippose - 16 August 2010 + * Save surface mesh as STL file + * with a separate solid definition + * for each face + * - This helps in splitting up the + * STL into named boundary faces + * when using a third-party mesher + */ + +void WriteSTLExtFormat (const Mesh & mesh, + const string & filename) +{ + cout << "\nWrite STL Surface Mesh (with separated boundary faces)" << endl; + + ofstream outfile (filename.c_str()); + + outfile.precision(10); + + Array faceBCs; + + // Collect the BC numbers used in the mesh + for(int faceNr = 1; faceNr <= mesh.GetNFD(); faceNr++) + { + int bcNum = mesh.GetFaceDescriptor(faceNr).BCProperty(); + + if(!faceBCs.Contains(bcNum)) + { + faceBCs.Append(bcNum); + } + } + + // Now actually write the data to file + for(int bcInd = 1; bcInd <= faceBCs.Size(); bcInd++) + { + outfile << "solid Boundary_" << faceBCs.Elem(bcInd) << "\n"; + + for(int faceNr = 1;faceNr <= mesh.GetNFD(); faceNr++) + { + if(mesh.GetFaceDescriptor(faceNr).BCProperty() != faceBCs.Elem(bcInd)) + { + continue; + } + + Array faceSei; + mesh.GetSurfaceElementsOfFace(faceNr,faceSei); + + for (int i = 0; i < faceSei.Size(); i++) + { + outfile << "facet normal "; + const Point3d& p1 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(1)); + const Point3d& p2 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(2)); + const Point3d& p3 = mesh.Point(mesh.SurfaceElement(faceSei[i]).PNum(3)); + + Vec3d normal = Cross(p2-p1,p3-p1); + if (normal.Length() != 0) + { + normal /= (normal.Length()); + } + + outfile << normal.X() << " " << normal.Y() << " " << normal.Z() << "\n"; + outfile << "outer loop\n"; + + outfile << "vertex " << p1.X() << " " << p1.Y() << " " << p1.Z() << "\n"; + outfile << "vertex " << p2.X() << " " << p2.Y() << " " << p2.Z() << "\n"; + outfile << "vertex " << p3.X() << " " << p3.Y() << " " << p3.Z() << "\n"; + + outfile << "endloop\n"; + outfile << "endfacet\n"; + } + } + outfile << "endsolid Boundary_" << faceBCs.Elem(bcInd) << "\n"; + } +} + + + + /* * * write surface mesh as VRML file diff --git a/libsrc/interface/writeuser.hpp b/libsrc/interface/writeuser.hpp index b9fc9869..3e06b1ee 100644 --- a/libsrc/interface/writeuser.hpp +++ b/libsrc/interface/writeuser.hpp @@ -40,6 +40,16 @@ extern void WriteSTLFormat (const Mesh & mesh, const string & filename); + +// Philippose - 16 August 2010 +// Added the STL Extended format in which +// each face of the geometry is treated as +// a separate "solid" entity in the STL file +extern +void WriteSTLExtFormat (const Mesh & mesh, + const string & filename); + + extern void WriteVRMLFormat (const Mesh & mesh, bool faces,