Salome HOME
Updated copyright comment
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Fillet1D.cpp
index d62f6acf4a1dbc17090fedddeb9392d0ae2ddd65..efe33d041b88f18af1c86184a751d7c231145934 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020  CEA/DEN, EDF R&D
+// Copyright (C) 2020-2024  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -18,7 +18,6 @@
 //
 
 #include <FeaturesPlugin_Fillet1D.h>
-#include <FeaturesPlugin_Tools.h>
 
 #include <GeomAlgoAPI_Fillet1D.h>
 #include <GeomAlgoAPI_MapShapesAndAncestors.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Tools.h>
+
+void sendMessageWithFailedShapes(const ListOfShape& theVertices)
+{
+  std::shared_ptr<ModelAPI_ShapesFailedMessage> aMessage(
+      new ModelAPI_ShapesFailedMessage(Events_Loop::eventByName(EVENT_OPERATION_SHAPES_FAILED)));
+  aMessage->setShapes(theVertices);
+  Events_Loop::loop()->send(aMessage);
+}
 
 FeaturesPlugin_Fillet1D::FeaturesPlugin_Fillet1D()
 {
@@ -47,28 +56,40 @@ void FeaturesPlugin_Fillet1D::initAttributes()
 
 void FeaturesPlugin_Fillet1D::execute()
 {
-  MapShapeSubs aWireVertices;
-  if (!baseShapes(aWireVertices))
+  ListOfShape aWires;
+  MapShapeSubs aVertices;
+  if (!baseShapes(aWires, aVertices))
     return;
 
   int aResultIndex = 0;
-  for (MapShapeSubs::iterator anIt = aWireVertices.begin(); anIt != aWireVertices.end(); ++anIt)
-    if (!performFillet(anIt->first, anIt->second, aResultIndex++))
+  for (ListOfShape::iterator anIt = aWires.begin(); anIt != aWires.end(); ++anIt)
+    if (!performFillet(*anIt, aVertices[*anIt], aResultIndex++))
       break;
   removeResults(aResultIndex);
 }
 
-bool FeaturesPlugin_Fillet1D::baseShapes(MapShapeSubs& theWireVertices)
+void FeaturesPlugin_Fillet1D::attributeChanged(const std::string& theID)
+{
+  if (theID == CREATION_METHOD()) {
+    // creation method is changed, drop failed vertices and send the message
+    removeResults(0);
+    sendMessageWithFailedShapes(ListOfShape());
+  }
+}
+
+bool FeaturesPlugin_Fillet1D::baseShapes(ListOfShape& theWires, MapShapeSubs& theWireVertices)
 {
+  std::set<GeomShapePtr, GeomAPI_Shape::Comparator> aProcessedWires;
   std::string aMethod = string(CREATION_METHOD())->value();
   if (aMethod == CREATION_BY_WIRES()) {
     AttributeSelectionListPtr aSelList = selectionList(WIRE_LIST_ID());
 
-    std::set<GeomShapePtr> aProcessedWires;
     int aNbSel = aSelList->size();
     for (int ind = 0; ind < aNbSel; ++ind) {
       AttributeSelectionPtr aCurSel = aSelList->value(ind);
-      GeomShapePtr aWire = aCurSel->context()->shape();
+      GeomShapePtr aWire = aCurSel->value();
+      if (!aWire.get() && aCurSel->context().get())
+        aWire = aCurSel->context()->shape();
       if (aProcessedWires.find(aWire) != aProcessedWires.end())
         continue;
 
@@ -105,8 +126,8 @@ bool FeaturesPlugin_Fillet1D::baseShapes(MapShapeSubs& theWireVertices)
         return false;
       }
 
-
-      // keep the sequence of fillet vertices stable
+      // keep the sequence of wires and fillet vertices stable
+      theWires.push_back(aWire);
       for (GeomAPI_WireExplorer anExp(aWire->wire()); anExp.more(); anExp.next()) {
         GeomShapePtr aVertex = anExp.currentVertex();
         if (aFilletVertices.find(aVertex) != aFilletVertices.end())
@@ -121,6 +142,13 @@ bool FeaturesPlugin_Fillet1D::baseShapes(MapShapeSubs& theWireVertices)
       AttributeSelectionPtr aCurSel = aSelList->value(ind);
       GeomShapePtr aWire = aCurSel->context()->shape();
       GeomShapePtr aVertex = aCurSel->value();
+
+      // keep the sequence of wires stable
+      if (aProcessedWires.find(aWire) == aProcessedWires.end()) {
+        theWires.push_back(aWire);
+        aProcessedWires.insert(aWire);
+      }
+
       theWireVertices[aWire].push_back(aVertex);
     }
   }
@@ -137,10 +165,24 @@ bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
   std::shared_ptr<GeomAlgoAPI_Fillet1D> aFilletBuilder(
       new GeomAlgoAPI_Fillet1D(theWire, theVertices, aRadius));
 
+  bool isOk = true;
+  bool isSendMessage = !myFailedVertices.empty();
+  myFailedVertices = aFilletBuilder->failedVertices();
+
   std::string anError;
   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilletBuilder, getKind(), anError)) {
-    setError(anError);
-    return false;
+    isOk = false;
+    // in case of vertices, the fillet completed, send message to highlight them in the viewer
+    isSendMessage = true;
+    bool isAllFailed = myFailedVertices.size() == theVertices.size();
+    setError(anError, isAllFailed);
+    if (isAllFailed)
+      return isOk;
+  }
+
+  if (isSendMessage) {
+    // send message to highlight the failed vertices
+    sendMessageWithFailedShapes(myFailedVertices);
   }
 
   static const std::string THE_PREFIX = "Fillet1D";
@@ -149,12 +191,12 @@ bool FeaturesPlugin_Fillet1D::performFillet(const GeomShapePtr& theWire,
   ResultBodyPtr aResult = document()->createBody(data(), theResultIndex);
   ListOfShape anOriginal;
   anOriginal.push_back(theWire);
-  FeaturesPlugin_Tools::loadModifiedShapes(aResult, anOriginal, ListOfShape(),
-                                           aFilletBuilder, aFilletBuilder->shape(), THE_PREFIX);
+  ModelAPI_Tools::loadModifiedShapes(aResult, anOriginal, ListOfShape(),
+                                     aFilletBuilder, aFilletBuilder->shape(), THE_PREFIX);
   setResult(aResult, theResultIndex);
   // store new edges generated from vertices
   for (ListOfShape::const_iterator anIt = theVertices.begin(); anIt != theVertices.end(); ++anIt)
     aResult->loadGeneratedShapes(aFilletBuilder, *anIt, GeomAPI_Shape::VERTEX, THE_PREFIX, true);
 
-  return true;
+  return isOk;
 }