Salome HOME
updated copyright message
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Point.cpp
index 60b0af1c56b534fc677a4a609792c0215c320876..830920ad3005e7ae546a135ddbe947a4379b2f94 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "ConstructionPlugin_Point.h"
 
 #include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <GeomAlgoAPI_PointBuilder.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 
+#include <GeomAPI_Circ.h>
+#include <GeomDataAPI_Point.h>
+
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Vertex.h>
 #include <GeomAPI_Pln.h>
+#include <GeomAPI_ShapeIterator.h>
+
+#include <Config_PropManager.h>
 
 //==================================================================================================
 ConstructionPlugin_Point::ConstructionPlugin_Point()
@@ -49,20 +55,15 @@ const std::string& ConstructionPlugin_Point::getKind()
 //==================================================================================================
 void ConstructionPlugin_Point::initAttributes()
 {
-  data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
-  data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
-  data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
+  data()->addAttribute(POINT3D(), GeomDataAPI_Point::typeId());
 
   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
 
-/*
-  data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId());
-  data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId());
-*/
+  data()->addAttribute(INTERSECTION_LINE_1(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(INTERSECTION_LINE_2(), ModelAPI_AttributeSelection::typeId());
 
   data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
   data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
-
   data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
   data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
@@ -73,11 +74,19 @@ void ConstructionPlugin_Point::initAttributes()
   data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
 
-  data()->addAttribute(POINT_TO_PROJECT_ON_EDGE(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(POINT_TO_PROJECT(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
   data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
-
-  data()->addAttribute(POINT_TO_PROJECT_ON_FACE(), ModelAPI_AttributeSelection::typeId());
   data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
+
+  data()->addAttribute(INTERSECTION_TYPE(), ModelAPI_AttributeString::typeId());
+  data()->addAttribute(INTERSECTION_PLANE_1(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(INTERSECTION_PLANE_2(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(INTERSECTION_PLANE_3(), ModelAPI_AttributeSelection::typeId());
+
+  data()->addAttribute(GEOMETRICAL_PROPERTY_TYPE(), ModelAPI_AttributeString::typeId());
+  data()->addAttribute(OBJECT_FOR_CENTER_OF_GRAVITY(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(OBJECT_FOR_CENTER_OF_CIRCLE(), ModelAPI_AttributeSelection::typeId());
 }
 
 //==================================================================================================
@@ -93,26 +102,41 @@ void ConstructionPlugin_Point::execute()
     aShape = createByXYZ();
   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
     aShape = createByDistanceOnEdge();
-  } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION_ON_EDGE()) {
-    aShape = createByProjectionOnEdge();
-  } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION_ON_FACE()) {
-    aShape = createByProjectionOnFace();
-  } /* else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) {
-    aShape = createByLinesIntersection();
-  } */ else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION()) {
-    // this may produce several points
-    std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
-    if (!aPoints.empty()) { // if no points found produce the standard error later
-      int anIndex = 0;
-      std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
-      for(; aPIter != aPoints.end(); aPIter++, anIndex++) {
-        std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
-          document()->createConstruction(data(), anIndex);
-        aConstr->setShape(*aPIter);
-        setResult(aConstr, anIndex);
+  } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
+    if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
+      aShape = createByProjectionOnEdge();
+    } else {
+      aShape = createByProjectionOnFace();
+    }
+  } else if(aCreationMethod == CREATION_METHOD_BY_INTERSECTION()) {
+    std::string anIntersectionType = string(INTERSECTION_TYPE())->value();
+    if (anIntersectionType == INTERSECTION_TYPE_BY_LINES()) {
+      aShape = createByLinesIntersection();
+    } else if (anIntersectionType == INTERSECTION_TYPE_BY_LINE_AND_PLANE()) {
+      // this may produce several points
+      std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
+      if (!aPoints.empty()) { // if no points found produce the standard error later
+        int anIndex = 0;
+        std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
+        for (; aPIter != aPoints.end(); aPIter++, anIndex++) {
+          std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
+            document()->createConstruction(data(), anIndex);
+          aConstr->setInfinite(true);
+          aConstr->setShape(*aPIter);
+          setResult(aConstr, anIndex);
+        }
+        removeResults(anIndex);
+        return;
       }
-      removeResults(anIndex);
-      return;
+    } else {
+      aShape = createByPlanesIntersection();
+    }
+  } else if (aCreationMethod == CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
+    std::string aGeometricalPropertyType = string(GEOMETRICAL_PROPERTY_TYPE())->value();
+    if (aGeometricalPropertyType == GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY()) {
+      aShape = createByCenterOfGravity();
+    } else {
+      aShape = createByCenterOfCircle();
     }
   }
 
@@ -123,27 +147,42 @@ void ConstructionPlugin_Point::execute()
 
   removeResults(1); // for case the point type was switched from multi-results type
   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
+  aConstr->setInfinite(true);
   aConstr->setShape(aShape);
   setResult(aConstr);
 }
 
 //==================================================================================================
 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
-                                                     AISObjectPtr thePrs,
-                                                std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
+                                                     AISObjectPtr thePrs)
 {
-  bool isCustomized = theDefaultPrs.get() != NULL &&
-                      theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
+  std::vector<int> aColor;
+  // get color from the attribute of the result
+  if (theResult.get() != NULL &&
+    theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
+    AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
+    if (aColorAttr.get() && aColorAttr->size()) {
+      aColor.push_back(aColorAttr->value(0));
+      aColor.push_back(aColorAttr->value(1));
+      aColor.push_back(aColorAttr->value(2));
+    }
+  }
+  if (aColor.empty())
+    aColor = Config_PropManager::color("Visualization", COLOR_NAME());
+
+  bool isCustomized = false;
+  if (aColor.size() == 3)
+    isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
-  return true;
+  return isCustomized;
 }
 
 //==================================================================================================
 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
 {
-  return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
-                                          real(Y())->value(),
-                                          real(Z())->value());
+  AttributePointPtr aPoint =
+    std::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(POINT3D()));
+  return GeomAlgoAPI_PointBuilder::vertex(aPoint->x(), aPoint->y(), aPoint->z());
 }
 
 //==================================================================================================
@@ -178,7 +217,7 @@ std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge
 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
 {
   // Get point.
-  AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT_ON_EDGE());
+  AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
   GeomShapePtr aPointShape = aPointSelection->value();
   if (!aPointShape.get()) {
     aPointShape = aPointSelection->context()->shape();
@@ -200,7 +239,7 @@ std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEd
 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
 {
   // Get point.
-  AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT_ON_FACE());
+  AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
   GeomShapePtr aPointShape = aPointSelection->value();
   if(!aPointShape.get()) {
     aPointShape = aPointSelection->context()->shape();
@@ -218,12 +257,11 @@ std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFa
   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
 }
 
-/*
 //==================================================================================================
 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
 {
   // Get first line.
-  AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE());
+  AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
   if(!aFirstLineShape.get()) {
     aFirstLineShape = aFirstLineSelection->context()->shape();
@@ -231,7 +269,7 @@ std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersect
   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
 
   // Get second line.
-  AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE());
+  AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
   if(!aSecondLineShape.get()) {
     aSecondLineShape = aSecondLineSelection->context()->shape();
@@ -240,7 +278,6 @@ std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersect
 
   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
 }
-*/
 
 //==================================================================================================
 std::list<std::shared_ptr<GeomAPI_Vertex> >
@@ -252,7 +289,14 @@ std::list<std::shared_ptr<GeomAPI_Vertex> >
   if(!aLineShape.get()) {
     aLineShape = aLineSelection->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
+  GeomEdgePtr anEdge;
+  if (aLineShape->isEdge()) {
+    anEdge = aLineShape->edge();
+  }
+  else if (aLineShape->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aLineShape);
+    anEdge = anIt.current()->edge();
+  }
 
   // Get plane.
   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
@@ -260,7 +304,14 @@ std::list<std::shared_ptr<GeomAPI_Vertex> >
   if(!aPlaneShape.get()) {
     aPlaneShape = aPlaneSelection->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
+  GeomFacePtr aFace;
+  if (aPlaneShape->isFace()) {
+    aFace = aPlaneShape->face();
+  }
+  else if (aPlaneShape->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aPlaneShape);
+    aFace = anIt.current()->face();
+  }
 
   if (!string(USE_OFFSET())->value().empty()) {
     double anOffset = real(OFFSET())->value();
@@ -271,6 +322,92 @@ std::list<std::shared_ptr<GeomAPI_Vertex> >
     }
   }
 
-  return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
-    aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
+  return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace);
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
+{
+  // Get plane.
+  AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
+  GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
+  if (!aPlaneShape1.get()) {
+    aPlaneShape1 = aPlaneSelection1->context()->shape();
+  }
+  std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
+  std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
+
+  // Get plane.
+  AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
+  GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
+  if (!aPlaneShape2.get()) {
+    aPlaneShape2 = aPlaneSelection2->context()->shape();
+  }
+  std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
+  std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
+
+  // Get plane.
+  AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
+  GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
+  if (!aPlaneShape3.get()) {
+    aPlaneShape3 = aPlaneSelection3->context()->shape();
+  }
+  std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
+  std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
+
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+
+  std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
+  if (!anIntersectLine.get()) {
+    return aVertex;
+  }
+
+  std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
+  if (aPnt.get()) {
+    aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
+  }
+
+  return aVertex;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfGravity()
+{
+  // Get shape.
+  AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_GRAVITY());
+  GeomShapePtr aShape = aShapeSelection->value();
+  if (!aShape.get())
+  {
+    aShape = aShapeSelection->context()->shape();
+  }
+
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+  std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShape);
+  if (aPnt.get())
+  {
+    aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
+  }
+
+  return aVertex;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfCircle()
+{
+  // Get shape.
+  AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_CIRCLE());
+  GeomShapePtr aShape = aShapeSelection->value();
+  if (!aShape.get()) {
+    aShape = aShapeSelection->context()->shape();
+  }
+  std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
+  std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
+
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+  std::shared_ptr<GeomAPI_Pnt> aPnt = aCirc->center();
+  if (aPnt.get()) {
+    aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
+  }
+
+  return aVertex;
 }