Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Axis.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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "ConstructionPlugin_Axis.h"
21
22 #include <Config_PropManager.h>
23
24 #include <ModelAPI_AttributeBoolean.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Validator.h>
31
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34 #include <GeomAPI_Pln.h>
35 #include <GeomAPI_Vertex.h>
36 #include <GeomAlgoAPI_EdgeBuilder.h>
37 #include <GeomAlgoAPI_PointBuilder.h>
38
39 #include <math.h>
40
41 #ifdef _DEBUG
42 #include <iostream>
43 #endif
44
45 static const double defaultAxisSize = 100;
46
47 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
48 {
49 }
50
51 void ConstructionPlugin_Axis::initAttributes()
52 {
53   data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
54                        ModelAPI_AttributeString::typeId());
55
56   // Attributes needed to build the axis using the "two points" method
57   data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
58                        ModelAPI_AttributeSelection::typeId());
59   data()->addAttribute(ConstructionPlugin_Axis::POINT_SECOND(),
60                        ModelAPI_AttributeSelection::typeId());
61
62   // Attributes needed to build the axis using the "cylindrical face" method"
63   data()->addAttribute(ConstructionPlugin_Axis::CYLINDRICAL_FACE(),
64                        ModelAPI_AttributeSelection::typeId());
65   data()->addAttribute(ConstructionPlugin_Axis::X_DIRECTION(),
66                        ModelAPI_AttributeDouble::typeId());
67   data()->addAttribute(ConstructionPlugin_Axis::Y_DIRECTION(),
68                        ModelAPI_AttributeDouble::typeId());
69   data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
70                        ModelAPI_AttributeDouble::typeId());
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
72       ConstructionPlugin_Axis::X_DIRECTION());
73   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
74       ConstructionPlugin_Axis::Y_DIRECTION());
75   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
76       ConstructionPlugin_Axis::Z_DIRECTION());
77
78   //Attributes needed to build the axis using the "three dimensions" method
79   data()->addAttribute(ConstructionPlugin_Axis::DX(),
80                        ModelAPI_AttributeDouble::typeId());
81   data()->addAttribute(ConstructionPlugin_Axis::DY(),
82                        ModelAPI_AttributeDouble::typeId());
83   data()->addAttribute(ConstructionPlugin_Axis::DZ(),
84                        ModelAPI_AttributeDouble::typeId());
85
86   /// Attributes for axis by line.
87   data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
88
89   /// Attributes for axis by plane and point.
90   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
91   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
92
93   /// Attributes for axis by two planes.
94   data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
95   data()->addAttribute(USE_OFFSET1(), ModelAPI_AttributeString::typeId());
96   data()->addAttribute(OFFSET1(), ModelAPI_AttributeDouble::typeId());
97   data()->addAttribute(REVERSE_OFFSET1(), ModelAPI_AttributeBoolean::typeId());
98   data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
99   data()->addAttribute(USE_OFFSET2(), ModelAPI_AttributeString::typeId());
100   data()->addAttribute(OFFSET2(), ModelAPI_AttributeDouble::typeId());
101   data()->addAttribute(REVERSE_OFFSET2(), ModelAPI_AttributeBoolean::typeId());
102 }
103
104 void ConstructionPlugin_Axis::createAxisByTwoPoints()
105 {
106   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
107   AttributeSelectionPtr aRef2 = data()->selection(ConstructionPlugin_Axis::POINT_SECOND());
108   if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
109     GeomShapePtr aShape1 = aRef1->value();
110     if (!aShape1.get())
111       aShape1 = aRef1->context()->shape();
112     GeomShapePtr aShape2 = aRef2->value();
113     if (!aShape2.get())
114       aShape2 = aRef2->context()->shape();
115     if (aShape1->isVertex() && aShape2->isVertex() && (!aShape1->isEqual(aShape2))) {
116       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
117       std::shared_ptr<GeomAPI_Pnt> anEnd = GeomAlgoAPI_PointBuilder::point(aShape2);
118       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
119         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
120
121         ResultConstructionPtr aConstr = document()->createConstruction(data());
122         aConstr->setInfinite(true);
123         aConstr->setShape(anEdge);
124         setResult(aConstr);
125       }
126     }
127   }
128 }
129
130
131 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
132 {
133   AttributeSelectionPtr aRef1 = data()->selection(ConstructionPlugin_Axis::POINT_FIRST());
134   AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
135   AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
136   AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
137   if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) &&
138       (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
139     GeomShapePtr aShape1 = aRef1->value();
140     if (!aShape1.get())
141       aShape1 = aRef1->context()->shape();
142
143     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(),
144                                                                aYAttr->value(),
145                                                                aZAttr->value()));
146     if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
147       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
148       std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
149       if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
150         std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
151
152         ResultConstructionPtr aConstr = document()->createConstruction(data());
153         aConstr->setInfinite(true);
154         aConstr->setShape(anEdge);
155         setResult(aConstr);
156       }
157     }
158   }
159 }
160
161
162 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
163 {
164     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
165      // update arguments due to the selection value
166     if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
167       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
168
169       ResultConstructionPtr aConstr = document()->createConstruction(data());
170       aConstr->setInfinite(true);
171       aConstr->setShape(anEdge);
172       setResult(aConstr);
173     }
174 }
175
176 void ConstructionPlugin_Axis::createAxisByDimensions()
177 {
178   // Start by getting these dimensions
179   double aDX = data()->real(DX())->value();
180   double aDY = data()->real(DY())->value();
181   double aDZ = data()->real(DZ())->value();
182
183   if (fabs(aDX) < MINIMAL_LENGTH()  && fabs(aDY) < MINIMAL_LENGTH() &&
184       fabs(aDZ) < MINIMAL_LENGTH()) {
185     setError("Axis builder with dimensions  :: all dimensions are null", false);
186     return ;
187   }
188
189   // Make the axis, build the ResultConstructionPtr and write it
190   std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aDX, aDY, aDZ);
191   ResultConstructionPtr aConstr = document()->createConstruction(data());
192   aConstr->setInfinite(true);
193   aConstr->setShape(anEdge);
194   setResult(aConstr);
195 }
196
197 void ConstructionPlugin_Axis::createAxisByLine()
198 {
199   // Get edge.
200   AttributeSelectionPtr anEdgeSelection = selection(LINE());
201   GeomShapePtr aLineShape = anEdgeSelection->value();
202   if(!aLineShape.get()) {
203     aLineShape = anEdgeSelection->context()->shape();
204   }
205   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
206
207   ResultConstructionPtr aConstr = document()->createConstruction(data());
208   aConstr->setInfinite(true);
209   aConstr->setShape(anEdge);
210   setResult(aConstr);
211 }
212
213 void ConstructionPlugin_Axis::createAxisByPlaneAndPoint()
214 {
215   // Get face.
216   AttributeSelectionPtr aFaceSelection = selection(PLANE());
217   GeomShapePtr aFaceShape = aFaceSelection->value();
218   if(!aFaceShape.get()) {
219     aFaceShape = aFaceSelection->context()->shape();
220   }
221   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
222   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
223
224   // Get point.
225   AttributeSelectionPtr aPointSelection = selection(POINT());
226   GeomShapePtr aPointShape = aPointSelection->value();
227   if(!aPointShape.get()) {
228     aPointShape = aPointSelection->context()->shape();
229   }
230   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
231   std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
232
233   std::shared_ptr<GeomAPI_Pnt> aProjPnt = aPln->project(aPnt);
234
235   if(aProjPnt->isEqual(aPnt)) {
236     aPnt->translate(aPln->direction(), defaultAxisSize);
237   }
238
239   std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aPnt, aProjPnt);
240
241   ResultConstructionPtr aConstr = document()->createConstruction(data());
242   aConstr->setInfinite(true);
243   aConstr->setShape(anEdge);
244   setResult(aConstr);
245 }
246
247 void ConstructionPlugin_Axis::createAxisByTwoPlanes()
248 {
249   // Get face 1.
250   AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
251   GeomShapePtr aFaceShape1 = aFaceSelection1->value();
252   if(!aFaceShape1.get()) {
253     aFaceShape1 = aFaceSelection1->context()->shape();
254   }
255   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
256   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
257
258   std::string useOffset1 = string(USE_OFFSET1())->value();
259   if(!useOffset1.empty()) {
260     double anOffset1 = real(OFFSET1())->value();
261     bool reverseOffset1 = boolean(REVERSE_OFFSET1())->value();
262     if(reverseOffset1) {
263       anOffset1 = -anOffset1;
264     }
265     aPln1->translate(aPln1->direction(), anOffset1);
266   }
267
268   // Get face 2.
269   AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
270   GeomShapePtr aFaceShape2 = aFaceSelection2->value();
271   if(!aFaceShape2.get()) {
272     aFaceShape2 = aFaceSelection2->context()->shape();
273   }
274   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
275   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
276
277   std::string useOffset2 = string(USE_OFFSET2())->value();
278   if(!useOffset2.empty()) {
279     double anOffset2 = real(OFFSET2())->value();
280     bool reverseOffset2 = boolean(REVERSE_OFFSET2())->value();
281     if(reverseOffset2) {
282       anOffset2 = -anOffset2;
283     }
284     aPln2->translate(aPln2->direction(), anOffset2);
285   }
286
287   std::shared_ptr<GeomAPI_Lin> aLin = aPln1->intersect(aPln2);
288   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aLin->location();
289   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aLin->location();
290   aPnt2->translate(aLin->direction(), defaultAxisSize);
291
292   std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aPnt1, aPnt2);
293
294   ResultConstructionPtr aConstr = document()->createConstruction(data());
295   aConstr->setInfinite(true);
296   aConstr->setShape(anEdge);
297   setResult(aConstr);
298 }
299
300 void ConstructionPlugin_Axis::execute()
301 {
302   AttributeStringPtr aMethodTypeAttr = string(ConstructionPlugin_Axis::METHOD());
303   std::string aMethodType = aMethodTypeAttr->value();
304   if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) {
305     createAxisByTwoPoints();
306   } else if (aMethodType == CREATION_METHOD_BY_CYLINDRICAL_FACE()) {
307     createAxisByCylindricalFace();
308   } else if (aMethodType == CREATION_METHOD_BY_POINT_AND_DIRECTION()) {
309     createAxisByPointAndDirection();
310   } else if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) {
311     createAxisByDimensions();
312   } else if(aMethodType == CREATION_METHOD_BY_LINE()) {
313     createAxisByLine();
314   } else if(aMethodType == CREATION_METHOD_BY_PLANE_AND_POINT()) {
315     createAxisByPlaneAndPoint();
316   } else if(aMethodType == CREATION_METHOD_BY_TWO_PLANES()) {
317     createAxisByTwoPlanes();
318   }
319 }
320
321 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
322   std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
323 {
324   bool isCustomized = theDefaultPrs.get() != NULL &&
325                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
326
327   isCustomized = thePrs->setLineStyle(3) || isCustomized;
328   isCustomized = thePrs->setWidth(2) || isCustomized;
329
330   return isCustomized;
331 }