IfcSchema::IfcProductDefinitionShape* IfcGeom::tesselate(const TopoDS_Shape& shape, double deflection) {
BRepMesh_IncrementalMesh(shape, deflection);
IfcSchema::IfcFace::list::ptr faces(new IfcSchema::IfcFace::list);
for (TopExp_Explorer exp(shape, TopAbs_FACE); exp.More(); exp.Next()) {
const TopoDS_Face& face = TopoDS::Face(exp.Current());
TopLoc_Location loc;
Handle(Poly_Triangulation) tri = BRep_Tool::Triangulation(face, loc);
if (!tri.IsNull()) {
const TColgp_Array1OfPnt& nodes = tri->Nodes();
std::vector<IfcSchema::IfcCartesianPoint*> vertices;
for (int i = 1; i <= nodes.Length(); ++i) {
gp_Pnt pnt = nodes(i).Transformed(loc);
std::vector<double> xyz; xyz.push_back(pnt.X()); xyz.push_back(pnt.Y()); xyz.push_back(pnt.Z());
IfcSchema::IfcCartesianPoint* cpnt = new IfcSchema::IfcCartesianPoint(xyz);
vertices.push_back(cpnt);
}
const Poly_Array1OfTriangle& triangles = tri->Triangles();
for (int i = 1; i <= triangles.Length(); ++i) {
int n1, n2, n3;
triangles(i).Get(n1, n2, n3);
IfcSchema::IfcCartesianPoint::list::ptr points(new IfcSchema::IfcCartesianPoint::list);
points->push(vertices[n1 - 1]);
points->push(vertices[n2 - 1]);
points->push(vertices[n3 - 1]);
IfcSchema::IfcPolyLoop* loop = new IfcSchema::IfcPolyLoop(points);
IfcSchema::IfcFaceOuterBound* bound = new IfcSchema::IfcFaceOuterBound(loop, face.Orientation() != TopAbs_REVERSED);
IfcSchema::IfcFaceBound::list::ptr bounds(new IfcSchema::IfcFaceBound::list);
bounds->push(bound);
IfcSchema::IfcFace* face2 = new IfcSchema::IfcFace(bounds);
faces->push(face2);
}
}
}
IfcSchema::IfcOpenShell* shell = new IfcSchema::IfcOpenShell(faces);
IfcSchema::IfcConnectedFaceSet::list::ptr shells(new IfcSchema::IfcConnectedFaceSet::list);
shells->push(shell);
IfcSchema::IfcFaceBasedSurfaceModel* surface_model = new IfcSchema::IfcFaceBasedSurfaceModel(shells);
IfcSchema::IfcRepresentation::list::ptr reps(new IfcSchema::IfcRepresentation::list);
IfcSchema::IfcRepresentationItem::list::ptr items(new IfcSchema::IfcRepresentationItem::list);
items->push(surface_model);
IfcSchema::IfcShapeRepresentation* rep = new IfcSchema::IfcShapeRepresentation(
0, std::string("Facetation"), std::string("SurfaceModel"), items);
reps->push(rep);
IfcSchema::IfcProductDefinitionShape* shapedef = new IfcSchema::IfcProductDefinitionShape(boost::none, boost::none, reps);
return shapedef;
}
void Geometry::Shape2BVHMesh(const TopoDS_Shape &shape, BVHMeshPtr model) {
model->beginModel();
BRepMesh_IncrementalMesh(shape, IfcGeom::GetValue(IfcGeom::GV_DEFLECTION_TOLERANCE));
FaceSet faces;
GetFaces(shape,faces);
for (auto face=faces.begin();face!=faces.end();++face){
Face2BVHMesh(*face,model);
}
model->endModel();
}
OCCMesh *OCCFace::createMesh(double factor, double angle, bool qualityNormals = true)
{
OCCMesh *mesh = new OCCMesh();
try {
Bnd_Box aBox;
BRepBndLib::Add(this->getShape(), aBox);
Standard_Real aXmin, aYmin, aZmin;
Standard_Real aXmax, aYmax, aZmax;
aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
Standard_Real maxd = fabs(aXmax - aXmin);
maxd = std::max(maxd, fabs(aYmax - aYmin));
maxd = std::max(maxd, fabs(aZmax - aZmin));
BRepMesh_FastDiscret MSH(factor*maxd, angle, aBox, Standard_False, Standard_False,
Standard_True, Standard_True);
MSH.Perform(this->getShape());
BRepMesh_IncrementalMesh(this->getShape(),factor*maxd);
if (this->getShape().ShapeType() != TopAbs_FACE) {
TopExp_Explorer exFace;
for (exFace.Init(this->getShape(), TopAbs_FACE); exFace.More(); exFace.Next()) {
const TopoDS_Face& faceref = static_cast<const TopoDS_Face &>(exFace.Current());
mesh->extractFaceMesh(faceref, qualityNormals);
}
} else {
mesh->extractFaceMesh(this->getFace(), qualityNormals);
}
} catch(Standard_Failure &err) {
Handle_Standard_Failure e = Standard_Failure::Caught();
const Standard_CString msg = e->GetMessageString();
if (msg != NULL && strlen(msg) > 1) {
setErrorMessage(msg);
} else {
setErrorMessage("Failed to create mesh");
}
return NULL;
}
return mesh;
}
void CEdge::glCommands(bool select, bool marked, bool no_color){
if(!no_color){
wxGetApp().glColorEnsuringContrast(HeeksColor(0, 0, 0));
}
if(m_owner && m_owner->m_owner && m_owner->m_owner->GetType() == SolidType)
{
// triangulate a face on the edge first
if(this->m_faces.size() > 0)
{
TopLoc_Location fL;
Handle_Poly_Triangulation facing = BRep_Tool::Triangulation(m_faces.front()->Face(),fL);
if(!facing.IsNull())
{
// Get polygon
Handle_Poly_PolygonOnTriangulation polygon = BRep_Tool::PolygonOnTriangulation(m_topods_edge, facing, fL);
gp_Trsf tr = fL;
double m[16];
extract_transposed(tr, m);
glPushMatrix();
glMultMatrixd(m);
if (!polygon.IsNull())
{
glBegin(GL_LINE_STRIP);
const TColStd_Array1OfInteger& Nodes = polygon->Nodes();
const TColgp_Array1OfPnt& FNodes = facing->Nodes();
int nnn = polygon->NbNodes();
for (int nn = 1; nn <= nnn; nn++)
{
gp_Pnt v = FNodes(Nodes(nn));
glVertex3d(v.X(), v.Y(), v.Z());
}
glEnd();
}
glPopMatrix();
}
}
}
else
{
bool glwidth_done = false;
GLfloat save_depth_range[2];
if(m_owner == NULL || m_owner->m_owner == NULL || m_owner->m_owner->GetType() != WireType)
{
BRepTools::Clean(m_topods_edge);
double pixels_per_mm = wxGetApp().GetPixelScale();
BRepMesh_IncrementalMesh(m_topods_edge, 1/pixels_per_mm);
if(marked){
glGetFloatv(GL_DEPTH_RANGE, save_depth_range);
glDepthRange(0, 0);
glLineWidth(2);
glwidth_done = true;
}
}
TopLoc_Location L;
Handle(Poly_Polygon3D) Polyg = BRep_Tool::Polygon3D(m_topods_edge, L);
if (!Polyg.IsNull()) {
const TColgp_Array1OfPnt& Points = Polyg->Nodes();
Standard_Integer po;
glBegin(GL_LINE_STRIP);
for (po = Points.Lower(); po <= Points.Upper(); po++) {
gp_Pnt p = (Points.Value(po)).Transformed(L);
glVertex3d(p.X(), p.Y(), p.Z());
}
glEnd();
}
if(glwidth_done)
{
glLineWidth(1);
glDepthRange(save_depth_range[0], save_depth_range[1]);
}
}
}
// Maillage de la shape
VolumeSection::VolumeSection(TopoDS_Shape S , Standard_Real Precision):myShape(S),Tolerance(Precision)
{
// Maillage de la shape myShape
BRepMesh_IncrementalMesh(myShape,Tolerance);
}
int main(int argc, char** argv)
{
const char* stl_filepath = nullptr;
const char* igs_filepath = nullptr;
const char* stp_filepath = nullptr;
double linear_deflection = 0.1;
int iarg = 1;
while (iarg < argc) {
if (std::strcmp(argv[iarg], "--iges") == 0)
igs_filepath = argv[++iarg];
else if (std::strcmp(argv[iarg], "--step") == 0)
stp_filepath = argv[++iarg];
else if (std::strcmp(argv[iarg], "--linear-deflection") == 0)
linear_deflection = std::atof(argv[++iarg]);
else
stl_filepath = argv[iarg];
++iarg;
}
if (stl_filepath != nullptr) {
std::cout << std::endl
<< "gmio v" << GMIO_VERSION_STR << std::endl
<< "OpenCascade v" << OCC_VERSION_COMPLETE << std::endl
<< std::endl
<< "STL input file: " << stl_filepath << std::endl;
if (igs_filepath != nullptr) {
std::cout << "IGES input file: " << igs_filepath << std::endl;
BmkBRep::readInputIgesShape(igs_filepath);
}
else if (stp_filepath != nullptr) {
std::cout << "STEP input file: " << stp_filepath << std::endl;
BmkBRep::readInputStepShape(stp_filepath);
}
std::cout << std::endl << "Meshing with linear deflection="
<< linear_deflection
<< " ..." << std::endl;
for (TopExp_Explorer expl(BmkBRep::inputShape, TopAbs_FACE);
expl.More();
expl.Next())
{
const TopoDS_Face& face = TopoDS::Face(expl.Current());
TopLoc_Location location;
const auto& poly = BRep_Tool::Triangulation(face, location);
if (poly.IsNull() || poly->Triangles().Length() <= 0)
BRepMesh_IncrementalMesh(face, linear_deflection);
}
std::cout << std::endl << "Meshing done" << std::endl;
/* Declare benchmarks */
const benchmark_cmp_arg cmp_args[] = {
{ "read",
BmkGmio::stl_read, stl_filepath,
BmkOcc::RWStl_ReadFile, stl_filepath },
{ "meshwrite(ascii)",
BmkGmio::stla_mesh_write, "__bmk_occ_gmio_mesh.stla",
BmkOcc::RWStl_WriteAscii, "__bmk_occ_mesh.stla" },
{ "meshwrite(binary/le)",
BmkGmio::stlb_mesh_write_le, "__bmk_occ_gmio_mesh.stlb_le",
BmkOcc::RWStl_WriteBinary, "__bmk_occ_mesh.stlb_le" },
{ "meshwrite(binary/be)",
BmkGmio::stlb_mesh_write_be, "__bmk_occ_gmio_mesh.stlb_be",
NULL, NULL },
{ "brepwrite(ascii)",
BmkGmio::stla_brep_write, "__bmk_occ_gmio_brep.stla",
BmkOcc::StlAPI_WriteAscii, "__bmk_occ_brep.stla" },
{ "brepwrite(binary/le)",
BmkGmio::stlb_brep_write_le, "__bmk_occ_gmio_brep.stlb_le",
BmkOcc::StlAPI_WriteBinary, "__bmk_occ_brep.stlb_le" },
{ "brepwrite(binary/be)",
BmkGmio::stlb_brep_write_be, "__bmk_occ_gmio_brep.stlb_be",
NULL, NULL },
{}
};
/* Execute benchmarks */
std::vector<benchmark_cmp_result> cmp_res_vec;
cmp_res_vec.resize(GMIO_ARRAY_SIZE(cmp_args) - 1);
benchmark_cmp_batch(5, cmp_args, &cmp_res_vec[0], NULL, NULL);
/* Print results */
const benchmark_cmp_result_array res_array = {
&cmp_res_vec.at(0), cmp_res_vec.size() };
const benchmark_cmp_result_header header = {
"gmio", "OpenCascade" };
benchmark_print_results(
BENCHMARK_PRINT_FORMAT_MARKDOWN, header, res_array);
}
return 0;
}
void MeshFace(TopoDS_Face face, double pixels_per_mm)
{
BRepTools::Clean(face);
BRepMesh_IncrementalMesh(face, 1 / pixels_per_mm);
}