Salome HOME
Merge branch 'V9_11_BR'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Fillet1D.cpp
1 // Copyright (C) 2020-2023  CEA, EDF
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
22 #include <GeomAlgoAPI_Fillet1D.h>
23 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <GeomAlgoAPI_Tools.h>
26
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_WireExplorer.h>
30
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_AttributeString.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Tools.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->value();
91       if (!aWire.get() && aCurSel->context().get())
92         aWire = aCurSel->context()->shape();
93       if (aProcessedWires.find(aWire) != aProcessedWires.end())
94         continue;
95
96       aProcessedWires.insert(aWire);
97       // get vertices applicable for fillet
98       GeomAlgoAPI_MapShapesAndAncestors aMapVE(aWire, GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE);
99       const MapShapeToShapes& aSubshapes = aMapVE.map();
100       std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aFilletVertices;
101       for (MapShapeToShapes::const_iterator anIt = aSubshapes.begin();
102            anIt != aSubshapes.end(); ++anIt) {
103         // vertex should have 2 adjacent edges
104         if (anIt->second.size() != 2)
105           continue;
106
107         // skip vertices, which adjacent edges are not on the same plane
108         ListOfShape anEdges;
109         anEdges.insert(anEdges.end(), anIt->second.begin(), anIt->second.end());
110         GeomPlanePtr aPlane = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
111         if (!aPlane)
112           continue;
113
114         // skip vertices, which smoothly connect adjacent edges
115         GeomEdgePtr anEdge1(new GeomAPI_Edge(anEdges.front()));
116         GeomEdgePtr anEdge2(new GeomAPI_Edge(anEdges.back()));
117         GeomVertexPtr aSharedVertex(new GeomAPI_Vertex(anIt->first));
118         if (GeomAlgoAPI_ShapeTools::isTangent(anEdge1, anEdge2, aSharedVertex))
119           continue;
120
121         aFilletVertices.insert(anIt->first);
122       }
123
124       if (aFilletVertices.empty()) {
125         setError("Wire has no vertices for fillet.");
126         return false;
127       }
128
129       // keep the sequence of wires and fillet vertices stable
130       theWires.push_back(aWire);
131       for (GeomAPI_WireExplorer anExp(aWire->wire()); anExp.more(); anExp.next()) {
132         GeomShapePtr aVertex = anExp.currentVertex();
133         if (aFilletVertices.find(aVertex) != aFilletVertices.end())
134           theWireVertices[aWire].push_back(aVertex);
135       }
136     }
137   }
138   else if (aMethod == CREATION_BY_VERTICES()) {
139     AttributeSelectionListPtr aSelList = selectionList(VERTEX_LIST_ID());
140     int aNbSel = aSelList->size();
141     for (int ind = 0; ind < aNbSel; ++ind) {
142       AttributeSelectionPtr aCurSel = aSelList->value(ind);
143       GeomShapePtr aWire = aCurSel->context()->shape();
144       GeomShapePtr aVertex = aCurSel->value();
145
146       // keep the sequence of wires stable
147       if (aProcessedWires.find(aWire) == aProcessedWires.end()) {
148         theWires.push_back(aWire);
149         aProcessedWires.insert(aWire);
150       }
151
152       theWireVertices[aWire].push_back(aVertex);
153     }
154   }
155   return true;
156 }
157
158 bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
159                                             const ListOfShape& theVertices,
160                                             const int theResultIndex)
161 {
162   double aRadius = real(RADIUS_ID())->value();
163
164   // perform fillet operation
165   std::shared_ptr<GeomAlgoAPI_Fillet1D> aFilletBuilder(
166       new GeomAlgoAPI_Fillet1D(theWire, theVertices, aRadius));
167
168   bool isOk = true;
169   bool isSendMessage = !myFailedVertices.empty();
170   myFailedVertices = aFilletBuilder->failedVertices();
171
172   std::string anError;
173   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
174     isOk = false;
175     // in case of vertices, the fillet completed, send message to highlight them in the viewer
176     isSendMessage = true;
177     bool isAllFailed = myFailedVertices.size() == theVertices.size();
178     setError(anError, isAllFailed);
179     if (isAllFailed)
180       return isOk;
181   }
182
183   if (isSendMessage) {
184     // send message to highlight the failed vertices
185     sendMessageWithFailedShapes(myFailedVertices);
186   }
187
188   static const std::string THE_PREFIX = "Fillet1D";
189
190   // store result
191   ResultBodyPtr aResult = document()->createBody(data(), theResultIndex);
192   ListOfShape anOriginal;
193   anOriginal.push_back(theWire);
194   ModelAPI_Tools::loadModifiedShapes(aResult, anOriginal, ListOfShape(),
195                                      aFilletBuilder, aFilletBuilder->shape(), THE_PREFIX);
196   setResult(aResult, theResultIndex);
197   // store new edges generated from vertices
198   for (ListOfShape::const_iterator anIt = theVertices.begin(); anIt != theVertices.end(); ++anIt)
199     aResult->loadGeneratedShapes(aFilletBuilder, *anIt, GeomAPI_Shape::VERTEX, THE_PREFIX, true);
200
201   return isOk;
202 }