Salome HOME
Issue #2071 Fatal error when Create box
[modules/shaper.git] / src / SketchAPI / SketchAPI_Constraint.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : SketchAPI_Constraint.cpp
3 // Purpose:
4 //
5 // History:
6 // 08/08/16 - Artem ZHIDKOV - Creation of the file
7
8 #include "SketchAPI_Constraint.h"
9
10 #include <ModelHighAPI_Dumper.h>
11 #include <ModelHighAPI_Tools.h>
12
13 #include <SketchPlugin_Constraint.h>
14 #include <SketchPlugin_ConstraintAngle.h>
15 #include <SketchPlugin_ConstraintCoincidence.h>
16 #include <SketchPlugin_ConstraintCollinear.h>
17 #include <SketchPlugin_ConstraintDistance.h>
18 #include <SketchPlugin_ConstraintEqual.h>
19 #include <SketchPlugin_ConstraintHorizontal.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintMiddle.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintVertical.h>
28 #include <SketchPlugin_SketchEntity.h>
29
30 #include <SketcherPrs_Tools.h>
31
32 SketchAPI_Constraint::SketchAPI_Constraint(
33     const std::shared_ptr<ModelAPI_Feature> & theFeature)
34 : ModelHighAPI_Interface(theFeature)
35 {
36   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
37   if (aConstraint)
38     initialize();
39 }
40
41 SketchAPI_Constraint::~SketchAPI_Constraint()
42 {
43 }
44
45 bool SketchAPI_Constraint::initialize()
46 {
47   if (!feature()) {
48     throwException("Constraint exception: The feature is NULL.");
49     return false;
50   }
51   return true;
52 }
53
54 static const std::string& constraintTypeToSetter(const std::string& theType)
55 {
56   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
57     static const std::string COINCIDENCE_SETTER("setCoincident");
58     return COINCIDENCE_SETTER;
59   }
60   if (theType == SketchPlugin_ConstraintAngle::ID()) {
61     static const std::string ANGLE_SETTER("setAngle");
62     return ANGLE_SETTER;
63   }
64   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
65     static const std::string COLLINEAR_SETTER("setCollinear");
66     return COLLINEAR_SETTER;
67   }
68   if (theType == SketchPlugin_ConstraintDistance::ID()) {
69     static const std::string DISTANCE_SETTER("setDistance");
70     return DISTANCE_SETTER;
71   }
72   if (theType == SketchPlugin_ConstraintEqual::ID()) {
73     static const std::string EQUAL_SETTER("setEqual");
74     return EQUAL_SETTER;
75   }
76   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
77     static const std::string HORIZONTAL_SETTER("setHorizontal");
78     return HORIZONTAL_SETTER;
79   }
80   if (theType == SketchPlugin_ConstraintLength::ID()) {
81     static const std::string LENGTH_SETTER("setLength");
82     return LENGTH_SETTER;
83   }
84   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
85     static const std::string MIDDLE_SETTER("setMiddlePoint");
86     return MIDDLE_SETTER;
87   }
88   if (theType == SketchPlugin_ConstraintParallel::ID()) {
89     static const std::string PARALLEL_SETTER("setParallel");
90     return PARALLEL_SETTER;
91   }
92   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
93     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
94     return PERPENDICULAR_SETTER;
95   }
96   if (theType == SketchPlugin_ConstraintRadius::ID()) {
97     static const std::string RADIUS_SETTER("setRadius");
98     return RADIUS_SETTER;
99   }
100   if (theType == SketchPlugin_ConstraintRigid::ID()) {
101     static const std::string FIXED_SETTER("setFixed");
102     return FIXED_SETTER;
103   }
104   if (theType == SketchPlugin_ConstraintTangent::ID()) {
105     static const std::string TANGENT_SETTER("setTangent");
106     return TANGENT_SETTER;
107   }
108   if (theType == SketchPlugin_ConstraintVertical::ID()) {
109     static const std::string VERTICAL_SETTER("setVertical");
110     return VERTICAL_SETTER;
111   }
112
113   static const std::string DUMMY;
114   return DUMMY;
115 }
116
117 static std::string angleTypeToString(int theAngleType)
118 {
119   switch (theAngleType) {
120   case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
121     return std::string("Complementary");
122   case SketcherPrs_Tools::ANGLE_BACKWARD:
123     return std::string("Backward");
124   default:
125     break;
126   }
127   return std::string();
128 }
129
130 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
131 {
132   FeaturePtr aBase = feature();
133   const std::string& aSetter = constraintTypeToSetter(aBase->getKind());
134   if (aSetter.empty())
135     return; // incorrect constraint type
136
137   // do not need to dump "Fixed" constraint for external object
138   if (aBase->getKind() == SketchPlugin_ConstraintRigid::ID()) {
139     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ENTITY_A());
140     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
141     if (!aFeature)
142       return;
143     AttributeSelectionPtr aAttr =
144       aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
145     if (aAttr && aAttr->context().get() != NULL && !aAttr->isInvalid())
146       return;
147   }
148
149   bool isAngle = aBase->getKind() == SketchPlugin_ConstraintAngle::ID();
150   std::string aSetterSuffix;
151   if (isAngle)
152     aSetterSuffix = angleTypeToString(aBase->integer(
153                     SketchPlugin_ConstraintAngle::TYPE_ID())->value());
154
155   const std::string& aSketchName = theDumper.parentName(aBase);
156   theDumper << aBase << " = " << aSketchName << "." << aSetter << aSetterSuffix << "(";
157
158   bool isFirstAttr = true;
159   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
160     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
161     if (aRefAttr && aRefAttr->isInitialized()) {
162       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
163       isFirstAttr = false;
164     }
165   }
166
167   AttributeDoublePtr aValueAttr = aBase->real(
168       isAngle ? SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID() :SketchPlugin_Constraint::VALUE());
169   if (aValueAttr && aValueAttr->isInitialized())
170     theDumper << ", " << aValueAttr;
171
172   theDumper << ")" << std::endl;
173 }