Salome HOME
Add icons for chamfer feature
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Measurement.cpp
index 56cc199db42d8679483f91ab3281795c2f17948b..8318f3d08779db0209e154872c4e5923f8be6592 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018-20xx  CEA/DEN, EDF R&D
+// Copyright (C) 2018-2019  CEA/DEN, EDF R&D
 //
 // 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 "FeaturesPlugin_Measurement.h"
@@ -318,6 +317,18 @@ void FeaturesPlugin_Measurement::computeAngleByPoints()
 
 AISObjectPtr FeaturesPlugin_Measurement::getAISObject(AISObjectPtr thePrevious)
 {
+  AttributeDoubleArrayPtr aValues =
+    std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(attribute(RESULT_VALUES_ID()));
+  if ((aValues->size() == 0) || (aValues->value(0) <= Precision::Confusion()))
+    return AISObjectPtr();
+
+  if (!myScreenPlane) {
+    // initialize a default plane for dimension
+    GeomPointPtr anOrigin(new GeomAPI_Pnt(0., 0., 0.));
+    GeomDirPtr aNormal(new GeomAPI_Dir(0., 0., 1.));
+    myScreenPlane = GeomPlanePtr(new GeomAPI_Pln(anOrigin, aNormal));
+  }
+
   AISObjectPtr anAIS;
   std::string aKind = string(MEASURE_KIND())->value();
   if (aKind == MEASURE_LENGTH())
@@ -337,8 +348,6 @@ AISObjectPtr FeaturesPlugin_Measurement::getAISObject(AISObjectPtr thePrevious)
 AISObjectPtr FeaturesPlugin_Measurement::lengthDimension(AISObjectPtr thePrevious)
 {
   AISObjectPtr aAISObj;
-  if (!myScreenPlane.get())
-    return aAISObj;
 
   AttributeSelectionPtr aSelectedFeature = selection(EDGE_FOR_LENGTH_ID());
 
@@ -360,26 +369,38 @@ AISObjectPtr FeaturesPlugin_Measurement::lengthDimension(AISObjectPtr thePreviou
     gp_Pnt aPnt2(aPoint2->impl<gp_Pnt>());
 
     double aLength = aPnt1.Distance(aPnt2);
-    gp_Pln aPlane = myScreenPlane->impl<gp_Pln>(); // gce_MP.Value();
-    aPlane.SetLocation(aPnt1);
+    if (aLength > 0) {
+      gp_Pln aPlane = myScreenPlane->impl<gp_Pln>(); // gce_MP.Value();
+      aPlane.SetLocation(aPnt1);
+
+      gp_XYZ aNormal = aPlane.Axis().Direction().XYZ();
+      gp_XYZ aVec(aPnt2.X() - aPnt1.X(), aPnt2.Y() - aPnt1.Y(), aPnt2.Z() - aPnt1.Z());
+      double aDot = aNormal.Dot(aVec);
+      if (fabs(aDot - aLength) <= Precision::Confusion()) {
+        aPlane = gp_Pln(aPnt1, aPlane.XAxis().Direction());
+      }
 
-    Handle(AIS_LengthDimension) aDim;
-    if (thePrevious.get()) {
-      aAISObj = thePrevious;
-      Handle(AIS_InteractiveObject) aAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
-      aDim = Handle(AIS_LengthDimension)::DownCast(aAIS);
-      if (aDim.IsNull()) {
+      Handle(AIS_LengthDimension) aDim;
+      if (thePrevious.get()) {
+        aAISObj = thePrevious;
+        Handle(AIS_InteractiveObject) aAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+        aDim = Handle(AIS_LengthDimension)::DownCast(aAIS);
+        if (aDim.IsNull()) {
+          aDim = new AIS_LengthDimension(aTEdge, aPlane);
+          aAISObj = AISObjectPtr(new GeomAPI_AISObject());
+          aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
+        }
+        else {
+          aDim->SetMeasuredGeometry(aTEdge, aPlane);
+        }
+      }
+      else {
         aDim = new AIS_LengthDimension(aTEdge, aPlane);
+        aAISObj = AISObjectPtr(new GeomAPI_AISObject());
         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
-      } else {
-        aDim->SetMeasuredGeometry(aTEdge, aPlane);
       }
-    } else {
-      aDim = new AIS_LengthDimension(aTEdge, aPlane);
-      aAISObj = AISObjectPtr(new GeomAPI_AISObject());
-      aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
+      aDim->SetFlyout(aLength / 3.);
     }
-    aDim->SetFlyout(aLength / 3.);
   }
   return aAISObj;
 }
@@ -387,8 +408,6 @@ AISObjectPtr FeaturesPlugin_Measurement::lengthDimension(AISObjectPtr thePreviou
 AISObjectPtr FeaturesPlugin_Measurement::distanceDimension(AISObjectPtr thePrevious)
 {
   AISObjectPtr aAISObj;
-  if (!myScreenPlane.get())
-    return aAISObj;
 
   AttributeSelectionPtr aFirstFeature = selection(DISTANCE_FROM_OBJECT_ID());
   AttributeSelectionPtr aSecondFeature = selection(DISTANCE_TO_OBJECT_ID());
@@ -413,27 +432,39 @@ AISObjectPtr FeaturesPlugin_Measurement::distanceDimension(AISObjectPtr thePrevi
         gp_Pnt aPnt1 = aDist.PointOnShape1(1);
         gp_Pnt aPnt2 = aDist.PointOnShape2(1);
         double aDistance = aDist.Value();
-        gp_Pln aPlane = myScreenPlane->impl<gp_Pln>();
-        aPlane.SetLocation(aPnt1);
-
-        Handle(AIS_LengthDimension) aDim;
-        if (thePrevious.get()) {
-          aAISObj = thePrevious;
-          Handle(AIS_InteractiveObject) aAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
-          aDim = Handle(AIS_LengthDimension)::DownCast(aAIS);
-          if (aDim.IsNull()) {
+        if (aDistance > 0) {
+          gp_Pln aPlane = myScreenPlane->impl<gp_Pln>();
+          aPlane.SetLocation(aPnt1);
+
+          gp_XYZ aNormal = aPlane.Axis().Direction().XYZ();
+          gp_XYZ aVec(aPnt2.X() - aPnt1.X(), aPnt2.Y() - aPnt1.Y(), aPnt2.Z() - aPnt1.Z());
+          double aDot = aNormal.Dot(aVec);
+          if (fabs(aDot - aDistance) <= Precision::Confusion()) {
+            aPlane = gp_Pln(aPnt1, aPlane.XAxis().Direction());
+          }
+
+          Handle(AIS_LengthDimension) aDim;
+          if (thePrevious.get()) {
+            aAISObj = thePrevious;
+            Handle(AIS_InteractiveObject) aAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+            aDim = Handle(AIS_LengthDimension)::DownCast(aAIS);
+            if (aDim.IsNull()) {
+              aDim = new AIS_LengthDimension(aPnt1, aPnt2, aPlane);
+              aAISObj = AISObjectPtr(new GeomAPI_AISObject());
+              aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
+            }
+            else {
+              aDim->SetMeasuredGeometry(aPnt1, aPnt2, aPlane);
+              aDim->SetFlyout(aDistance / 3.);
+            }
+          }
+          else {
+            aAISObj = AISObjectPtr(new GeomAPI_AISObject());
             aDim = new AIS_LengthDimension(aPnt1, aPnt2, aPlane);
             aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
-          } else {
-            aDim->SetMeasuredGeometry(aPnt1, aPnt2, aPlane);
-            aDim->SetFlyout(aDistance / 3.);
           }
-        } else {
-          aAISObj = AISObjectPtr(new GeomAPI_AISObject());
-          aDim = new AIS_LengthDimension(aPnt1, aPnt2, aPlane);
-          aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
+          aDim->SetFlyout(aDistance / 3.);
         }
-        aDim->SetFlyout(aDistance / 3.);
       }
     }
   }
@@ -460,6 +491,7 @@ AISObjectPtr FeaturesPlugin_Measurement::radiusDimension(AISObjectPtr thePreviou
       Handle(AIS_RadiusDimension) aDim = Handle(AIS_RadiusDimension)::DownCast(aAIS);
       if (aDim.IsNull()) {
         aDim = new AIS_RadiusDimension(aShp);
+        aAISObj = AISObjectPtr(new GeomAPI_AISObject());
         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
       } else
         aDim->SetMeasuredGeometry(aShp);
@@ -500,6 +532,7 @@ AISObjectPtr FeaturesPlugin_Measurement::angleDimension(AISObjectPtr thePrevious
   if (anEdge1.get() && anEdge2.get()) {
     TopoDS_Edge aTEdge1 = TopoDS::Edge(anEdge1->impl<TopoDS_Shape>());
     TopoDS_Edge aTEdge2 = TopoDS::Edge(anEdge2->impl<TopoDS_Shape>());
+
     Handle(AIS_AngleDimension) aDim;
     if (thePrevious.get()) {
       aAISObj = thePrevious;
@@ -507,6 +540,7 @@ AISObjectPtr FeaturesPlugin_Measurement::angleDimension(AISObjectPtr thePrevious
       aDim = Handle(AIS_AngleDimension)::DownCast(aAIS);
       if (aDim.IsNull()) {
         aDim = new AIS_AngleDimension(aTEdge1, aTEdge2);
+        aAISObj = AISObjectPtr(new GeomAPI_AISObject());
         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
       } else
         aDim->SetMeasuredGeometry(aTEdge1, aTEdge2);
@@ -534,12 +568,18 @@ AISObjectPtr FeaturesPlugin_Measurement::angleByPointsDimension(AISObjectPtr the
     gp_Pnt aPnt2(aPoint2->impl<gp_Pnt>());
     gp_Pnt aPnt3(aPoint3->impl<gp_Pnt>());
 
+    if (aPnt1.IsEqual(aPnt2, Precision::Confusion()) ||
+      aPnt1.IsEqual(aPnt3, Precision::Confusion()) ||
+      aPnt2.IsEqual(aPnt3, Precision::Confusion()))
+      return thePrevious;
+
     if (thePrevious.get()) {
       aAISObj = thePrevious;
       Handle(AIS_InteractiveObject) aAIS = aAISObj->impl<Handle(AIS_InteractiveObject)>();
       Handle(AIS_AngleDimension) aDim = Handle(AIS_AngleDimension)::DownCast(aAIS);
       if (aDim.IsNull()) {
         aDim = new AIS_AngleDimension(aPnt1, aPnt2, aPnt3);
+        aAISObj = AISObjectPtr(new GeomAPI_AISObject());
         aAISObj->setImpl(new Handle(AIS_InteractiveObject)(aDim));
       } else
         aDim->SetMeasuredGeometry(aPnt1, aPnt2, aPnt3);
@@ -560,6 +600,7 @@ void FeaturesPlugin_Measurement::setupDimension(AISObjectPtr theDim)
     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAIS);
     int aSize = Config_PropManager::integer("Visualization", "dimension_arrow_size");
     int aTextSize = Config_PropManager::integer("Visualization", "dimension_value_size");
+    std::string aFont = Config_PropManager::string("Visualization", "dimension_font");
 
     Handle(Prs3d_DimensionAspect) anAspect = aDim->DimensionAspect();
     anAspect->MakeArrows3d(false);
@@ -567,14 +608,13 @@ void FeaturesPlugin_Measurement::setupDimension(AISObjectPtr theDim)
     anAspect->MakeTextShaded(false);
     anAspect->MakeUnitsDisplayed(false);
     anAspect->MakeUnitsDisplayed(false);
-    anAspect->TextAspect()->SetFont(Config_PropManager::string("Visualization",
-      "dimension_font").c_str());
-    anAspect->TextAspect()->SetHeight(aTextSize / mySceenScale);
+    anAspect->TextAspect()->SetFont(aFont.c_str());
+    anAspect->TextAspect()->SetHeight(aTextSize);
     anAspect->ArrowAspect()->SetLength(aSize / mySceenScale);
     anAspect->SetExtensionSize((aTextSize / mySceenScale + aSize) / 2.0);
     aDim->SetDimensionAspect(anAspect);
 
-    aDim->SetZLayer(1);
+    aDim->SetZLayer(Graphic3d_ZLayerId_Top);
     std::vector<int> aColor = Config_PropManager::color("Visualization", "sketch_dimension_color");
     theDim->setColor(aColor[0], aColor[1], aColor[2]);
   }