Salome HOME
Issue #1366: "Partition" feature now modified to "Generalized Partition"
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Projection.cpp
index de39ac7816b00a37d696b6492912c14b577e91d6..eaf073c8b7702a9caeda15e061116fb724df09ee 100644 (file)
@@ -10,6 +10,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 +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>
@@ -44,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()
@@ -53,56 +57,15 @@ 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()));
-
-    // 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);
-    }
-  }
+  if (!lastResult().get() && aProjection->lastResult().get()) {
+    ResultConstructionPtr aConstr = document()->createConstruction(data());
+    aConstr->setShape(aProjection->lastResult()->shape());
+    aConstr->setIsInHistory(false);
+    aConstr->setDisplayed(false);
+    setResult(aConstr);
 
-  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)
@@ -139,12 +102,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);
     }
@@ -152,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());
@@ -161,7 +135,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,7 +153,7 @@ 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
@@ -198,7 +172,7 @@ 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
@@ -216,6 +190,19 @@ void SketchPlugin_Projection::computeProjection(const std::string& theID)
   aProjection->execute();
   aRefAttr->setObject(aProjection);
 
-  if (theID == EXTERNAL_FEATURE_ID())
-    selection(EXTERNAL_ID())->setValue(aExtFeature->context(), aExtFeature->context()->shape());
+  if (!hasPrevProj) {
+    FeaturePtr aFixed = sketch()->addFeature(SketchPlugin_ConstraintRigid::ID());
+    aFixed->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(aProjection->lastResult());
+    aFixed->execute();
+  }
+
+  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());
+    }
+  }
 }