1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "ConstructionPlugin_Axis.h"
22 #include <Config_PropManager.h>
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>
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>
45 static const double defaultAxisSize = 100;
47 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
51 void ConstructionPlugin_Axis::initAttributes()
53 data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
54 ModelAPI_AttributeString::typeId());
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());
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());
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());
86 /// Attributes for axis by line.
87 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
89 /// Attributes for axis by plane and point.
90 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
91 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
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());
104 void ConstructionPlugin_Axis::createAxisByTwoPoints()
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();
111 aShape1 = aRef1->context()->shape();
112 GeomShapePtr aShape2 = aRef2->value();
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);
121 ResultConstructionPtr aConstr = document()->createConstruction(data());
122 aConstr->setInfinite(true);
123 aConstr->setShape(anEdge);
131 void ConstructionPlugin_Axis::createAxisByPointAndDirection()
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();
141 aShape1 = aRef1->context()->shape();
143 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->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);
152 ResultConstructionPtr aConstr = document()->createConstruction(data());
153 aConstr->setInfinite(true);
154 aConstr->setShape(anEdge);
162 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
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);
169 ResultConstructionPtr aConstr = document()->createConstruction(data());
170 aConstr->setInfinite(true);
171 aConstr->setShape(anEdge);
176 void ConstructionPlugin_Axis::createAxisByDimensions()
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();
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);
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);
197 void ConstructionPlugin_Axis::createAxisByLine()
200 AttributeSelectionPtr anEdgeSelection = selection(LINE());
201 GeomShapePtr aLineShape = anEdgeSelection->value();
202 if(!aLineShape.get()) {
203 aLineShape = anEdgeSelection->context()->shape();
205 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
207 ResultConstructionPtr aConstr = document()->createConstruction(data());
208 aConstr->setInfinite(true);
209 aConstr->setShape(anEdge);
213 void ConstructionPlugin_Axis::createAxisByPlaneAndPoint()
216 AttributeSelectionPtr aFaceSelection = selection(PLANE());
217 GeomShapePtr aFaceShape = aFaceSelection->value();
218 if(!aFaceShape.get()) {
219 aFaceShape = aFaceSelection->context()->shape();
221 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
222 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
225 AttributeSelectionPtr aPointSelection = selection(POINT());
226 GeomShapePtr aPointShape = aPointSelection->value();
227 if(!aPointShape.get()) {
228 aPointShape = aPointSelection->context()->shape();
230 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
231 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
233 std::shared_ptr<GeomAPI_Pnt> aProjPnt = aPln->project(aPnt);
235 if(aProjPnt->isEqual(aPnt)) {
236 aPnt->translate(aPln->direction(), defaultAxisSize);
239 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aPnt, aProjPnt);
241 ResultConstructionPtr aConstr = document()->createConstruction(data());
242 aConstr->setInfinite(true);
243 aConstr->setShape(anEdge);
247 void ConstructionPlugin_Axis::createAxisByTwoPlanes()
250 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
251 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
252 if(!aFaceShape1.get()) {
253 aFaceShape1 = aFaceSelection1->context()->shape();
255 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
256 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
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();
263 anOffset1 = -anOffset1;
265 aPln1->translate(aPln1->direction(), anOffset1);
269 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
270 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
271 if(!aFaceShape2.get()) {
272 aFaceShape2 = aFaceSelection2->context()->shape();
274 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
275 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
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();
282 anOffset2 = -anOffset2;
284 aPln2->translate(aPln2->direction(), anOffset2);
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);
292 std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aPnt1, aPnt2);
294 ResultConstructionPtr aConstr = document()->createConstruction(data());
295 aConstr->setInfinite(true);
296 aConstr->setShape(anEdge);
300 void ConstructionPlugin_Axis::execute()
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()) {
314 } else if(aMethodType == CREATION_METHOD_BY_PLANE_AND_POINT()) {
315 createAxisByPlaneAndPoint();
316 } else if(aMethodType == CREATION_METHOD_BY_TWO_PLANES()) {
317 createAxisByTwoPlanes();
321 bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
322 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
324 bool isCustomized = theDefaultPrs.get() != NULL &&
325 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
327 isCustomized = thePrs->setLineStyle(3) || isCustomized;
328 isCustomized = thePrs->setWidth(2) || isCustomized;