Salome HOME
Merge remote-tracking branch 'remotes/origin/Dev_ResultNames'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
index de39ac7816b00a37d696b6492912c14b577e91d6..3d783a40850a9c902b5b2db01cfe41b1d25dfc27 100644 (file)
@@ -1,15 +1,31 @@
-// 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>
 
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_ConstraintRigid.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAPI_Circ.h>
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_Vertex.h>
 #include <GeomAlgoAPI_EdgeBuilder.h>
 #include <GeomDataAPI_Point2D.h>
 
@@ -44,6 +62,10 @@ void SketchPlugin_Projection::initDerivedClassAttributes()
 
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
+
+  data()->addAttribute(INCLUDE_INTO_RESULT(), ModelAPI_AttributeBoolean::typeId());
+
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), AUXILIARY_ID());
 }
 
 void SketchPlugin_Projection::execute()
@@ -53,61 +75,19 @@ void SketchPlugin_Projection::execute()
     return;
   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
 
-  bool hasResult = lastResult();
-
-  std::shared_ptr<GeomAPI_Edge> anEdge;
-  if (aProjection->getKind() == SketchPlugin_Line::ID()) {
-    std::shared_ptr<GeomDataAPI_Point2D> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aProjection->attribute(SketchPlugin_Line::START_ID()));
-    std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aProjection->attribute(SketchPlugin_Line::END_ID()));
+  if (!lastResult().get() && aProjection->lastResult().get()) {
+    ResultConstructionPtr aConstr = document()->createConstruction(data());
+    aConstr->setShape(aProjection->lastResult()->shape());
+    aConstr->setIsInHistory(false);
+    aConstr->setDisplayed(false);
+    setResult(aConstr);
 
-    // edge to store result
-    std::shared_ptr<GeomAPI_Pnt> aFirst = sketch()->to3D(aStartPnt->x(), aStartPnt->y());
-    std::shared_ptr<GeomAPI_Pnt> aLast = sketch()->to3D(aEndPnt->x(), aEndPnt->y());
-    anEdge = GeomAlgoAPI_EdgeBuilder::line(aFirst, aLast);
-  }
-  else {
-    std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
-    std::shared_ptr<GeomAPI_Dir> aNormal = aSketchPlane->direction();
-
-    if (aProjection->getKind() == SketchPlugin_Circle::ID()) {
-      std::shared_ptr<GeomDataAPI_Point2D> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-          aProjection->attribute(SketchPlugin_Circle::CENTER_ID()));
-
-      // edge to store result
-      std::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterPnt->x(), aCenterPnt->y());
-      double aRadius = aProjection->real(SketchPlugin_Circle::RADIUS_ID())->value();
-      anEdge = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
-    }
-    else if (aProjection->getKind() == SketchPlugin_Arc::ID()) {
-      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>(
-          aProjection->attribute(SketchPlugin_Arc::START_ID()));
-      std::shared_ptr<GeomDataAPI_Point2D> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-          aProjection->attribute(SketchPlugin_Arc::END_ID()));
-
-      // edge to store result
-      std::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterPnt->x(), aCenterPnt->y());
-      std::shared_ptr<GeomAPI_Pnt> aFirst = sketch()->to3D(aStartPnt->x(), aStartPnt->y());
-      std::shared_ptr<GeomAPI_Pnt> aLast = sketch()->to3D(aEndPnt->x(), aEndPnt->y());
-      anEdge = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aFirst, aLast, aNormal);
-    }
-  }
-
-  std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
-  aConstr->setShape(anEdge);
-  aConstr->setIsInHistory(false);
-  setResult(aConstr);
-
-  if (!hasResult)
     aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), lastResult()->shape());
-}
+  }
 
-void SketchPlugin_Projection::move(double theDeltaX, double theDeltaY)
-{
-  // feature cannot be moved
+  // 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::attributeChanged(const std::string& theID)
@@ -119,18 +99,42 @@ void SketchPlugin_Projection::attributeChanged(const std::string& theID)
   }
 }
 
+static bool isValidProjectionType(FeaturePtr theProjection,
+                                  GeomEdgePtr theEdge,
+                                  GeomVertexPtr theVertex)
+{
+  if (theVertex && theProjection->getKind() == SketchPlugin_Point::ID())
+    return true;
+  if (theEdge) {
+    if (theEdge->isLine() && theProjection->getKind() == SketchPlugin_Line::ID())
+      return true;
+    else if (theEdge->isCircle() && theProjection->getKind() == SketchPlugin_Circle::ID())
+      return true;
+    else if (theEdge->isArc() && theProjection->getKind() == SketchPlugin_Arc::ID())
+      return true;
+  }
+  return false;
+}
+
 void SketchPlugin_Projection::computeProjection(const std::string& theID)
 {
   AttributeSelectionPtr aExtFeature =
       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attribute(EXTERNAL_FEATURE_ID()));
 
-  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()) {
-    anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aExtFeature->context()->shape()));
+  GeomShapePtr aShape;
+  GeomEdgePtr anEdge;
+  GeomVertexPtr aVertex;
+  if (aExtFeature)
+    aShape = aExtFeature->value();
+  if (!aShape && aExtFeature->context())
+    aShape = aExtFeature->context()->shape();
+  if (aShape) {
+    if (aShape->isEdge())
+      anEdge = GeomEdgePtr(new GeomAPI_Edge(aShape));
+    else if (aShape->isVertex())
+      aVertex = GeomVertexPtr(new GeomAPI_Vertex(aShape));
   }
-  if (!anEdge.get())
+  if (!anEdge && !aVertex)
     return;
 
   AttributeRefAttrPtr aRefAttr = data()->refattr(PROJECTED_FEATURE_ID());
@@ -139,20 +143,40 @@ 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) {
-    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);
-      aProjection = FeaturePtr();
-      aRefAttr->setObject(aProjection);
-    }
+  bool hasPrevProj = aProjection.get() != 0;
+  if (hasPrevProj && !isValidProjectionType(aProjection, anEdge, aVertex)) {
+    DocumentPtr aDoc = sketch()->document();
+
+    aRefAttr->setObject(data()->owner()); // to not remove of this remove reference to aProjection
+    std::set<FeaturePtr> aFeaturesToBeRemoved;
+    aFeaturesToBeRemoved.insert(aProjection);
+    ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
+    aProjection = FeaturePtr();
+    aRefAttr->setObject(aProjection);
+    hasPrevProj = false;
   }
 
   std::shared_ptr<GeomAPI_Pln> aSketchPlane = sketch()->plane();
 
-  if (anEdge->isLine()) {
+  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 (aVertex) {
+    std::shared_ptr<GeomAPI_Pnt> aPrjPnt = aSketchPlane->project(aVertex->point());
+    std::shared_ptr<GeomAPI_Pnt2d> aPntInSketch = sketch()->to2D(aPrjPnt);
+
+    if (!hasPrevProj)
+      aProjection = sketch()->addFeature(SketchPlugin_Point::ID());
+
+    // update coordinates of projection
+    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aProjection->attribute(SketchPlugin_Point::COORD_ID()))->setValue(aPntInSketch);
+  }
+  else if (anEdge->isLine()) {
     std::shared_ptr<GeomAPI_Pnt> aFirst = aSketchPlane->project(anEdge->firstPoint());
     std::shared_ptr<GeomAPI_Pnt> aLast = aSketchPlane->project(anEdge->lastPoint());
 
@@ -161,7 +185,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
@@ -179,11 +203,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);
@@ -198,24 +223,44 @@ 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)
+    bool isInversed = aCircle->normal()->dot(aSketchPlane->direction()) < 0.;
+
+    if (!hasPrevProj)
       aProjection = sketch()->addFeature(SketchPlugin_Arc::ID());
 
+    bool aWasBlocked = aProjection->data()->blockSendAttributeUpdated(true);
+
     // 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(SketchPlugin_Arc::REVERSED_ID())->setValue(isInversed);
 
+    aProjection->data()->blockSendAttributeUpdated(aWasBlocked, false);
+  } else
+    return;
+
+  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())->selectValue(aExtFeature);
+
+    if (aResult) {
+      aResult->setShape(aProjection->lastResult()->shape());
+      setResult(aResult);
+      GeomShapePtr anEmptyVal;
+      aProjection->selection(EXTERNAL_ID())->setValue(lastResult(), anEmptyVal);
+    }
+  }
 }