1 // Copyright (C) 2014-2020 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
20 #include "ConstructionPlugin_Point.h"
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeIntArray.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 #include <Config_PropManager.h>
43 //==================================================================================================
44 ConstructionPlugin_Point::ConstructionPlugin_Point()
48 //==================================================================================================
49 const std::string& ConstructionPlugin_Point::getKind()
51 static std::string MY_KIND = ConstructionPlugin_Point::ID();
55 //==================================================================================================
56 void ConstructionPlugin_Point::initAttributes()
58 data()->addAttribute(POINT3D(), GeomDataAPI_Point::typeId());
60 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
62 data()->addAttribute(INTERSECTION_LINE_1(), ModelAPI_AttributeSelection::typeId());
63 data()->addAttribute(INTERSECTION_LINE_2(), ModelAPI_AttributeSelection::typeId());
65 data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
68 data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
69 data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
71 data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(OFFSET_TYPE(), ModelAPI_AttributeString::typeId());
73 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
74 data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
75 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
77 data()->addAttribute(POINT_TO_PROJECT(), ModelAPI_AttributeSelection::typeId());
78 data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
79 data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
80 data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
82 data()->addAttribute(INTERSECTION_TYPE(), ModelAPI_AttributeString::typeId());
83 data()->addAttribute(INTERSECTION_PLANE_1(), ModelAPI_AttributeSelection::typeId());
84 data()->addAttribute(INTERSECTION_PLANE_2(), ModelAPI_AttributeSelection::typeId());
85 data()->addAttribute(INTERSECTION_PLANE_3(), ModelAPI_AttributeSelection::typeId());
87 data()->addAttribute(GEOMETRICAL_PROPERTY_TYPE(), ModelAPI_AttributeString::typeId());
88 data()->addAttribute(OBJECT_FOR_CENTER_OF_GRAVITY(), ModelAPI_AttributeSelection::typeId());
89 data()->addAttribute(OBJECT_FOR_CENTER_OF_CIRCLE(), ModelAPI_AttributeSelection::typeId());
92 //==================================================================================================
93 void ConstructionPlugin_Point::execute()
97 // to support compatibility with old documents where aCreationMethod did not exist
98 std::string aCreationMethod =
99 string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
100 string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
101 if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
102 aShape = createByXYZ();
103 } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
104 aShape = createByDistanceOnEdge();
105 } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
106 if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
107 aShape = createByProjectionOnEdge();
109 aShape = createByProjectionOnFace();
111 } else if(aCreationMethod == CREATION_METHOD_BY_INTERSECTION()) {
112 std::string anIntersectionType = string(INTERSECTION_TYPE())->value();
113 if (anIntersectionType == INTERSECTION_TYPE_BY_LINES()) {
114 aShape = createByLinesIntersection();
115 } else if (anIntersectionType == INTERSECTION_TYPE_BY_LINE_AND_PLANE()) {
116 // this may produce several points
117 std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
118 if (!aPoints.empty()) { // if no points found produce the standard error later
120 std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
121 for (; aPIter != aPoints.end(); aPIter++, anIndex++) {
122 std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
123 document()->createConstruction(data(), anIndex);
124 aConstr->setShape(*aPIter);
125 setResult(aConstr, anIndex);
127 removeResults(anIndex);
131 aShape = createByPlanesIntersection();
133 } else if (aCreationMethod == CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
134 std::string aGeometricalPropertyType = string(GEOMETRICAL_PROPERTY_TYPE())->value();
135 if (aGeometricalPropertyType == GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY()) {
136 aShape = createByCenterOfGravity();
138 aShape = createByCenterOfCircle();
143 setError("Error: intersection not found.");
147 removeResults(1); // for case the point type was switched from multi-results type
148 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
149 aConstr->setInfinite(true);
150 aConstr->setShape(aShape);
154 //==================================================================================================
155 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
158 std::vector<int> aColor;
159 // get color from the attribute of the result
160 if (theResult.get() != NULL &&
161 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
162 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
163 if (aColorAttr.get() && aColorAttr->size()) {
164 aColor.push_back(aColorAttr->value(0));
165 aColor.push_back(aColorAttr->value(1));
166 aColor.push_back(aColorAttr->value(2));
170 aColor = Config_PropManager::color("Visualization", COLOR_NAME());
172 bool isCustomized = false;
173 if (aColor.size() == 3)
174 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
175 //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
179 //==================================================================================================
180 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
182 AttributePointPtr aPoint =
183 std::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(POINT3D()));
184 return GeomAlgoAPI_PointBuilder::vertex(aPoint->x(), aPoint->y(), aPoint->z());
187 //==================================================================================================
188 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
191 AttributeSelectionPtr anEdgeSelection = selection(EDGE());
192 GeomShapePtr aShape = anEdgeSelection->value();
194 aShape = anEdgeSelection->context()->shape();
196 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
198 // Get distance value and percent flag.
200 bool anIsPercent = false;
201 if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
202 aValue = real(DISTANCE())->value();
205 aValue = real(RATIO())->value() * 100.0;
210 bool anIsReverse = boolean(REVERSE())->value();
212 return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
215 //==================================================================================================
216 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
219 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
220 GeomShapePtr aPointShape = aPointSelection->value();
221 if (!aPointShape.get()) {
222 aPointShape = aPointSelection->context()->shape();
224 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
227 AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
228 GeomShapePtr anEdgeShape = anEdgeSelection->value();
229 if (!anEdgeShape.get()) {
230 anEdgeShape = anEdgeSelection->context()->shape();
232 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
234 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
237 //==================================================================================================
238 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
241 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
242 GeomShapePtr aPointShape = aPointSelection->value();
243 if(!aPointShape.get()) {
244 aPointShape = aPointSelection->context()->shape();
246 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
249 AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
250 GeomShapePtr aPlaneShape = aPlaneSelection->value();
251 if(!aPlaneShape.get()) {
252 aPlaneShape = aPlaneSelection->context()->shape();
254 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
256 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
259 //==================================================================================================
260 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
263 AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
264 GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
265 if(!aFirstLineShape.get()) {
266 aFirstLineShape = aFirstLineSelection->context()->shape();
268 std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
271 AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
272 GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
273 if(!aSecondLineShape.get()) {
274 aSecondLineShape = aSecondLineSelection->context()->shape();
276 std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
278 return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
281 //==================================================================================================
282 std::list<std::shared_ptr<GeomAPI_Vertex> >
283 ConstructionPlugin_Point::createByLineAndPlaneIntersection()
286 AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
287 GeomShapePtr aLineShape = aLineSelection->value();
288 if(!aLineShape.get()) {
289 aLineShape = aLineSelection->context()->shape();
292 if (aLineShape->isEdge()) {
293 anEdge = aLineShape->edge();
295 else if (aLineShape->isCompound()) {
296 GeomAPI_ShapeIterator anIt(aLineShape);
297 anEdge = anIt.current()->edge();
301 AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
302 GeomShapePtr aPlaneShape = aPlaneSelection->value();
303 if(!aPlaneShape.get()) {
304 aPlaneShape = aPlaneSelection->context()->shape();
307 if (aPlaneShape->isFace()) {
308 aFace = aPlaneShape->face();
310 else if (aPlaneShape->isCompound()) {
311 GeomAPI_ShapeIterator anIt(aPlaneShape);
312 aFace = anIt.current()->face();
315 if (!string(USE_OFFSET())->value().empty()) {
316 double anOffset = real(OFFSET())->value();
317 if (boolean(REVERSE_OFFSET())->value())
318 anOffset = -anOffset;
319 if (fabs(anOffset) > 1.e-9) { // move face
320 aFace->translate(aFace->getPlane()->direction(), anOffset);
324 return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace);
327 //==================================================================================================
328 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
331 AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
332 GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
333 if (!aPlaneShape1.get()) {
334 aPlaneShape1 = aPlaneSelection1->context()->shape();
336 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
337 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
340 AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
341 GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
342 if (!aPlaneShape2.get()) {
343 aPlaneShape2 = aPlaneSelection2->context()->shape();
345 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
346 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
349 AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
350 GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
351 if (!aPlaneShape3.get()) {
352 aPlaneShape3 = aPlaneSelection3->context()->shape();
354 std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
355 std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
357 std::shared_ptr<GeomAPI_Vertex> aVertex;
359 std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
360 if (!anIntersectLine.get()) {
364 std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
366 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
372 //==================================================================================================
373 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfGravity()
376 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_GRAVITY());
377 GeomShapePtr aShape = aShapeSelection->value();
380 aShape = aShapeSelection->context()->shape();
383 std::shared_ptr<GeomAPI_Vertex> aVertex;
384 std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShape);
387 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
393 //==================================================================================================
394 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfCircle()
397 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_CIRCLE());
398 GeomShapePtr aShape = aShapeSelection->value();
400 aShape = aShapeSelection->context()->shape();
402 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
403 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
405 std::shared_ptr<GeomAPI_Vertex> aVertex;
406 std::shared_ptr<GeomAPI_Pnt> aPnt = aCirc->center();
408 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));