Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Face.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "BuildPlugin_Face.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Pln.h>
28 #include <GeomAPI_ShapeExplorer.h>
29
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_SketchBuilder.h>
32 #include <GeomAlgoAPI_Copy.h>
33
34 //=================================================================================================
35 BuildPlugin_Face::BuildPlugin_Face()
36 {
37 }
38
39 //=================================================================================================
40 void BuildPlugin_Face::initAttributes()
41 {
42   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
43 }
44
45 //=================================================================================================
46 void BuildPlugin_Face::execute()
47 {
48   // Get base objects list.
49   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
50   if(!aSelectionList.get()) {
51     setError("Error: Could not get selection list.");
52     return;
53   }
54   if(aSelectionList->size() == 0) {
55     setError("Error: Empty selection list.");
56     return;
57   }
58
59   // Collect base shapes.
60   ListOfShape anEdges;
61   std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
62   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
63     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
64     GeomShapePtr aShape = aSelection->value();
65     GeomShapePtr aContext = aSelection->context()->shape();
66     if(!aShape.get()) {
67       aShape = aContext;
68     }
69     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
70       GeomShapePtr anEdge = anExp.current();
71       anEdges.push_back(anEdge);
72     }
73
74     // check whether the context is a sketch, in this case store its normal for further needs
75     std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
76         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
77     if (aSketch)
78       aListOfNormals.push_back(aSketch->norm());
79   }
80
81   // Get plane.
82   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
83   std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
84   bool isReverse = !aListOfNormals.empty();
85   std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = aListOfNormals.begin();
86   for (; aNormIt != aListOfNormals.end() && isReverse; ++aNormIt)
87     if ((*aNormIt)->dot(aNormal) > 1.e-7)
88       isReverse = false;
89   if (isReverse) {
90     aNormal->reverse();
91     aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
92   }
93
94   // Get faces.
95   ListOfShape aFaces;
96   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
97                                          aPln->direction(), anEdges, aFaces);
98
99   // Get wires from faces.
100   ListOfShape aWires;
101   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
102     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
103     //for(GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
104     //  if(anExp.current()->orientation() == GeomAPI_Shape::REVERSED) {
105     //    continue;
106     //  }
107     //  aWires.push_back(anExp.current());
108     //}
109   }
110
111   // Make faces with holes.
112   aFaces.clear();
113   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(), aWires, aFaces);
114
115   // Store result.
116   int anIndex = 0;
117   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
118     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
119     GeomShapePtr aShape = *anIt;
120     GeomAlgoAPI_Copy aCopy(aShape);
121     aShape = aCopy.shape();
122     aResultBody->store(aShape);
123
124     // Store edges.
125     int anEdgeIndex = 1;
126     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
127       GeomShapePtr anEdge = anExp.current();
128       aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeIndex), anEdgeIndex);
129       ++anEdgeIndex;
130     }
131
132     setResult(aResultBody, anIndex);
133     ++anIndex;
134   }
135
136   removeResults(anIndex);
137 }