Salome HOME
273937e2de2f9fedad49e4e673b6d4539ee7ed1d
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Intersection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Intersection.cpp
4 // Created:     16 Feb 2016
5 // Author:      Dmitry Bobylev
6
7 #include "GeomAlgoAPI_Intersection.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11
12 #include <BRepAlgoAPI_Section.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <TopoDS_Builder.hxx>
15
16 //=================================================================================================
17 GeomAlgoAPI_Intersection::GeomAlgoAPI_Intersection(const ListOfShape& theObjects,
18                                                    const ListOfShape& theTools)
19 {
20   build(theObjects, theTools);
21 }
22
23 //=================================================================================================
24 void GeomAlgoAPI_Intersection::build(const ListOfShape& theObjects,
25                                      const ListOfShape& theTools)
26 {
27   if (theObjects.empty() || theTools.empty()) {
28     return;
29   }
30
31   // Creating partition operation.
32   BRepAlgoAPI_Section* anOperation = new BRepAlgoAPI_Section;
33   this->setImpl(anOperation);
34   this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
35
36   TopAbs_ShapeEnum aShapeType = TopAbs_COMPOUND;
37
38   // Getting objects.
39   TopTools_ListOfShape anObjects;
40   for (ListOfShape::const_iterator 
41     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
42     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
43     if(!aShape.IsNull()) {
44       anObjects.Append(aShape);
45     }
46   }
47   anOperation->SetArguments(anObjects);
48
49   // Getting tools.
50   TopTools_ListOfShape aTools;
51   for (ListOfShape::const_iterator 
52     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++) {
53     const TopoDS_Shape& aShape = (*aToolsIt)->impl<TopoDS_Shape>();
54     if(!aShape.IsNull()) {
55       aTools.Append(aShape);
56     }
57   }
58   anOperation->SetTools(aTools);
59
60   // Building and getting result.
61   anOperation->Build();
62   if(!anOperation->IsDone()) {
63     return;
64   }
65   TopoDS_Shape aResult = anOperation->Shape();
66
67   if(aResult.ShapeType() == TopAbs_COMPOUND) {
68     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
69   }
70
71   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
72   aShape->setImpl(new TopoDS_Shape(aResult));
73   this->setShape(aShape);
74   this->setDone(true);
75 }