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