Salome HOME
Fix for crash in Object Browser when object was deleted but message not sent
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
index 377fc0beddf6fc491423951a2417c3ca474e0a2e..7b7fb78d0e1608a9afbbf4ab8c677feccadb3508 100644 (file)
 #include <GeomAPI_AISObject.h>
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_PlanarEdges.h>
-#include <GeomAPI_XYZ.h>
 
-#include <GeomDataAPI_Dir.h>
-#include <GeomDataAPI_Point.h>
 #include <GeomAlgoAPI_FaceBuilder.h>
 
+#include <GeomDataAPI_Point2D.h>
+#include <GeomAlgoAPI_PointBuilder.h>
+
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Events.h>
 
 #include <SketchPlugin_Sketch.h>
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_SketchEntity.h>
 
+#include <Events_Loop.h>
+
 #include <memory>
 
 #include <math.h>
@@ -122,17 +125,19 @@ void SketchPlugin_Sketch::execute()
 
 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
 {
-  std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
+  std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
   if (aNew) {
     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
   }
+   // set as current also after it becomes sub to set correctly enabled for other sketch subs
+  document()->setCurrentFeature(aNew, false);
   return aNew;
 }
 
 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
 {
-  if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
+  if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
     return;
   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
@@ -188,69 +193,6 @@ bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
   return false;
 }
 
-std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
-{
-  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
-
-  std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
-      ->added(aY->xyz()->multiplied(theY));
-
-  return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
-}
-
-std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
-  const std::shared_ptr<GeomAPI_Pnt>& thePnt)
-{
-  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
-  return thePnt->to2D(aC->pnt(), aX->dir(), aY);
-}
-
-
-bool SketchPlugin_Sketch::isPlaneSet()
-{
-  std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
-
-  return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
-}
-
-std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
-{
-  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
-
-  if (!anOrigin || !aNorm)
-    return std::shared_ptr<GeomAPI_Pln>();
-
-  return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
-}
-
-std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::coordinatePlane() const
-{
-  DataPtr aData = data();
-  std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-    aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-    aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-    aData->attribute(SketchPlugin_Sketch::NORM_ID()));
-
-  return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aNorm->dir()));
-}
 
 void SketchPlugin_Sketch::erase()
 {
@@ -306,5 +248,35 @@ void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
       }
     }
+  } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_ID()) {
+    // send all sub-elements are also updated: all entities become created on different plane
+    static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+    std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
+    std::list<ObjectPtr>::iterator aSub = aSubs.begin();
+    for(; aSub != aSubs.end(); aSub++) {
+      ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
+    }
   }
 }
+
+void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
+                                              SketchPlugin_Sketch* theSketch,
+                                              const std::string& theAttributeID, const int theIndex)
+{
+  std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    theFeature->attribute(theAttributeID));
+
+  if (!aPoint || !aPoint->isInitialized())
+    return;
+
+  std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
+  //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
+  // make a visible point
+  std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
+  std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
+                     theFeature->data(), theIndex);
+  aResult->setShape(aCenterPointShape);
+  aResult->setIsInHistory(false);
+
+  theFeature->setResult(aResult, theIndex);
+}