Salome HOME
Issue #1708: Export - Apply is enabled when no objects is selected
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index 188a41f9ae80361ce4b74dd83ad2f9463da2fae0..d88fd2ac20da9cfaa78d8898040c52feaee2aec7 100755 (executable)
@@ -15,6 +15,9 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Events.h>
+#include <ModelAPI_Validator.h>
+
+#include <ModelGeomAlgo_Point2D.h>
 
 #include <Events_Loop.h>
 
@@ -53,6 +56,7 @@
 
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_Tools.h>
 
 #include <V3d_View.hxx>
 #include <gp_Pln.hxx>
@@ -304,7 +308,7 @@ std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumen
 
   // may be feature is not updated yet, execute is not performed and references features
   // are not created. Case: rectangle macro feature
-  ModuleBase_ModelWidget::updateObject(theFeature);
+  ModuleBase_Tools::flushUpdated(theFeature);
 
   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
                                           ModelAPI_AttributeRefList::typeId());
@@ -332,10 +336,14 @@ std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const Fe
                                     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
   std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
       aLast = anAttiributes.end();
+  ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
+
   for (; anIt != aLast && !aFPoint; anIt++) {
     std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
-      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
-    if (aCurPoint && (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
+                                             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+    if (aCurPoint && aCurPoint->isInitialized() &&
+        aValidators->isCase(theFeature, aCurPoint->id()) &&
+        (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
       aFPoint = aCurPoint;
       break;
     }
@@ -378,7 +386,7 @@ void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr the
   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
   for (; anIt != aLast; anIt++) {
     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
-    if (!aFeature.get() || theFeature == aFeature)
+    if (!aFeature.get() || (theFeature == aFeature) || (aFeaturePoint->owner() == aFeature))
       continue;
     std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
                                                                                 aClickedPoint);
@@ -391,29 +399,15 @@ std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theS
 {
   std::shared_ptr<GeomAPI_Pln> aPlane;
 
-  std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
-  if (aData) {
-    std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-        aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-    std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-        aData->attribute(SketchPlugin_Sketch::NORM_ID()));
-    if (aNormal.get() && aNormal->isInitialized() &&
-        anOrigin.get() && anOrigin->isInitialized()) {
-      double adX = aNormal->x();
-      double adY = aNormal->y();
-      double adZ = aNormal->z();
-
-      if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
-        double aX = anOrigin->x();
-        double aY = anOrigin->y();
-        double aZ = anOrigin->z();
-        gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
-        double aA, aB, aC, aD;
-        aPln.Coefficients(aA, aB, aC, aD);
-        aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
-      }
-    }
-  }
+  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+      theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
+
+  if (aNormal.get() && aNormal->isInitialized() &&
+      anOrigin.get() && anOrigin->isInitialized())
+    aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
+
   return aPlane;
 }
 
@@ -545,6 +539,8 @@ ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShap
           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
             setObject(aMyFeature->lastResult());
+          // we need to flush created signal in order to fixed constraint is processed by solver
+          Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
         //}
         return aMyFeature->lastResult();
       }
@@ -603,6 +599,8 @@ ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShap
           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
             setObject(aMyFeature->lastResult());
+          // we need to flush created signal in order to fixed constraint is processed by solver
+          Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
         //}
         return aMyFeature->lastResult();
       }
@@ -748,26 +746,9 @@ GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
                                                        const std::string& theAttribute)
 {
-  std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
-
-  if (!theFeature->data())
-    return std::shared_ptr<GeomAPI_Pnt2d>();
-
-  FeaturePtr aFeature;
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
-      ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
-  if (!anAttr)
-    return std::shared_ptr<GeomAPI_Pnt2d>();
-
-  aFeature = ModelAPI_Feature::feature(anAttr->object());
-
-  if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
-
-  else if (anAttr->attr()) {
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
-  }
+  std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
+                                          theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
+                                          SketchPlugin_Point::COORD_ID());
   if (aPointAttr.get() != NULL)
     return aPointAttr->pnt();
   return std::shared_ptr<GeomAPI_Pnt2d>();
@@ -940,6 +921,8 @@ AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
         for (; anIt != aLast && !anAttribute; anIt++) {
           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+          if (!aCurPoint->isInitialized())
+            continue;
 
           std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {