562{
563 bool task_result = false;
564 bool cb_cancel = false;
565 float progress = .0;
566 std::atomic<int> meshed_solid_num = 0;
567 std::vector<NamedSolid> namedSolids;
568 float progress_2 = .0;
569 ModelObject* new_object = model->add_object();
571 new_object->name.assign((last_slash ==
nullptr) ?
m_path.c_str() : last_slash + 1);
572 new_object->input_file =
m_path.c_str();
573
575 NCollection_Sequence<TDF_Label> topLevelShapes;
576 m_shape_tool->GetFreeShapes(topLevelShapes);
577 unsigned int id{ 1 };
578 int topShapeLength = topLevelShapes.Length() + 1;
579
580 for (int iLabel = 1; iLabel < topShapeLength; ++iLabel) {
581 progress = static_cast<double>(iLabel) / (topShapeLength-1);
582 if (cb_cancel) {
583 return;
584 }
585 getNamedSolids(TopLoc_Location{},
"", id, m_shape_tool, topLevelShapes.Value(iLabel), namedSolids, isSplitCompound);
586 }
587
588 std::vector<stl_file> stl;
589 stl.resize(namedSolids.size());
590 tbb::parallel_for(tbb::blocked_range<size_t>(0, namedSolids.size()), [&](
const tbb::blocked_range<size_t>&
range) {
591 for (size_t i = range.begin(); i < range.end(); i++) {
592 BRepMesh_IncrementalMesh mesh(namedSolids[i].solid, linear_deflection, false, angle_deflection, true);
593
594 int aNbNodes = 0;
595 int aNbTriangles = 0;
596 for (TopExp_Explorer anExpSF(namedSolids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) {
597 TopLoc_Location aLoc;
598 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(anExpSF.Current()), aLoc);
599 if (!aTriangulation.IsNull()) {
600 aNbNodes += aTriangulation->NbNodes();
601 aNbTriangles += aTriangulation->NbTriangles();
602 }
603 }
604
605 if (aNbTriangles == 0 || aNbNodes == 0)
606
607 continue;
608
609 stl[i].stats.type = inmemory;
610 stl[i].stats.number_of_facets = (uint32_t)aNbTriangles;
611 stl[i].stats.original_num_facets = stl[i].stats.number_of_facets;
612 stl_allocate(&stl[i]);
613
614 std::vector<Vec3f> points;
615 points.reserve(aNbNodes);
616
617 int aNbFacesNoTri = 0;
618
619 int aNodeOffset = 0;
620 int aTriangleOffet = 0;
621 for (TopExp_Explorer anExpSF(namedSolids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) {
622 const TopoDS_Shape& aFace = anExpSF.Current();
623 TopLoc_Location aLoc;
624 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(aFace), aLoc);
625 if (aTriangulation.IsNull()) {
626 ++aNbFacesNoTri;
627 continue;
628 }
629
630 gp_Trsf aTrsf = aLoc.Transformation();
631 for (int aNodeIter = 1; aNodeIter <= aTriangulation->NbNodes(); ++aNodeIter) {
632 gp_Pnt aPnt = aTriangulation->Node(aNodeIter);
633 aPnt.Transform(aTrsf);
634 points.emplace_back(Vec3f(aPnt.X(), aPnt.Y(), aPnt.Z()));
635 }
636
637 const TopAbs_Orientation anOrientation = anExpSF.Current().Orientation();
638 int anId[3] = {};
639 for (int aTriIter = 1; aTriIter <= aTriangulation->NbTriangles(); ++aTriIter) {
640 Poly_Triangle aTri = aTriangulation->Triangle(aTriIter);
641
642 aTri.Get(anId[0], anId[1], anId[2]);
643 if (anOrientation == TopAbs_REVERSED)
644 std::swap(anId[1], anId[2]);
645
646 stl_facet facet;
647 facet.vertex[0] = points[anId[0] + aNodeOffset - 1].cast<float>();
648 facet.vertex[1] = points[anId[1] + aNodeOffset - 1].cast<float>();
649 facet.vertex[2] = points[anId[2] + aNodeOffset - 1].cast<float>();
650 facet.extra[0] = 0;
651 facet.extra[1] = 0;
652 stl_normal normal;
653 stl_calculate_normal(normal, &facet);
654 stl_normalize_vector(normal);
655 facet.normal = normal;
656 stl[i].facet_start[aTriangleOffet + aTriIter - 1] = facet;
657 }
658
659 aNodeOffset += aTriangulation->NbNodes();
660 aTriangleOffet += aTriangulation->NbTriangles();
661 }
662 meshed_solid_num.fetch_add(1, std::memory_order_relaxed);
663 }
664 });
665
666
667 for (size_t i = 0; i < stl.size(); i++) {
668 progress_2 = static_cast<float>(i) / stl.size();
669 if (cb_cancel)
670 return;
671
672
673 if (stl[i].stats.number_of_facets > 0) {
674 TriangleMesh triangle_mesh;
675 triangle_mesh.from_stl(stl[i]);
676 ModelVolume* new_volume = new_object->add_volume(std::move(triangle_mesh));
677 new_volume->name = namedSolids[i].name;
678 new_volume->source.input_file =
m_path.c_str();
679 new_volume->source.object_idx = (int)model->objects.size() - 1;
680 new_volume->source.volume_idx = (int)new_object->volumes.size() - 1;
681 }
682 }
683 task_result = true;
684 }));
685
686 while (!task_result) {
687 boost::this_thread::sleep_for(boost::chrono::milliseconds(300));
688 if (progress_2 > 0) {
689
691 }else {
692 if (meshed_solid_num.load()) {
693
694 int meshed_solid = meshed_solid_num.load();
696 } else {
697 if (progress > 0) {
698
700 }
701 }
702 }
703
704
705 if (cb_cancel) {
706 if (task) {
707 if (task->joinable()) {
708 task->join();
709 delete task;
710 }
711 }
713 }
714 }
715 if (task) {
716 if (task->joinable()) {
717 task->join();
718 delete task;
719 }
720 }
721
722
723 if (new_object->volumes.size() == 0) {
724 model->delete_object(new_object);
726 }
728}
#define DIR_SEPARATOR
Definition DRC.cpp:34
@ MESH_SUCCESS
Definition STEP.hpp:103
@ MESH_ERROR
Definition STEP.hpp:104
const int LOAD_STEP_STAGE_GET_MESH
Definition STEP.hpp:30
const int LOAD_STEP_STAGE_GET_SOLID
Definition STEP.hpp:29