Salome HOME
updated copyright message
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Axis.cpp
index 1317356de7f708746508ccba492e7957ccfad393..57741479645e9c19cbfc6b964b388754a3d6f784 100644 (file)
@@ -1,10 +1,21 @@
-// Copyright (C) 2014-2016 CEA/DEN, EDF R&D
-
-// File:        ConstructionPlugin_Axis.cpp
-// Created:     12 Dec 2014
-// Author:      Vitaly Smetannikov
-
-// Modified by CEA (delegation to Alyotech) : 29 Mar 2016 
+// 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
+// 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
+//
 
 #include "ConstructionPlugin_Axis.h"
 
@@ -21,7 +32,9 @@
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
 #include <GeomAPI_Pln.h>
+#include <GeomAPI_ShapeIterator.h>
 #include <GeomAPI_Vertex.h>
+#include <GeomAPI_XYZ.h>
 #include <GeomAlgoAPI_EdgeBuilder.h>
 #include <GeomAlgoAPI_PointBuilder.h>
 
@@ -31,8 +44,6 @@
 #include <iostream>
 #endif
 
-using namespace std;
-
 static const double defaultAxisSize = 100;
 
 ConstructionPlugin_Axis::ConstructionPlugin_Axis()
@@ -43,7 +54,7 @@ void ConstructionPlugin_Axis::initAttributes()
 {
   data()->addAttribute(ConstructionPlugin_Axis::METHOD(),
                        ModelAPI_AttributeString::typeId());
-  
+
   // Attributes needed to build the axis using the "two points" method
   data()->addAttribute(ConstructionPlugin_Axis::POINT_FIRST(),
                        ModelAPI_AttributeSelection::typeId());
@@ -59,11 +70,11 @@ void ConstructionPlugin_Axis::initAttributes()
                        ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(ConstructionPlugin_Axis::Z_DIRECTION(),
                        ModelAPI_AttributeDouble::typeId());
-  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
       ConstructionPlugin_Axis::X_DIRECTION());
-  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
       ConstructionPlugin_Axis::Y_DIRECTION());
-  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), 
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
       ConstructionPlugin_Axis::Z_DIRECTION());
 
   //Attributes needed to build the axis using the "three dimensions" method
@@ -125,26 +136,30 @@ void ConstructionPlugin_Axis::createAxisByPointAndDirection()
   AttributeDoublePtr aXAttr = data()->real(ConstructionPlugin_Axis::X_DIRECTION());
   AttributeDoublePtr aYAttr = data()->real(ConstructionPlugin_Axis::Y_DIRECTION());
   AttributeDoublePtr aZAttr = data()->real(ConstructionPlugin_Axis::Z_DIRECTION());
-  if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) && 
+  if ((aRef1.get() != NULL) && (aXAttr.get() != NULL) &&
       (aYAttr.get() != NULL) && (aZAttr.get() != NULL)) {
     GeomShapePtr aShape1 = aRef1->value();
     if (!aShape1.get())
       aShape1 = aRef1->context()->shape();
 
-    std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(), 
+    std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aXAttr->value(),
                                                                aYAttr->value(),
                                                                aZAttr->value()));
-    if (aShape1->isVertex() && (!aShape1->isEqual(aVertex))) {
+    if (aShape1->isVertex() &&
+        (fabs(aXAttr->value()) > MINIMAL_LENGTH() ||
+         fabs(aYAttr->value()) > MINIMAL_LENGTH() ||
+         fabs(aZAttr->value()) > MINIMAL_LENGTH())) {
       std::shared_ptr<GeomAPI_Pnt> aStart = GeomAlgoAPI_PointBuilder::point(aShape1);
-      std::shared_ptr<GeomAPI_Pnt> anEnd = aVertex->point();
-      if (aStart->distance(anEnd) > ConstructionPlugin_Axis::MINIMAL_LENGTH()) {
-        std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
+      std::shared_ptr<GeomAPI_Pnt> anEnd(new GeomAPI_Pnt(aStart->x() + aXAttr->value(),
+                                                         aStart->y() + aYAttr->value(),
+                                                         aStart->z() + aZAttr->value()));
 
-        ResultConstructionPtr aConstr = document()->createConstruction(data());
-        aConstr->setInfinite(true);
-        aConstr->setShape(anEdge);
-        setResult(aConstr);
-      }
+      std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
+
+      ResultConstructionPtr aConstr = document()->createConstruction(data());
+      aConstr->setInfinite(true);
+      aConstr->setShape(anEdge);
+      setResult(aConstr);
     }
   }
 }
@@ -152,16 +167,26 @@ void ConstructionPlugin_Axis::createAxisByPointAndDirection()
 
 void ConstructionPlugin_Axis::createAxisByCylindricalFace()
 {
-    std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
-     // update arguments due to the selection value
-    if (aSelection && !aSelection->isNull() && aSelection->isFace()) {
-      std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
+  std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(CYLINDRICAL_FACE())->value();
+    // update arguments due to the selection value
 
-      ResultConstructionPtr aConstr = document()->createConstruction(data());
-      aConstr->setInfinite(true);
-      aConstr->setShape(anEdge);
-      setResult(aConstr);
-    }
+  if (!aSelection.get() || aSelection->isNull()) {
+    return;
+  }
+
+  if (aSelection->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aSelection);
+    aSelection = anIt.current();
+  }
+
+  if (aSelection->isFace()) {
+    std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::cylinderAxis(aSelection);
+
+    ResultConstructionPtr aConstr = document()->createConstruction(data());
+    aConstr->setInfinite(true);
+    aConstr->setShape(anEdge);
+    setResult(aConstr);
+  }
 }
 
 void ConstructionPlugin_Axis::createAxisByDimensions()
@@ -171,7 +196,8 @@ void ConstructionPlugin_Axis::createAxisByDimensions()
   double aDY = data()->real(DY())->value();
   double aDZ = data()->real(DZ())->value();
 
-  if (fabs(aDX) < MINIMAL_LENGTH()  && fabs(aDY) < MINIMAL_LENGTH() && fabs(aDZ) < MINIMAL_LENGTH()){
+  if (fabs(aDX) < MINIMAL_LENGTH()  && fabs(aDY) < MINIMAL_LENGTH() &&
+      fabs(aDZ) < MINIMAL_LENGTH()) {
     setError("Axis builder with dimensions  :: all dimensions are null", false);
     return ;
   }
@@ -181,7 +207,7 @@ void ConstructionPlugin_Axis::createAxisByDimensions()
   ResultConstructionPtr aConstr = document()->createConstruction(data());
   aConstr->setInfinite(true);
   aConstr->setShape(anEdge);
-  setResult(aConstr);    
+  setResult(aConstr);
 }
 
 void ConstructionPlugin_Axis::createAxisByLine()
@@ -192,7 +218,36 @@ void ConstructionPlugin_Axis::createAxisByLine()
   if(!aLineShape.get()) {
     aLineShape = anEdgeSelection->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
+  GeomEdgePtr anEdge;
+  if (aLineShape->isEdge()) {
+    anEdge = aLineShape->edge();
+  }
+  else if (aLineShape->isCompound()) {
+    // create an edge which covers all edges from compounds (they are on the same line)
+    GeomPointPtr aFirst, aLast;
+    GeomXYZPtr aLineVec;
+    for(GeomAPI_ShapeIterator anIt(aLineShape); anIt.more(); anIt.next()) {
+      GeomEdgePtr aSub = anIt.current()->edge();
+      if (aSub.get() && aSub->isLine()) {
+        if (!aLineVec.get()) {
+          aFirst = aSub->firstPoint();
+          aLast = aSub->lastPoint();
+        } else { // set aFirst and aLast by extreme points
+          GeomXYZPtr aFirstVec = aSub->firstPoint()->xyz()->decreased(aFirst->xyz());
+          bool aSameDirection =
+            aSub->lastPoint()->xyz()->decreased(aSub->firstPoint()->xyz())->dot(aLineVec) > 0;
+          if (aLineVec->dot(aFirstVec) < -1.e-7) { // first point is changed
+            aFirst = aSameDirection ? aSub->firstPoint() : aSub->lastPoint();
+          } else { // last point is changed
+            aLast = aSameDirection ? aSub->lastPoint() : aSub->firstPoint();
+          }
+        }
+        aLineVec = aLast->xyz()->decreased(aFirst->xyz());
+      }
+    }
+    if (aLineVec.get())
+      anEdge = GeomAlgoAPI_EdgeBuilder::line(aFirst, aLast);
+  }
 
   ResultConstructionPtr aConstr = document()->createConstruction(data());
   aConstr->setInfinite(true);
@@ -208,7 +263,16 @@ void ConstructionPlugin_Axis::createAxisByPlaneAndPoint()
   if(!aFaceShape.get()) {
     aFaceShape = aFaceSelection->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
+  GeomFacePtr aFace;
+  if (aFaceShape->isFace()) {
+    aFace = aFaceShape->face();
+  }
+  else if (aFaceShape->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aFaceShape);
+    aFace = anIt.current()->face();
+  }
+  if (!aFace)
+    return;
   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
 
   // Get point.
@@ -242,7 +306,16 @@ void ConstructionPlugin_Axis::createAxisByTwoPlanes()
   if(!aFaceShape1.get()) {
     aFaceShape1 = aFaceSelection1->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
+  std::shared_ptr<GeomAPI_Face> aFace1;
+  if (aFaceShape1->isFace()) {
+    aFace1 = aFaceShape1->face();
+  }
+  else if (aFaceShape1->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aFaceShape1);
+    aFace1 = anIt.current()->face();
+  }
+  if (!aFace1)
+    return;
   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
 
   std::string useOffset1 = string(USE_OFFSET1())->value();
@@ -261,7 +334,16 @@ void ConstructionPlugin_Axis::createAxisByTwoPlanes()
   if(!aFaceShape2.get()) {
     aFaceShape2 = aFaceSelection2->context()->shape();
   }
-  std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
+  std::shared_ptr<GeomAPI_Face> aFace2;
+  if (aFaceShape2->isFace()) {
+    aFace2 = aFaceShape2->face();
+  }
+  else if (aFaceShape2->isCompound()) {
+    GeomAPI_ShapeIterator anIt(aFaceShape2);
+    aFace2 = anIt.current()->face();
+  }
+  if (!aFace2)
+    return;
   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
 
   std::string useOffset2 = string(USE_OFFSET2())->value();
@@ -308,11 +390,9 @@ void ConstructionPlugin_Axis::execute()
   }
 }
 
-bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
-  std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
+bool ConstructionPlugin_Axis::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs)
 {
-  bool isCustomized = theDefaultPrs.get() != NULL &&
-                      theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
+  bool isCustomized = false;
 
   isCustomized = thePrs->setLineStyle(3) || isCustomized;
   isCustomized = thePrs->setWidth(2) || isCustomized;