Salome HOME
[PythonAPI] Fix error in parameter wrapper
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
index a5b6001e7efd03e2d301ae14fcc6bd0b106c6a75..1859ec2156d74979f1f349667fde5cc3adfd31f6 100644 (file)
 #include <SketchSolver_ConstraintCoincidence.h>
 #include <SketchSolver_Error.h>
-#include <SketchSolver_Group.h>
+#include <SketchSolver_Manager.h>
 
-#include <map>
-
-bool SketchSolver_ConstraintCoincidence::hasConstraint(ConstraintPtr theConstraint) const
+void SketchSolver_ConstraintCoincidence::getAttributes(
+    double& theValue,
+    std::vector<EntityWrapperPtr>& theAttributes)
 {
-  if (myBaseConstraint == theConstraint)
-    return true;
-  std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
-  for (; anIt != myExtraCoincidence.end(); anIt++)
-    if (anIt->first == theConstraint)
-      return true;
-  return false;
-}
-
-std::list<ConstraintPtr> SketchSolver_ConstraintCoincidence::constraints() const
-{
-  std::list<ConstraintPtr> aConstraints;
-  aConstraints.push_back(myBaseConstraint);
-  std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
-  for (; anIt != myExtraCoincidence.end(); anIt++)
-    aConstraints.push_back(anIt->first);
-  return aConstraints;
-}
-
-bool SketchSolver_ConstraintCoincidence::isCoincide(
-    std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint) const
-{
-  std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFeatIter = myFeatureMap.begin();
-  for (; aFeatIter != myFeatureMap.end(); aFeatIter++)
-    if (theConstraint->myFeatureMap.find(aFeatIter->first) != theConstraint->myFeatureMap.end())
-      return true;
-  std::map<AttributePtr, Slvs_hEntity>::const_iterator anAttrIter = myAttributeMap.begin();
-  for (; anAttrIter != myAttributeMap.end(); anAttrIter++)
-    if (theConstraint->myAttributeMap.find(anAttrIter->first) != theConstraint->myAttributeMap.end())
-      return true;
-  return false;
-}
-
-void SketchSolver_ConstraintCoincidence::attach(
-    std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint)
-{
-  cleanErrorMsg();
-  Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
-  // Remove constraints from theConstraint
-  std::vector<Slvs_hConstraint>::iterator aCIter = theConstraint->mySlvsConstraints.begin();
-  for (; aCIter != theConstraint->mySlvsConstraints.end(); aCIter++)
-    theConstraint->myStorage->removeConstraint(*aCIter);
-
-  if (myStorage == theConstraint->myStorage) {
-    // Clean removed items
-    std::set<Slvs_hParam> aRemParams;
-    std::set<Slvs_hEntity> aRemEnts;
-    std::set<Slvs_hConstraint> aRemConstr;
-    theConstraint->myStorage->getRemoved(aRemParams, aRemEnts, aRemConstr);
+  SketchSolver_Constraint::getAttributes(theValue, theAttributes);
+  if (!myErrorMsg.empty() || !theAttributes[0]) {
+    theAttributes.clear();
+    return;
   }
 
-  // Copy data.
-  addConstraint(theConstraint->myBaseConstraint);
-  std::map<ConstraintPtr, Slvs_hConstraint>::iterator aConstrIter =
-      theConstraint->myExtraCoincidence.begin();
-  for (; aConstrIter != theConstraint->myExtraCoincidence.end(); aConstrIter++)
-    addConstraint(aConstrIter->first);
-  // Clear the lists to not remove the entities on destruction
-  theConstraint->mySlvsConstraints.clear();
-  theConstraint->myFeatureMap.clear();
-  theConstraint->myAttributeMap.clear();
-}
-
-Slvs_hConstraint SketchSolver_ConstraintCoincidence::addConstraint(
-    Slvs_hEntity thePoint1, Slvs_hEntity thePoint2)
-{
-  Slvs_Constraint aNewConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-      SLVS_C_POINTS_COINCIDENT, myGroup->getWorkplaneId(), 0.0, thePoint1, thePoint2, 
-      SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
-  Slvs_hConstraint aNewID = myStorage->addConstraint(aNewConstraint);
-  mySlvsConstraints.push_back(aNewID);
-  return aNewID;
-}
-
-void SketchSolver_ConstraintCoincidence::addConstraint(ConstraintPtr theConstraint)
-{
-  std::list<AttributePtr> anAttrList =
-      theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-  std::list<AttributePtr>::iterator anIter = anAttrList.begin();
-  std::vector<Slvs_hEntity> anEntities;
-  for (; anIter != anAttrList.end(); anIter++) {
-    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
-    if (!aRefAttr || aRefAttr->isObject() ||
-        myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end())
-      continue;
-    int aType;
-    Slvs_hEntity anEntityID = myGroup->getAttributeId(aRefAttr->attr());
-    if (anEntityID == SLVS_E_UNKNOWN)
-      anEntityID = changeEntity(aRefAttr->attr(), aType);
-    anEntities.push_back(anEntityID);
+  if (theAttributes[1]) {
+    myType = CONSTRAINT_PT_PT_COINCIDENT;
+    if (myStorage->isFixed(theAttributes[1]) && !myStorage->isFixed(theAttributes[0])) {
+      // fixed point should go first
+      EntityWrapperPtr aTemp = theAttributes[0];
+      theAttributes[0] = theAttributes[1];
+      theAttributes[1] = aTemp;
+    }
+    // Set the slave (second) point the same as master (first) point.
+    // This will allow to skip adding point-point coincidence to the set of constraints
+    // and give us speed-up in solving the set of equations
+    myStorage->addCoincidentPoints(theAttributes[0], theAttributes[1]);
   }
-
-  Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
-  Slvs_hConstraint aNewConstr = SLVS_E_UNKNOWN;
-  std::vector<Slvs_hEntity>::iterator anEntIter = anEntities.begin();
-  for (; anEntIter != anEntities.end(); anEntIter++)
-    aNewConstr = addConstraint(aBaseCoincidence.ptA, *anEntIter);
-  myExtraCoincidence[theConstraint] = aNewConstr;
+  else if (theAttributes[2]) {
+    // check the type of entity (line or circle)
+    SketchSolver_EntityType anEntType = theAttributes[2]->type();
+    if (anEntType == ENTITY_LINE)
+      myType = CONSTRAINT_PT_ON_LINE;
+    else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
+      myType = CONSTRAINT_PT_ON_CIRCLE;
+    else
+      myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+  } else
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
 }
 
-bool SketchSolver_ConstraintCoincidence::remove(ConstraintPtr theConstraint)
+
+static bool isBase(const std::list<ConstraintWrapperPtr>& theConstraints, AttributePtr theAttribute)
 {
-  cleanErrorMsg();
-  ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
-  int aPos = -1; // position of constraint in the list (-1 for base constraint)
-  if (aConstraint != myBaseConstraint) {
-    std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
-    for (aPos = 0; anIt != myExtraCoincidence.end(); anIt++, aPos++)
-      if (anIt->first == aConstraint)
-        break;
-    if (aPos >= (int)myExtraCoincidence.size())
-      return false; // there is no constraint, which is specified to remove
+  AttributePtr anAttribute = theAttribute;
+  FeaturePtr aFeature;
+  AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+  if (aRefAttr) {
+    if (aRefAttr->isObject())
+      aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+    else
+      anAttribute = aRefAttr->attr();
   }
 
-  bool isFullyRemoved = myStorage->removeConstraint(mySlvsConstraints[aPos+1]);
-  mySlvsConstraints.erase(mySlvsConstraints.begin() + (1+aPos));
-  cleanRemovedEntities();
-  if (aPos < 0 && !myExtraCoincidence.empty()) {
-    // Need to specify another base coincidence constraint
-    myBaseConstraint = myExtraCoincidence.begin()->first;
-    myExtraCoincidence.erase(myExtraCoincidence.begin());
-    std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
-    Slvs_Constraint aBase = myStorage->getConstraint(*aCIter);
-    for (++aCIter; aCIter != mySlvsConstraints.end(); aCIter++) {
-      Slvs_Constraint aConstr = myStorage->getConstraint(*aCIter);
-      aConstr.ptA = aBase.ptA;
-      myStorage->updateConstraint(aConstr);
-    }
+  std::list<ConstraintWrapperPtr>::const_iterator aCIt = theConstraints.begin();
+  for (; aCIt != theConstraints.end(); ++aCIt) {
+    std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
+    std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
+    for (; aSIt != aSubs.end(); ++aSIt)
+      if ((aFeature && (*aSIt)->isBase(aFeature)) ||
+         (!aFeature && (*aSIt)->isBase(anAttribute)))
+        return true;
   }
-  return true;
+  return false;
 }
-