Salome HOME
Updated copyright comment
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Point.cpp
index ef2ccc8100f05a2b888c5aa240aa8b23197d84e3..2f44c5081d1d22075f255fe22f4878e27000ed3c 100644 (file)
@@ -1,21 +1,33 @@
-// Name   : ConstructionAPI_Point.cpp
-// Purpose: 
+// Copyright (C) 2014-2024  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-// History:
-// 29/03/16 - Sergey POKHODENKO - Creation of the file
 
 #include "ConstructionAPI_Point.h"
 
+#include <GeomAPI_Pnt.h>
 #include <GeomAPI_Shape.h>
+#include <GeomAPI_Vertex.h>
 
+#include <ModelHighAPI_Double.h>
+#include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Selection.h>
 #include <ModelHighAPI_Tools.h>
 
-#include <algorithm>
-
-static GeomAPI_Shape::ShapeType shapeTypeByStr(const std::string& theShapeTypeStr);
-static GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection);
-
 //==================================================================================================
 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature)
 : ModelHighAPI_Interface(theFeature)
@@ -38,13 +50,13 @@ ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feat
 //==================================================================================================
 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
                                              const ModelHighAPI_Selection& theEdge,
-                                             const ModelHighAPI_Double& theDistanceValue,
-                                             const bool theDistancePercent,
+                                             const ModelHighAPI_Double& theOffset,
+                                             const bool theUseRatio,
                                              const bool theReverse)
 : ModelHighAPI_Interface(theFeature)
 {
   if(initialize()) {
-    setByDistanceOnEdge(theEdge, theDistanceValue, theDistancePercent, theReverse);
+    setByOffsetOnEdge(theEdge, theOffset, theUseRatio, theReverse);
   }
 }
 
@@ -57,10 +69,12 @@ ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feat
   if(initialize()) {
     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
-
     if(aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::FACE) {
       // If first object is vertex and second object is face then set by projection.
-      setByProjection(theObject1, theObject2);
+      setByProjectionOnFace(theObject1, theObject2);
+    } else if (aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::EDGE) {
+      // If first object is vertex and second object is edge then set by projection.
+      setByProjectionOnEdge(theObject1, theObject2);
     } else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::EDGE) {
       // If both objects are edges then set by lines intersection.
       setByLinesIntersection(theObject1, theObject2);
@@ -71,6 +85,76 @@ ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feat
   }
 }
 
+//==================================================================================================
+ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                                             const ModelHighAPI_Selection& theObject1,
+                                             const ModelHighAPI_Selection& theObject2,
+                                             const ModelHighAPI_Selection& theObject3)
+: ModelHighAPI_Interface(theFeature)
+{
+  if (initialize())
+  {
+    GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
+    GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
+    GeomAPI_Shape::ShapeType aType3 = getShapeType(theObject3);
+    if (aType1 == GeomAPI_Shape::FACE
+        && aType2 == GeomAPI_Shape::FACE
+        && aType3 == GeomAPI_Shape::FACE)
+    {
+      setByPlanesIntersection(theObject1, theObject2, theObject3);
+    }
+  }
+}
+
+//==================================================================================================
+ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                                             const ModelHighAPI_Selection& theObject,
+                                             const bool theIsCircularEdge,
+                                             const bool theIsXYZSelection)
+: ModelHighAPI_Interface(theFeature)
+{
+  if (initialize())
+  {
+    if (theIsCircularEdge) {
+      setByCenterOfCircle(theObject);
+    } else if (theObject.shapeType() == "VERTEX" && theIsXYZSelection) {
+      // This is tricky way to get vertex shape.
+      fillAttribute(theObject, mypointToProject);
+      GeomShapePtr aShape = mypointToProject->value();
+      if (!aShape.get()) {
+        ResultPtr aContext = mypointToProject->context();
+        if (!aContext.get()) {
+          fillAttribute(ModelHighAPI_Selection(), mypointToProject);
+          return;
+        }
+        aShape = aContext->shape();
+      }
+
+      if (!aShape.get()) {
+        fillAttribute(ModelHighAPI_Selection(), mypointToProject);
+        return;
+      }
+
+      GeomVertexPtr aVertex = aShape->vertex();
+      if (!aVertex.get()) {
+        fillAttribute(ModelHighAPI_Selection(), mypointToProject);
+        return;
+      }
+
+      GeomPointPtr aPnt = aVertex->point();
+      if (!aPnt.get()) {
+        fillAttribute(ModelHighAPI_Selection(), mypointToProject);
+        return;
+      }
+
+      fillAttribute(ModelHighAPI_Selection(), mypointToProject);
+      setByXYZ(aPnt->x(), aPnt->y(), aPnt->z());
+    } else {
+      setByCenterOfGravity(theObject);
+    }
+  }
+}
+
 //==================================================================================================
 ConstructionAPI_Point::~ConstructionAPI_Point()
 {
@@ -83,35 +167,56 @@ void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
                                      const ModelHighAPI_Double& theZ)
 {
   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
-  fillAttribute(theX, myx);
-  fillAttribute(theY, myy);
-  fillAttribute(theZ, myz);
+  fillAttribute(theX, theY, theZ, mypoint);
 
-  execute();
+  execute(false);
 }
 
 //==================================================================================================
-void ConstructionAPI_Point::setByDistanceOnEdge(const ModelHighAPI_Selection& theEdge,
-                                                const ModelHighAPI_Double& theDistanceValue,
-                                                const bool theDistancePercent,
-                                                const bool theReverse)
+void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
+                                              const ModelHighAPI_Double& theOffset,
+                                              const bool theUseRatio,
+                                              const bool theReverse)
 {
   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
   fillAttribute(theEdge, myedge);
-  fillAttribute(theDistanceValue, mydistanceValue);
-  fillAttribute(theDistancePercent, mydistancePercent);
+  if (theUseRatio) {
+    fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
+    fillAttribute(theOffset, myratio);
+  }
+  else {
+    fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
+    fillAttribute(theOffset, mydistance);
+  }
   fillAttribute(theReverse, myreverse);
 
   execute();
 }
 
 //==================================================================================================
-void ConstructionAPI_Point::setByProjection(const ModelHighAPI_Selection& theVertex,
-                                            const ModelHighAPI_Selection& theFace)
+void ConstructionAPI_Point::setByProjectionOnEdge(const ModelHighAPI_Selection& theVertex,
+                                                  const ModelHighAPI_Selection& theEdge)
 {
-  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(), mycreationMethod);
-  fillAttribute(theVertex, mypoint);
-  fillAttribute(theFace, myplane);
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
+                mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE(),
+                myprojectionType);
+  fillAttribute(theVertex, mypointToProject);
+  fillAttribute(theEdge, myedgeForPointProjection);
+
+  execute();
+}
+
+//==================================================================================================
+void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
+                                                  const ModelHighAPI_Selection& theFace)
+{
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
+                mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_FACE(),
+                myprojectionType);
+  fillAttribute(theVertex, mypointToProject);
+  fillAttribute(theFace, myfaceForPointProjection);
 
   execute();
 }
@@ -120,9 +225,11 @@ void ConstructionAPI_Point::setByProjection(const ModelHighAPI_Selection& theVer
 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
                                                    const ModelHighAPI_Selection& theEdge2)
 {
-  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINES_INTERSECTION(), mycreationMethod);
-  fillAttribute(theEdge1, myfirstLine);
-  fillAttribute(theEdge2, mysecondLine);
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES(),
+                myintersectionType);
+  fillAttribute(theEdge1, myintersectionLine1);
+  fillAttribute(theEdge2, myintersectionLine2);
 
   execute();
 }
@@ -131,20 +238,123 @@ void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection&
 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
                                                           const ModelHighAPI_Selection& theFace)
 {
-  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_LINE_AND_PLANE_INTERSECTION(), mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE(),
+                myintersectionType);
   fillAttribute(theEdge, myintersectionLine);
   fillAttribute(theFace, myintersectionPlane);
+  fillAttribute("", useOffset()); // not used by default
+  execute();
+}
+
+//==================================================================================================
+void ConstructionAPI_Point::setByPlanesIntersection(const ModelHighAPI_Selection& theFace1,
+                                                    const ModelHighAPI_Selection& theFace2,
+                                                    const ModelHighAPI_Selection& theFace3)
+{
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES(),
+                myintersectionType);
+  fillAttribute(theFace1, myintersectionPlane1);
+  fillAttribute(theFace2, myintersectionPlane2);
+  fillAttribute(theFace3, myintersectionPlane3);
 
   execute();
 }
 
+//==================================================================================================
+void ConstructionAPI_Point::setByCenterOfGravity(const ModelHighAPI_Selection& theObject)
+{
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
+                mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY(),
+                mygeometricalPropertyType);
+  fillAttribute(theObject, myobjectForCenterOfGravity);
+
+  execute();
+}
+
+//==================================================================================================
+void ConstructionAPI_Point::setByCenterOfCircle(const ModelHighAPI_Selection& theObject)
+{
+  fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
+                mycreationMethod);
+  fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_CIRCLE(),
+                mygeometricalPropertyType);
+  fillAttribute(theObject, myobjectForCenterOfCircle);
+
+  execute();
+}
+
+//==================================================================================================
+void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
+{
+  FeaturePtr aBase = feature();
+  const std::string& aDocName = theDumper.name(aBase->document());
+  const std::string aMeth = creationMethod()->value();
+
+  // common part
+  theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
+
+  if (aMeth == "" || // default is XYZ
+      aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
+    theDumper << point();
+  } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION()) {
+    const std::string anIntersectionType = intersectionType()->value();
+    if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES())
+    {
+      theDumper << intersectionLine1() << ", " << intersectionLine2();
+    }
+    else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE())
+    {
+      theDumper << intersectionLine() << ", " << intersectionPlane();
+      if (!useOffset()->value().empty()) { // call method with defined offset
+        theDumper << ", " << offset() << ", " << reverseOffset();
+      }
+    }
+    else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES())
+    {
+      theDumper << intersectionPlane1() << ", "
+                << intersectionPlane2() << ", "
+                << intersectionPlane3();
+    }
+  } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
+    theDumper << edge() << ", ";
+    if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
+      theDumper << distance() << ", " << false;
+    }
+    else {
+      theDumper << ratio() << ", " << true;
+    }
+    theDumper << ", " << reverse()->value();
+  } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION()) {
+    theDumper << pointToProject() << ", ";
+    if (projectionType()->value() == ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE()) {
+      theDumper << edgeForPointProjection();
+    } else {
+      theDumper << faceForPointProjection();
+    }
+  } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
+    if (geometricalPropertyType()->value() ==
+          ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY())
+    {
+      theDumper << objectForCenterOfGravity();
+    }
+    else
+    {
+      theDumper << objectForCenterOfCircle() << ", " << true;
+    }
+  }
+
+  theDumper << ")" << std::endl;
+}
+
 //==================================================================================================
 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
                   const ModelHighAPI_Double& theX,
                   const ModelHighAPI_Double& theY,
                   const ModelHighAPI_Double& theZ)
 {
-  // TODO(spo): check that thePart is not empty
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
 }
@@ -152,13 +362,12 @@ PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
 //==================================================================================================
 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
                   const ModelHighAPI_Selection& theEdge,
-                  const ModelHighAPI_Double& theDistanceValue,
-                  const bool theDistancePercent,
+                  const ModelHighAPI_Double& theOffset,
+                  const bool theUseRatio,
                   const bool theReverse)
 {
-  // TODO(spo): check that thePart is not empty
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
-  return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theDistanceValue, theDistancePercent, theReverse));
+  return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
 }
 
 //==================================================================================================
@@ -166,67 +375,50 @@ PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
                   const ModelHighAPI_Selection& theObject1,
                   const ModelHighAPI_Selection& theObject2)
 {
-  // TODO(spo): check that thePart is not empty
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
 }
 
 //==================================================================================================
-GeomAPI_Shape::ShapeType shapeTypeByStr(const std::string& theShapeTypeStr)
-{
-  GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
-
-  std::string aShapeTypeStr = theShapeTypeStr;
-  std::transform(aShapeTypeStr.begin(), aShapeTypeStr.end(), aShapeTypeStr.begin(), ::tolower);
-
-  if(theShapeTypeStr == "compound") {
-    aShapeType = GeomAPI_Shape::COMPOUND;
-  } else if(theShapeTypeStr == "compsolid") {
-    aShapeType = GeomAPI_Shape::COMPSOLID;
-  } else if(theShapeTypeStr == "solid") {
-    aShapeType = GeomAPI_Shape::SOLID;
-  } else if(theShapeTypeStr == "shell") {
-    aShapeType = GeomAPI_Shape::SHELL;
-  } else if(theShapeTypeStr == "face") {
-    aShapeType = GeomAPI_Shape::FACE;
-  } else if(theShapeTypeStr == "wire") {
-    aShapeType = GeomAPI_Shape::WIRE;
-  } else if(theShapeTypeStr == "edge") {
-    aShapeType = GeomAPI_Shape::EDGE;
-  } else if(theShapeTypeStr == "vertex") {
-    aShapeType = GeomAPI_Shape::VERTEX;
-  } else if(theShapeTypeStr == "shape") {
-    aShapeType = GeomAPI_Shape::SHAPE;
-  }
+PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
+                  const ModelHighAPI_Selection& theObject1,
+                  const ModelHighAPI_Selection& theObject2,
+                  const ModelHighAPI_Double& theDistanceValue,
+                  const bool theReverse)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
+  PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
 
-  return aShapeType;
+  fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
+  fillAttribute(theDistanceValue, anAPI->offset());
+  fillAttribute(theReverse, anAPI->reverseOffset());
+
+  return anAPI;
 }
 
 //==================================================================================================
-GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
+PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
+                  const ModelHighAPI_Selection& theObject1,
+                  const ModelHighAPI_Selection& theObject2,
+                  const ModelHighAPI_Selection& theObject3)
 {
-  GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
+  return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2, theObject3));
+}
 
-  switch(theSelection.variantType()) {
-    case ModelHighAPI_Selection::VT_ResultSubShapePair: {
-      ResultSubShapePair aPair = theSelection.resultSubShapePair();
-      GeomShapePtr aShape = aPair.second;
-      if(!aShape.get()) {
-        aShape = aPair.first->shape();
-      }
-      if(!aShape.get()) {
-        return aShapeType;
-      }
-      aShapeType = aShape->shapeType();
-      break;
-    }
-    case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
-      TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
-      std::string aType = aPair.first;
-      aShapeType = shapeTypeByStr(aType);
-      break;
-    }
-  }
+//==================================================================================================
+PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
+                  const ModelHighAPI_Selection& theObject,
+                  const bool theIsCircularEdge)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
+  return PointPtr(new ConstructionAPI_Point(aFeature, theObject, theIsCircularEdge));
+}
 
-  return aShapeType;
+//==================================================================================================
+PointPtr addPointXYZ(const std::shared_ptr<ModelAPI_Document> & thePart,
+                     const ModelHighAPI_Selection& theObject)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
+  return PointPtr(new ConstructionAPI_Point(aFeature, theObject, false, true));
 }