Salome HOME
2.3.3.1 Point creation by projection of another point on a line
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Point.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ConstructionPlugin_Point.h"
22
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultConstruction.h>
28
29 #include <GeomAlgoAPI_PointBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_Pln.h>
36
37 //==================================================================================================
38 ConstructionPlugin_Point::ConstructionPlugin_Point()
39 {
40 }
41
42 //==================================================================================================
43 const std::string& ConstructionPlugin_Point::getKind()
44 {
45   static std::string MY_KIND = ConstructionPlugin_Point::ID();
46   return MY_KIND;
47 }
48
49 //==================================================================================================
50 void ConstructionPlugin_Point::initAttributes()
51 {
52   data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
53   data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
54   data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
57
58 /*
59   data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId());
60   data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId());
61 */
62
63   data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
64   data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
65
66   data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
67   data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
68   data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
69
70   data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
71   data()->addAttribute(OFFSET_TYPE(), ModelAPI_AttributeString::typeId());
72   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
73   data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
74   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
75
76   data()->addAttribute(POINT_TO_PROJECT_ON_EDGE(), ModelAPI_AttributeSelection::typeId());
77   data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
78
79   data()->addAttribute(POINT_TO_PROJECT_ON_FACE(), ModelAPI_AttributeSelection::typeId());
80   data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
81 }
82
83 //==================================================================================================
84 void ConstructionPlugin_Point::execute()
85 {
86   GeomShapePtr aShape;
87
88   // to support compatibility with old documents where aCreationMethod did not exist
89   std::string aCreationMethod =
90     string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
91     string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
92   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
93     aShape = createByXYZ();
94   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
95     aShape = createByDistanceOnEdge();
96   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION_ON_EDGE()) {
97     aShape = createByProjectionOnEdge();
98   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION_ON_FACE()) {
99     aShape = createByProjectionOnFace();
100   } /* else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
101     aShape = createByLinesIntersection();
102   } */ else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
103     // this may produce several points
104     std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
105     if (!aPoints.empty()) { // if no points found produce the standard error later
106       int anIndex = 0;
107       std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
108       for(; aPIter != aPoints.end(); aPIter++, anIndex++) {
109         std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
110           document()->createConstruction(data(), anIndex);
111         aConstr->setShape(*aPIter);
112         setResult(aConstr, anIndex);
113       }
114       removeResults(anIndex);
115       return;
116     }
117   }
118
119   if(!aShape.get()) {
120     setError("Error: intersection not found.");
121     return;
122   }
123
124   removeResults(1); // for case the point type was switched from multi-results type
125   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
126   aConstr->setShape(aShape);
127   setResult(aConstr);
128 }
129
130 //==================================================================================================
131 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
132                                                      AISObjectPtr thePrs,
133                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
134 {
135   bool isCustomized = theDefaultPrs.get() != NULL &&
136                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
137   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
138   return true;
139 }
140
141 //==================================================================================================
142 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
143 {
144   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
145                                           real(Y())->value(),
146                                           real(Z())->value());
147 }
148
149 //==================================================================================================
150 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
151 {
152   // Get edge.
153   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
154   GeomShapePtr aShape = anEdgeSelection->value();
155   if(!aShape.get()) {
156     aShape = anEdgeSelection->context()->shape();
157   }
158   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
159
160   // Get distance value and percent flag.
161   double aValue;
162   bool anIsPercent = false;
163   if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
164     aValue = real(DISTANCE())->value();
165     anIsPercent = false;
166   } else {
167     aValue = real(RATIO())->value() * 100.0;
168     anIsPercent = true;
169   }
170
171   // Get reverse flag.
172   bool anIsReverse = boolean(REVERSE())->value();
173
174   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
175 }
176
177 //==================================================================================================
178 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
179 {
180   // Get point.
181   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT_ON_EDGE());
182   GeomShapePtr aPointShape = aPointSelection->value();
183   if (!aPointShape.get()) {
184     aPointShape = aPointSelection->context()->shape();
185   }
186   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
187
188   // Get edge.
189   AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
190   GeomShapePtr anEdgeShape = anEdgeSelection->value();
191   if (!anEdgeShape.get()) {
192     anEdgeShape = anEdgeSelection->context()->shape();
193   }
194   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
195
196   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
197 }
198
199 //==================================================================================================
200 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
201 {
202   // Get point.
203   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT_ON_FACE());
204   GeomShapePtr aPointShape = aPointSelection->value();
205   if(!aPointShape.get()) {
206     aPointShape = aPointSelection->context()->shape();
207   }
208   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
209
210   // Get plane.
211   AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
212   GeomShapePtr aPlaneShape = aPlaneSelection->value();
213   if(!aPlaneShape.get()) {
214     aPlaneShape = aPlaneSelection->context()->shape();
215   }
216   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
217
218   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
219 }
220
221 /*
222 //==================================================================================================
223 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
224 {
225   // Get first line.
226   AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
227   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
228   if(!aFirstLineShape.get()) {
229     aFirstLineShape = aFirstLineSelection->context()->shape();
230   }
231   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
232
233   // Get second line.
234   AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
235   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
236   if(!aSecondLineShape.get()) {
237     aSecondLineShape = aSecondLineSelection->context()->shape();
238   }
239   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
240
241   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
242 }
243 */
244
245 //==================================================================================================
246 std::list<std::shared_ptr<GeomAPI_Vertex> >
247   ConstructionPlugin_Point::createByLineAndPlaneIntersection()
248 {
249   // Get line.
250   AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
251   GeomShapePtr aLineShape = aLineSelection->value();
252   if(!aLineShape.get()) {
253     aLineShape = aLineSelection->context()->shape();
254   }
255   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
256
257   // Get plane.
258   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
259   GeomShapePtr aPlaneShape = aPlaneSelection->value();
260   if(!aPlaneShape.get()) {
261     aPlaneShape = aPlaneSelection->context()->shape();
262   }
263   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
264
265   if (!string(USE_OFFSET())->value().empty()) {
266     double anOffset = real(OFFSET())->value();
267     if (boolean(REVERSE_OFFSET())->value())
268       anOffset = -anOffset;
269     if (fabs(anOffset) > 1.e-9) { // move face
270       aFace->translate(aFace->getPlane()->direction(), anOffset);
271     }
272   }
273
274   return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
275     aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
276 }