Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
index eeabd7478946db8f218095a51cf9b93be51f0196..31d975a82c01b6adc15e5b0a39a4de7a34011ef3 100644 (file)
@@ -1,7 +1,27 @@
+// Copyright (C) 2014-2022  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include <SketchSolver_Constraint.h>
-#include <SketchSolver_Group.h>
 #include <SketchSolver_Error.h>
-#include <SketchSolver_Manager.h>
+
+#include <PlaneGCSSolver_AttributeBuilder.h>
+#include <PlaneGCSSolver_Tools.h>
 
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
@@ -10,6 +30,7 @@
 
 #include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_ConstraintCoincidenceInternal.h>
 #include <SketchPlugin_ConstraintCollinear.h>
 #include <SketchPlugin_ConstraintDistance.h>
 #include <SketchPlugin_ConstraintEqual.h>
 SketchSolver_Constraint::SketchSolver_Constraint(
     ConstraintPtr  theConstraint)
   : myBaseConstraint(theConstraint),
-    myGroupID(GID_UNKNOWN),
     myType(CONSTRAINT_UNKNOWN)
 {
 }
 
-void SketchSolver_Constraint::process(StoragePtr theStorage,
-                                      const GroupID& theGroupID,
-                                      const EntityID& theSketchID)
+void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
 {
   myStorage = theStorage;
-  myGroupID = theGroupID;
-  mySketchID = theSketchID;
+  blockEvents(theEvensBlocked);
   // Process constraint according to its type
   process();
 }
 
+void SketchSolver_Constraint::blockEvents(bool isBlocked)
+{
+  myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
+}
+
 
 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
 {
   const std::string& aType = theConstraint->getKind();
-  if (aType == SketchPlugin_ConstraintCoincidence::ID())
+  if (aType == SketchPlugin_ConstraintCoincidence::ID() ||
+      aType == SketchPlugin_ConstraintCoincidenceInternal::ID())
     return CONSTRAINT_COINCIDENCE;
   else if (aType == SketchPlugin_ConstraintRigid::ID())
     return CONSTRAINT_FIXED;
@@ -91,13 +114,13 @@ SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConst
 void SketchSolver_Constraint::process()
 {
   cleanErrorMsg();
-  if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
+  if (!myBaseConstraint || !myStorage) {
     // Not enough parameters are assigned
     return;
   }
 
   SketchSolver_ConstraintType aConstrType = getType();
-  double aValue;
+  EntityWrapperPtr aValue;
   std::vector<EntityWrapperPtr> anAttributes;
   getAttributes(aValue, anAttributes);
   if (!myErrorMsg.empty())
@@ -109,11 +132,14 @@ void SketchSolver_Constraint::process()
   if (aConstrType == CONSTRAINT_UNKNOWN)
     aConstrType = getType();
 
-  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-  std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
-      myBaseConstraint, myGroupID, mySketchID, aConstrType,
+  ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
+      myBaseConstraint, aConstrType,
       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
-  myStorage->addConstraint(myBaseConstraint, aNewConstraints);
+  if (!aNewConstraint) {
+    myErrorMsg = SketchSolver_Error::WRONG_CONSTRAINT_TYPE();
+    return;
+  }
+  myStorage->addConstraint(myBaseConstraint, aNewConstraint);
 
   adjustConstraint();
 }
@@ -121,69 +147,42 @@ void SketchSolver_Constraint::process()
 void SketchSolver_Constraint::update()
 {
   cleanErrorMsg();
-  std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
-  std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
 
-  // Check if attributes of constraint are changed, rebuild constraint
-  std::set<AttributePtr> anAttributes;
-  std::set<AttributePtr>::iterator aFoundAttr;
-  std::set<FeaturePtr> aFeatures;
-  std::set<FeaturePtr>::iterator aFoundFeat;
+  // Get list of attributes of the constraint and compare it with previously stored.
+  // If the lists are different, fully rebuild constraint
+  std::set<EntityWrapperPtr> anAttributes;
   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
     AttributePtr anAttr =
         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
     if (!anAttr)
       continue;
 
-    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
-    if (aRefAttr) {
-      if (aRefAttr->isObject()) {
-        FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
-        if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
-          aFeatures.insert(aFeat);
-        else {
-          // Workaround for the Length constraint: add points of line, not line itself
-          anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
-          anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
-        }
-      } else
-        anAttributes.insert(aRefAttr->attr());
-    } else
-      anAttributes.insert(anAttr);
-  }
-  bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
-  for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
-    const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
-    std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
-    for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
-      if ((*aSIt)->baseAttribute()) {
-        aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
-        if (aFoundAttr != anAttributes.end())
-          anAttributes.erase(aFoundAttr);
-      } else {
-        aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
-        if (aFoundFeat != aFeatures.end())
-          aFeatures.erase(aFoundFeat);
+    if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
+      AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
+      FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
+      if (aFeat) {
+        // Workaround for the Length constraint: add points of line, not line itself
+        anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
+        anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
       }
-      hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
-    }
+    } else
+      anAttributes.insert(myStorage->entity(anAttr));
   }
-  if (hasNewAttr) {
+
+  std::set<EntityWrapperPtr>::iterator aFound;
+  std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
+  for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
+    anAttributes.erase(*anAttrIt);
+
+  if (!anAttributes.empty()) {
     remove();
     process();
     return;
   }
 
-  AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
-  if (aValueAttr) {
-    for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
-      if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
-        (*aWIt)->setValue(aValueAttr->value());
-        myStorage->setNeedToResolve(true);
-      }
-  }
-  myStorage->addConstraint(myBaseConstraint, aWrapper);
+  AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
+  if (aValueAttr)
+    myStorage->update(aValueAttr);
 
   adjustConstraint();
 }
@@ -192,11 +191,12 @@ bool SketchSolver_Constraint::remove()
 {
   cleanErrorMsg();
   myType = CONSTRAINT_UNKNOWN;
+  myStorage->unsubscribeUpdates(this);
   return myStorage->removeConstraint(myBaseConstraint);
 }
 
 void SketchSolver_Constraint::getAttributes(
-    double& theValue,
+    EntityWrapperPtr& theValue,
     std::vector<EntityWrapperPtr>& theAttributes)
 {
   static const int anInitNbOfAttr = 4;
@@ -204,13 +204,15 @@ void SketchSolver_Constraint::getAttributes(
   myAttributes.clear();
 
   DataPtr aData = myBaseConstraint->data();
-  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
 
   myType = TYPE(myBaseConstraint);
 
-  AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      aData->attribute(SketchPlugin_Constraint::VALUE()));
-  theValue = aValueAttr ? aValueAttr->value() : 0.0;
+  AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
+  if (aValueAttr) {
+    PlaneGCSSolver_AttributeBuilder aValueBuilder;
+    theValue = aValueBuilder.createAttribute(aValueAttr);
+    myStorage->addEntity(aValueAttr, theValue);
+  }
 
   int aPtInd = 0; // index of first point in the list of attributes
   int aEntInd = 2; // index of first entity in the list of attributes
@@ -224,19 +226,14 @@ void SketchSolver_Constraint::getAttributes(
       return;
     }
 
-    myStorage->update(aRefAttr/*, myGroupID*/);
-    EntityWrapperPtr anEntity = myStorage->entity(aRefAttr);
-    if (!anEntity) {
-      // Force creation of an entity
-      myStorage->update(aRefAttr, GID_UNKNOWN, true);
-      anEntity = myStorage->entity(aRefAttr);
-    }
+    myStorage->update(*anIter, true);
+    EntityWrapperPtr anEntity = myStorage->entity(*anIter);
     myAttributes.push_back(anEntity);
 
     SketchSolver_EntityType aType = anEntity->type();
     if (aType == ENTITY_UNKNOWN)
       continue;
-    else if (aType == ENTITY_POINT)
+    else if (aType == ENTITY_POINT || aType == ENTITY_POINT_ARRAY)
       theAttributes[aPtInd++] = anEntity; // the point is created
     else { // another entity (not a point) is created
       if (aEntInd < anInitNbOfAttr)
@@ -247,40 +244,3 @@ void SketchSolver_Constraint::getAttributes(
     }
   }
 }
-
-bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
-{
-  const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
-  std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
-  for (; aCIt != aCList.end(); ++aCIt)
-    if ((*aCIt)->isUsed(theFeature))
-      return true;
-
-  std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
-  std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
-  for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
-    if (isUsed(*anAttrIt))
-      return true;
-
-  return false;
-}
-
-bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
-{
-  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();
-  }
-
-  const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
-  std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
-  for (; aCIt != aCList.end(); ++aCIt)
-    if ((*aCIt)->isUsed(theAttribute))
-      return true;
-  return false;
-}