几何模型导入 stl 格式的数据

pull/1/head
剑古敛锋 2024-07-23 10:32:05 +08:00
parent c28e313e18
commit 8f58b98b2f
2 changed files with 52 additions and 22 deletions

View File

@ -3,7 +3,12 @@
#include "Geometry/geometryData.h" #include "Geometry/geometryData.h"
#include <QFileInfo> #include <QFileInfo>
#include <vtkSTLReader.h> #include <vtkSTLReader.h>
#include <TopoDS_Shape.hxx>
#include <StlAPI_Reader.hxx>
#include <StlAPI_Writer.hxx>
#include <TopoDS_Compound.hxx>
#include <BRep_Builder.hxx>
#include <QDebug>
namespace Geometry namespace Geometry
{ {
STLdataExchange::STLdataExchange(const QString & fileName, GeometryOperation operation, STLdataExchange::STLdataExchange(const QString & fileName, GeometryOperation operation,
@ -31,7 +36,7 @@ namespace Geometry
break; break;
case GEOMETRY_WRITE: case GEOMETRY_WRITE:
emit showInformation(tr("Export Geometry File From \"%1\"").arg(_fileName)); emit showInformation(tr("Export Geometry File From \"%1\"").arg(_fileName));
// _isWrite = writeBrep(); _isWrite = writeSTL();
break; break;
} }
deriveGeometryTaskFinished(); deriveGeometryTaskFinished();
@ -39,28 +44,55 @@ namespace Geometry
bool STLdataExchange::readSTL() bool STLdataExchange::readSTL()
{ {
std::string str = _fileName.toStdString();
const char* c_fn = str.c_str(); QFileInfo info(_fileName);
auto reader = vtkSTLReader::New(); const QString name = info.fileName();
reader->SetFileName(c_fn); StlAPI_Reader reader;
reader->Update(); TopoDS_Shape stlshape;
auto stlTris = reader->GetOutput(); Standard_Boolean readok=reader.Read(stlshape, _fileName.toLocal8Bit().constData());
const int nt = stlTris->GetNumberOfCells(); qDebug()<<"read stl to memory success or not: "<<readok;
if (nt > 0) if (readok)
{ {
auto geoSet = new GeometrySet(STL); auto geoSet = new GeometrySet(STL);
vtkPolyData* p = vtkPolyData::New(); TopoDS_Shape *sh = new TopoDS_Shape;
p->DeepCopy(stlTris); *sh = stlshape;
geoSet->setPoly(p); geoSet->setShape(sh);
QFileInfo info(_fileName);
const QString name = info.fileName();
geoSet->setName(name); geoSet->setName(name);
GeometryData::getInstance()->appendGeometrySet(geoSet); GeometryData::getInstance()->appendGeometrySet(geoSet);
getResult().append(geoSet); getResult().append(geoSet);
} }else{
reader->Delete();
return nt > 0; }
return readok;
}
bool STLdataExchange::writeSTL()
{
StlAPI_Writer stlWriter;
Geometry::GeometryData *data = Geometry::GeometryData::getInstance();
const int nset = data->getGeometrySetCount();
bool success = false;
TopoDS_Compound compound;
BRep_Builder builder;
builder.MakeCompound(compound);
for (int i = 0; i < nset; ++i)
{
GeometrySet *set = data->getGeometrySetAt(i);
if (set == nullptr)
continue;
if(set->isVisible()){
continue;
}
TopoDS_Shape *shape = set->getShape();
builder.Add(compound, *shape);
success = true;
}
if (!success)
return false;
success=stlWriter.Write(compound, _fileName.toLocal8Bit().constData());
// success = stpWriter.Write(_fileName.toStdString().c_str());
return success;
} }
void STLdataExchange::deriveGeometryTaskFinished() void STLdataExchange::deriveGeometryTaskFinished()
@ -75,7 +107,6 @@ namespace Geometry
msg.type = Common::Message::Normal; msg.type = Common::Message::Normal;
msg.message = QString("Successful Import Geometry From \"%1\"").arg(_fileName); msg.message = QString("Successful Import Geometry From \"%1\"").arg(_fileName);
auto result = getResult(); auto result = getResult();
for (auto set : result) for (auto set : result)
{ {
if (set != result.last()) if (set != result.last())
@ -106,6 +137,7 @@ namespace Geometry
} }
emit showGeometryMessageSig(msg); emit showGeometryMessageSig(msg);
defaultGeometryFinished(); defaultGeometryFinished();
qDebug()<<"stl read or write finished !!!";
} }
} }

View File

@ -47,7 +47,7 @@ namespace Geometry
private: private:
bool readSTL(); bool readSTL();
bool writeSTL();
void deriveGeometryTaskFinished(); void deriveGeometryTaskFinished();
private: private:
@ -71,8 +71,6 @@ namespace Geometry
* @since 2.5.0 * @since 2.5.0
*/ */
bool _isWrite{ false }; bool _isWrite{ false };
}; };