Salome HOME
It removes commented not used code.
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintRigid.cpp
index 3483973fe30f2ae5661d04de2e26a16984926532..ed8ae371271336fa7c9ef9724b6c92a0a5c0c46c 100644 (file)
@@ -6,12 +6,15 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_ConstraintRigid.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_XY.h>
 #include <GeomDataAPI_Point2D.h>
 #include <ModelAPI_AttributeDouble.h>
 
+#include <math.h>
+
 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
   : SketchSolver_Constraint(),
     myBaseFeature(theFeature)
@@ -32,24 +35,77 @@ void SketchSolver_ConstraintRigid::process()
   double aValue;
   std::vector<Slvs_hEntity> anEntities;
   getAttributes(aValue, anEntities);
-  if (!myErrorMsg.empty() || myFeatureMap.empty())
+  if (!myErrorMsg.empty() || (myFeatureMap.empty() && myAttributeMap.empty()))
     return;
+  fixFeature();
+}
 
-  if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Line::ID()) {
-    Slvs_Entity aLine = myStorage->getEntity(myFeatureMap.begin()->second);
-    fixLine(aLine);
+void SketchSolver_ConstraintRigid::update(ConstraintPtr theConstraint)
+{
+  cleanErrorMsg();
+  if (theConstraint && theConstraint == myBaseConstraint &&
+      theConstraint->getKind() == myBaseConstraint->getKind() &&
+      checkAttributesChanged(theConstraint)) {
+    // remove previous constraint and set the given one
+    remove(myBaseConstraint);
+    myBaseConstraint = theConstraint;
+    process();
   }
-  else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
-    Slvs_Entity anArc = myStorage->getEntity(myFeatureMap.begin()->second);
-    fixArc(anArc);
+}
+
+static void fixEntity(StoragePtr theStorage, const Slvs_hEntity& theEntID)
+{
+  Slvs_Entity anEntity = theStorage->getEntity(theEntID);
+  anEntity.group = SLVS_G_OUTOFGROUP;
+  theStorage->updateEntity(anEntity);
+  // move out of group all sub-entities
+  for (int i = 0; i < 4; ++i)
+    if (anEntity.point[i] != SLVS_E_UNKNOWN)
+      fixEntity(theStorage, anEntity.point[i]);
+  // move out of group the radius of circle
+  if (anEntity.distance != SLVS_E_UNKNOWN)
+    fixEntity(theStorage, anEntity.distance);
+  // move out of group parameters
+  for (int i = 0; i < 4; ++i)
+    if (anEntity.param[i] != SLVS_E_UNKNOWN) {
+      Slvs_Param aParam = theStorage->getParameter(anEntity.param[i]);
+      aParam.group = SLVS_G_OUTOFGROUP;
+      theStorage->updateParameter(aParam);
+    }
+}
+
+void SketchSolver_ConstraintRigid::fixFeature()
+{
+  Slvs_hEntity anEntID = fixedEntity();
+  if (anEntID != SLVS_E_UNKNOWN)
+    fixEntity(myStorage, anEntID);
+}
+
+Slvs_hEntity SketchSolver_ConstraintRigid::fixedEntity() const
+{
+  Slvs_hEntity anEntID = SLVS_E_UNKNOWN;
+  if (myBaseConstraint) {
+    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+        myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+    if (aRefAttr->isObject()) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+      std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFound = myFeatureMap.find(aFeature);
+      if (aFound != myFeatureMap.end())
+        anEntID = aFound->second;
+    } else {
+      std::map<AttributePtr, Slvs_hEntity>::const_iterator aFound = myAttributeMap.find(aRefAttr->attr());
+      if (aFound != myAttributeMap.end())
+        anEntID = aFound->second;
+    }
   }
-  else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
-    Slvs_Entity aCirc = myStorage->getEntity(myFeatureMap.begin()->second);
-    fixArc(aCirc);
+  else if (myBaseFeature) {
+    std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFound = myFeatureMap.find(myBaseFeature);
+    if (aFound != myFeatureMap.end())
+      anEntID = aFound->second;
   }
+  return anEntID;
 }
 
-
 void SketchSolver_ConstraintRigid::getAttributes(
     double& theValue,
     std::vector<Slvs_hEntity>& theAttributes)
@@ -90,38 +146,6 @@ void SketchSolver_ConstraintRigid::getAttributes(
     theAttributes.push_back(anEntityID);
 }
 
-void SketchSolver_ConstraintRigid::adjustConstraint()
-{
-  if (myFeatureMap.empty() || (
-      myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() && 
-      myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
-    return;
-  FeaturePtr aFeature = myFeatureMap.begin()->first;
-
-  // Search radius constraints and update them
-  Slvs_Constraint aConstraint;
-  std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
-  for (; aCIter != mySlvsConstraints.end(); aCIter++) {
-    aConstraint = myStorage->getConstraint(*aCIter);
-    if (aConstraint.type != SLVS_C_DIAMETER)
-      continue;
-    double aRadius = 0.0;
-    if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
-      std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
-      std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
-      aRadius = aCenter->distance(aStart);
-    } else {
-      aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-          aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
-    }
-
-    aConstraint.valA = aRadius * 2.0;
-    *aCIter = myStorage->updateConstraint(aConstraint);
-  }
-}
-
 
 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
 {
@@ -129,23 +153,33 @@ bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
   if (theConstraint && theConstraint != myBaseConstraint)
     return false;
   bool isFullyRemoved = true;
+
   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
-  for (; aCIter != mySlvsConstraints.end(); aCIter++)
+  for (; aCIter != mySlvsConstraints.end(); ++aCIter)
     isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
 
+  std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = myFeatureMap.begin();
+  for (; aFIter != myFeatureMap.end(); ++aFIter)
+    isFullyRemoved = myStorage->removeEntity(aFIter->second) && isFullyRemoved;
+
+  std::map<AttributePtr, Slvs_hEntity>::iterator anAtIter = myAttributeMap.begin();
+  for (; anAtIter != myAttributeMap.end(); ++anAtIter)
+    isFullyRemoved = myStorage->removeEntity(anAtIter->second) && isFullyRemoved;
+
   if (isFullyRemoved) {
     myFeatureMap.clear();
     myAttributeMap.clear();
     myValueMap.clear();
+    mySlvsConstraints.clear();
   } else
     cleanRemovedEntities();
   return true;
 }
 
-void SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
+Slvs_hConstraint SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
 {
   if (thePointID == SLVS_E_UNKNOWN)
-    return;
+    return SLVS_C_UNKNOWN;
 
   Slvs_Constraint aConstraint;
   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
@@ -153,28 +187,63 @@ void SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
   bool isForceUpdate = (isFixed && !myBaseConstraint &&
                         myStorage->isTemporary(aConstrID));
   if (!isForceUpdate) { // create new constraint
-    if (isFixed) return;
-    aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
+    if (isFixed) return aConstrID;
+    aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
         0.0, thePointID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
     aConstraint.h = myStorage->addConstraint(aConstraint);
     mySlvsConstraints.push_back(aConstraint.h);
     if (!myBaseConstraint)
       myStorage->addConstraintWhereDragged(aConstraint.h);
   } else { // update already existent constraint
-    if (!isFixed || aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
-      return;
+    if (!isFixed || aConstrID == SLVS_C_UNKNOWN || myBaseConstraint)
+      return SLVS_C_UNKNOWN;
     aConstraint = myStorage->getConstraint(aConstrID);
     aConstraint.ptA = thePointID;
     myStorage->addConstraint(aConstraint);
     if (!myBaseConstraint)
       myStorage->addConstraintWhereDragged(aConstraint.h);
   }
+  return aConstraint.h;
 }
 
 void SketchSolver_ConstraintRigid::fixLine(const Slvs_Entity& theLine)
 {
   Slvs_Constraint anEqual;
-  if (isUsedInEqual(theLine, anEqual)) {
+  if (myStorage->isAxisParallel(theLine.h)) {
+    // Fix one point and a line length
+    Slvs_hConstraint aFixed;
+    if (!myStorage->isPointFixed(theLine.point[0], aFixed, true) &&
+        !myStorage->isPointFixed(theLine.point[1], aFixed, true))
+      fixPoint(theLine.point[0]);
+    if (!myStorage->isUsedInEqual(theLine.h, anEqual)) {
+      // Check the distance is not set yet
+      std::list<Slvs_Constraint> aDistConstr = myStorage->getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
+      std::list<Slvs_Constraint>::const_iterator aDIt = aDistConstr.begin();
+      for (; aDIt != aDistConstr.end(); aDIt++)
+        if ((aDIt->ptA == theLine.point[0] && aDIt->ptB == theLine.point[1]) ||
+            (aDIt->ptA == theLine.point[1] && aDIt->ptB == theLine.point[0]))
+          return;
+      // Calculate distance between points on the line
+      double aCoords[4];
+      for (int i = 0; i < 2; i++) {
+        Slvs_Entity aPnt = myStorage->getEntity(theLine.point[i]);
+        for (int j = 0; j < 2; j++) {
+          Slvs_Param aParam = myStorage->getParameter(aPnt.param[j]);
+          aCoords[2*i+j] = aParam.val;
+        }
+      }
+      double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) + 
+                            (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
+      // fix line length
+      Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
+          SLVS_C_PT_PT_DISTANCE, myGroup->getWorkplaneId(), aLength,
+          theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
+      aDistance.h = myStorage->addConstraint(aDistance);
+      mySlvsConstraints.push_back(aDistance.h);
+    }
+    return;
+  }
+  else if (myStorage->isUsedInEqual(theLine.h, anEqual)) {
     // Check another entity of Equal is already fixed
     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
     if (myStorage->isEntityFixed(anOtherEntID, true)) {
@@ -219,7 +288,7 @@ void SketchSolver_ConstraintRigid::fixCircle(const Slvs_Entity& theCircle)
   bool isFixRadius = true;
   // Verify the arc is under Equal constraint
   Slvs_Constraint anEqual;
-  if (isUsedInEqual(theCircle, anEqual)) {
+  if (myStorage->isUsedInEqual(theCircle.h, anEqual)) {
     // Check another entity of Equal is already fixed
     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
     if (myStorage->isEntityFixed(anOtherEntID, true))
@@ -229,6 +298,13 @@ void SketchSolver_ConstraintRigid::fixCircle(const Slvs_Entity& theCircle)
   fixPoint(theCircle.point[0]);
 
   if (isFixRadius) {
+    // Search the radius is already fixed
+    std::list<Slvs_Constraint> aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
+    std::list<Slvs_Constraint>::const_iterator aDiamIter = aDiamConstr.begin();
+    for (; aDiamIter != aDiamConstr.end(); aDiamIter++)
+      if (aDiamIter->entityA == theCircle.h)
+        return;
+
     // Fix radius of a circle
     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
         myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
@@ -249,7 +325,7 @@ void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
 
   // Verify the arc is under Equal constraint
   Slvs_Constraint anEqual;
-  if (isUsedInEqual(theArc, anEqual)) {
+  if (myStorage->isUsedInEqual(theArc.h, anEqual)) {
     // Check another entity of Equal is already fixed
     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
     if (myStorage->isEntityFixed(anOtherEntID, true)) {
@@ -302,7 +378,7 @@ void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
     std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
     std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
     for (; anIt != aDiamConstraints.end() && !isExists; anIt++)
-      if (anIt->entityA == myFeatureMap.begin()->second)
+      if (anIt->entityA == theArc.h)
         isExists = true;
     if (!isExists) {
       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
@@ -315,23 +391,3 @@ void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
     }
   }
 }
-
-bool SketchSolver_ConstraintRigid::isUsedInEqual(
-    const Slvs_Entity& theEntity, Slvs_Constraint& theEqual) const
-{
-  // Check the entity is used in Equal constraint
-  std::list<Slvs_Constraint> anEqualConstr = myStorage->getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
-  std::list<Slvs_Constraint> anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN);
-  anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
-  anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
-  anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
-
-  std::list<Slvs_Constraint>::const_iterator anEqIter = anEqualConstr.begin();
-  for (; anEqIter != anEqualConstr.end(); anEqIter++)
-    if (anEqIter->entityA == theEntity.h || anEqIter->entityB == theEntity.h) {
-      theEqual = *anEqIter;
-      return true;
-    }
-  return false;
-}
-