Salome HOME
Pipe validator fix
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintTangent.cpp
index 7ec41bc79c0ff66fa18a1d60abf10bcc70fd5774..299ed53fa94356949d8cf004d6be036f5f7f79f8 100644 (file)
@@ -3,6 +3,9 @@
 #include <SketchSolver_Manager.h>
 
 #include <GeomAPI_Pnt2d.h>
+#include <SketchPlugin_Circle.h>
+
+#include <cmath>
 
 
 /// \brief Check whether the entities has only one shared point
@@ -21,8 +24,12 @@ static bool hasSingleCoincidence(EntityWrapperPtr theEntity1, EntityWrapperPtr t
   int aNbCoinc = 0;
   std::list<EntityWrapperPtr>::const_iterator anIt1, anIt2;
   for (anIt1 = aStartIt1; anIt1 != aPoints1.end(); ++anIt1) {
+    if ((*anIt1)->type() != ENTITY_POINT)
+      continue;
     std::shared_ptr<GeomAPI_Pnt2d> aPt1 = aBuilder->point(*anIt1);
     for (anIt2 = aStartIt2; anIt2 != aPoints2.end(); ++anIt2) {
+      if ((*anIt2)->type() != ENTITY_POINT)
+        continue;
       std::shared_ptr<GeomAPI_Pnt2d> aPt2 = aBuilder->point(*anIt2);
       if (aPt1->distance(aPt2) < tolerance)
         ++aNbCoinc;
@@ -45,6 +52,7 @@ void SketchSolver_ConstraintTangent::getAttributes(
   // Check the quantity of entities of each type and their order (arcs first)
   int aNbLines = 0;
   int aNbArcs = 0;
+  int aNbCircles = 0;
   bool isSwap = false; // whether need to swap arguments (arc goes before line)
   std::vector<EntityWrapperPtr>::iterator anEntIt = theAttributes.begin() + 2;
   for (; anEntIt != theAttributes.end(); ++anEntIt) {
@@ -54,14 +62,22 @@ void SketchSolver_ConstraintTangent::getAttributes(
       ++aNbArcs;
       isSwap = aNbLines > 0;
     }
+    else if ((*anEntIt)->type() == ENTITY_CIRCLE) {
+      ++aNbCircles;
+      isSwap = aNbLines > 0;
+    }
   }
 
-  if (aNbArcs < 1) {
+  if (aNbArcs < 1 && aNbCircles < 1) {
     myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
     return;
   }
-  if (aNbLines == 1 && aNbArcs == 1)
-    myType = CONSTRAINT_TANGENT_ARC_LINE;
+  if (aNbLines == 1) {
+    if (aNbArcs == 1)
+      myType = CONSTRAINT_TANGENT_ARC_LINE;
+    else if (aNbCircles == 1)
+      myType = CONSTRAINT_TANGENT_CIRCLE_LINE;
+  }
   else if (aNbArcs == 2)
     myType = CONSTRAINT_TANGENT_ARC_ARC;
   else {
@@ -69,8 +85,9 @@ void SketchSolver_ConstraintTangent::getAttributes(
     return;
   }
 
-  if (!hasSingleCoincidence(theAttributes[2], theAttributes[3]))
-    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+  if (myType == CONSTRAINT_TANGENT_ARC_LINE && 
+      !hasSingleCoincidence(theAttributes[2], theAttributes[3]))
+    myErrorMsg = SketchSolver_Error::TANGENCY_FAILED();
 
   if (isSwap) {
     EntityWrapperPtr aTemp = theAttributes[2];
@@ -79,83 +96,26 @@ void SketchSolver_ConstraintTangent::getAttributes(
   }
 }
 
+void SketchSolver_ConstraintTangent::adjustConstraint()
+{
+  if (myType != CONSTRAINT_TANGENT_CIRCLE_LINE)
+    return;
 
-////void SketchSolver_ConstraintTangent::process()
-////{
-////  cleanErrorMsg();
-////  if (!myBaseConstraint || !myStorage || myGroup == 0) {
-////    /// TODO: Put error message here
-////    return;
-////  }
-////  if (!mySlvsConstraints.empty()) // some data is changed, update constraint
-////    update(myBaseConstraint);
-////
-////  double aValue;
-////  std::vector<Slvs_hEntity> anEntID;
-////  getAttributes(aValue, anEntID);
-////  if (!myErrorMsg.empty())
-////    return;
-////  // Check the quantity of entities of each type and their order (arcs first)
-////  int aNbLines = 0;
-////  int aNbArcs = 0;
-////  Slvs_Entity anEntities[2];
-////  myType = SLVS_C_CURVE_CURVE_TANGENT;
-////  std::vector<Slvs_hEntity>::iterator anEntIter = anEntID.begin();
-////  for (; anEntIter != anEntID.end(); anEntIter++) {
-////    Slvs_Entity anEnt = myStorage->getEntity(*anEntIter);
-////    if (anEnt.type == SLVS_E_LINE_SEGMENT) {
-////      if (aNbLines == 0)
-////        anEntities[1 + aNbLines] = anEnt;
-////      aNbLines++;
-////      myType = SLVS_C_ARC_LINE_TANGENT;
-////    }
-////    else if (anEnt.type == SLVS_E_ARC_OF_CIRCLE) {
-////      if (aNbArcs < 2)
-////        anEntities[aNbArcs] = anEnt;
-////      aNbArcs++;
-////    }
-////  }
-////
-////  if (aNbLines + aNbArcs != 2) {
-////    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
-////    return;
-////  } else if (aNbArcs < 1) {
-////    myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
-////    return;
-////  }
-////
-////  // It is necessary to identify which points of entities are coincident
-////  int aSlvsOtherFlag = 0;
-////  int aSlvsOther2Flag = 0;
-////  // Obtain start and end points of entities
-////  Slvs_hEntity aPointsToFind[4];
-////  for (int i = 0; i < 2; i++) {
-////    int aShift = anEntities[i].type == SLVS_E_ARC_OF_CIRCLE ? 1 : 0;
-////    aPointsToFind[2*i]  = anEntities[i].point[aShift];
-////    aPointsToFind[2*i+1]= anEntities[i].point[aShift+1];
-////  }
-////  // Search coincident points
-////  bool isPointFound = false;
-////  for (int i = 0; i < 2 && !isPointFound; i++)
-////    for (int j = 2; j < 4 && !isPointFound; j++)
-////      if (myStorage->isEqual(aPointsToFind[i], aPointsToFind[j])) {
-////        aSlvsOtherFlag = i;
-////        aSlvsOther2Flag = j - 2;
-////        isPointFound = true;
-////      }
-////  if (!isPointFound) {
-////    // There is no coincident points between tangential objects. Generate error message
-////    myErrorMsg = SketchSolver_Error::NO_COINCIDENT_POINTS();
-////    return;
-////  }
-////
-////  Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(),
-////      getType(), myGroup->getWorkplaneId(), aValue,
-////      SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, anEntities[0].h, anEntities[1].h);
-////  aConstraint.other = aSlvsOtherFlag;
-////  aConstraint.other2 = aSlvsOther2Flag;
-////  aConstraint.h = myStorage->addConstraint(aConstraint);
-////  mySlvsConstraints.push_back(aConstraint.h);
-////  adjustConstraint();
-////}
+  ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
+  AttributePtr aCircleCenter = aConstraint->entities().front()->baseAttribute();
+  if (!aCircleCenter)
+    return;
+  FeaturePtr aCircle = ModelAPI_Feature::feature(aCircleCenter->owner());
+  AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      aCircle->attribute(SketchPlugin_Circle::RADIUS_ID()));
+
+  if (fabs(aRadius->value()) == fabs(aConstraint->value()))
+    return;
 
+  aConstraint->setValue(aRadius->value());
+
+  // Adjust the sign of constraint value
+  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+  aBuilder->adjustConstraint(aConstraint);
+  myStorage->addConstraint(myBaseConstraint, aConstraint);
+}