Salome HOME
Issue #3222: 1d fillet
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Fillet1D.cpp
1 // Copyright (C) 2020  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 <FeaturesPlugin_Fillet1D.h>
21 #include <FeaturesPlugin_Tools.h>
22
23 #include <GeomAlgoAPI_Fillet1D.h>
24 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
25 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <GeomAlgoAPI_Tools.h>
27
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_WireExplorer.h>
31
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_Events.h>
36
37 FeaturesPlugin_Fillet1D::FeaturesPlugin_Fillet1D()
38 {
39 }
40
41 void FeaturesPlugin_Fillet1D::initAttributes()
42 {
43   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
44   data()->addAttribute(WIRE_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
45   data()->addAttribute(VERTEX_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
46   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
47 }
48
49 void FeaturesPlugin_Fillet1D::execute()
50 {
51   ListOfShape aWires;
52   MapShapeSubs aVertices;
53   if (!baseShapes(aWires, aVertices))
54     return;
55
56   int aResultIndex = 0;
57   for (ListOfShape::iterator anIt = aWires.begin(); anIt != aWires.end(); ++anIt)
58     if (!performFillet(*anIt, aVertices[*anIt], aResultIndex++))
59       break;
60   removeResults(aResultIndex);
61 }
62
63 bool FeaturesPlugin_Fillet1D::baseShapes(ListOfShape& theWires, MapShapeSubs& theWireVertices)
64 {
65   std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aProcessedWires;
66   std::string aMethod = string(CREATION_METHOD())->value();
67   if (aMethod == CREATION_BY_WIRES()) {
68     AttributeSelectionListPtr aSelList = selectionList(WIRE_LIST_ID());
69
70     int aNbSel = aSelList->size();
71     for (int ind = 0; ind < aNbSel; ++ind) {
72       AttributeSelectionPtr aCurSel = aSelList->value(ind);
73       GeomShapePtr aWire = aCurSel->context()->shape();
74       if (aProcessedWires.find(aWire) != aProcessedWires.end())
75         continue;
76
77       aProcessedWires.insert(aWire);
78       // get vertices applicable for fillet
79       GeomAlgoAPI_MapShapesAndAncestors aMapVE(aWire, GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE);
80       const MapShapeToShapes& aSubshapes = aMapVE.map();
81       std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aFilletVertices;
82       for (MapShapeToShapes::const_iterator anIt = aSubshapes.begin();
83            anIt != aSubshapes.end(); ++anIt) {
84         // vertex should have 2 adjacent edges
85         if (anIt->second.size() != 2)
86           continue;
87
88         // skip vertices, which adjacent edges are not on the same plane
89         ListOfShape anEdges;
90         anEdges.insert(anEdges.end(), anIt->second.begin(), anIt->second.end());
91         GeomPlanePtr aPlane = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
92         if (!aPlane)
93           continue;
94
95         // skip vertices, which smoothly connect adjacent edges
96         GeomEdgePtr anEdge1(new GeomAPI_Edge(anEdges.front()));
97         GeomEdgePtr anEdge2(new GeomAPI_Edge(anEdges.back()));
98         GeomVertexPtr aSharedVertex(new GeomAPI_Vertex(anIt->first));
99         if (GeomAlgoAPI_ShapeTools::isTangent(anEdge1, anEdge2, aSharedVertex))
100           continue;
101
102         aFilletVertices.insert(anIt->first);
103       }
104
105       if (aFilletVertices.empty()) {
106         setError("Wire has no vertices for fillet.");
107         return false;
108       }
109
110
111       // keep the sequence of wires and fillet vertices stable
112       theWires.push_back(aWire);
113       for (GeomAPI_WireExplorer anExp(aWire->wire()); anExp.more(); anExp.next()) {
114         GeomShapePtr aVertex = anExp.currentVertex();
115         if (aFilletVertices.find(aVertex) != aFilletVertices.end())
116           theWireVertices[aWire].push_back(aVertex);
117       }
118     }
119   }
120   else if (aMethod == CREATION_BY_VERTICES()) {
121     AttributeSelectionListPtr aSelList = selectionList(VERTEX_LIST_ID());
122     int aNbSel = aSelList->size();
123     for (int ind = 0; ind < aNbSel; ++ind) {
124       AttributeSelectionPtr aCurSel = aSelList->value(ind);
125       GeomShapePtr aWire = aCurSel->context()->shape();
126       GeomShapePtr aVertex = aCurSel->value();
127
128       // keep the sequence of wires stable
129       if (aProcessedWires.find(aWire) == aProcessedWires.end()) {
130         theWires.push_back(aWire);
131         aProcessedWires.insert(aWire);
132       }
133
134       theWireVertices[aWire].push_back(aVertex);
135     }
136   }
137   return true;
138 }
139
140 bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
141                                             const ListOfShape& theVertices,
142                                             const int theResultIndex)
143 {
144   double aRadius = real(RADIUS_ID())->value();
145
146   // perform fillet operation
147   std::shared_ptr<GeomAlgoAPI_Fillet1D> aFilletBuilder(
148       new GeomAlgoAPI_Fillet1D(theWire, theVertices, aRadius));
149
150   bool isOk = true;
151   bool isSendMessage = !myFailedVertices.empty();
152   myFailedVertices = aFilletBuilder->failedVertices();
153
154   std::string anError;
155   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
156     isOk = false;
157     // in case of vertices, the fillet completed, send message to highlight them in the viewer
158     isSendMessage = true;
159     bool isAllFailed = myFailedVertices.size() == theVertices.size();
160     setError(anError, isAllFailed);
161     if (isAllFailed)
162       return isOk;
163   }
164
165   if (isSendMessage) {
166     // send message to highlight the failed vertices
167     std::shared_ptr<ModelAPI_Fillet1DFailedMessage> aMessage(
168         new ModelAPI_Fillet1DFailedMessage(Events_Loop::eventByName(EVENT_1DFILLET_FAILED)));
169     aMessage->setVertices(myFailedVertices);
170     Events_Loop::loop()->send(aMessage);
171   }
172
173   static const std::string THE_PREFIX = "Fillet1D";
174
175   // store result
176   ResultBodyPtr aResult = document()->createBody(data(), theResultIndex);
177   ListOfShape anOriginal;
178   anOriginal.push_back(theWire);
179   FeaturesPlugin_Tools::loadModifiedShapes(aResult, anOriginal, ListOfShape(),
180                                            aFilletBuilder, aFilletBuilder->shape(), THE_PREFIX);
181   setResult(aResult, theResultIndex);
182   // store new edges generated from vertices
183   for (ListOfShape::const_iterator anIt = theVertices.begin(); anIt != theVertices.end(); ++anIt)
184     aResult->loadGeneratedShapes(aFilletBuilder, *anIt, GeomAPI_Shape::VERTEX, THE_PREFIX, true);
185
186   return isOk;
187 }