Salome HOME
[EDF] (2023-T1) Sketch middle point constrain should create point if missing
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
index d8ca207c4c513beaadd3f9649d793691278fa9b6..d69c35f884c2aaf7aae9354b360b9d30dbd4f505 100644 (file)
@@ -1,20 +1,35 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-// Name   : SketchAPI_Constraint.cpp
-// Purpose: 
+// Copyright (C) 2014-2023  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
 //
-// History:
-// 08/08/16 - Artem ZHIDKOV - Creation of the file
 
 #include "SketchAPI_Constraint.h"
 
 #include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Tools.h>
+#include <SketchAPI_SketchEntity.h>
+#include <SketchAPI_Point.h>
 
 #include <SketchPlugin_Constraint.h>
-#include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_ConstraintCoincidence.h>
 #include <SketchPlugin_ConstraintCollinear.h>
 #include <SketchPlugin_ConstraintDistance.h>
+#include <SketchPlugin_ConstraintDistanceHorizontal.h>
+#include <SketchPlugin_ConstraintDistanceVertical.h>
 #include <SketchPlugin_ConstraintEqual.h>
 #include <SketchPlugin_ConstraintHorizontal.h>
 #include <SketchPlugin_ConstraintLength.h>
 #include <SketchPlugin_ConstraintTangent.h>
 #include <SketchPlugin_ConstraintVertical.h>
 #include <SketchPlugin_SketchEntity.h>
+#include <SketchPlugin_Point.h>
 
 #include <SketcherPrs_Tools.h>
 
+#include <limits>
+
 SketchAPI_Constraint::SketchAPI_Constraint(
     const std::shared_ptr<ModelAPI_Feature> & theFeature)
 : ModelHighAPI_Interface(theFeature)
@@ -51,16 +69,44 @@ bool SketchAPI_Constraint::initialize()
   return true;
 }
 
+void SketchAPI_Constraint::setEntityA(const ModelHighAPI_RefAttr& theEntity)
+{
+  fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_A()));
+}
+
+void SketchAPI_Constraint::setEntityB(const ModelHighAPI_RefAttr& theEntity)
+{
+  fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_B()));
+}
+
+void SketchAPI_Constraint::setEntityC(const ModelHighAPI_RefAttr& theEntity)
+{
+  fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_C()));
+}
+
+void SketchAPI_Constraint::setEntityD(const ModelHighAPI_RefAttr& theEntity)
+{
+  fillAttribute(theEntity, feature()->refattr(SketchPlugin_Constraint::ENTITY_D()));
+}
+
+void SketchAPI_Constraint::setValue(const ModelHighAPI_Double& theValue)
+{
+  fillAttribute(theValue, feature()->real(SketchPlugin_Constraint::VALUE()));
+}
+
+double SketchAPI_Constraint::value() const
+{
+  AttributeDoublePtr aValueAttr = feature()->real(SketchPlugin_Constraint::VALUE());
+  return aValueAttr->isInitialized() ? aValueAttr->value()
+                                     : std::numeric_limits<double>::quiet_NaN();
+}
+
 static const std::string& constraintTypeToSetter(const std::string& theType)
 {
   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
     static const std::string COINCIDENCE_SETTER("setCoincident");
     return COINCIDENCE_SETTER;
   }
-  if (theType == SketchPlugin_ConstraintAngle::ID()) {
-    static const std::string ANGLE_SETTER("setAngle");
-    return ANGLE_SETTER;
-  }
   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
     static const std::string COLLINEAR_SETTER("setCollinear");
     return COLLINEAR_SETTER;
@@ -69,6 +115,14 @@ static const std::string& constraintTypeToSetter(const std::string& theType)
     static const std::string DISTANCE_SETTER("setDistance");
     return DISTANCE_SETTER;
   }
+  if (theType == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
+    static const std::string DISTANCE_SETTER("setHorizontalDistance");
+    return DISTANCE_SETTER;
+  }
+  if (theType == SketchPlugin_ConstraintDistanceVertical::ID()) {
+    static const std::string DISTANCE_SETTER("setVerticalDistance");
+    return DISTANCE_SETTER;
+  }
   if (theType == SketchPlugin_ConstraintEqual::ID()) {
     static const std::string EQUAL_SETTER("setEqual");
     return EQUAL_SETTER;
@@ -114,17 +168,18 @@ static const std::string& constraintTypeToSetter(const std::string& theType)
   return DUMMY;
 }
 
-static std::string angleTypeToString(int theAngleType)
+bool SketchAPI_Constraint::areAllAttributesDumped(ModelHighAPI_Dumper& theDumper) const
 {
-  switch (theAngleType) {
-  case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
-    return std::string("Complementary");
-  case SketcherPrs_Tools::ANGLE_BACKWARD:
-    return std::string("Backward");
-  default:
-    break;
-  }
-  return std::string();
+  bool areAttributesDumped = true;
+  FeaturePtr aBase = feature();
+  for (int i = 0; i < CONSTRAINT_ATTR_SIZE && areAttributesDumped; ++i) {
+    AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+    if (aRefAttr && aRefAttr->isInitialized())
+      areAttributesDumped = theDumper.isDumped(aRefAttr);
+  }
+  if (!areAttributesDumped)
+    theDumper.postpone(aBase);
+  return areAttributesDumped;
 }
 
 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
@@ -140,34 +195,67 @@ void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
     if (!aFeature)
       return;
-    AttributeSelectionPtr aAttr = 
+    AttributeSelectionPtr aAttr =
       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
       return;
   }
 
-  bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
-  std::string aSetterSuffix;
-  if (isAngle)
-    aSetterSuffix = angleTypeToString(aBase->integer(
-                    SketchPlugin_ConstraintAngle::TYPE_ID())->value());
-
   const std::string& aSketchName = theDumper.parentName(aBase);
-  theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
 
-  bool isFirstAttr = true;
-  for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
-    AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+  // Dump middle constraint by object
+  if (SketchPlugin_ConstraintMiddle::ID() == aBase->getKind() &&
+    aBase->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE())->value() == SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE())
+  {
+    FeaturePtr aFeature;
+    AttributeRefAttrPtr aPointRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(1));
+    AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(0));
+
+    if (!theDumper.isDumped(aRefAttr))
+    {
+      theDumper.postpone(aBase);
+      return;
+    }
+    aFeature = ModelAPI_Feature::feature(aPointRefAttr->object());
+
+    theDumper.name(aFeature, false, true, true); // mark point as dumped
+
+    theDumper << theDumper.name(aFeature) << " = " << aSketchName << "." << aSetter << "(";
     if (aRefAttr && aRefAttr->isInitialized()) {
-      theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
-      isFirstAttr = false;
+      theDumper << aRefAttr;
     }
+    theDumper << ")" << std::endl;
   }
+  else
+  {
+    // postpone constraint until all its attributes be dumped
+    if (!areAllAttributesDumped(theDumper))
+      return;
 
-  AttributeDoublePtr aValueAttr = aBase->real(
-      isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
-  if (aValueAttr && aValueAttr->isInitialized())
-    theDumper << ", " << aValueAttr;
+    theDumper << aSketchName << "." << aSetter << "(";
+    bool isFirstAttr = true;
+    for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+      AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+      if (aRefAttr && aRefAttr->isInitialized()) {
+        theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
+        isFirstAttr = false;
+      }
+    }
+
+    AttributeDoublePtr aValueAttr;
+    if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
+      aBase->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
+      aValueAttr = aBase->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
+    else
+      aValueAttr = aBase->real(SketchPlugin_Constraint::VALUE());
+    if (aValueAttr && aValueAttr->isInitialized())
+      theDumper << ", " << aValueAttr;
+
+    if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
+      AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
+      theDumper << ", " << isSigned->value();
+    }
+    theDumper << ")" << std::endl;
+  }
 
-  theDumper << ")" << std::endl;
 }