Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
index c4af99b80e70e34148bbe38c69067424be158264..ef83cce1d2db49d8a2b7fc0c3302de40b4908eaa 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:    SketchPlugin_Projection.cpp
-// Created: 07 May 2014
-// Author:  Artem ZHIDKOV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <SketchPlugin_Projection.h>
 
@@ -10,6 +24,7 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_ConstraintRigid.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
@@ -17,6 +32,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>
@@ -44,6 +60,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()
@@ -53,14 +71,19 @@ void SketchPlugin_Projection::execute()
     return;
   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
 
-  if (!lastResult()) {
+  if (!lastResult().get() && aProjection->lastResult().get()) {
     ResultConstructionPtr aConstr = document()->createConstruction(data());
-    aConstr->setShape(std::shared_ptr<GeomAPI_Edge>());
+    aConstr->setShape(aProjection->lastResult()->shape());
     aConstr->setIsInHistory(false);
+    aConstr->setDisplayed(false);
     setResult(aConstr);
 
     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
   }
+
+  // is sketch plane is changed (issue 1791), attribute of projection is not changed, but
+  // projection must be fully recomputed
+  computeProjection(EXTERNAL_FEATURE_ID());
 }
 
 void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
@@ -85,7 +108,8 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
   std::shared_ptr<GeomAPI_Edge> anEdge;
   if (aExtFeature && aExtFeature->value() && aExtFeature->value()->isEdge()) {
     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->value()));
-  } else if (aExtFeature->context() && aExtFeature->context()->shape() && aExtFeature->context()->shape()->isEdge()) {
+  } else if (aExtFeature->context() && aExtFeature->context()->shape() &&
+             aExtFeature->context()->shape()->isEdge()) {
     anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
   }
   if (!anEdge.get())
@@ -97,12 +121,16 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     aProjection = ModelAPI_Feature::feature(aRefAttr->object());
 
   // if the type of feature differs with already selected, remove it and create once again
-  if (aProjection) {
+  bool hasPrevProj = aProjection.get() != 0;
+  if (hasPrevProj) {
     if ((anEdge->isLine() && aProjection->getKind() != SketchPlugin_Line::ID()) ||
         (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);
     }
@@ -110,6 +138,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() && theID == EXTERNAL_FEATURE_ID()) {
+    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());
@@ -119,7 +154,7 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     if (aFirstInSketch->distance(aLastInSketch) < tolerance)
       return; // line is semi-orthogonal to the sketch plane
 
-    if (!aProjection)
+    if (!hasPrevProj)
       aProjection = sketch()->addFeature(SketchPlugin_Line::ID());
 
     // update attributes of projection
@@ -137,11 +172,12 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
 
-    if (!aProjection)
+    if (!hasPrevProj)
       aProjection = sketch()->addFeature(SketchPlugin_Circle::ID());
 
     // update attributes of projection
-    std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
         aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
     aCenterPnt->setValue(aCenterInSketch);
     aProjection->real(SketchPlugin_Circle::RADIUS_ID())->setValue(aRadius);
@@ -156,24 +192,35 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
     std::shared_ptr<GeomAPI_Pnt> aCenter = aSketchPlane->project(aCircle->center());
     std::shared_ptr<GeomAPI_Pnt2d> aCenterInSketch = sketch()->to2D(aCenter);
 
-    if (!aProjection)
+    if (!hasPrevProj)
       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
 
     // update attributes of projection
-    std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
         aProjection->attribute(SketchPlugin_Arc::CENTER_ID()));
-    std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    std::shared_ptr<GeomDataAPI_Point2D> aStartPnt =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
         aProjection->attribute(SketchPlugin_Arc::START_ID()));
-    std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    std::shared_ptr<GeomDataAPI_Point2D> aEndPnt =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
         aProjection->attribute(SketchPlugin_Arc::END_ID()));
     aStartPnt->setValue(aFirstInSketch);
     aEndPnt->setValue(aLastInSketch);
     aCenterPnt->setValue(aCenterInSketch);
   }
 
+  aProjection->boolean(COPY_ID())->setValue(true);
   aProjection->execute();
   aRefAttr->setObject(aProjection);
 
-  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());
+    }
+  }
 }