1 // Copyright (C) 2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_Fillet1D.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <GeomAlgoAPI_Fillet1D.h>
24 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
25 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <GeomAlgoAPI_Tools.h>
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_WireExplorer.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_Events.h>
37 FeaturesPlugin_Fillet1D::FeaturesPlugin_Fillet1D()
41 void FeaturesPlugin_Fillet1D::initAttributes()
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());
49 void FeaturesPlugin_Fillet1D::execute()
52 MapShapeSubs aVertices;
53 if (!baseShapes(aWires, aVertices))
57 for (ListOfShape::iterator anIt = aWires.begin(); anIt != aWires.end(); ++anIt)
58 if (!performFillet(*anIt, aVertices[*anIt], aResultIndex++))
60 removeResults(aResultIndex);
63 bool FeaturesPlugin_Fillet1D::baseShapes(ListOfShape& theWires, MapShapeSubs& theWireVertices)
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());
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())
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)
88 // skip vertices, which adjacent edges are not on the same plane
90 anEdges.insert(anEdges.end(), anIt->second.begin(), anIt->second.end());
91 GeomPlanePtr aPlane = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
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))
102 aFilletVertices.insert(anIt->first);
105 if (aFilletVertices.empty()) {
106 setError("Wire has no vertices for fillet.");
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);
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();
128 // keep the sequence of wires stable
129 if (aProcessedWires.find(aWire) == aProcessedWires.end()) {
130 theWires.push_back(aWire);
131 aProcessedWires.insert(aWire);
134 theWireVertices[aWire].push_back(aVertex);
140 bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
141 const ListOfShape& theVertices,
142 const int theResultIndex)
144 double aRadius = real(RADIUS_ID())->value();
146 // perform fillet operation
147 std::shared_ptr<GeomAlgoAPI_Fillet1D> aFilletBuilder(
148 new GeomAlgoAPI_Fillet1D(theWire, theVertices, aRadius));
151 bool isSendMessage = !myFailedVertices.empty();
152 myFailedVertices = aFilletBuilder->failedVertices();
155 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
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);
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);
173 static const std::string THE_PREFIX = "Fillet1D";
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);