]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/SketchSolver/SketchSolver_ConstraintMulti.cpp
Salome HOME
SketchSolver Refactoring: Eliminate SolveSpace as a sketch solver.
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
index a86ab8b5fbd51731f26d30fdade40e31a67a402d..4ca7e088b2911bf1067f87703b6c126d84485e8d 100644 (file)
 void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theEntities)
 {
   myAdjusted = false;
-  DataPtr aData = myBaseConstraint->data();
 
   // Lists of objects and number of copies
-  AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr anInitialRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_A());
   myNumberOfObjects = anInitialRefList->size();
-  myNumberOfCopies = aData->integer(nameNbObjects())->value() - 1;
+  myNumberOfCopies = myBaseConstraint->integer(nameNbObjects())->value() - 1;
   if (myNumberOfCopies <= 0)
     return;
 
-  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  AttributeRefListPtr aRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
   if (!aRefList || aRefList->size() == 0) {
     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
     return;
@@ -48,7 +47,7 @@ void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theE
 
     // the entity is not created, so it is a copy in "multi" constraint, force its creation
     if (!myStorage->update(aFeature))
-      myStorage->update(aFeature, myGroupID, true);
+      myStorage->update(aFeature, true);
     theEntities.push_back(myStorage->entity(aFeature));
     myFeatures.insert(aFeature);
     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
@@ -62,20 +61,17 @@ void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theE
 
 bool SketchSolver_ConstraintMulti::remove()
 {
+  myStorage->unsubscribeUpdates(this);
+
   myFeatures.clear();
   return SketchSolver_Constraint::remove();
 }
 
 void SketchSolver_ConstraintMulti::update()
-{
-  update(false);
-}
-
-void SketchSolver_ConstraintMulti::update(bool isForce)
 {
   cleanErrorMsg();
-  AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr anInitialRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_A());
   AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
   if (!anInitialRefList || !aNbObjects)
     return; // the "Multi" constraint is in queue to remove
@@ -83,8 +79,8 @@ void SketchSolver_ConstraintMulti::update(bool isForce)
     anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies;
   if (!isUpdated) {
     // additional check that the features and their copies are changed
-    AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-        myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+    AttributeRefListPtr aRefList =
+        myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
     if (aRefList && aRefList->size() != 0) {
       FeaturePtr aFeature;
       std::list<ObjectPtr> anObjectList = aRefList->list();
@@ -102,26 +98,21 @@ void SketchSolver_ConstraintMulti::update(bool isForce)
   if (isUpdated) {
     remove();
     process();
-    return;
   }
-
-  // update derivative object
-  updateLocal();
-  if (isForce)
-    myAdjusted = false;
-  adjustConstraint();
 }
 
 void SketchSolver_ConstraintMulti::adjustConstraint()
 {
-  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  AttributeRefListPtr aRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
   if (!aRefList || aRefList->size() == 0) {
     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
     return;
   }
 
   FeaturePtr anOriginal, aFeature;
+  std::list<double>::iterator aXIt, aYIt;
+
   std::list<ObjectPtr> anObjectList = aRefList->list();
   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
   while (anObjIt != anObjectList.end()) {
@@ -131,25 +122,19 @@ void SketchSolver_ConstraintMulti::adjustConstraint()
 
     // Fill lists of coordinates of points composing a feature
     std::list<double> aX, aY;
-    std::list<double>::iterator aXIt, aYIt;
     double aXCoord, aYCoord;
-    EntityWrapperPtr anEntity = myStorage->entity(anOriginal);
-    std::list<EntityWrapperPtr> aSubs = anEntity->subEntities();
-    std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
-    for (; aSIt != aSubs.end(); ++aSIt) {
-      if ((*aSIt)->type() != ENTITY_POINT)
-        continue;
-      AttributePoint2DPtr aPoint =
-          std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSIt)->baseAttribute());
-      if (aPoint) {
-        aXCoord = aPoint->x();
-        aYCoord = aPoint->y();
-      } else {
-        std::list<ParameterWrapperPtr> aParameters = (*aSIt)->parameters();
-        aXCoord = aParameters.front()->value();
-        aYCoord = aParameters.back()->value();
-      }
-      getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
+    std::list<AttributePtr> aPoints =
+        anOriginal->data()->attributes(GeomDataAPI_Point2D::typeId());
+    std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
+    for (; aPtIt != aPoints.end(); ++aPtIt) {
+      AttributePoint2DPtr aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
+      if (aPoint2D->isInitialized()) {
+        aXCoord = aPoint2D->x();
+        aYCoord = aPoint2D->y();
+        getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
+      } else
+        aXCoord = aYCoord = 0;
+
       aX.push_back(aXCoord);
       aY.push_back(aYCoord);
     }
@@ -159,78 +144,51 @@ void SketchSolver_ConstraintMulti::adjustConstraint()
       aFeature = ModelAPI_Feature::feature(*anObjIt);
       if (!aFeature)
         continue;
-      anEntity = myStorage->entity(aFeature);
-
-      bool aWasBlocked = false;
-      if (!anEntity || !myStorage->isEventsBlocked())
-        aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
-
-      std::list<AttributePtr> aPoints;
-      if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::START_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::END_ID()));
-      } else if (aFeature->getKind() == SketchPlugin_Line::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Line::START_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Line::END_ID()));
-      } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
-        // update circle's radius
+
+      if (myIsEventsBlocked)
+        aFeature->data()->blockSendAttributeUpdated(true);
+
+      if (aFeature->getKind() == SketchPlugin_Circle::ID()) // update circle's radius
         aFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(
             anOriginal->real(SketchPlugin_Circle::RADIUS_ID())->value());
-      } else if (aFeature->getKind() == SketchPlugin_Point::ID() ||
-               aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
 
-      std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
-      for (aXIt = aX.begin(), aYIt = aY.begin(); aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
+      aPoints = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+      for (aPtIt = aPoints.begin(), aXIt = aX.begin(), aYIt = aY.begin();
+           aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
+        if (!(*aPtIt)->isInitialized())
+          continue;
         transformRelative(*aXIt, *aYIt);
         getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
 
-        std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
+        AttributePoint2DPtr aPoint2D =
             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
         aPoint2D->setValue(aXCoord, aYCoord);
       }
-
-      // update feature in the storage if it is used by another constraints
-      if (anEntity)
-        myStorage->update(aFeature);
-      else { // update attributes, if they exist in the storage
-        for (aPtIt = aPoints.begin(); aPtIt != aPoints.end(); ++aPtIt) {
-          EntityWrapperPtr aPntEnt = myStorage->entity(*aPtIt);
-          if (aPntEnt)
-            myStorage->update(*aPtIt);
-        }
-      }
-
-      if (!anEntity || !myStorage->isEventsBlocked())
-        aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
     }
   }
 
   myAdjusted = true;
 }
 
-bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
+void SketchSolver_ConstraintMulti::notify(const FeaturePtr& theFeature,
+                                          PlaneGCSSolver_Update*)
 {
-  return theFeature && (myFeatures.find(theFeature) != myFeatures.end() ||
-         SketchSolver_Constraint::isUsed(theFeature));
+  if (myFeatures.find(theFeature) == myFeatures.end())
+    return; // the feature is not used by constraint => nothing to update
+
+  // update derivative object
+  updateLocal();
+  myAdjusted = false;
+  adjustConstraint();
 }
 
-bool SketchSolver_ConstraintMulti::isUsed(AttributePtr theAttribute) const
+void SketchSolver_ConstraintMulti::blockEvents(bool isBlocked)
 {
-  AttributePtr anAttribute = theAttribute;
-  AttributeRefAttrPtr aRefAttr =
-      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
-  if (aRefAttr) {
-    if (aRefAttr->isObject())
-      return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
-    else
-      anAttribute = aRefAttr->attr();
-  }
-  if (!anAttribute)
-    return false;
+  myIsEventsBlocked = isBlocked;
+
+  std::set<FeaturePtr>::iterator anIt = myFeatures.begin();
+  for (; anIt != myFeatures.end(); ++anIt)
+    (*anIt)->data()->blockSendAttributeUpdated(isBlocked);
 
-  FeaturePtr anOwner = ModelAPI_Feature::feature(anAttribute->owner());
-  return myFeatures.find(anOwner) != myFeatures.end();
+  SketchSolver_Constraint::blockEvents(isBlocked);
 }