Salome HOME
Added option to create Construction Point by intersection of line and plane.
[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 #include "ConstructionAPI_Point.h"
8
9 #include <GeomAPI_Shape.h>
10
11 #include <ModelHighAPI_Selection.h>
12 #include <ModelHighAPI_Tools.h>
13
14 #include <algorithm>
15
16 static GeomAPI_Shape::ShapeType shapeTypeByStr(const std::string& theShapeTypeStr);
17 static GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection);
18
19 //==================================================================================================
20 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature)
21 : ModelHighAPI_Interface(theFeature)
22 {
23   initialize();
24 }
25
26 //==================================================================================================
27 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
28                                              const ModelHighAPI_Double& theX,
29                                              const ModelHighAPI_Double& theY,
30                                              const ModelHighAPI_Double& theZ)
31 : ModelHighAPI_Interface(theFeature)
32 {
33   if(initialize()) {
34     setByXYZ(theX, theY, theZ);
35   }
36 }
37
38 //==================================================================================================
39 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
40                                              const ModelHighAPI_Selection& theEdge,
41                                              const ModelHighAPI_Double& theDistanceValue,
42                                              const bool theDistancePercent,
43                                              const bool theReverse)
44 : ModelHighAPI_Interface(theFeature)
45 {
46   if(initialize()) {
47     setByDistanceOnEdge(theEdge, theDistanceValue, theDistancePercent, theReverse);
48   }
49 }
50
51 //==================================================================================================
52 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
53                                              const ModelHighAPI_Selection& theObject1,
54                                              const ModelHighAPI_Selection& theObject2)
55 : ModelHighAPI_Interface(theFeature)
56 {
57   if(initialize()) {
58     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
59     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
60
61     if(aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::FACE) {
62       // If first object is vertex and second object is face then set by projection.
63       setByProjection(theObject1, theObject2);
64     } else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::EDGE) {
65       // If both objects are edges then set by lines intersection.
66       setByLinesIntersection(theObject1, theObject2);
67     } else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::FACE) {
68       // If first object is edge and second object is face then set by line and plane intersection.
69       setByLineAndPlaneIntersection(theObject1, theObject2);
70     }
71   }
72 }
73
74 //==================================================================================================
75 ConstructionAPI_Point::~ConstructionAPI_Point()
76 {
77
78 }
79
80 //==================================================================================================
81 void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
82                                      const ModelHighAPI_Double& theY,
83                                      const ModelHighAPI_Double& theZ)
84 {
85   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
86   fillAttribute(theX, myx);
87   fillAttribute(theY, myy);
88   fillAttribute(theZ, myz);
89
90   execute();
91 }
92
93 //==================================================================================================
94 void ConstructionAPI_Point::setByDistanceOnEdge(const ModelHighAPI_Selection& theEdge,
95                                                 const ModelHighAPI_Double& theDistanceValue,
96                                                 const bool theDistancePercent,
97                                                 const bool theReverse)
98 {
99   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
100   fillAttribute(theEdge, myedge);
101   fillAttribute(theDistanceValue, mydistanceValue);
102   fillAttribute(theDistancePercent, mydistancePercent);
103   fillAttribute(theReverse, myreverse);
104
105   execute();
106 }
107
108 //==================================================================================================
109 void ConstructionAPI_Point::setByProjection(const ModelHighAPI_Selection& theVertex,
110                                             const ModelHighAPI_Selection& theFace)
111 {
112   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(), mycreationMethod);
113   fillAttribute(theVertex, mypoint);
114   fillAttribute(theFace, myplane);
115
116   execute();
117 }
118
119 //==================================================================================================
120 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
121                                                    const ModelHighAPI_Selection& theEdge2)
122 {
123   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINES_INTERSECTION(), mycreationMethod);
124   fillAttribute(theEdge1, myfirstLine);
125   fillAttribute(theEdge2, mysecondLine);
126
127   execute();
128 }
129
130 //==================================================================================================
131 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
132                                                           const ModelHighAPI_Selection& theFace)
133 {
134   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION(), mycreationMethod);
135   fillAttribute(theEdge, myintersectionLine);
136   fillAttribute(theFace, myintersectionPlane);
137
138   execute();
139 }
140
141 //==================================================================================================
142 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
143                   const ModelHighAPI_Double& theX,
144                   const ModelHighAPI_Double& theY,
145                   const ModelHighAPI_Double& theZ)
146 {
147   // TODO(spo): check that thePart is not empty
148   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
149   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
150 }
151
152 //==================================================================================================
153 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
154                   const ModelHighAPI_Selection& theEdge,
155                   const ModelHighAPI_Double& theDistanceValue,
156                   const bool theDistancePercent,
157                   const bool theReverse)
158 {
159   // TODO(spo): check that thePart is not empty
160   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
161   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theDistanceValue, theDistancePercent, theReverse));
162 }
163
164 //==================================================================================================
165 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
166                   const ModelHighAPI_Selection& theObject1,
167                   const ModelHighAPI_Selection& theObject2)
168 {
169   // TODO(spo): check that thePart is not empty
170   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
171   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
172 }
173
174 //==================================================================================================
175 GeomAPI_Shape::ShapeType shapeTypeByStr(const std::string& theShapeTypeStr)
176 {
177   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
178
179   std::string aShapeTypeStr = theShapeTypeStr;
180   std::transform(aShapeTypeStr.begin(), aShapeTypeStr.end(), aShapeTypeStr.begin(), ::tolower);
181
182   if(theShapeTypeStr == "compound") {
183     aShapeType = GeomAPI_Shape::COMPOUND;
184   } else if(theShapeTypeStr == "compsolid") {
185     aShapeType = GeomAPI_Shape::COMPSOLID;
186   } else if(theShapeTypeStr == "solid") {
187     aShapeType = GeomAPI_Shape::SOLID;
188   } else if(theShapeTypeStr == "shell") {
189     aShapeType = GeomAPI_Shape::SHELL;
190   } else if(theShapeTypeStr == "face") {
191     aShapeType = GeomAPI_Shape::FACE;
192   } else if(theShapeTypeStr == "wire") {
193     aShapeType = GeomAPI_Shape::WIRE;
194   } else if(theShapeTypeStr == "edge") {
195     aShapeType = GeomAPI_Shape::EDGE;
196   } else if(theShapeTypeStr == "vertex") {
197     aShapeType = GeomAPI_Shape::VERTEX;
198   } else if(theShapeTypeStr == "shape") {
199     aShapeType = GeomAPI_Shape::SHAPE;
200   }
201
202   return aShapeType;
203 }
204
205 //==================================================================================================
206 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
207 {
208   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
209
210   switch(theSelection.variantType()) {
211     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
212       ResultSubShapePair aPair = theSelection.resultSubShapePair();
213       GeomShapePtr aShape = aPair.second;
214       if(!aShape.get()) {
215         aShape = aPair.first->shape();
216       }
217       if(!aShape.get()) {
218         return aShapeType;
219       }
220       aShapeType = aShape->shapeType();
221       break;
222     }
223     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
224       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
225       std::string aType = aPair.first;
226       aShapeType = shapeTypeByStr(aType);
227       break;
228     }
229   }
230
231   return aShapeType;
232 }