Salome HOME
[CEA] Improve ShaperResults
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
index 1490a091cc6403b7bde5b41d86ca58c4666fc316..95877d61025934a9d862d6767870dad36627dd89 100644 (file)
-// File:    SketchSolver_Constraint.cpp
-// Created: 27 May 2014
-// Author:  Artem ZHIDKOV
-
-#include "SketchSolver_Constraint.h"
-#include <SketchSolver_Solver.h>
+// Copyright (C) 2014-2024  CEA, EDF
+//
+// 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_Error.h>
+
+#include <PlaneGCSSolver_AttributeBuilder.h>
+#include <PlaneGCSSolver_Tools.h>
 
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Point.h>
-#include <SketchPlugin_Circle.h>
-#include <SketchPlugin_Arc.h>
+
+#include <SketchPlugin_ConstraintAngle.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_ConstraintCoincidenceInternal.h>
+#include <SketchPlugin_ConstraintCollinear.h>
 #include <SketchPlugin_ConstraintDistance.h>
+#include <SketchPlugin_ConstraintEqual.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
 #include <SketchPlugin_ConstraintLength.h>
+#include <SketchPlugin_ConstraintMiddle.h>
+#include <SketchPlugin_ConstraintMirror.h>
 #include <SketchPlugin_ConstraintParallel.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
 #include <SketchPlugin_ConstraintRadius.h>
+#include <SketchPlugin_ConstraintRigid.h>
+#include <SketchPlugin_ConstraintTangent.h>
+#include <SketchPlugin_ConstraintVertical.h>
 
-#include <ModelAPI_AttributeRefAttr.h>
-#include <ModelAPI_Data.h>
-
+#include <GeomAPI_Dir2d.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_ResultConstruction.h>
 
+#include <math.h>
 
-SketchSolver_Constraint::SketchSolver_Constraint()
-  : myConstraint(boost::shared_ptr<SketchPlugin_Constraint>()),
-    myType(SLVS_C_UNKNOWN),
-    myAttributesList()
+SketchSolver_Constraint::SketchSolver_Constraint(
+    ConstraintPtr  theConstraint)
+  : myBaseConstraint(theConstraint),
+    myType(CONSTRAINT_UNKNOWN)
 {
 }
 
-SketchSolver_Constraint::SketchSolver_Constraint(
-                boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
-  : myConstraint(theConstraint),
-    myAttributesList()
+void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
 {
-  myType = getType(myConstraint);
+  myStorage = theStorage;
+  blockEvents(theEvensBlocked);
+  // Process constraint according to its type
+  process();
 }
 
-const int& SketchSolver_Constraint::getType(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
+void SketchSolver_Constraint::blockEvents(bool isBlocked)
 {
-  myType = SLVS_C_UNKNOWN;
-  if (!theConstraint)
-    return getType();
-
-  // Assign empty names of attributes
-  myAttributesList.clear();
-  for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
-    myAttributesList.push_back(std::string());
-
-  const std::string& aConstraintKind = theConstraint->getKind();
-  // Constraint for coincidence of two points
-  if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
-  {
-    int anAttrPos = 0;
-    // Verify the constraint has only two attributes and they are points
-    int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
-    int aPt3d = 0; // bit-mapped field, the same information for 3D points
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
-    {
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
-        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-          theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
-        );
-      if (!anAttr) continue;
-      // Verify the attribute is a 2D point
-      boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
-      if (aPoint2D)
-      {
-        aPt2d |= (1 << indAttr);
-        myAttributesList[anAttrPos++] = CONSTRAINT_ATTRIBUTES[indAttr];
-        continue;
-      }
-      // Verify the attribute is a 3D point
-      boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
-      if (aPoint3D)
-      {
-        aPt3d |= (1 << indAttr);
-        myAttributesList[anAttrPos++] = CONSTRAINT_ATTRIBUTES[indAttr];
-        continue;
-      }
-      // Attribute neither 2D nor 3D point is not supported by this type of constraint
-      return getType();
-    }
-    // The constrained points should be in first and second positions,
-    // so the expected value of aPt2d or aPt3d is 3
-    if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
-      myType = SLVS_C_POINTS_COINCIDENT;
-    // Constraint parameters are wrong
-    return getType();
+  myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
+}
+
+
+SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
+{
+  const std::string& aType = theConstraint->getKind();
+  if (aType == SketchPlugin_ConstraintCoincidence::ID() ||
+      aType == SketchPlugin_ConstraintCoincidenceInternal::ID())
+    return CONSTRAINT_COINCIDENCE;
+  else if (aType == SketchPlugin_ConstraintRigid::ID())
+    return CONSTRAINT_FIXED;
+  else if (aType == SketchPlugin_ConstraintHorizontal::ID())
+    return CONSTRAINT_HORIZONTAL;
+  else if (aType == SketchPlugin_ConstraintVertical::ID())
+    return CONSTRAINT_VERTICAL;
+  else if (aType == SketchPlugin_ConstraintAngle::ID())
+    return CONSTRAINT_ANGLE;
+  else if (aType == SketchPlugin_ConstraintDistance::ID())
+    return CONSTRAINT_DISTANCE;
+  else if (aType == SketchPlugin_ConstraintEqual::ID())
+    return CONSTRAINT_EQUAL;
+  else if (aType == SketchPlugin_ConstraintLength::ID())
+    return CONSTRAINT_PT_PT_DISTANCE;
+  else if (aType == SketchPlugin_ConstraintMirror::ID())
+    return CONSTRAINT_SYMMETRIC;
+  else if (aType == SketchPlugin_ConstraintParallel::ID())
+    return CONSTRAINT_PARALLEL;
+  else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
+    return CONSTRAINT_PERPENDICULAR;
+  else if (aType == SketchPlugin_ConstraintRadius::ID())
+    return CONSTRAINT_RADIUS;
+  else if (aType == SketchPlugin_ConstraintTangent::ID())
+    return CONSTRAINT_TANGENT;
+  else if (aType == SketchPlugin_ConstraintCollinear::ID())
+    return CONSTRAINT_COLLINEAR;
+  else if (aType == SketchPlugin_ConstraintMiddle::ID())
+    return CONSTRAINT_MIDDLE_POINT;
+  return CONSTRAINT_UNKNOWN;
+}
+
+void SketchSolver_Constraint::process()
+{
+  cleanErrorMsg();
+  if (!myBaseConstraint || !myStorage) {
+    // Not enough parameters are assigned
+    return;
   }
 
-  // Constraint for distance between point and another entity
-  if (aConstraintKind.compare(SKETCH_CONSTRAINT_DISTANCE_KIND) == 0)
-  {
-    int aNbPoints = 0;
-    int aNbEntities = 0;
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
-    {
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
-        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-          theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
-        );
-      if (!anAttr) continue;
-      if (anAttr->isObject() && anAttr->object())
-      { // verify posiible entities
-        const std::string& aKind = boost::dynamic_pointer_cast<ModelAPI_Feature>
-          (anAttr->object())->getKind();
-        if (aKind.compare(SKETCH_POINT_KIND) == 0)
-        {
-          myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr];
-          continue;
-        }
-        else if(aKind.compare(SKETCH_LINE_KIND) == 0)
-        {
-          // entities are placed starting from CONSTRAINT_ATTR_ENTITY_C attribute
-          myAttributesList[2 + aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr];
-          myType = SLVS_C_PT_LINE_DISTANCE;
-          continue;
-        }
-      }
-      else
-      { // verify points
-        // Verify the attribute is a 2D point
-        boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
-          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
-        if (aPoint2D)
-        {
-          myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr];
-          continue;
-        }
-        // Verify the attribute is a 3D point
-        boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
-          boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
-        if (aPoint3D)
-        {
-          myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr];
-          continue;
-        }
-      }
-    }
-    // Verify the correctness of constraint arguments
-    if (aNbPoints == 2 && aNbEntities ==0)
-      myType = SLVS_C_PT_PT_DISTANCE;
-    else if (aNbPoints == 1 && aNbEntities == 1)
-      myType = SLVS_C_UNKNOWN;
-    return getType();
+  SketchSolver_ConstraintType aConstrType = getType();
+  EntityWrapperPtr aValue;
+  std::vector<EntityWrapperPtr> anAttributes;
+  getAttributes(aValue, anAttributes);
+  if (!myErrorMsg.empty())
+    return;
+  if (anAttributes.empty()) {
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+    return;
+  }
+  if (aConstrType == CONSTRAINT_UNKNOWN)
+    aConstrType = getType();
+
+  ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
+      myBaseConstraint, aConstrType,
+      aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
+  if (!aNewConstraint) {
+    myErrorMsg = SketchSolver_Error::WRONG_CONSTRAINT_TYPE();
+    return;
   }
+  myStorage->addConstraint(myBaseConstraint, aNewConstraint);
 
-  // Constraint for the given length of a line
-  if (aConstraintKind.compare(SKETCH_CONSTRAINT_LENGTH_KIND) == 0)
-  {
-    int aNbLines = 0;
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
-    {
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
-        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-          theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
-        );
-      if (!anAttr) continue;
-      if (anAttr->isObject() && anAttr->object() &&
-        boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object())->getKind().
-        compare(SKETCH_LINE_KIND) == 0)
-      {
-        myAttributesList[aNbLines++] = CONSTRAINT_ATTRIBUTES[indAttr];
-        break;
+  adjustConstraint();
+}
+
+void SketchSolver_Constraint::update()
+{
+  cleanErrorMsg();
+
+  // 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;
+
+    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())));
       }
-    }
-    if (aNbLines == 1)
-      myType = SLVS_C_PT_PT_DISTANCE;
-    return getType();
+    } else
+      anAttributes.insert(myStorage->entity(anAttr));
   }
 
-  // Constraint for two parallel/perpendicular lines
-  bool isParallel = (aConstraintKind.compare(SKETCH_CONSTRAINT_PARALLEL_KIND) == 0);
-  bool isPerpendicular = (aConstraintKind.compare(SKETCH_CONSTRAINT_PERPENDICULAR_KIND) == 0);
-  if (isParallel || isPerpendicular)
-  {
-    int aNbEntities = 2; // lines in SolveSpace constraints should started from CONSTRAINT_ATTR_ENTITY_C attribute
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
-    {
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
-        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-          theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
-        );
-      if (!anAttr || !anAttr->isObject() || !anAttr->object()) continue;
-      const std::string& aKind = boost::dynamic_pointer_cast<ModelAPI_Feature>
-        (anAttr->object())->getKind();
-      if (aKind.compare(SKETCH_LINE_KIND) == 0)
-      {
-        myAttributesList[aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr];
-        continue;
-      }
-    }
-    if (aNbEntities == 4)
-      myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR;
-    return getType();
+  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;
   }
 
-  // Constraint for radius of a circle or an arc of circle
-  if (aConstraintKind.compare(SKETCH_CONSTRAINT_RADIUS_KIND) == 0)
-  {
-    int aNbEntities = 2; // lines in SolveSpace constraints should started from CONSTRAINT_ATTR_ENTITY_C attribute
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
-    {
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
-        boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-          theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
-        );
-      if (!anAttr || !anAttr->isObject() || !anAttr->object()) continue;
-      const std::string& aKind = boost::dynamic_pointer_cast<ModelAPI_Feature>
-        (anAttr->object())->getKind();
-      if (aKind.compare(SKETCH_CIRCLE_KIND) == 0 || aKind.compare(SKETCH_ARC_KIND) == 0)
-      {
-        myAttributesList[aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr];
-        continue;
-      }
-    }
-    if (aNbEntities == 3)
-      myType = SLVS_C_DIAMETER;
-    return getType();
+  AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
+  if (aValueAttr)
+    myStorage->update(aValueAttr);
+
+  adjustConstraint();
+}
+
+bool SketchSolver_Constraint::remove()
+{
+  cleanErrorMsg();
+  myType = CONSTRAINT_UNKNOWN;
+  myStorage->unsubscribeUpdates(this);
+  return myStorage->removeConstraint(myBaseConstraint);
+}
+
+void SketchSolver_Constraint::getAttributes(
+    EntityWrapperPtr& theValue,
+    std::vector<EntityWrapperPtr>& theAttributes)
+{
+  static const int anInitNbOfAttr = 4;
+  theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
+  myAttributes.clear();
+
+  DataPtr aData = myBaseConstraint->data();
+
+  myType = TYPE(myBaseConstraint);
+
+  AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
+  if (aValueAttr) {
+    PlaneGCSSolver_AttributeBuilder aValueBuilder;
+    theValue = aValueBuilder.createAttribute(aValueAttr);
+    myStorage->addEntity(aValueAttr, theValue);
   }
 
-  /// \todo Implement other kind of constrtaints
+  int aPtInd = 0; // index of first point in the list of attributes
+  int aEntInd = 2; // index of first entity in the list of attributes
+  std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
+  std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
+  for (; anIter != aConstrAttrs.end(); anIter++) {
+    AttributeRefAttrPtr aRefAttr =
+        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
+    if (!aRefAttr || !aRefAttr->isInitialized()) {
+      myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
+      return;
+    }
 
-  return getType();
+    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 || aType == ENTITY_POINT_ARRAY)
+      theAttributes[aPtInd++] = anEntity; // the point is created
+    else { // another entity (not a point) is created
+      if (aEntInd < anInitNbOfAttr)
+        theAttributes[aEntInd] = anEntity;
+      else
+        theAttributes.push_back(anEntity);
+      aEntInd++;
+    }
+  }
 }