Salome HOME
Update viewer on delete an item
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Face.cpp
1 // Copyright (C) 2014-2019  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
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_MakeShapeList.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_SketchBuilder.h>
33 #include <GeomAlgoAPI_Copy.h>
34
35 //=================================================================================================
36 BuildPlugin_Face::BuildPlugin_Face()
37 {
38 }
39
40 //=================================================================================================
41 void BuildPlugin_Face::initAttributes()
42 {
43   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
44 }
45
46 //=================================================================================================
47 void BuildPlugin_Face::execute()
48 {
49   // Get base objects list.
50   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
51   if(!aSelectionList.get()) {
52     setError("Error: Could not get selection list.");
53     return;
54   }
55   if(aSelectionList->size() == 0) {
56     setError("Error: Empty selection list.");
57     return;
58   }
59
60   // Collect base shapes.
61   ListOfShape anEdges;
62   ListOfShape anOriginalFaces;
63   ListOfShape aContexts;
64   getOriginalShapesAndContexts(BASE_OBJECTS_ID(), anOriginalFaces, aContexts);
65   anOriginalFaces.clear();
66   std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
67   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
68     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
69     GeomShapePtr aShape = aSelection->value();
70     GeomShapePtr aContext = aSelection->context()->shape();
71     if(!aShape.get()) {
72       aShape = aContext;
73     }
74     // keep selected faces "as is"
75     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
76       anOriginalFaces.push_back(aShape);
77       continue;
78     }
79
80     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
81       GeomShapePtr anEdge = anExp.current();
82       anEdges.push_back(anEdge);
83     }
84
85     // check whether the context is a sketch, in this case store its normal for further needs
86     std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
87         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
88     if (aSketch)
89       aListOfNormals.push_back(aSketch->norm());
90   }
91
92   // Build faces by edges.
93   ListOfShape aFaces;
94   GeomMakeShapePtr aFaceBuilder;
95   if (!anEdges.empty())
96     buildFacesByEdges(anEdges, aListOfNormals, aFaces, aFaceBuilder);
97   int aNbFacesFromEdges = (int)aFaces.size();
98
99   // Add faces selected by user.
100   aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
101
102   // Store result.
103   int anIndex = 0;
104   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
105     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
106     if (anIndex < aNbFacesFromEdges)
107       aMakeShapeList->appendAlgo(aFaceBuilder);
108
109     GeomShapePtr aShape = *anIt;
110     GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
111     aMakeShapeList->appendAlgo(aCopy);
112
113     ListOfShape aBaseShapes;
114     if (anIndex < aNbFacesFromEdges)
115       aBaseShapes = anEdges;
116     else
117       aBaseShapes.push_back(aShape);
118     storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
119   }
120
121   removeResults(anIndex);
122 }
123
124 void BuildPlugin_Face::buildFacesByEdges(
125     const ListOfShape& theEdges,
126     const std::list< std::shared_ptr<GeomAPI_Dir> >& theNormals,
127     ListOfShape& theFaces,
128     std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
129 {
130   // Get plane.
131   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(theEdges);
132   std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
133   bool isReverse = !theNormals.empty();
134   std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = theNormals.begin();
135   for (; aNormIt != theNormals.end() && isReverse; ++aNormIt)
136     if ((*aNormIt)->dot(aNormal) > 1.e-7)
137       isReverse = false;
138   if (isReverse) {
139     aNormal->reverse();
140     aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
141   }
142
143   // Get faces.
144   std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
145       new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
146   theFaces = aSketchBuilder->faces();
147   theBuilderAlgo = aSketchBuilder;
148
149   // Get wires from faces.
150   ListOfShape aWires;
151   for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
152     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
153
154   // Make faces with holes.
155   theFaces.clear();
156   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
157                                              aWires, theFaces);
158 }