几何模型导入 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 <QFileInfo>
#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
{
STLdataExchange::STLdataExchange(const QString & fileName, GeometryOperation operation,
@ -31,7 +36,7 @@ namespace Geometry
break;
case GEOMETRY_WRITE:
emit showInformation(tr("Export Geometry File From \"%1\"").arg(_fileName));
// _isWrite = writeBrep();
_isWrite = writeSTL();
break;
}
deriveGeometryTaskFinished();
@ -39,28 +44,55 @@ namespace Geometry
bool STLdataExchange::readSTL()
{
std::string str = _fileName.toStdString();
const char* c_fn = str.c_str();
auto reader = vtkSTLReader::New();
reader->SetFileName(c_fn);
reader->Update();
auto stlTris = reader->GetOutput();
const int nt = stlTris->GetNumberOfCells();
if (nt > 0)
QFileInfo info(_fileName);
const QString name = info.fileName();
StlAPI_Reader reader;
TopoDS_Shape stlshape;
Standard_Boolean readok=reader.Read(stlshape, _fileName.toLocal8Bit().constData());
qDebug()<<"read stl to memory success or not: "<<readok;
if (readok)
{
auto geoSet = new GeometrySet(STL);
vtkPolyData* p = vtkPolyData::New();
p->DeepCopy(stlTris);
geoSet->setPoly(p);
QFileInfo info(_fileName);
const QString name = info.fileName();
TopoDS_Shape *sh = new TopoDS_Shape;
*sh = stlshape;
geoSet->setShape(sh);
geoSet->setName(name);
GeometryData::getInstance()->appendGeometrySet(geoSet);
getResult().append(geoSet);
}
reader->Delete();
}else{
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()
@ -75,7 +107,6 @@ namespace Geometry
msg.type = Common::Message::Normal;
msg.message = QString("Successful Import Geometry From \"%1\"").arg(_fileName);
auto result = getResult();
for (auto set : result)
{
if (set != result.last())
@ -106,6 +137,7 @@ namespace Geometry
}
emit showGeometryMessageSig(msg);
defaultGeometryFinished();
qDebug()<<"stl read or write finished !!!";
}
}

View File

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