Salome HOME
[EDF] (2023-T1) Sketch middle point constrain should create point if missing
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
index 9b0fb1827cdc663caf1a6422fdabf11942bf3b99..d69c35f884c2aaf7aae9354b360b9d30dbd4f505 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
+// 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
@@ -21,6 +21,8 @@
 
 #include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Tools.h>
+#include <SketchAPI_SketchEntity.h>
+#include <SketchAPI_Point.h>
 
 #include <SketchPlugin_Constraint.h>
 #include <SketchPlugin_ConstraintCoincidence.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)
@@ -89,6 +94,13 @@ 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()) {
@@ -189,35 +201,61 @@ void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
       return;
   }
 
-  // postpone constraint until all its attributes be dumped
-  if (!areAllAttributesDumped(theDumper))
-    return;
-
   const std::string& aSketchName = theDumper.parentName(aBase);
-  theDumper << aSketchName << "." << aSetter << "(";
 
-  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;
-  if (aBase->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
+    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;
+      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();
+    if (aBase->getKind() == SketchPlugin_ConstraintDistance::ID()) {
+      AttributeBooleanPtr isSigned = aBase->boolean(SketchPlugin_ConstraintDistance::SIGNED());
+      theDumper << ", " << isSigned->value();
+    }
+    theDumper << ")" << std::endl;
   }
 
-  theDumper << ")" << std::endl;
 }