Salome HOME
Fix errors and adjust unit tests
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
index 6240c933b2b54e2682e1ed1de001b46d54fe25bb..85dade4280451efbcb8cefba8724157a992674d5 100644 (file)
@@ -18,6 +18,7 @@
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAPI_Circ.h>
 #include <GeomAPI_Edge.h>
@@ -45,6 +46,8 @@ void SketchPlugin_Projection::initDerivedClassAttributes()
 
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
+
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
 }
 
 void SketchPlugin_Projection::execute()
@@ -54,10 +57,11 @@ void SketchPlugin_Projection::execute()
     return;
   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
 
-  if (!lastResult().get()) {
+  if (!lastResult().get() && aProjection->lastResult().get()) {
     ResultConstructionPtr aConstr = document()->createConstruction(data());
     aConstr->setShape(aProjection->lastResult()->shape());
     aConstr->setIsInHistory(false);
+    aConstr->setDisplayed(false);
     setResult(aConstr);
 
     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
@@ -76,16 +80,6 @@ void SketchPlugin_Projection::attributeChanged(const std::string& theID)
     computeProjection(theID);
     myIsComputing = false;
   }
-  else if (theID == AUXILIARY_ID())
-  {
-    AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
-    if (!aRefAttr || !aRefAttr->isInitialized())
-      return;
-    FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
-    if (!aProjection)
-      return;
-    aProjection->boolean(AUXILIARY_ID())->setValue(boolean(AUXILIARY_ID())->value());
-  }
 }
 
 void SketchPlugin_Projection::computeProjection(const std::string& theID)
@@ -114,7 +108,10 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
         (anEdge->isCircle() && aProjection->getKind() != SketchPlugin_Circle::ID()) ||
         (anEdge->isArc() && aProjection->getKind() != SketchPlugin_Arc::ID())) {
       DocumentPtr aDoc = sketch()->document();
-      aDoc->removeFeature(aProjection);
+
+      std::set<FeaturePtr> aFeaturesToBeRemoved;
+      aFeaturesToBeRemoved.insert(aProjection);
+      ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
       aProjection = FeaturePtr();
       aRefAttr->setObject(aProjection);
     }
@@ -122,6 +119,13 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
 
   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
 
+  ResultConstructionPtr aResult =
+      std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(lastResult());
+  if (aResult && aResult->shape()) {
+    aResult->setShape(std::shared_ptr<GeomAPI_Edge>());
+    aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
+  }
+
   if (anEdge->isLine()) {
     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
@@ -183,6 +187,7 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     aCenterPnt->setValue(aCenterInSketch);
   }
 
+  aProjection->boolean(COPY_ID())->setValue(true);
   aProjection->execute();
   aRefAttr->setObject(aProjection);
 
@@ -192,6 +197,13 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     aFixed->execute();
   }
 
-  if (theID == EXTERNAL_FEATURE_ID())
-    selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->context()->shape());
+  if (theID == EXTERNAL_FEATURE_ID()) {
+    selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->value());
+
+    if (aResult) {
+      aResult->setShape(aProjection->lastResult()->shape());
+      setResult(aResult);
+      aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
+    }
+  }
 }