Salome HOME
updated copyright message
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_LengthDimension.cpp
index 0ec0382e49a01bbdb995b6f679bd6aeef76dd035..f81fe13c244851c82ad2a2f4e4926b6285eaf630 100644 (file)
@@ -1,24 +1,35 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        SketcherPrs_LengthDimension.cpp
-// Created:     27 March 2015
-// Author:      Vitaly SMETANNIKOV
+// 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 "SketcherPrs_LengthDimension.h"
 #include "SketcherPrs_Tools.h"
+#include "SketcherPrs_DimensionStyle.h"
 
 #include <SketchPlugin_Constraint.h>
 #include <SketchPlugin_ConstraintLength.h>
 #include <SketchPlugin_ConstraintDistance.h>
+#include <SketchPlugin_ConstraintDistanceHorizontal.h>
+#include <SketchPlugin_ConstraintDistanceVertical.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Circle.h>
 
-#include <SketcherPrs_Tools.h>
-
-#include <Events_Error.h>
-#include <Events_Loop.h>
-
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_XYZ.h>
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
 
-#include <AIS_DisplaySpecialSymbol.hxx>
+
+#define OCCT_28850_FIXED
+
+/// Creates an aspect to be shown in length/radius dimension presentations
+/// \return an instance of aspect
+Handle(Prs3d_DimensionAspect) createDimensionAspect()
+{
+  Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
+  anAspect->MakeArrows3d(false);
+  anAspect->MakeText3d(false);
+  anAspect->MakeTextShaded(false);
+  anAspect->MakeUnitsDisplayed(false);
+  anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
+  anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
+
+  return anAspect;
+}
+
+/// Update variable aspect parameters (depending on viewer scale)
+/// \param theDimAspect an aspect to be changed
+/// \param theDimValue an arrow value
+/// \param theTextSize an arrow value
+void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
+  double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType)
+{
+  if (theLocationType == SketcherPrs_Tools::LOCATION_AUTOMATIC) {
+    double anArrowLength = theDimAspect->ArrowAspect()->Length();
+     // This is not realy correct way to get viewer scale.
+    double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
+
+    if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
+      theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
+      theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
+      theDimAspect->SetExtensionSize(
+        (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
+    } else {
+      theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
+      theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
+    }
+  }
+  else if (theLocationType == SketcherPrs_Tools::LOCATION_RIGHT ||
+           theLocationType == SketcherPrs_Tools::LOCATION_LEFT) {
+    theDimAspect->SetTextHorizontalPosition(
+      theLocationType == SketcherPrs_Tools::LOCATION_LEFT ? Prs3d_DTHP_Left : Prs3d_DTHP_Right);
+    theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
+
+    double anArrowLength = theDimAspect->ArrowAspect()->Length();
+    // This is not realy correct way to get viewer scale.
+    double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
+    theDimAspect->SetExtensionSize(
+        (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
+  }
+  theDimAspect->TextAspect()->SetFont(Config_PropManager::string("Visualization",
+                                      "dimension_font").c_str());
+
+  theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
+  // The value of vertical aligment is sometimes changed
+  theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
+}
 
 
 static const gp_Pnt MyDefStart(0,0,0);
 static const gp_Pnt MyDefEnd(1,0,0);
 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
 
-IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
-IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
+IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, PrsDim_LengthDimension);
 
 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
-                                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane)
-: AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
+  SketchPlugin_Sketch* theSketcher)
+: PrsDim_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
   myConstraint(theConstraint),
-  myPlane(thePlane),
-  myAspect(new Prs3d_DimensionAspect())
+  mySketcher(theSketcher),
+  myFirstPoint(MyDefStart),
+  mySecondPoint(MyDefEnd),
+  myPlane(MyDefPln),
+  myDistance(1),
+  myValue(0., false, "")
 {
-  myAspect->MakeArrows3d(false);
-  myAspect->MakeText3d(false);
-  myAspect->MakeTextShaded(false);
-  myAspect->MakeUnitsDisplayed(false);
-  myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
-  myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
-
-  SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
-  SetDimensionAspect(myAspect);
-
-  Events_Loop* aLoop = Events_Loop::loop();
-  const Events_ID kDocCreatedEvent =
-                SketcherPrs_ParameterStyleMessage::eventId();
-  aLoop->registerListener(this, kDocCreatedEvent, NULL, false);
-}
+  SetDimensionAspect(createDimensionAspect());
+  myStyleListener = new SketcherPrs_DimensionStyle();
 
-SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
-{
-  Events_Loop* aLoop = Events_Loop::loop();
-  aLoop->removeListener(this);
+  setDirection(theConstraint, plane());
 }
 
-void SketcherPrs_LengthDimension::processEvent(const std::shared_ptr<Events_Message>& theMessage)
+SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
 {
-  const Events_ID kParameterStyleEvent = SketcherPrs_ParameterStyleMessage::eventId();
-  if (theMessage->eventID() == kParameterStyleEvent) {
-    std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::dynamic_pointer_cast<
-                                            SketcherPrs_ParameterStyleMessage>(theMessage);
-    myStyle = aMessage->style();
-  }
+  delete myStyleListener;
 }
 
 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
 {
-  bool aReadyToDisplay = false;
-
-  gp_Pnt aPnt1, aPnt2;
-  aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
+  gp_Pnt2d aPnt1, aPnt2;
+  return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
+}
 
-  return aReadyToDisplay;
+static bool isEqualPoints(ModelAPI_Feature* theConstraint,
+  const gp_Pnt2d& theP1, const gp_Pnt2d& theP2)
+{
+  bool isEqual = false;
+  if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
+    isEqual = Abs(theP1.X() - theP2.X()) < Precision::Confusion();
+  else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
+    isEqual = Abs(theP1.Y() - theP2.Y()) < Precision::Confusion();
+  else {
+    isEqual = theP1.SquareDistance(theP2) < Precision::SquareConfusion();
+  }
+  return isEqual;
 }
 
-void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
-                                 const Handle(Prs3d_Presentation)& thePresentation, 
-                                 const Standard_Integer theMode)
+void SketcherPrs_LengthDimension::Compute(
+  const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
+  const Handle(Prs3d_Presentation)& thePresentation,
+  const Standard_Integer theMode)
 {
-  if (!SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, myPlane)) {
-    Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
+  if (!plane().get())
     return;
-  }
+  gp_Pnt2d aP1, aP2;
+  bool aReadyToDisplay = readyToDisplay(myConstraint, plane(), aP1, aP2);
+  if (aReadyToDisplay) {
+    if (isEqualPoints(myConstraint, aP1, aP2)) {
+      // adjust points to draw the dimension presentation
+      std::shared_ptr<GeomDataAPI_Dir> aDirAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+          myConstraint->attribute(SketchPlugin_ConstraintDistance::DIRECTION_ID()));
+      double x = 0.0, y = 0.0;
+      if (aDirAttr && aDirAttr->isInitialized()) {
+        x = aDirAttr->x();
+        y = aDirAttr->y();
+        if (x == 0.0 && y == 0.0)
+          x = 1.0;
+      }
+      else if (myConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
+        y = 1.0;
+      else
+        x = 1.0;
+
+      gp_XY aDir(x, y);
+      aP1.ChangeCoord().Add(aDir * (-Precision::Confusion()));
+      aP2.ChangeCoord().Add(aDir * Precision::Confusion());
+    }
+
+    myFirstPoint = plane()->to3D(aP1.X(), aP1.Y())->impl<gp_Pnt>();
+    mySecondPoint = plane()->to3D(aP2.X(), aP2.Y())->impl<gp_Pnt>();
+
+    myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
+    myPlane = gp_Pln(plane()->impl<gp_Ax3>());
 
-  gp_Pnt aPnt1, aPnt2;
-  getPoints(myConstraint, myPlane, aPnt1, aPnt2);
+    DataPtr aData = myConstraint->data();
+    AttributeDoublePtr anAttributeValue;
+    if (myConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
+        myConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
+      anAttributeValue = aData->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
+    else
+      anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
+    myValue.init(anAttributeValue);
+
+    setDirection(myConstraint, plane());
+  }
 
   // compute flyout distance
-  SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
-  SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
+  SetFlyout(myDistance);
+  SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
 
   // Update variable aspect parameters (depending on viewer scale)
-  double anArrowLength = myAspect->ArrowAspect()->Length();
-   // This is not realy correct way to get viewer scale.
-  double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
-  double aDimensionValue = GetValue();
   double aTextSize = 0.0;
   GetValueString(aTextSize);
 
-  if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
-    myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
-    myAspect->SetArrowOrientation(Prs3d_DAO_External);
-    myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
-  } else {
-    myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
-    myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
-  }
-  myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
-
-  // The value of vertical aligment is sometimes changed
-  myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
-
-  AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
-  bool aHasParameters = aValue->usedParameters().size() > 0;
-  if (aHasParameters) {
-    bool isParameterValueStyle = myStyle == SketcherPrs_ParameterStyleMessage::ParameterValue;
-    SketcherPrs_Tools::setDisplaySpecialSymbol(this, isParameterValueStyle);
-    SketcherPrs_Tools::setDisplayParameter(this, aValue->text(), !isParameterValueStyle);
-  }
-  else {
-    SketcherPrs_Tools::setDisplaySpecialSymbol(this, false);
-    SketcherPrs_Tools::setDisplayParameter(this, aValue->text(), false);
-  }
-  AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
+  std::string aLocationAttribute;
+  std::string aConstraintKind = myConstraint->getKind();
+  if (aConstraintKind == SketchPlugin_ConstraintLength::ID())
+    aLocationAttribute = SketchPlugin_ConstraintLength::LOCATION_TYPE_ID();
+  else if (aConstraintKind == SketchPlugin_ConstraintDistance::ID())
+    aLocationAttribute = SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID();
+  else if (aConstraintKind == SketchPlugin_ConstraintDistanceHorizontal::ID())
+    aLocationAttribute = SketchPlugin_ConstraintDistanceHorizontal::LOCATION_TYPE_ID();
+  else if (aConstraintKind == SketchPlugin_ConstraintDistanceVertical::ID())
+    aLocationAttribute = SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID();
+
+  AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
+    (myConstraint->data()->attribute(aLocationAttribute));
+  SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
+    (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
+  updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
+
+  // Update text visualization: parameter value or parameter text
+  myStyleListener->updateDimensions(this, myValue);
+
+  PrsDim_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
+
+  if (!aReadyToDisplay)
+    SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
+                              "An empty AIS presentation: SketcherPrs_LengthDimension");
 }
 
-bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
-                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane,
-                                            gp_Pnt& thePnt1, gp_Pnt& thePnt2)
+bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
+                                                 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
+                                                 gp_Pnt2d& thePnt1, gp_Pnt2d& thePnt2)
 {
+  if (!thePlane)
+    return false;
+
   DataPtr aData = theConstraint->data();
   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
     // The constraint is length
-    std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
+    std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
     if (!anAttr)
@@ -158,17 +256,19 @@ bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
 
     // Get geometry of the object
     DataPtr aLineData = aFeature->data();
-    std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
+    std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
       (aLineData->attribute(SketchPlugin_Line::START_ID()));
-    std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
+    std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
       (aLineData->attribute(SketchPlugin_Line::END_ID()));
-    thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
-    thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
+    thePnt1 = gp_Pnt2d(aStartPoint->x(), aStartPoint->y());
+    thePnt2 = gp_Pnt2d(aEndPoint->x(), aEndPoint->y());
     return true;
 
-  } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
+  } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
+             theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
+             theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
     // The constraint is distance
     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
@@ -202,10 +302,8 @@ bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
       return false;
 
     // Get points from these object
-    std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
-    std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
-    thePnt1 = aPoint1->impl<gp_Pnt>();
-    thePnt2 = aPoint2->impl<gp_Pnt>();
+    thePnt1 = gp_Pnt2d(aPnt_A->x(), aPnt_A->y());
+    thePnt2 = gp_Pnt2d(aPnt_B->x(), aPnt_B->y());
     return true;
   }
   return false;
@@ -237,6 +335,17 @@ void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Select
     return;
   }
   }
-  SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
-  AIS_LengthDimension::ComputeSelection(aSelection, aMode);
+  SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
+  PrsDim_LengthDimension::ComputeSelection(aSelection, aMode);
+}
+
+void SketcherPrs_LengthDimension::setDirection(ModelAPI_Feature* theConstraint,
+                                               const std::shared_ptr<GeomAPI_Ax3>& thePlane)
+{
+#ifdef OCCT_28850_FIXED
+  if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
+    SetDirection(thePlane->dirX()->impl<gp_Dir>(), true);
+  else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
+    SetDirection(thePlane->dirY()->impl<gp_Dir>(), true);
+#endif
 }