Salome HOME
Test ConstructionAPI_Point by old PythonAPI tests
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Point.cpp
1 // Name   : ConstructionAPI_Point.cpp
2 // Purpose: 
3 //
4 // History:
5 // 29/03/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "ConstructionAPI_Point.h"
9 //--------------------------------------------------------------------------------------
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13
14 #include <ModelHighAPI_Double.h>
15 //--------------------------------------------------------------------------------------
16 ConstructionAPI_Point::ConstructionAPI_Point(
17     const std::shared_ptr<ModelAPI_Feature> & theFeature)
18 : ModelHighAPI_Interface(theFeature)
19 {
20   initialize();
21 }
22
23 ConstructionAPI_Point::ConstructionAPI_Point(
24     const std::shared_ptr<ModelAPI_Feature> & theFeature,
25     const ModelHighAPI_Double & theX,
26     const ModelHighAPI_Double & theY,
27     const ModelHighAPI_Double & theZ)
28 : ModelHighAPI_Interface(theFeature)
29 {
30   if (initialize()) {
31     setPoint(theX, theY, theZ);
32     execute();
33   }
34 }
35
36 ConstructionAPI_Point::~ConstructionAPI_Point()
37 {
38
39 }
40
41 //--------------------------------------------------------------------------------------
42 bool ConstructionAPI_Point::initialize()
43 {
44   if (!myFeature) {
45     throwException("ConstructionAPI_Point exception: The feature is NULL.");
46     return false;
47   }
48
49   if (feature()->getKind() != "Point") {
50     throwException("ConstructionAPI_Point exception: Wrong feature kind.");
51     return false;
52   }
53
54   // data() throws exceptions if the attribute is invalid
55   myX = feature()->real("x");
56   myY = feature()->real("y");
57   myZ = feature()->real("z");
58
59   if (!myX || !myY || !myZ)
60     return false;
61
62   return true;
63 }
64
65 //--------------------------------------------------------------------------------------
66 void ConstructionAPI_Point::setPoint(const ModelHighAPI_Double & theX,
67                                      const ModelHighAPI_Double & theY,
68                                      const ModelHighAPI_Double & theZ)
69 {
70   theX.fillAttribute(myX);
71   theY.fillAttribute(myY);
72   theZ.fillAttribute(myZ);
73 }
74
75 //--------------------------------------------------------------------------------------
76 std::shared_ptr<ModelAPI_AttributeDouble> ConstructionAPI_Point::x() const
77 {
78   return myX;
79 }
80 std::shared_ptr<ModelAPI_AttributeDouble> ConstructionAPI_Point::y() const
81 {
82   return myY;
83 }
84 std::shared_ptr<ModelAPI_AttributeDouble> ConstructionAPI_Point::z() const
85 {
86   return myZ;
87 }
88
89 //--------------------------------------------------------------------------------------
90 // TODO(spo): make add* as static functions of the class
91 PointPtr addPoint(
92     std::shared_ptr<ModelAPI_Document> thePart,
93     const ModelHighAPI_Double& theX,
94     const ModelHighAPI_Double& theY,
95     const ModelHighAPI_Double& theZ)
96 {
97   // TODO(spo): check that thePart is not empty
98   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature("Point");
99   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
100 }