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_Circ.h>
33 #include <GeomDataAPI_Point.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Pnt.h>
37 #include <GeomAPI_Vertex.h>
38 #include <GeomAPI_Pln.h>
39 #include <GeomAPI_ShapeIterator.h>
41 //==================================================================================================
42 ConstructionPlugin_Point::ConstructionPlugin_Point()
46 //==================================================================================================
47 const std::string& ConstructionPlugin_Point::getKind()
49 static std::string MY_KIND = ConstructionPlugin_Point::ID();
53 //==================================================================================================
54 void ConstructionPlugin_Point::initAttributes()
56 data()->addAttribute(POINT3D(), GeomDataAPI_Point::typeId());
58 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
60 data()->addAttribute(INTERSECTION_LINE_1(), ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(INTERSECTION_LINE_2(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
64 data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
66 data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
69 data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(OFFSET_TYPE(), ModelAPI_AttributeString::typeId());
71 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
72 data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
73 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
75 data()->addAttribute(POINT_TO_PROJECT(), ModelAPI_AttributeSelection::typeId());
76 data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
77 data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
78 data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
80 data()->addAttribute(INTERSECTION_TYPE(), ModelAPI_AttributeString::typeId());
81 data()->addAttribute(INTERSECTION_PLANE_1(), ModelAPI_AttributeSelection::typeId());
82 data()->addAttribute(INTERSECTION_PLANE_2(), ModelAPI_AttributeSelection::typeId());
83 data()->addAttribute(INTERSECTION_PLANE_3(), ModelAPI_AttributeSelection::typeId());
85 data()->addAttribute(GEOMETRICAL_PROPERTY_TYPE(), ModelAPI_AttributeString::typeId());
86 data()->addAttribute(OBJECT_FOR_CENTER_OF_GRAVITY(), ModelAPI_AttributeSelection::typeId());
87 data()->addAttribute(OBJECT_FOR_CENTER_OF_CIRCLE(), ModelAPI_AttributeSelection::typeId());
90 //==================================================================================================
91 void ConstructionPlugin_Point::execute()
95 // to support compatibility with old documents where aCreationMethod did not exist
96 std::string aCreationMethod =
97 string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
98 string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
99 if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
100 aShape = createByXYZ();
101 } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
102 aShape = createByDistanceOnEdge();
103 } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
104 if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
105 aShape = createByProjectionOnEdge();
107 aShape = createByProjectionOnFace();
109 } else if(aCreationMethod == CREATION_METHOD_BY_INTERSECTION()) {
110 std::string anIntersectionType = string(INTERSECTION_TYPE())->value();
111 if (anIntersectionType == INTERSECTION_TYPE_BY_LINES()) {
112 aShape = createByLinesIntersection();
113 } else if (anIntersectionType == INTERSECTION_TYPE_BY_LINE_AND_PLANE()) {
114 // this may produce several points
115 std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
116 if (!aPoints.empty()) { // if no points found produce the standard error later
118 std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
119 for (; aPIter != aPoints.end(); aPIter++, anIndex++) {
120 std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
121 document()->createConstruction(data(), anIndex);
122 aConstr->setShape(*aPIter);
123 setResult(aConstr, anIndex);
125 removeResults(anIndex);
129 aShape = createByPlanesIntersection();
131 } else if (aCreationMethod == CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
132 std::string aGeometricalPropertyType = string(GEOMETRICAL_PROPERTY_TYPE())->value();
133 if (aGeometricalPropertyType == GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY()) {
134 aShape = createByCenterOfGravity();
136 aShape = createByCenterOfCircle();
141 setError("Error: intersection not found.");
145 removeResults(1); // for case the point type was switched from multi-results type
146 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
147 aConstr->setInfinite(true);
148 aConstr->setShape(aShape);
152 //==================================================================================================
153 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
155 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
157 bool isCustomized = theDefaultPrs.get() != NULL &&
158 theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
159 //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
163 //==================================================================================================
164 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
166 AttributePointPtr aPoint =
167 std::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(POINT3D()));
168 return GeomAlgoAPI_PointBuilder::vertex(aPoint->x(), aPoint->y(), aPoint->z());
171 //==================================================================================================
172 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
175 AttributeSelectionPtr anEdgeSelection = selection(EDGE());
176 GeomShapePtr aShape = anEdgeSelection->value();
178 aShape = anEdgeSelection->context()->shape();
180 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
182 // Get distance value and percent flag.
184 bool anIsPercent = false;
185 if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
186 aValue = real(DISTANCE())->value();
189 aValue = real(RATIO())->value() * 100.0;
194 bool anIsReverse = boolean(REVERSE())->value();
196 return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
199 //==================================================================================================
200 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
203 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
204 GeomShapePtr aPointShape = aPointSelection->value();
205 if (!aPointShape.get()) {
206 aPointShape = aPointSelection->context()->shape();
208 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
211 AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
212 GeomShapePtr anEdgeShape = anEdgeSelection->value();
213 if (!anEdgeShape.get()) {
214 anEdgeShape = anEdgeSelection->context()->shape();
216 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
218 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
221 //==================================================================================================
222 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
225 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
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));
233 AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
234 GeomShapePtr aPlaneShape = aPlaneSelection->value();
235 if(!aPlaneShape.get()) {
236 aPlaneShape = aPlaneSelection->context()->shape();
238 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
240 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
243 //==================================================================================================
244 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
247 AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
248 GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
249 if(!aFirstLineShape.get()) {
250 aFirstLineShape = aFirstLineSelection->context()->shape();
252 std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
255 AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
256 GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
257 if(!aSecondLineShape.get()) {
258 aSecondLineShape = aSecondLineSelection->context()->shape();
260 std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
262 return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
265 //==================================================================================================
266 std::list<std::shared_ptr<GeomAPI_Vertex> >
267 ConstructionPlugin_Point::createByLineAndPlaneIntersection()
270 AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
271 GeomShapePtr aLineShape = aLineSelection->value();
272 if(!aLineShape.get()) {
273 aLineShape = aLineSelection->context()->shape();
276 if (aLineShape->isEdge()) {
277 anEdge = aLineShape->edge();
279 else if (aLineShape->isCompound()) {
280 GeomAPI_ShapeIterator anIt(aLineShape);
281 anEdge = anIt.current()->edge();
285 AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
286 GeomShapePtr aPlaneShape = aPlaneSelection->value();
287 if(!aPlaneShape.get()) {
288 aPlaneShape = aPlaneSelection->context()->shape();
291 if (aPlaneShape->isFace()) {
292 aFace = aPlaneShape->face();
294 else if (aPlaneShape->isCompound()) {
295 GeomAPI_ShapeIterator anIt(aPlaneShape);
296 aFace = anIt.current()->face();
299 if (!string(USE_OFFSET())->value().empty()) {
300 double anOffset = real(OFFSET())->value();
301 if (boolean(REVERSE_OFFSET())->value())
302 anOffset = -anOffset;
303 if (fabs(anOffset) > 1.e-9) { // move face
304 aFace->translate(aFace->getPlane()->direction(), anOffset);
308 return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
309 aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
312 //==================================================================================================
313 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
316 AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
317 GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
318 if (!aPlaneShape1.get()) {
319 aPlaneShape1 = aPlaneSelection1->context()->shape();
321 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
322 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
325 AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
326 GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
327 if (!aPlaneShape2.get()) {
328 aPlaneShape2 = aPlaneSelection2->context()->shape();
330 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
331 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
334 AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
335 GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
336 if (!aPlaneShape3.get()) {
337 aPlaneShape3 = aPlaneSelection3->context()->shape();
339 std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
340 std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
342 std::shared_ptr<GeomAPI_Vertex> aVertex;
344 std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
345 if (!anIntersectLine.get()) {
349 std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
351 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
357 //==================================================================================================
358 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfGravity()
361 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_GRAVITY());
362 GeomShapePtr aShape = aShapeSelection->value();
365 aShape = aShapeSelection->context()->shape();
368 std::shared_ptr<GeomAPI_Vertex> aVertex;
369 std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShape);
372 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
378 //==================================================================================================
379 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfCircle()
382 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_CIRCLE());
383 GeomShapePtr aShape = aShapeSelection->value();
385 aShape = aShapeSelection->context()->shape();
387 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
388 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
390 std::shared_ptr<GeomAPI_Vertex> aVertex;
391 std::shared_ptr<GeomAPI_Pnt> aPnt = aCirc->center();
393 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));