1 // Copyright (C) 2020-2022 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>
22 #include <GeomAlgoAPI_Fillet1D.h>
23 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <GeomAlgoAPI_Tools.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_WireExplorer.h>
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>
37 void sendMessageWithFailedShapes(const ListOfShape& theVertices)
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);
45 FeaturesPlugin_Fillet1D::FeaturesPlugin_Fillet1D()
49 void FeaturesPlugin_Fillet1D::initAttributes()
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());
57 void FeaturesPlugin_Fillet1D::execute()
60 MapShapeSubs aVertices;
61 if (!baseShapes(aWires, aVertices))
65 for (ListOfShape::iterator anIt = aWires.begin(); anIt != aWires.end(); ++anIt)
66 if (!performFillet(*anIt, aVertices[*anIt], aResultIndex++))
68 removeResults(aResultIndex);
71 void FeaturesPlugin_Fillet1D::attributeChanged(const std::string& theID)
73 if (theID == CREATION_METHOD()) {
74 // creation method is changed, drop failed vertices and send the message
76 sendMessageWithFailedShapes(ListOfShape());
80 bool FeaturesPlugin_Fillet1D::baseShapes(ListOfShape& theWires, MapShapeSubs& theWireVertices)
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());
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())
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)
107 // skip vertices, which adjacent edges are not on the same plane
109 anEdges.insert(anEdges.end(), anIt->second.begin(), anIt->second.end());
110 GeomPlanePtr aPlane = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
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))
121 aFilletVertices.insert(anIt->first);
124 if (aFilletVertices.empty()) {
125 setError("Wire has no vertices for fillet.");
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);
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();
146 // keep the sequence of wires stable
147 if (aProcessedWires.find(aWire) == aProcessedWires.end()) {
148 theWires.push_back(aWire);
149 aProcessedWires.insert(aWire);
152 theWireVertices[aWire].push_back(aVertex);
158 bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
159 const ListOfShape& theVertices,
160 const int theResultIndex)
162 double aRadius = real(RADIUS_ID())->value();
164 // perform fillet operation
165 std::shared_ptr<GeomAlgoAPI_Fillet1D> aFilletBuilder(
166 new GeomAlgoAPI_Fillet1D(theWire, theVertices, aRadius));
169 bool isSendMessage = !myFailedVertices.empty();
170 myFailedVertices = aFilletBuilder->failedVertices();
173 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
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);
184 // send message to highlight the failed vertices
185 sendMessageWithFailedShapes(myFailedVertices);
188 static const std::string THE_PREFIX = "Fillet1D";
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);