1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0028840: Data Exchange - rewrite the STL Reader/Writer

STL Reader and Writer tools have been refactored to improve performance and usability:
- Basic reading of STL file is separated to abstract class RWStl_Reader which is not bound to particular data structures; the target data model can be bound via inheritance.
- RWStl package uses class Poly_Triangulation to represent triangular mesh.
- Obsolete data structures and tools (packages StlMesh and StlTransfer) are removed.
This commit is contained in:
aml
2017-06-14 08:07:26 +03:00
committed by bugmaster
parent c5b39011b1
commit 4178b3531b
44 changed files with 1168 additions and 2692 deletions

View File

@@ -17,6 +17,7 @@
#include <AIS_InteractiveContext.hxx>
#include <Aspect_TypeOfMarker.hxx>
#include <Bnd_Box.hxx>
#include <BRep_Builder.hxx>
#include <DBRep.hxx>
#include <Draw.hxx>
#include <Draw_Interpretor.hxx>
@@ -45,13 +46,12 @@
#include <StdSelect_ViewerSelector3d.hxx>
#include <StlAPI.hxx>
#include <StlAPI_Writer.hxx>
#include <StlMesh_Mesh.hxx>
#include <StlMesh_SequenceOfMeshTriangle.hxx>
#include <TColgp_SequenceOfXYZ.hxx>
#include <TCollection_AsciiString.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_HPackedMapOfInteger.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <V3d_View.hxx>
#include <ViewerTest.hxx>
@@ -90,27 +90,50 @@ static Standard_Integer writestl
}
StlAPI_Writer aWriter;
aWriter.ASCIIMode() = isASCIIMode;
StlAPI_ErrorStatus aStatus = aWriter.Write (aShape, argv[2]);
switch (aStatus)
{
case StlAPI_MeshIsEmpty: di << "** Error **: Mesh is empty. Please, compute triangulation before."; break;
case StlAPI_CannotOpenFile: di << "** Error **: Cannot create/open a file with the passed name."; break;
case StlAPI_StatusOK: default: break;
}
Standard_Boolean isOK = aWriter.Write (aShape, argv[2]);
if (!isOK)
di << "** Error **: Mesh writing has been failed.\n";
}
return 0;
}
static Standard_Integer readstl
(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
//=============================================================================
//function : readstl
//purpose : Reads stl file
//=============================================================================
static Standard_Integer readstl(Draw_Interpretor& theDI,
Standard_Integer theArgc,
const char** theArgv)
{
if (argc<3) di << "wrong number of parameters" << "\n";
else {
TopoDS_Shape aShape ;
StlAPI::Read(aShape,argv[2]);
DBRep::Set(argv[1],aShape);
if (theArgc < 3)
{
theDI << "wrong number of parameters" << "\n";
return 1;
}
else
{
if (theArgc == 4 &&
strcmp("triangulation", theArgv[3]) == 0)
{
// Read STL file to the triangulation.
Handle(Poly_Triangulation) aTriangulation = RWStl::ReadFile (theArgv[2]);
TopoDS_Face aFace;
BRep_Builder aB;
aB.MakeFace(aFace);
aB.UpdateFace(aFace, aTriangulation);
DBRep::Set(theArgv[1], aFace);
}
else
{
TopoDS_Shape aShape;
Standard_DISABLE_DEPRECATION_WARNINGS
StlAPI::Read(aShape, theArgv[2]);
Standard_ENABLE_DEPRECATION_WARNINGS
DBRep::Set(theArgv[1], aShape);
}
}
return 0;
}
@@ -260,7 +283,7 @@ static Standard_Integer createmesh
// Progress indicator
OSD_Path aFile( argv[2] );
Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
Handle(Poly_Triangulation) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
di << "Reading OK...\n";
Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );
@@ -275,7 +298,7 @@ static Standard_Integer createmesh
// Hide all nodes by default
Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
Standard_Integer aLen = aSTLMesh->Vertices().Length();
Standard_Integer aLen = aSTLMesh->Nodes().Length();
for ( Standard_Integer anIndex = 1; anIndex <= aLen; anIndex++ )
aNodes->ChangeMap().Add( anIndex );
aMesh->SetHiddenNodes( aNodes );
@@ -1223,7 +1246,7 @@ void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands)
theCommands.Add ("writevrml", "shape file [version VRML#1.0/VRML#2.0 (1/2): 2 by default] [representation shaded/wireframe/both (0/1/2): 1 by default]",__FILE__,writevrml,g);
theCommands.Add ("writestl", "shape file [ascii/binary (0/1) : 1 by default] [InParallel (0/1) : 0 by default]",__FILE__,writestl,g);
theCommands.Add ("readstl", "shape file",__FILE__,readstl,g);
theCommands.Add ("readstl", "shape file [triangulation: no by default]",__FILE__,readstl,g);
theCommands.Add ("loadvrml" , "shape file",__FILE__,loadvrml,g);
theCommands.Add ("meshfromstl", "creates MeshVS_Mesh from STL file", __FILE__, createmesh, g );

View File

@@ -13,15 +13,13 @@
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <XSDRAWSTLVRML_DataSource.hxx>
#include <Precision.hxx>
#include <Standard_Type.hxx>
#include <StlMesh_Mesh.hxx>
#include <StlMesh_MeshTriangle.hxx>
#include <StlMesh_SequenceOfMeshTriangle.hxx>
#include <TColgp_SequenceOfXYZ.hxx>
#include <TColStd_DataMapOfIntegerInteger.hxx>
#include <TColStd_DataMapOfIntegerReal.hxx>
#include <XSDRAWSTLVRML_DataSource.hxx>
IMPLEMENT_STANDARD_RTTIEXT(XSDRAWSTLVRML_DataSource,MeshVS_DataSource)
@@ -29,13 +27,13 @@ IMPLEMENT_STANDARD_RTTIEXT(XSDRAWSTLVRML_DataSource,MeshVS_DataSource)
// Function : Constructor
// Purpose :
//================================================================
XSDRAWSTLVRML_DataSource::XSDRAWSTLVRML_DataSource( const Handle( StlMesh_Mesh )& aMesh )
XSDRAWSTLVRML_DataSource::XSDRAWSTLVRML_DataSource (const Handle(Poly_Triangulation)& aMesh)
{
myMesh = aMesh;
if( !myMesh.IsNull() )
{
const TColgp_SequenceOfXYZ& aCoords = myMesh->Vertices();
const TColgp_Array1OfPnt& aCoords = myMesh->Nodes();
Standard_Integer len = aCoords.Length(), i, j;
myNodeCoords = new TColStd_HArray2OfReal(1, len, 1, 3);
cout << "Nodes : " << len << endl;
@@ -45,14 +43,14 @@ XSDRAWSTLVRML_DataSource::XSDRAWSTLVRML_DataSource( const Handle( StlMesh_Mesh )
for( i = 1; i <= len; i++ )
{
myNodes.Add( i );
xyz = aCoords(i);
xyz = aCoords(i).XYZ();
myNodeCoords->SetValue(i, 1, xyz.X());
myNodeCoords->SetValue(i, 2, xyz.Y());
myNodeCoords->SetValue(i, 3, xyz.Z());
}
const StlMesh_SequenceOfMeshTriangle& aSeq = myMesh->Triangles();
const Poly_Array1OfTriangle& aSeq = myMesh->Triangles();
len = aSeq.Length();
myElemNormals = new TColStd_HArray2OfReal(1, len, 1, 3);
myElemNodes = new TColStd_HArray2OfInteger(1, len, 1, 3);
@@ -62,19 +60,33 @@ XSDRAWSTLVRML_DataSource::XSDRAWSTLVRML_DataSource( const Handle( StlMesh_Mesh )
for( i = 1; i <= len; i++ )
{
myElements.Add( i );
Handle( StlMesh_MeshTriangle ) aTriangle = aSeq.Value( i );
Standard_Integer V[3]; Standard_Real nx, ny, nz;
aTriangle->GetVertexAndOrientation( V[0], V[1], V[2], nx, ny, nz );
const Poly_Triangle& aTri = aSeq(i);
Standard_Integer V[3];
aTri.Get (V[0], V[1], V[2]);
const gp_Pnt aP1 = aCoords (V[0]);
const gp_Pnt aP2 = aCoords (V[1]);
const gp_Pnt aP3 = aCoords (V[2]);
gp_Vec aV1(aP1, aP2);
gp_Vec aV2(aP2, aP3);
gp_Vec aN = aV1.Crossed(aV2);
if (aN.SquareMagnitude() > Precision::SquareConfusion())
aN.Normalize();
else
aN.SetCoord(0.0, 0.0, 0.0);
for( j = 0; j < 3; j++ )
{
myElemNodes->SetValue(i, j+1, V[j]);
}
myElemNormals->SetValue(i, 1, nx);
myElemNormals->SetValue(i, 2, ny);
myElemNormals->SetValue(i, 3, nz);
myElemNormals->SetValue (i, 1, aN.X());
myElemNormals->SetValue (i, 2, aN.Y());
myElemNormals->SetValue (i, 3, aN.Z());
}
}
cout << "Construction is finished" << endl;

View File

@@ -30,7 +30,7 @@
#include <Standard_Address.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <Standard_Real.hxx>
class StlMesh_Mesh;
#include <Poly_Triangulation.hxx>
class XSDRAWSTLVRML_DataSource;
@@ -44,7 +44,7 @@ public:
//! Constructor
Standard_EXPORT XSDRAWSTLVRML_DataSource(const Handle(StlMesh_Mesh)& aMesh);
Standard_EXPORT XSDRAWSTLVRML_DataSource(const Handle(Poly_Triangulation)& aMesh);
//! Returns geometry information about node ( if IsElement is False ) or element ( IsElement is True )
//! by co-ordinates. For element this method must return all its nodes co-ordinates in the strict order: X, Y, Z and
@@ -85,7 +85,7 @@ protected:
private:
Handle(StlMesh_Mesh) myMesh;
Handle(Poly_Triangulation) myMesh;
TColStd_PackedMapOfInteger myNodes;
TColStd_PackedMapOfInteger myElements;
Handle(TColStd_HArray2OfInteger) myElemNodes;

View File

@@ -15,8 +15,6 @@
#include <Standard_Type.hxx>
#include <StlMesh_MeshTriangle.hxx>
#include <StlMesh_SequenceOfMeshTriangle.hxx>
#include <TColgp_SequenceOfXYZ.hxx>
#include <TColStd_DataMapOfIntegerInteger.hxx>
#include <TColStd_DataMapOfIntegerReal.hxx>