Salome HOME
Some improvements in GUI
[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(), ModelAPI_AttributeSelection::typeId());
77   data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
78   data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
79   data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
80 }
81
82 //==================================================================================================
83 void ConstructionPlugin_Point::execute()
84 {
85   GeomShapePtr aShape;
86
87   // to support compatibility with old documents where aCreationMethod did not exist
88   std::string aCreationMethod =
89     string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
90     string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
91   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
92     aShape = createByXYZ();
93   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
94     aShape = createByDistanceOnEdge();
95   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
96     if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
97       aShape = createByProjectionOnEdge();
98     } else {
99       aShape = createByProjectionOnFace();
100     }
101   } /* else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
102     aShape = createByLinesIntersection();
103   } */ else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
104     // this may produce several points
105     std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
106     if (!aPoints.empty()) { // if no points found produce the standard error later
107       int anIndex = 0;
108       std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
109       for(; aPIter != aPoints.end(); aPIter++, anIndex++) {
110         std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
111           document()->createConstruction(data(), anIndex);
112         aConstr->setShape(*aPIter);
113         setResult(aConstr, anIndex);
114       }
115       removeResults(anIndex);
116       return;
117     }
118   }
119
120   if(!aShape.get()) {
121     setError("Error: intersection not found.");
122     return;
123   }
124
125   removeResults(1); // for case the point type was switched from multi-results type
126   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
127   aConstr->setShape(aShape);
128   setResult(aConstr);
129 }
130
131 //==================================================================================================
132 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
133                                                      AISObjectPtr thePrs,
134                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
135 {
136   bool isCustomized = theDefaultPrs.get() != NULL &&
137                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
138   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
139   return true;
140 }
141
142 //==================================================================================================
143 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
144 {
145   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
146                                           real(Y())->value(),
147                                           real(Z())->value());
148 }
149
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
152 {
153   // Get edge.
154   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
155   GeomShapePtr aShape = anEdgeSelection->value();
156   if(!aShape.get()) {
157     aShape = anEdgeSelection->context()->shape();
158   }
159   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
160
161   // Get distance value and percent flag.
162   double aValue;
163   bool anIsPercent = false;
164   if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
165     aValue = real(DISTANCE())->value();
166     anIsPercent = false;
167   } else {
168     aValue = real(RATIO())->value() * 100.0;
169     anIsPercent = true;
170   }
171
172   // Get reverse flag.
173   bool anIsReverse = boolean(REVERSE())->value();
174
175   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
176 }
177
178 //==================================================================================================
179 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
180 {
181   // Get point.
182   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
183   GeomShapePtr aPointShape = aPointSelection->value();
184   if (!aPointShape.get()) {
185     aPointShape = aPointSelection->context()->shape();
186   }
187   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
188
189   // Get edge.
190   AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
191   GeomShapePtr anEdgeShape = anEdgeSelection->value();
192   if (!anEdgeShape.get()) {
193     anEdgeShape = anEdgeSelection->context()->shape();
194   }
195   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
196
197   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
198 }
199
200 //==================================================================================================
201 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
202 {
203   // Get point.
204   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
205   GeomShapePtr aPointShape = aPointSelection->value();
206   if(!aPointShape.get()) {
207     aPointShape = aPointSelection->context()->shape();
208   }
209   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
210
211   // Get plane.
212   AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
213   GeomShapePtr aPlaneShape = aPlaneSelection->value();
214   if(!aPlaneShape.get()) {
215     aPlaneShape = aPlaneSelection->context()->shape();
216   }
217   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
218
219   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
220 }
221
222 /*
223 //==================================================================================================
224 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
225 {
226   // Get first line.
227   AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
228   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
229   if(!aFirstLineShape.get()) {
230     aFirstLineShape = aFirstLineSelection->context()->shape();
231   }
232   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
233
234   // Get second line.
235   AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
236   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
237   if(!aSecondLineShape.get()) {
238     aSecondLineShape = aSecondLineSelection->context()->shape();
239   }
240   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
241
242   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
243 }
244 */
245
246 //==================================================================================================
247 std::list<std::shared_ptr<GeomAPI_Vertex> >
248   ConstructionPlugin_Point::createByLineAndPlaneIntersection()
249 {
250   // Get line.
251   AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
252   GeomShapePtr aLineShape = aLineSelection->value();
253   if(!aLineShape.get()) {
254     aLineShape = aLineSelection->context()->shape();
255   }
256   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
257
258   // Get plane.
259   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
260   GeomShapePtr aPlaneShape = aPlaneSelection->value();
261   if(!aPlaneShape.get()) {
262     aPlaneShape = aPlaneSelection->context()->shape();
263   }
264   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
265
266   if (!string(USE_OFFSET())->value().empty()) {
267     double anOffset = real(OFFSET())->value();
268     if (boolean(REVERSE_OFFSET())->value())
269       anOffset = -anOffset;
270     if (fabs(anOffset) > 1.e-9) { // move face
271       aFace->translate(aFace->getPlane()->direction(), anOffset);
272     }
273   }
274
275   return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
276     aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
277 }