OpenCascade源码分析之StlAPI(网格离散化后导出stl文件)
奈何调库满足不了项目需求,并且网上没有详细资料啊 大家都偷懒的话只能自己来分析了,那就分析一下源码 看它对曲面的离散的步骤是怎么样的吧
一、StlAPI.hxx|StlAPI.cxx
我们在调用将离散网格数据写入stl的时候,一般会用到如下方法:
//需要离散的拓扑Shape
TopoDS_Shape cur;
//保存的文件名
const char* name = "a.stl";
StlAPI::Write(cur,name);
那么对于Write方法是如何运作的呢,接下来来到源码部分
//=============================================================================
//function : Write
//purpose :
//=============================================================================
Standard_Boolean StlAPI::Write (const TopoDS_Shape& theShape,
const Standard_CString theFile,
const Standard_Boolean theAsciiMode)
{
StlAPI_Writer aWriter;
aWriter.ASCIIMode() = theAsciiMode;
return aWriter.Write (theShape, theFile);
}
代码中调用了 StlAPI_Writer.hxx 同时设置了写文件是以文本格式还是二进制模式输出
接下来来到StlAPI_Writer部分查看实现
二、StlAPI_Writer.hxx | StlAPI_Writer.cxx
首先补充几个步骤 方便之后的代码阅读
关于Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (aFace), aLoc);
Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (aFace), aLoc);
该句代码主要调用了
const Poly_ListOfTriangulation& BRep_Tool::Triangulations (const TopoDS_Face& theFace,
TopLoc_Location& theLocation)
{
theLocation = theFace.Location();
const BRep_TFace* aTFace = static_cast<const BRep_TFace*>(theFace.TShape().get());
return aTFace->Triangulations();
}
//而aTFace调用了下面的方法
//=======================================================================
//function : Triangulation
//purpose :
//=======================================================================
const Handle(Poly_Triangulation)& BRep_TFace::Triangulation (const Poly_MeshPurpose thePurpose) const
{
//这边thePurpose默认为NONE
if (thePurpose == Poly_MeshPurpose_NONE)
{
return ActiveTriangulation();
}
xxxxx //之后的代码 没用到 就不解读了先删除
}
//ActiveTrigulation()返回了成员Handle(Poly_Triangulation) myActiveTriangulation;
const Handle(Poly_Triangulation)& ActiveTriangulation() const {
return myActiveTriangulation; }
此文件中只实现了一个方法,就是Write方法
引用的头文件为:
#include <StlAPI_Writer.hxx>
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <Message.hxx>
#include <Message_Messenger.hxx>