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