Salome HOME
Issue #2209 : New option of creation of construction points
[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   /*data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
59   data()->addAttribute(DISTANCE_VALUE(), ModelAPI_AttributeDouble::typeId());
60   data()->addAttribute(DISTANCE_PERCENT(), ModelAPI_AttributeBoolean::typeId());
61   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
62
63   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
64   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
65
66   data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId());
67   data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId());
68 */
69   data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
70   data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
71
72   data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
73   data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
74   data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
75 }
76
77 //==================================================================================================
78 void ConstructionPlugin_Point::execute()
79 {
80   GeomShapePtr aShape;
81
82    // to support compatibility with old documents where aCreationMethod did not exist
83   std::string aCreationMethod =
84     string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
85     string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
86   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
87     aShape = createByXYZ();
88   }/* else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
89     aShape = createByDistanceOnEdge();
90   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
91     aShape = createByProjection();
92   } else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
93     aShape = createByLinesIntersection();
94   }*/ else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
95     aShape = createByLineAndPlaneIntersection();
96   }
97
98   if(!aShape.get()) {
99     setError("Error: intersection not found.");
100     return;
101   }
102
103   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
104   aConstr->setShape(aShape);
105   setResult(aConstr);
106 }
107
108 //==================================================================================================
109 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
110                                                      AISObjectPtr thePrs,
111                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
112 {
113   bool isCustomized = theDefaultPrs.get() != NULL &&
114                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
115   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
116   return true;
117 }
118
119 //==================================================================================================
120 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
121 {
122   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
123                                           real(Y())->value(),
124                                           real(Z())->value());
125 }
126
127 /*//==================================================================================================
128 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
129 {
130   // Get edge.
131   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
132   GeomShapePtr aShape = anEdgeSelection->value();
133   if(!aShape.get()) {
134     aShape = anEdgeSelection->context()->shape();
135   }
136   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
137
138   // Get distance value and percent flag.
139   double aValue = real(DISTANCE_VALUE())->value();
140   bool anIsPercent = boolean(DISTANCE_PERCENT())->value();
141
142   // Get reverse flag.
143   bool anIsReverse = boolean(REVERSE())->value();
144
145   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
146 }
147
148 //==================================================================================================
149 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjection()
150 {
151   // Get point.
152   AttributeSelectionPtr aPointSelection = selection(POINT());
153   GeomShapePtr aPointShape = aPointSelection->value();
154   if(!aPointShape.get()) {
155     aPointShape = aPointSelection->context()->shape();
156   }
157   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
158
159   // Get plane.
160   AttributeSelectionPtr aPlaneSelection = selection(PLANE());
161   GeomShapePtr aPlaneShape = aPlaneSelection->value();
162   if(!aPlaneShape.get()) {
163     aPlaneShape = aPlaneSelection->context()->shape();
164   }
165   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
166
167   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
168 }
169
170 //==================================================================================================
171 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
172 {
173   // Get first line.
174   AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
175   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
176   if(!aFirstLineShape.get()) {
177     aFirstLineShape = aFirstLineSelection->context()->shape();
178   }
179   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
180
181   // Get second line.
182   AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
183   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
184   if(!aSecondLineShape.get()) {
185     aSecondLineShape = aSecondLineSelection->context()->shape();
186   }
187   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
188
189   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
190 }
191 */
192
193 //==================================================================================================
194 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLineAndPlaneIntersection()
195 {
196   // Get line.
197   AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
198   GeomShapePtr aLineShape = aLineSelection->value();
199   if(!aLineShape.get()) {
200     aLineShape = aLineSelection->context()->shape();
201   }
202   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
203
204   // Get plane.
205   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
206   GeomShapePtr aPlaneShape = aPlaneSelection->value();
207   if(!aPlaneShape.get()) {
208     aPlaneShape = aPlaneSelection->context()->shape();
209   }
210   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
211
212   if (!string(USE_OFFSET())->value().empty()) {
213     double anOffset = real(OFFSET())->value();
214     if (boolean(REVERSE_OFFSET())->value())
215       anOffset = -anOffset;
216     if (fabs(anOffset) > 1.e-9) { // move face
217       aFace->translate(aFace->getPlane()->direction(), anOffset);
218     }
219   }
220
221   return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace);
222 }