1 // Copyright (C) 2014-2024 CEA, EDF
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->setInfinite(true);
125 aConstr->setShape(*aPIter);
126 setResult(aConstr, anIndex);
128 removeResults(anIndex);
132 aShape = createByPlanesIntersection();
134 } else if (aCreationMethod == CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
135 std::string aGeometricalPropertyType = string(GEOMETRICAL_PROPERTY_TYPE())->value();
136 if (aGeometricalPropertyType == GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY()) {
137 aShape = createByCenterOfGravity();
139 aShape = createByCenterOfCircle();
144 setError("Error: intersection not found.");
148 removeResults(1); // for case the point type was switched from multi-results type
149 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
150 aConstr->setInfinite(true);
151 aConstr->setShape(aShape);
155 //==================================================================================================
156 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
159 std::vector<int> aColor;
160 // get color from the attribute of the result
161 if (theResult.get() != NULL &&
162 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
163 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
164 if (aColorAttr.get() && aColorAttr->size()) {
165 aColor.push_back(aColorAttr->value(0));
166 aColor.push_back(aColorAttr->value(1));
167 aColor.push_back(aColorAttr->value(2));
171 aColor = Config_PropManager::color("Visualization", COLOR_NAME());
173 bool isCustomized = false;
174 if (aColor.size() == 3)
175 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
176 //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
180 //==================================================================================================
181 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
183 AttributePointPtr aPoint =
184 std::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(POINT3D()));
185 return GeomAlgoAPI_PointBuilder::vertex(aPoint->x(), aPoint->y(), aPoint->z());
188 //==================================================================================================
189 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
192 AttributeSelectionPtr anEdgeSelection = selection(EDGE());
193 GeomShapePtr aShape = anEdgeSelection->value();
195 aShape = anEdgeSelection->context()->shape();
197 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
199 // Get distance value and percent flag.
201 bool anIsPercent = false;
202 if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
203 aValue = real(DISTANCE())->value();
206 aValue = real(RATIO())->value() * 100.0;
211 bool anIsReverse = boolean(REVERSE())->value();
213 return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
216 //==================================================================================================
217 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
220 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
221 GeomShapePtr aPointShape = aPointSelection->value();
222 if (!aPointShape.get()) {
223 aPointShape = aPointSelection->context()->shape();
225 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
228 AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
229 GeomShapePtr anEdgeShape = anEdgeSelection->value();
230 if (!anEdgeShape.get()) {
231 anEdgeShape = anEdgeSelection->context()->shape();
233 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
235 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
238 //==================================================================================================
239 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
242 AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
243 GeomShapePtr aPointShape = aPointSelection->value();
244 if(!aPointShape.get()) {
245 aPointShape = aPointSelection->context()->shape();
247 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
250 AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
251 GeomShapePtr aPlaneShape = aPlaneSelection->value();
252 if(!aPlaneShape.get()) {
253 aPlaneShape = aPlaneSelection->context()->shape();
255 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
257 return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
260 //==================================================================================================
261 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
264 AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
265 GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
266 if(!aFirstLineShape.get()) {
267 aFirstLineShape = aFirstLineSelection->context()->shape();
269 std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
272 AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
273 GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
274 if(!aSecondLineShape.get()) {
275 aSecondLineShape = aSecondLineSelection->context()->shape();
277 std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
279 return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
282 //==================================================================================================
283 std::list<std::shared_ptr<GeomAPI_Vertex> >
284 ConstructionPlugin_Point::createByLineAndPlaneIntersection()
287 AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
288 GeomShapePtr aLineShape = aLineSelection->value();
289 if(!aLineShape.get()) {
290 aLineShape = aLineSelection->context()->shape();
293 if (aLineShape->isEdge()) {
294 anEdge = aLineShape->edge();
296 else if (aLineShape->isCompound()) {
297 GeomAPI_ShapeIterator anIt(aLineShape);
298 anEdge = anIt.current()->edge();
302 AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
303 GeomShapePtr aPlaneShape = aPlaneSelection->value();
304 if(!aPlaneShape.get()) {
305 aPlaneShape = aPlaneSelection->context()->shape();
308 if (aPlaneShape->isFace()) {
309 aFace = aPlaneShape->face();
311 else if (aPlaneShape->isCompound()) {
312 GeomAPI_ShapeIterator anIt(aPlaneShape);
313 aFace = anIt.current()->face();
316 if (!string(USE_OFFSET())->value().empty()) {
317 double anOffset = real(OFFSET())->value();
318 if (boolean(REVERSE_OFFSET())->value())
319 anOffset = -anOffset;
320 if (fabs(anOffset) > 1.e-9) { // move face
321 aFace->translate(aFace->getPlane()->direction(), anOffset);
325 return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace);
328 //==================================================================================================
329 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
332 AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
333 GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
334 if (!aPlaneShape1.get()) {
335 aPlaneShape1 = aPlaneSelection1->context()->shape();
337 std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
338 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
341 AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
342 GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
343 if (!aPlaneShape2.get()) {
344 aPlaneShape2 = aPlaneSelection2->context()->shape();
346 std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
347 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
350 AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
351 GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
352 if (!aPlaneShape3.get()) {
353 aPlaneShape3 = aPlaneSelection3->context()->shape();
355 std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
356 std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
358 std::shared_ptr<GeomAPI_Vertex> aVertex;
360 std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
361 if (!anIntersectLine.get()) {
365 std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
367 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
373 //==================================================================================================
374 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfGravity()
377 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_GRAVITY());
378 GeomShapePtr aShape = aShapeSelection->value();
381 aShape = aShapeSelection->context()->shape();
384 std::shared_ptr<GeomAPI_Vertex> aVertex;
385 std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShape);
388 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
394 //==================================================================================================
395 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfCircle()
398 AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_CIRCLE());
399 GeomShapePtr aShape = aShapeSelection->value();
401 aShape = aShapeSelection->context()->shape();
403 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
404 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
406 std::shared_ptr<GeomAPI_Vertex> aVertex;
407 std::shared_ptr<GeomAPI_Pnt> aPnt = aCirc->center();
409 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));