Salome HOME
57c01c05d5e7a6feceabfaac6fd410f222900539
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Name   : SketchAPI_Sketch.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "SketchAPI_Sketch.h"
9 //--------------------------------------------------------------------------------------
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 //#include <SketchPlugin_ConstraintBase.h>
13 #include <SketchPlugin_ConstraintCoincidence.h>
14 #include <SketchPlugin_ConstraintCollinear.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintEqual.h>
17 #include <SketchPlugin_ConstraintFillet.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintMirror.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 //--------------------------------------------------------------------------------------
29 #include <ModelAPI_CompositeFeature.h>
30 #include <ModelHighAPI_Tools.h>
31 #include "SketchAPI_Line.h"
32 //--------------------------------------------------------------------------------------
33 SketchAPI_Sketch::SketchAPI_Sketch(
34     const std::shared_ptr<ModelAPI_Feature> & theFeature)
35 : ModelHighAPI_Interface(theFeature)
36 {
37   initialize();
38 }
39
40 SketchAPI_Sketch::SketchAPI_Sketch(
41     const std::shared_ptr<ModelAPI_Feature> & theFeature,
42     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
43 : ModelHighAPI_Interface(theFeature)
44 {
45   if (initialize()) {
46     setPlane(thePlane);
47   }
48 }
49
50 SketchAPI_Sketch::SketchAPI_Sketch(
51     const std::shared_ptr<ModelAPI_Feature> & theFeature,
52     const ModelHighAPI_Selection & theExternal)
53 : ModelHighAPI_Interface(theFeature)
54 {
55   if (initialize()) {
56     setExternal(theExternal);
57   }
58 }
59
60 SketchAPI_Sketch::~SketchAPI_Sketch()
61 {
62
63 }
64
65 //--------------------------------------------------------------------------------------
66 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
67 {
68   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
69 }
70
71 //--------------------------------------------------------------------------------------
72 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
73 {
74   fillAttribute(thePlane->origin(), myorigin);
75   fillAttribute(thePlane->dirX(), mydirX);
76   fillAttribute(thePlane->normal(), mynormal);
77
78   execute();
79 }
80
81 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
82 {
83   fillAttribute(theExternal, myexternal);
84
85   execute();
86 }
87
88 //--------------------------------------------------------------------------------------
89 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
90                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
91 {
92   // TODO(spo): check that thePart is not empty
93   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
94   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
95 }
96
97 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
98                     const ModelHighAPI_Selection & theExternal)
99 {
100   // TODO(spo): check that thePart is not empty
101   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
102   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
103 }
104
105 //--------------------------------------------------------------------------------------
106 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
107 {
108   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
109   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
110 }
111 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
112     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
113     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
114 {
115   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
116   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
117 }
118 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
119 {
120   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
121   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
122 }
123 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
124 {
125   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
126   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
127   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
128 }
129
130 //--------------------------------------------------------------------------------------
131 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
132     const ModelHighAPI_RefAttr & thePoint1,
133     const ModelHighAPI_RefAttr & thePoint2)
134 {
135   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
136   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
137   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
138   aFeature->execute();
139   return aFeature;
140 }
141
142 //--------------------------------------------------------------------------------------
143 void SketchAPI_Sketch::setValue(
144     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
145     const ModelHighAPI_Double & theValue)
146 {
147   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
148 }
149
150 //--------------------------------------------------------------------------------------