void GdmlWriter::addSolid(TopoDS_Shape shape, QString name, QString material) { Handle_StlMesh_Mesh aMesh = new StlMesh_Mesh(); int index = names.size(); StlTransfer::BuildIncrementalMesh(shape, 1.0, #if OCC_VERSION_HEX >= 0x060503 Standard_True, #endif aMesh); typedef struct { Standard_Integer V1; Standard_Integer V2; Standard_Integer V3; } Triangle; QList<gp_XYZ> verts; QMap<gp_XYZ, int> dedup; QList<int> steps; QList<int> triconv; int idx = 0; for (int i = 1; i <= aMesh->NbDomains(); i++) { const TColgp_SequenceOfXYZ& seq = aMesh->Vertices(i); for (int j = 1; j <= seq.Length(); j++) { const gp_XYZ& pt = seq.Value(j); int val = dedup.value(pt, -1); if (val == -1) { verts.append(pt); triconv.append(idx); dedup[pt] = idx; idx += 1; } else { triconv.append(val); } } steps.append(seq.Length()); } dedup.clear(); _(" <define>\n"); int numverts = verts.size(); for (int i = 0; i < numverts; i++) { _(" <position name=\"%d-%d\" x=\"%f\" y=\"%f\" z=\"%f\" unit=\"mm\"/>\n", index, i, verts[i].X(), verts[i].Y(), verts[i].Z()); } _(" </define>\n"); int num_verts = verts.size(); verts.clear(); _(" <solids>\n"); _(" <tessellated name=\"T-%s\">\n", name.toUtf8().data()); idx = 0; int num_tris = 0; for (int i = 1; i <= aMesh->NbDomains(); i++) { const StlMesh_SequenceOfMeshTriangle& x = aMesh->Triangles(i); for (int j = 1; j <= x.Length(); j++) { int V1, V2, V3; x.Value(j)->GetVertex(V1, V2, V3); int R1, R2, R3; R1 = triconv[V1 + idx - 1]; R2 = triconv[V2 + idx - 1]; R3 = triconv[V3 + idx - 1]; _(" <triangular vertex1=\"%d-%d\" vertex2=\"%d-%d\" vertex3=\"%d-%d\" type=\"ABSOLUTE\"/>\n", index, R1, index, R2, index, R3); } num_tris += x.Length(); idx += steps[i - 1]; } _(" </tessellated>\n"); _(" </solids>\n"); names.append(name); materials.append(material); BRepBndLib::Add(shape, bounds); printf("% 6d vertices, % 6d triangles <- %s\n", num_verts, num_tris, name.toUtf8().data()); }
Mesh::MeshObject* Mesher::createMesh() const { // OCC standard mesher if (method == Standard) { Handle_StlMesh_Mesh aMesh = new StlMesh_Mesh(); if (!shape.IsNull()) { BRepTools::Clean(shape); #if OCC_VERSION_HEX >= 0x060801 BRepMesh_IncrementalMesh bMesh(shape, deflection, Standard_False, angularDeflection); StlTransfer::RetrieveMesh(shape,aMesh); #else StlTransfer::BuildIncrementalMesh(shape, deflection, #if OCC_VERSION_HEX >= 0x060503 Standard_True, #endif aMesh); #endif } std::map<uint32_t, std::vector<std::size_t> > colorMap; for (std::size_t i=0; i<colors.size(); i++) { colorMap[colors[i]].push_back(i); } bool createSegm = (static_cast<int>(colors.size()) == aMesh->NbDomains()); MeshCore::MeshFacetArray faces; faces.reserve(aMesh->NbTriangles()); std::set<Vertex> vertices; Standard_Real x1, y1, z1; Standard_Real x2, y2, z2; Standard_Real x3, y3, z3; std::vector< std::vector<unsigned long> > meshSegments; std::size_t numMeshFaces = 0; StlMesh_MeshExplorer xp(aMesh); for (Standard_Integer nbd=1;nbd<=aMesh->NbDomains();nbd++) { std::size_t numDomainFaces = 0; for (xp.InitTriangle(nbd); xp.MoreTriangle(); xp.NextTriangle()) { xp.TriangleVertices(x1,y1,z1,x2,y2,z2,x3,y3,z3); std::set<Vertex>::iterator it; MeshCore::MeshFacet face; // 1st vertex Vertex v1(x1,y1,z1); it = vertices.find(v1); if (it == vertices.end()) { v1.i = vertices.size(); face._aulPoints[0] = v1.i; vertices.insert(v1); } else { face._aulPoints[0] = it->i; } // 2nd vertex Vertex v2(x2,y2,z2); it = vertices.find(v2); if (it == vertices.end()) { v2.i = vertices.size(); face._aulPoints[1] = v2.i; vertices.insert(v2); } else { face._aulPoints[1] = it->i; } // 3rd vertex Vertex v3(x3,y3,z3); it = vertices.find(v3); if (it == vertices.end()) { v3.i = vertices.size(); face._aulPoints[2] = v3.i; vertices.insert(v3); } else { face._aulPoints[2] = it->i; } // make sure that we don't insert invalid facets if (face._aulPoints[0] != face._aulPoints[1] && face._aulPoints[1] != face._aulPoints[2] && face._aulPoints[2] != face._aulPoints[0]) { faces.push_back(face); numDomainFaces++; } } // add a segment for the face if (createSegm || this->segments) { std::vector<unsigned long> segment(numDomainFaces); std::generate(segment.begin(), segment.end(), Base::iotaGen<unsigned long>(numMeshFaces)); numMeshFaces += numDomainFaces; meshSegments.push_back(segment); } } MeshCore::MeshPointArray verts; verts.resize(vertices.size()); for (auto it : vertices) verts[it.i] = it.toPoint(); MeshCore::MeshKernel kernel; kernel.Adopt(verts, faces, true); Mesh::MeshObject* meshdata = new Mesh::MeshObject(); meshdata->swap(kernel); if (createSegm) { int index = 0; for (auto it : colorMap) { Mesh::Segment segm(meshdata, false); for (auto jt : it.second) { segm.addIndices(meshSegments[jt]); } segm.save(true); std::stringstream str; str << "patch" << index++; segm.setName(str.str()); meshdata->addSegment(segm); } } else { for (auto it : meshSegments) { meshdata->addSegment(it); } } return meshdata; } #ifndef HAVE_SMESH throw Base::Exception("SMESH is not available on this platform"); #else std::list<SMESH_Hypothesis*> hypoth; SMESH_Gen* meshgen = SMESH_Gen::get(); SMESH_Mesh* mesh = meshgen->CreateMesh(0, true); int hyp=0; switch (method) { #if defined (HAVE_NETGEN) case Netgen: { NETGENPlugin_Hypothesis_2D* hyp2d = new NETGENPlugin_Hypothesis_2D(hyp++,0,meshgen); if (fineness >=0 && fineness < 5) { hyp2d->SetFineness(NETGENPlugin_Hypothesis_2D::Fineness(fineness)); } // user defined values else { if (growthRate > 0) hyp2d->SetGrowthRate(growthRate); if (nbSegPerEdge > 0) hyp2d->SetNbSegPerEdge(nbSegPerEdge); if (nbSegPerRadius > 0) hyp2d->SetNbSegPerRadius(nbSegPerRadius); } hyp2d->SetQuadAllowed(allowquad); hyp2d->SetOptimize(optimize); hyp2d->SetSecondOrder(secondOrder); // apply bisecting to create four triangles out of one hypoth.push_back(hyp2d); NETGENPlugin_NETGEN_2D* alg2d = new NETGENPlugin_NETGEN_2D(hyp++,0,meshgen); hypoth.push_back(alg2d); } break; #endif #if defined (HAVE_MEFISTO) case Mefisto: { if (maxLength > 0) { StdMeshers_MaxLength* hyp1d = new StdMeshers_MaxLength(hyp++, 0, meshgen); hyp1d->SetLength(maxLength); hypoth.push_back(hyp1d); } else if (localLength > 0) { StdMeshers_LocalLength* hyp1d = new StdMeshers_LocalLength(hyp++,0,meshgen); hyp1d->SetLength(localLength); hypoth.push_back(hyp1d); } else if (maxArea > 0) { StdMeshers_MaxElementArea* hyp2d = new StdMeshers_MaxElementArea(hyp++,0,meshgen); hyp2d->SetMaxArea(maxArea); hypoth.push_back(hyp2d); } else if (deflection > 0) { StdMeshers_Deflection1D* hyp1d = new StdMeshers_Deflection1D(hyp++,0,meshgen); hyp1d->SetDeflection(deflection); hypoth.push_back(hyp1d); } else if (minLen > 0 && maxLen > 0) { StdMeshers_Arithmetic1D* hyp1d = new StdMeshers_Arithmetic1D(hyp++,0,meshgen); hyp1d->SetLength(minLen, false); hyp1d->SetLength(maxLen, true); hypoth.push_back(hyp1d); } else { StdMeshers_AutomaticLength* hyp1d = new StdMeshers_AutomaticLength(hyp++,0,meshgen); hypoth.push_back(hyp1d); } { StdMeshers_NumberOfSegments* hyp1d = new StdMeshers_NumberOfSegments(hyp++,0,meshgen); hyp1d->SetNumberOfSegments(1); hypoth.push_back(hyp1d); } if (regular) { StdMeshers_Regular_1D* hyp1d = new StdMeshers_Regular_1D(hyp++,0,meshgen); hypoth.push_back(hyp1d); } StdMeshers_TrianglePreference* hyp2d_1 = new StdMeshers_TrianglePreference(hyp++,0,meshgen); hypoth.push_back(hyp2d_1); StdMeshers_MEFISTO_2D* alg2d = new StdMeshers_MEFISTO_2D(hyp++,0,meshgen); hypoth.push_back(alg2d); } break; #endif default: break; } // Set new cout MeshingOutput stdcout; std::streambuf* oldcout = std::cout.rdbuf(&stdcout); // Apply the hypothesis and create the mesh mesh->ShapeToMesh(shape); for (int i=0; i<hyp;i++) mesh->AddHypothesis(shape, i); meshgen->Compute(*mesh, mesh->GetShapeToMesh()); // Restore old cout std::cout.rdbuf(oldcout); // build up the mesh structure SMDS_FaceIteratorPtr aFaceIter = mesh->GetMeshDS()->facesIterator(); SMDS_NodeIteratorPtr aNodeIter = mesh->GetMeshDS()->nodesIterator(); MeshCore::MeshPointArray verts; MeshCore::MeshFacetArray faces; verts.reserve(mesh->NbNodes()); faces.reserve(mesh->NbFaces()); int index=0; std::map<const SMDS_MeshNode*, int> mapNodeIndex; for (;aNodeIter->more();) { const SMDS_MeshNode* aNode = aNodeIter->next(); MeshCore::MeshPoint p; p.Set((float)aNode->X(), (float)aNode->Y(), (float)aNode->Z()); verts.push_back(p); mapNodeIndex[aNode] = index++; } for (;aFaceIter->more();) { const SMDS_MeshFace* aFace = aFaceIter->next(); if (aFace->NbNodes() == 3) { MeshCore::MeshFacet f; for (int i=0; i<3;i++) { const SMDS_MeshNode* node = aFace->GetNode(i); f._aulPoints[i] = mapNodeIndex[node]; } faces.push_back(f); } else if (aFace->NbNodes() == 4) { MeshCore::MeshFacet f1, f2; const SMDS_MeshNode* node0 = aFace->GetNode(0); const SMDS_MeshNode* node1 = aFace->GetNode(1); const SMDS_MeshNode* node2 = aFace->GetNode(2); const SMDS_MeshNode* node3 = aFace->GetNode(3); f1._aulPoints[0] = mapNodeIndex[node0]; f1._aulPoints[1] = mapNodeIndex[node1]; f1._aulPoints[2] = mapNodeIndex[node2]; f2._aulPoints[0] = mapNodeIndex[node0]; f2._aulPoints[1] = mapNodeIndex[node2]; f2._aulPoints[2] = mapNodeIndex[node3]; faces.push_back(f1); faces.push_back(f2); } else if (aFace->NbNodes() == 6) { MeshCore::MeshFacet f1, f2, f3, f4; const SMDS_MeshNode* node0 = aFace->GetNode(0); const SMDS_MeshNode* node1 = aFace->GetNode(1); const SMDS_MeshNode* node2 = aFace->GetNode(2); const SMDS_MeshNode* node3 = aFace->GetNode(3); const SMDS_MeshNode* node4 = aFace->GetNode(4); const SMDS_MeshNode* node5 = aFace->GetNode(5); f1._aulPoints[0] = mapNodeIndex[node0]; f1._aulPoints[1] = mapNodeIndex[node3]; f1._aulPoints[2] = mapNodeIndex[node5]; f2._aulPoints[0] = mapNodeIndex[node1]; f2._aulPoints[1] = mapNodeIndex[node4]; f2._aulPoints[2] = mapNodeIndex[node3]; f3._aulPoints[0] = mapNodeIndex[node2]; f3._aulPoints[1] = mapNodeIndex[node5]; f3._aulPoints[2] = mapNodeIndex[node4]; f4._aulPoints[0] = mapNodeIndex[node3]; f4._aulPoints[1] = mapNodeIndex[node4]; f4._aulPoints[2] = mapNodeIndex[node5]; faces.push_back(f1); faces.push_back(f2); faces.push_back(f3); faces.push_back(f4); } else if (aFace->NbNodes() == 8) { MeshCore::MeshFacet f1, f2, f3, f4, f5, f6; const SMDS_MeshNode* node0 = aFace->GetNode(0); const SMDS_MeshNode* node1 = aFace->GetNode(1); const SMDS_MeshNode* node2 = aFace->GetNode(2); const SMDS_MeshNode* node3 = aFace->GetNode(3); const SMDS_MeshNode* node4 = aFace->GetNode(4); const SMDS_MeshNode* node5 = aFace->GetNode(5); const SMDS_MeshNode* node6 = aFace->GetNode(6); const SMDS_MeshNode* node7 = aFace->GetNode(7); f1._aulPoints[0] = mapNodeIndex[node0]; f1._aulPoints[1] = mapNodeIndex[node4]; f1._aulPoints[2] = mapNodeIndex[node7]; f2._aulPoints[0] = mapNodeIndex[node1]; f2._aulPoints[1] = mapNodeIndex[node5]; f2._aulPoints[2] = mapNodeIndex[node4]; f3._aulPoints[0] = mapNodeIndex[node2]; f3._aulPoints[1] = mapNodeIndex[node6]; f3._aulPoints[2] = mapNodeIndex[node5]; f4._aulPoints[0] = mapNodeIndex[node3]; f4._aulPoints[1] = mapNodeIndex[node7]; f4._aulPoints[2] = mapNodeIndex[node6]; // Two solutions are possible: // <4,6,7>, <4,5,6> or <4,5,7>, <5,6,7> Base::Vector3d v4(node4->X(),node4->Y(),node4->Z()); Base::Vector3d v5(node5->X(),node5->Y(),node5->Z()); Base::Vector3d v6(node6->X(),node6->Y(),node6->Z()); Base::Vector3d v7(node7->X(),node7->Y(),node7->Z()); double dist46 = Base::DistanceP2(v4,v6); double dist57 = Base::DistanceP2(v5,v7); if (dist46 > dist57) { f5._aulPoints[0] = mapNodeIndex[node4]; f5._aulPoints[1] = mapNodeIndex[node6]; f5._aulPoints[2] = mapNodeIndex[node7]; f6._aulPoints[0] = mapNodeIndex[node4]; f6._aulPoints[1] = mapNodeIndex[node5]; f6._aulPoints[2] = mapNodeIndex[node6]; } else { f5._aulPoints[0] = mapNodeIndex[node4]; f5._aulPoints[1] = mapNodeIndex[node5]; f5._aulPoints[2] = mapNodeIndex[node7]; f6._aulPoints[0] = mapNodeIndex[node5]; f6._aulPoints[1] = mapNodeIndex[node6]; f6._aulPoints[2] = mapNodeIndex[node7]; } faces.push_back(f1); faces.push_back(f2); faces.push_back(f3); faces.push_back(f4); faces.push_back(f5); faces.push_back(f6); } else { Base::Console().Warning("Face with %d nodes ignored\n", aFace->NbNodes()); } } // clean up TopoDS_Shape aNull; mesh->ShapeToMesh(aNull); mesh->Clear(); delete mesh; for (std::list<SMESH_Hypothesis*>::iterator it = hypoth.begin(); it != hypoth.end(); ++it) delete *it; MeshCore::MeshKernel kernel; kernel.Adopt(verts, faces, true); Mesh::MeshObject* meshdata = new Mesh::MeshObject(); meshdata->swap(kernel); return meshdata; #endif // HAVE_SMESH }