Salome HOME
Remove default values from properties requests
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Plane.cpp
index 28b6e8b27b4fff1c79149b652161e98a655ab752..dc305d29ee3a8c3889ce4c45e3004800d845fdfe 100644 (file)
@@ -20,6 +20,7 @@
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Vertex.h>
+#include <GeomAPI_XYZ.h>
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeIntArray.h>
@@ -33,8 +34,9 @@
 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
                                         const std::shared_ptr<GeomAPI_Vertex> theV2,
                                         const std::shared_ptr<GeomAPI_Vertex> theV3);
-static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
-                                                         const std::shared_ptr<GeomAPI_Pln> thePln);
+static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
+  const std::shared_ptr<GeomAPI_Face> theFace,
+  const std::shared_ptr<GeomAPI_Pln> thePln);
 
 //==================================================================================================
 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
@@ -44,8 +46,11 @@ ConstructionPlugin_Plane::ConstructionPlugin_Plane()
 //==================================================================================================
 void ConstructionPlugin_Plane::initAttributes()
 {
-  data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
+  data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
+                       ModelAPI_AttributeString::typeId());
 
+  data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
+  data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
   // By general equation.
   data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
@@ -68,8 +73,6 @@ void ConstructionPlugin_Plane::initAttributes()
 
   // By other plane.
   data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
-  data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
-  data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
   data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
   data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
@@ -86,7 +89,8 @@ void ConstructionPlugin_Plane::execute()
   GeomShapePtr aShape;
 
   std::string aCreationMethod = string(CREATION_METHOD())->value();
-  if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION()) {
+  if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
+      aCreationMethod == "PlaneByGeneralEquation") {
     aShape = createByGeneralEquation();
   } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
     aShape = createByThreePoints();
@@ -121,11 +125,12 @@ void ConstructionPlugin_Plane::execute()
 
 //==================================================================================================
 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
-                                                     std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
+                                                std::shared_ptr<GeomAPI_ICustomPrs> 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) {
+  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));
@@ -134,8 +139,7 @@ bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObj
     }
   }
   if (aColor.empty())
-    aColor = Config_PropManager::color("Visualization", "construction_plane_color",
-                                       ConstructionPlugin_Plane::DEFAULT_COLOR());
+    aColor = Config_PropManager::color("Visualization", "construction_plane_color");
 
   bool isCustomized = false;
   if (aColor.size() == 3)
@@ -160,10 +164,9 @@ std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation
       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
     double aA = anAttrA->value(), aB = anAttrB->value(),
            aC = anAttrC->value(), aD = anAttrD->value();
-    std::shared_ptr<GeomAPI_Pln> aPlane = 
+    std::shared_ptr<GeomAPI_Pln> aPlane =
       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
-    std::string kDefaultPlaneSize = "200";
-    double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
+    double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size");
     aSize *= 4.;
     aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
   }
@@ -229,9 +232,11 @@ std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
     std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
-    double aSize = aLin->distance(aPnt);
-    aSize *= 2.0;
-    aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize);
+    double aSize = aLin->distance(aPnt) * 2;
+    // point may belong to line, so for the face size use maximum distance between point and line
+    // and the line size (distance between the start and end point)
+    double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
+    aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
   } else {
     std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
     GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
@@ -265,7 +270,7 @@ std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOth
 
     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
 
-    std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
+    std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
 
@@ -325,15 +330,25 @@ std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
   }
   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
 
-  std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
-                                                                                     anEdge->line()->direction()));
+  std::shared_ptr<GeomAPI_Ax1> anAxis =
+    std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
+                                                 anEdge->line()->direction()));
 
   // Getting angle.
   double anAngle = real(ANGLE())->value();
 
   GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
-  std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
+  if (!aRotationAlgo.check()) {
+    setError(aRotationAlgo.getError());
+    return GeomShapePtr();
+  }
+  aRotationAlgo.build();
+  if (!aRotationAlgo.isDone()) {
+    setError("Error: Failed to rotate plane");
+    return GeomShapePtr();
+  }
 
+  std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
   return aRes;
 }
 
@@ -358,19 +373,13 @@ std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlan
   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
 
-  double aDist = aPln1->distance(aPln2) / 2.0;
+  std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
+  std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
 
-  std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
-  std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
+  std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
+    aPntOnPln2->xyz())->multiplied(0.5)));
 
-  aOrig1->translate(aDir1, aDist);
-  std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
-
-  if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
-    aDir1->reverse();
-    aOrig1->translate(aDir1, 2.0 * aDist);
-    aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
-  }
+  std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
 
   std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
 
@@ -382,13 +391,15 @@ GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
                                  const std::shared_ptr<GeomAPI_Vertex> theV2,
                                  const std::shared_ptr<GeomAPI_Vertex> theV3)
 {
-  std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
+  std::shared_ptr<GeomAPI_Face> aFace =
+    GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
 
   ListOfShape anObjects;
   anObjects.push_back(theV1);
   anObjects.push_back(theV2);
   anObjects.push_back(theV3);
-  std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
+  std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
+    GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
   GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
 
   return aRes;
@@ -427,7 +438,8 @@ std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_
   double aWgap = (aMaxX2d - aMinX2d) * 0.1;
   double aHgap = (aMaxY2d - aMinY2d) * 0.1;
   std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
-    aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);
+    aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
+    aMaxY2d - aMinY2d + 2. * aHgap);
 
   return aResFace;
 }