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