Salome HOME
Avoid crash if there is no parent of selection attribute.
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
index 6ed3e193fac98bdd0e4560a4b8cb0cf517050231..82b5dfab474f323c0f1634ec230e4b25c5f98cfa 100644 (file)
@@ -1,8 +1,22 @@
-// Name   : SketchAPI_Constraint.cpp
-// Purpose: 
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// 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<mailto:webmaster.salome@opencascade.com>
 //
-// History:
-// 08/08/16 - Artem ZHIDKOV - Creation of the file
 
 #include "SketchAPI_Constraint.h"
 
@@ -14,6 +28,8 @@
 #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>
@@ -68,6 +84,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;
@@ -139,11 +163,24 @@ void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
     if (!aFeature)
       return;
-    AttributeSelectionPtr aAttr = aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
+    AttributeSelectionPtr aAttr =
+      aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
       return;
   }
 
+  // Check all attributes are already dumped. If not, store the constraint as postponed.
+  bool areAttributesDumped = true;
+  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;
+  }
+
   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
   std::string aSetterSuffix;
   if (isAngle)
@@ -162,10 +199,21 @@ void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
     }
   }
 
-  AttributeDoublePtr aValueAttr = aBase->real(
-      isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
+  AttributeDoublePtr aValueAttr;
+  if (isAngle)
+    aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
+  else 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;
 }