Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[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
28 SketchAPI_Constraint::SketchAPI_Constraint(
29     const std::shared_ptr<ModelAPI_Feature> & theFeature)
30 : ModelHighAPI_Interface(theFeature)
31 {
32   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
33   if (aConstraint)
34     initialize();
35 }
36
37 SketchAPI_Constraint::~SketchAPI_Constraint()
38 {
39 }
40
41 bool SketchAPI_Constraint::initialize()
42 {
43   if (!feature()) {
44     throwException("Constraint exception: The feature is NULL.");
45     return false;
46   }
47   return true;
48 }
49
50 static const std::string& constraintTypeToSetter(const std::string& theType)
51 {
52   if (theType == SketchPlugin_ConstraintCoincidence::ID()) {
53     static const std::string COINCIDENCE_SETTER("setCoincident");
54     return COINCIDENCE_SETTER;
55   }
56   if (theType == SketchPlugin_ConstraintAngle::ID()) {
57     static const std::string ANGLE_SETTER("setAngle");
58     return ANGLE_SETTER;
59   }
60   if (theType == SketchPlugin_ConstraintCollinear::ID()) {
61     static const std::string COLLINEAR_SETTER("setCollinear");
62     return COLLINEAR_SETTER;
63   }
64   if (theType == SketchPlugin_ConstraintDistance::ID()) {
65     static const std::string DISTANCE_SETTER("setDistance");
66     return DISTANCE_SETTER;
67   }
68   if (theType == SketchPlugin_ConstraintEqual::ID()) {
69     static const std::string EQUAL_SETTER("setEqual");
70     return EQUAL_SETTER;
71   }
72   if (theType == SketchPlugin_ConstraintHorizontal::ID()) {
73     static const std::string HORIZONTAL_SETTER("setHorizontal");
74     return HORIZONTAL_SETTER;
75   }
76   if (theType == SketchPlugin_ConstraintLength::ID()) {
77     static const std::string LENGTH_SETTER("setLength");
78     return LENGTH_SETTER;
79   }
80   if (theType == SketchPlugin_ConstraintMiddle::ID()) {
81     static const std::string MIDDLE_SETTER("setMiddlePoint");
82     return MIDDLE_SETTER;
83   }
84   if (theType == SketchPlugin_ConstraintParallel::ID()) {
85     static const std::string PARALLEL_SETTER("setParallel");
86     return PARALLEL_SETTER;
87   }
88   if (theType == SketchPlugin_ConstraintPerpendicular::ID()) {
89     static const std::string PERPENDICULAR_SETTER("setPerpendicular");
90     return PERPENDICULAR_SETTER;
91   }
92   if (theType == SketchPlugin_ConstraintRadius::ID()) {
93     static const std::string RADIUS_SETTER("setRadius");
94     return RADIUS_SETTER;
95   }
96   if (theType == SketchPlugin_ConstraintRigid::ID()) {
97     static const std::string FIXED_SETTER("setFixed");
98     return FIXED_SETTER;
99   }
100   if (theType == SketchPlugin_ConstraintTangent::ID()) {
101     static const std::string TANGENT_SETTER("setTangent");
102     return TANGENT_SETTER;
103   }
104   if (theType == SketchPlugin_ConstraintVertical::ID()) {
105     static const std::string VERTICAL_SETTER("setVertical");
106     return VERTICAL_SETTER;
107   }
108
109   static const std::string DUMMY;
110   return DUMMY;
111 }
112
113 void SketchAPI_Constraint::dump(ModelHighAPI_Dumper& theDumper) const
114 {
115   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(feature());
116   if (!aConstraint)
117     return; // dump constraints only
118
119   const std::string& aSetter = constraintTypeToSetter(aConstraint->getKind());
120   if (aSetter.empty())
121     return; // incorrect constraint type
122
123   const std::string& aSketchName = theDumper.parentName(aConstraint);
124   theDumper << aConstraint << " = " << aSketchName << "." << aSetter << "(";
125
126   bool isFirstAttr = true;
127   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
128     AttributeRefAttrPtr aRefAttr = aConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
129     if (aRefAttr && aRefAttr->isInitialized()) {
130       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
131       isFirstAttr = false;
132     }
133   }
134
135   AttributeDoublePtr aValueAttr = aConstraint->real(SketchPlugin_Constraint::VALUE());
136   if (aValueAttr && aValueAttr->isInitialized())
137     theDumper << ", " << aValueAttr;
138
139   theDumper << ")" << std::endl;
140 }