Salome HOME
Avoid of reference to not-initialized attributes values
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Tools.cpp
index ab4b33023372d92a1b4d0ebe93ad5ed3590eb418..b8e37cc53219fad4b3248a423e77942ea8077958 100644 (file)
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Events.h>
+
+#include <ModelGeomAlgo_Point2D.h>
+
+#include <Events_InfoMessage.h>
 
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAPI_Lin2d.h>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Vertex.hxx>
 
+#include <Prs3d_DimensionAspect.hxx>
+
 #include <BRep_Tool.hxx>
 #include <Precision.hxx>
 
 #include <AIS_Dimension.hxx>
 
-// it is not possible to use 0x2211 as summ symbol because it is not supported by
-// debian Linux platform
-static const Standard_ExtCharacter MyEmptySymbol(' ');
-static const Standard_ExtCharacter MySigmaSymbol('=');//0x03A3); // using equal instead of sigma
-
 namespace SketcherPrs_Tools {
 
 AttributePtr getAttribute(ModelAPI_Feature* theFeature, const std::string& theAttrName)
 {
-  std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
-  return !anAttr->isObject() ? anAttr->attr() : AttributePtr();
+  AttributePtr anAttribute;
+  if (theFeature) {
+    std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+    if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
+      std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
+      if (!anAttr->isObject())
+        anAttribute = anAttr->attr();
+    }
+  }
+  return anAttribute;
 }
 
 ObjectPtr getResult(ModelAPI_Feature* theFeature, const std::string& theAttrName)
 {
-  std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
-  return anAttr->object();
+  ObjectPtr anObject;
+  if (theFeature) {
+    std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+    if (aData.get() && aData->isValid()) { /// essential check as it is called in openGl thread
+      std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = aData->refattr(theAttrName);
+      if (anAttr.get())
+        anObject = anAttr->object();
+    }
+  }
+  return anObject;
 }
 
 
 std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
 {
   ResultConstructionPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
-  if (aRes.get() != NULL) {
+  if (aRes.get() != NULL && aRes->data()->isValid()) {/// essential check as it is called in openGl thread
     return aRes->shape();
   }
   return std::shared_ptr<GeomAPI_Shape>();
@@ -63,26 +79,8 @@ std::shared_ptr<GeomAPI_Shape> getShape(ObjectPtr theObject)
 std::shared_ptr<GeomAPI_Pnt2d> getPoint(ModelAPI_Feature* theFeature,
                                         const std::string& theAttribute)
 {
-  std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
-
-  if (!theFeature->data())
-    return std::shared_ptr<GeomAPI_Pnt2d>();
-
-  FeaturePtr aFeature;
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
-      ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
-  if(!anAttr.get()) {
-    return std::shared_ptr<GeomAPI_Pnt2d>();
-  }
-  aFeature = ModelAPI_Feature::feature(anAttr->object());
-
-  if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
-
-  else if (anAttr->attr()) {
-    aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
-  }
+  std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
+               theFeature, theAttribute, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
   if (aPointAttr.get() != NULL)
     return aPointAttr->pnt();
   return std::shared_ptr<GeomAPI_Pnt2d>();
@@ -139,7 +137,7 @@ std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
 {
   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
 
-  if (!theData)
+  if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread
     return aPointAttr;
 
   FeaturePtr aFeature;
@@ -179,7 +177,7 @@ FeaturePtr getFeatureLine(DataPtr theData,
                           const std::string& theAttribute)
 {
   FeaturePtr aLine;
-  if (!theData)
+  if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
     return aLine;
 
   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
@@ -198,6 +196,9 @@ std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const FeaturePtr theLine,
                                                   const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
 {
   DataPtr aData = theLine->data();
+  if (!aData.get() || !aData->isValid())
+    return std::shared_ptr<GeomAPI_Pnt2d>();
+
   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(SketchPlugin_Line::START_ID()));
   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
@@ -245,7 +246,9 @@ double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       const_cast<ModelAPI_Feature*>(theConstraint)->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
-
+  // for not initialized values return zero distance to avoid Presentation crash
+  if (!aFlyoutPoint->isInitialized())
+    return 0;
   return aFlyoutPoint->y();
 }
 
@@ -281,33 +284,6 @@ std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstrain
   return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
 }
 
-void setDisplaySpecialSymbol(AIS_Dimension* theDimension, const bool& theToDisplay)
-{
-  if (theToDisplay) {
-    theDimension->SetSpecialSymbol(MySigmaSymbol);
-    theDimension->SetDisplaySpecialSymbol(AIS_DSS_Before);
-  }
-  else {
-    theDimension->SetSpecialSymbol(MyEmptySymbol);
-    theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
-  }
-}
-
-void setDisplayParameter(AIS_Dimension* theDimension, const std::string& theParameter,
-                         const bool& theToDisplay)
-{
-   if (theToDisplay) {
-    theDimension->DimensionAspect()->MakeUnitsDisplayed(true);
-    theDimension->SetDisplayUnits(TCollection_AsciiString(theParameter.c_str()));
-    theDimension->DimensionAspect()->SetValueStringFormat("");
-  }
-  else {
-    theDimension->DimensionAspect()->MakeUnitsDisplayed(false);
-    theDimension->SetDisplayUnits(TCollection_AsciiString()); // THE_UNDEFINED_UNITS in AIS_Dimension
-    theDimension->DimensionAspect()->SetValueStringFormat("%g");
-  }
-}
-
 void sendExpressionShownEvent(const bool& theState)
 {
   static Events_ID anId = SketcherPrs_ParameterStyleMessage::eventId();
@@ -319,4 +295,43 @@ void sendExpressionShownEvent(const bool& theState)
   Events_Loop::loop()->flush(anId);
 }
 
+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::getDefaultTextHeight());
+  anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
+
+  return anAspect;
+}
+
+void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect, double theDimValue, double theTextSize)
+{
+  double anArrowLength = theDimAspect->ArrowAspect()->Length();
+   // This is not realy correct way to get viewer scale.
+  double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / 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);
+  }
+  theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
+  // The value of vertical aligment is sometimes changed
+  theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
+}
+
+void sendEmptyPresentationError(ModelAPI_Feature* theFeature, const std::string theError)
+{
+  Events_InfoMessage("SketcherPrs_Tools", "An empty AIS presentation: SketcherPrs_LengthDimension").send();
+  static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
+  std::shared_ptr<ModelAPI_Object> aConstraintPtr(theFeature);
+  ModelAPI_EventCreator::get()->sendUpdated(aConstraintPtr, anEvent);
+}
 };