]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/SketchSolver/SketchSolver_ConstraintDistance.cpp
Salome HOME
Revert "First phase of SketchSolver refactoring"
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
index e82568d3ef089f60cd569a3c5895a3bc565dab25..63bd0ae7ddbd2c3d2f8bffb7a278a8d245cda1bd 100644 (file)
 #include <SketchSolver_ConstraintDistance.h>
+#include <SketchSolver_Group.h>
 #include <SketchSolver_Error.h>
-#include <SketchSolver_Manager.h>
 
-#include <GeomAPI_Dir2d.h>
-#include <GeomAPI_Lin2d.h>
-#include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_XY.h>
 
 #include <math.h>
 
 
-void SketchSolver_ConstraintDistance::getAttributes(
-    double& theValue,
-    std::vector<EntityWrapperPtr>& theAttributes)
+void SketchSolver_ConstraintDistance::process()
 {
-  SketchSolver_Constraint::getAttributes(theValue, theAttributes);
-  if (!myErrorMsg.empty() || !theAttributes[0]) {
-    theAttributes.clear();
+  cleanErrorMsg();
+  if (!myBaseConstraint || !myStorage || myGroup == 0) {
+    /// TODO: Put error message here
     return;
   }
+  if (!mySlvsConstraints.empty()) // some data is changed, update constraint
+    update(myBaseConstraint);
 
-  if (theAttributes[1])
-    myType = CONSTRAINT_PT_PT_DISTANCE;
-  else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
-    myType = CONSTRAINT_PT_LINE_DISTANCE;
-  else
-    theAttributes.clear();
+  double aValue;
+  std::vector<Slvs_hEntity> anEntities;
+  getAttributes(aValue, anEntities);
+  if (!myErrorMsg.empty())
+    return;
+
+  // Obtain entities to identify the type of distance
+  static const int aNbPoints = 2;
+  Slvs_hEntity aPoint[aNbPoints] = {SLVS_E_UNKNOWN, SLVS_E_UNKNOWN};
+  Slvs_hEntity aLine = SLVS_E_UNKNOWN;
+  myType = SLVS_C_PT_PT_DISTANCE;
+  int aPtPos = 0;
+  std::vector<Slvs_hEntity>::iterator anEntIter = anEntities.begin();
+  for (; anEntIter != anEntities.end(); anEntIter++) {
+    if (*anEntIter == SLVS_E_UNKNOWN)
+      continue;
+    Slvs_Entity anEnt = myStorage->getEntity(*anEntIter);
+    if (anEnt.type == SLVS_E_POINT_IN_2D || anEnt.type == SLVS_E_POINT_IN_3D) {
+      if (aPtPos >= aNbPoints) {
+        myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+        return;
+      }
+      aPoint[aPtPos++] = *anEntIter;
+    }
+    else if (anEnt.type == SLVS_E_LINE_SEGMENT) {
+      if (myType == SLVS_C_PT_LINE_DISTANCE) {
+        myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+        return;
+      }
+      aLine = *anEntIter;
+      myType = SLVS_C_PT_LINE_DISTANCE;
+    }
+  }
+
+  Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(),
+      getType(), myGroup->getWorkplaneId(), aValue, aPoint[0], aPoint[1], aLine, SLVS_E_UNKNOWN);
+  aConstraint.h = myStorage->addConstraint(aConstraint);
+  mySlvsConstraints.push_back(aConstraint.h);
 
   myPrevValue = 0.0;
+  adjustConstraint();
 }
 
 void SketchSolver_ConstraintDistance::adjustConstraint()
 {
   // Adjust point-line distance
-  if (getType() != CONSTRAINT_PT_LINE_DISTANCE)
+  if (getType() != SLVS_C_PT_LINE_DISTANCE)
     return;
 
   // Check the sign of distance is changed
-  ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
-  if (fabs(myPrevValue) == fabs(aConstraint->value())) {
-    aConstraint->setValue(myPrevValue);
-    myStorage->addConstraint(myBaseConstraint, aConstraint);
+  Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
+  if (fabs(myPrevValue) == fabs(aConstraint.valA)) {
+    aConstraint.valA = myPrevValue;
+    myStorage->updateConstraint(aConstraint);
     return;
   }
 
   // Get constraint parameters and check the sign of constraint value
-  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-  std::shared_ptr<GeomAPI_Pnt2d> aPoint;
-  std::shared_ptr<GeomAPI_Lin2d> aLine;
-  std::list<EntityWrapperPtr> aSubs = aConstraint->entities();
-  std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
-  for (; aSIt != aSubs.end(); ++aSIt) {
-    if ((*aSIt)->type() == ENTITY_POINT)
-      aPoint = aBuilder->point(*aSIt);
-    else if ((*aSIt)->type() == ENTITY_LINE)
-      aLine = aBuilder->line(*aSIt);
-  }
-
-  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
-  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
-  if (aPtLineVec->cross(aLineVec) * aConstraint->value() < 0.0 || myIsNegative) {
-    aConstraint->setValue(aConstraint->value() * (-1.0));
-    myStorage->addConstraint(myBaseConstraint, aConstraint);
-    myIsNegative = true;
+  std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
+  for (; aCIter != mySlvsConstraints.end(); aCIter++) {
+    aConstraint = myStorage->getConstraint(*aCIter);
+    Slvs_Entity aLine = myStorage->getEntity(aConstraint.entityA);
+    // Obtain point and line coordinates
+    Slvs_hEntity aPointID[3] = {aConstraint.ptA, aLine.point[0], aLine.point[1]};
+    std::shared_ptr<GeomAPI_XY> aPoints[3];
+    for (int i = 0; i < 3; i++) {
+      Slvs_Entity aPoint = myStorage->getEntity(aPointID[i]);
+      Slvs_Param aParams[2] = {
+          myStorage->getParameter(aPoint.param[0]),
+          myStorage->getParameter(aPoint.param[1])};
+      aPoints[i] = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(aParams[0].val, aParams[1].val));
+    }
+    std::shared_ptr<GeomAPI_XY> aLineVec = aPoints[2]->decreased(aPoints[1]);
+    std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoints[0]->decreased(aPoints[1]);
+    if (aPtLineVec->cross(aLineVec) * aConstraint.valA < 0.0 || myIsNegative) {
+      aConstraint.valA *= -1.0;
+      myStorage->updateConstraint(aConstraint);
+      myIsNegative = true;
+    }
   }
 }
 
-void SketchSolver_ConstraintDistance::update()
+void SketchSolver_ConstraintDistance::update(ConstraintPtr theConstraint)
 {
-  ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
-  myPrevValue = aConstraint->value();
-
-  SketchSolver_Constraint::update();
+  Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
+  myPrevValue = aConstraint.valA;
+  SketchSolver_Constraint::update(theConstraint);
 }