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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ConstructionPlugin_Point.h"
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>
29 #include <GeomAlgoAPI_PointBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_Pln.h>
37 //==================================================================================================
38 ConstructionPlugin_Point::ConstructionPlugin_Point()
42 //==================================================================================================
43 const std::string& ConstructionPlugin_Point::getKind()
45 static std::string MY_KIND = ConstructionPlugin_Point::ID();
49 //==================================================================================================
50 void ConstructionPlugin_Point::initAttributes()
52 data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
58 data()->addAttribute(INTERSECTION_LINE_1(), ModelAPI_AttributeSelection::typeId());
59 data()->addAttribute(INTERSECTION_LINE_2(), ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
62 data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
64 data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
65 data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
67 data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
68 data()->addAttribute(OFFSET_TYPE(), ModelAPI_AttributeString::typeId());
69 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
70 data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
71 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
73 data()->addAttribute(POINT_TO_PROJECT(), ModelAPI_AttributeSelection::typeId());
74 data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
75 data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
76 data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
78 data()->addAttribute(INTERSECTION_TYPE(), ModelAPI_AttributeString::typeId());
80 data()->addAttribute(INTERSECTION_PLANE_1(), ModelAPI_AttributeSelection::typeId());
81 data()->addAttribute(INTERSECTION_PLANE_2(), ModelAPI_AttributeSelection::typeId());
82 data()->addAttribute(INTERSECTION_PLANE_3(), ModelAPI_AttributeSelection::typeId());
85 //==================================================================================================
86 void ConstructionPlugin_Point::execute()
90 // to support compatibility with old documents where aCreationMethod did not exist
91 std::string aCreationMethod =
92 string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
93 string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
94 if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
95 aShape = createByXYZ();
96 } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
97 aShape = createByDistanceOnEdge();
98 } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
99 if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
100 aShape = createByProjectionOnEdge();
102 aShape = createByProjectionOnFace();
104 } else if(aCreationMethod == CREATION_METHOD_BY_INTERSECTION()) {
105 std::string anIntersectionType = string(INTERSECTION_TYPE())->value();
106 if (anIntersectionType == INTERSECTION_TYPE_BY_LINES()) {
107 aShape = createByLinesIntersection();
108 } else if (anIntersectionType == INTERSECTION_TYPE_BY_LINE_AND_PLANE()) {
109 // this may produce several points
110 std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
111 if (!aPoints.empty()) { // if no points found produce the standard error later
113 std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
114 for (; aPIter != aPoints.end(); aPIter++, anIndex++) {
115 std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
116 document()->createConstruction(data(), anIndex);
117 aConstr->setShape(*aPIter);
118 setResult(aConstr, anIndex);
120 removeResults(anIndex);
124 aShape = createByPlanesIntersection();
129 setError("Error: intersection not found.");
133 removeResults(1); // for case the point type was switched from multi-results type
134 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
135 aConstr->setShape(aShape);
139 //==================================================================================================
140 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
142 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
144 bool isCustomized = theDefaultPrs.get() != NULL &&
145 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
146 //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
153 return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
158 //==================================================================================================
159 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
162 AttributeSelectionPtr anEdgeSelection = selection(EDGE());
163 GeomShapePtr aShape = anEdgeSelection->value();
165 aShape = anEdgeSelection->context()->shape();
167 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
169 // Get distance value and percent flag.
171 bool anIsPercent = false;
172 if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
173 aValue = real(DISTANCE())->value();
176 aValue = real(RATIO())->value() * 100.0;
181 bool anIsReverse = boolean(REVERSE())->value();
183 return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
186 //==================================================================================================
187 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
190 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
191 GeomShapePtr aPointShape = aPointSelection->value();
192 if (!aPointShape.get()) {
193 aPointShape = aPointSelection->context()->shape();
195 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
198 AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
199 GeomShapePtr anEdgeShape = anEdgeSelection->value();
200 if (!anEdgeShape.get()) {
201 anEdgeShape = anEdgeSelection->context()->shape();
203 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
205 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
208 //==================================================================================================
209 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
212 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
213 GeomShapePtr aPointShape = aPointSelection->value();
214 if(!aPointShape.get()) {
215 aPointShape = aPointSelection->context()->shape();
217 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
220 AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
221 GeomShapePtr aPlaneShape = aPlaneSelection->value();
222 if(!aPlaneShape.get()) {
223 aPlaneShape = aPlaneSelection->context()->shape();
225 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
227 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
230 //==================================================================================================
231 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
234 AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
235 GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
236 if(!aFirstLineShape.get()) {
237 aFirstLineShape = aFirstLineSelection->context()->shape();
239 std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
242 AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
243 GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
244 if(!aSecondLineShape.get()) {
245 aSecondLineShape = aSecondLineSelection->context()->shape();
247 std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
249 return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
252 //==================================================================================================
253 std::list<std::shared_ptr<GeomAPI_Vertex> >
254 ConstructionPlugin_Point::createByLineAndPlaneIntersection()
257 AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
258 GeomShapePtr aLineShape = aLineSelection->value();
259 if(!aLineShape.get()) {
260 aLineShape = aLineSelection->context()->shape();
262 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
265 AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
266 GeomShapePtr aPlaneShape = aPlaneSelection->value();
267 if(!aPlaneShape.get()) {
268 aPlaneShape = aPlaneSelection->context()->shape();
270 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
272 if (!string(USE_OFFSET())->value().empty()) {
273 double anOffset = real(OFFSET())->value();
274 if (boolean(REVERSE_OFFSET())->value())
275 anOffset = -anOffset;
276 if (fabs(anOffset) > 1.e-9) { // move face
277 aFace->translate(aFace->getPlane()->direction(), anOffset);
281 return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
282 aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
285 //==================================================================================================
286 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
289 AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
290 GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
291 if (!aPlaneShape1.get()) {
292 aPlaneShape1 = aPlaneSelection1->context()->shape();
294 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
295 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
298 AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
299 GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
300 if (!aPlaneShape2.get()) {
301 aPlaneShape2 = aPlaneSelection2->context()->shape();
303 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
304 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
307 AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
308 GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
309 if (!aPlaneShape3.get()) {
310 aPlaneShape3 = aPlaneSelection3->context()->shape();
312 std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
313 std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
315 std::shared_ptr<GeomAPI_Vertex> aVertex;
317 std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
318 if (!anIntersectLine.get()) {
322 std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
324 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));