Salome HOME
Naming now stores primitives on tags 11+.
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "BuildPlugin_Face.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_PlanarEdges.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_ShapeExplorer.h>
30
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   std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
64   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
65     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
66     GeomShapePtr aShape = aSelection->value();
67     GeomShapePtr aContext = aSelection->context()->shape();
68     if(!aShape.get()) {
69       aShape = aContext;
70     }
71     // keep selected faces "as is"
72     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
73       anOriginalFaces.push_back(aShape);
74       continue;
75     }
76
77     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
78       GeomShapePtr anEdge = anExp.current();
79       anEdges.push_back(anEdge);
80     }
81
82     // check whether the context is a sketch, in this case store its normal for further needs
83     std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
84         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
85     if (aSketch)
86       aListOfNormals.push_back(aSketch->norm());
87   }
88
89   // Build faces by edges.
90   ListOfShape aFaces;
91   if (!anEdges.empty())
92     buildFacesByEdges(anEdges, aListOfNormals, aFaces);
93
94   // Add faces selected by user.
95   aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
96
97   // Store result.
98   int anIndex = 0;
99   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
100     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
101     GeomShapePtr aShape = *anIt;
102     GeomAlgoAPI_Copy aCopy(aShape);
103     aShape = aCopy.shape();
104     aResultBody->store(aShape);
105
106     // Store edges.
107     int anEdgeIndex = 1;
108     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
109       GeomShapePtr anEdge = anExp.current();
110       aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeIndex));
111       ++anEdgeIndex;
112     }
113
114     setResult(aResultBody, anIndex);
115     ++anIndex;
116   }
117
118   removeResults(anIndex);
119 }
120
121 void BuildPlugin_Face::buildFacesByEdges(
122     const ListOfShape& theEdges,
123     const std::list< std::shared_ptr<GeomAPI_Dir> >& theNormals,
124     ListOfShape& theFaces) const
125 {
126   // Get plane.
127   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(theEdges);
128   std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
129   bool isReverse = !theNormals.empty();
130   std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = theNormals.begin();
131   for (; aNormIt != theNormals.end() && isReverse; ++aNormIt)
132     if ((*aNormIt)->dot(aNormal) > 1.e-7)
133       isReverse = false;
134   if (isReverse) {
135     aNormal->reverse();
136     aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
137   }
138
139   // Get faces.
140   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
141                                          aPln->direction(), theEdges, theFaces);
142
143   // Get wires from faces.
144   ListOfShape aWires;
145   for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
146     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
147
148   // Make faces with holes.
149   theFaces.clear();
150   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
151                                              aWires, theFaces);
152 }