Salome HOME
setDisplayed has to be called in order to synchronize internal state of the object
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
index 365db4ca69b59d5d4aca8311afa65980b1aca50a..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>
@@ -44,12 +47,12 @@ SketchPlugin_Sketch::SketchPlugin_Sketch()
 
 void SketchPlugin_Sketch::initAttributes()
 {
-  data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
-  data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
-  data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
-  data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
+  data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
+  data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
+  data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
+  data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
   // the selected face, base for the sketcher plane, not obligatory
-  data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
+  data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
   ModelAPI_Session::get()->validators()->registerNotObligatory(
     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
 }
@@ -115,9 +118,6 @@ void SketchPlugin_Sketch::execute()
     aBigWire->addEdge(*aShapeIt);
   }
   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
-
-//  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
-//                                         aFeaturesPreview, aLoops, aWires);
   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
   aConstr->setShape(aBigWire);
   setResult(aConstr);
@@ -125,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();
@@ -191,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()
 {
@@ -309,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);
+}