Salome HOME
#1404 Random crash with Shaper: AIS presentations: avoid IsReadyToDisplay calling...
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_LengthDimension.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_LengthDimension.cpp
4 // Created:     27 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_LengthDimension.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
10
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_ConstraintLength.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_Line.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Circle.h>
17
18 #include <Events_Error.h>
19
20 #include <GeomDataAPI_Point2D.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_XYZ.h>
23 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAPI_Lin2d.h>
25
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_AttributeDouble.h>
28
29 #include <AIS_DisplaySpecialSymbol.hxx>
30
31
32 static const gp_Pnt MyDefStart(0,0,0);
33 static const gp_Pnt MyDefEnd(1,0,0);
34 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
35
36 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
37 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
38
39 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
40                                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
41 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
42   myConstraint(theConstraint),
43   mySketcherPlane(thePlane)
44 {
45   SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
46   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
47
48   myStyleListener = new SketcherPrs_DimensionStyleListener();
49 }
50
51 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
52 {
53   delete myStyleListener;
54 }
55
56 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
57                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
58 {
59   gp_Pnt aPnt1, aPnt2;
60   return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
61 }
62
63 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
64                                           const Handle(Prs3d_Presentation)& thePresentation, 
65                                           const Standard_Integer theMode)
66 {
67   gp_Pnt aPnt1, aPnt2;
68   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
69   if (aReadyToDisplay) {
70     myFirstPoint = aPnt1;
71     mySecondPoint = aPnt2;
72
73     myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
74     myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
75
76     AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
77
78     myHasParameters = anAttributeValue->usedParameters().size() > 0;
79     myValue = anAttributeValue->text();
80   }
81
82   // compute flyout distance
83   SetFlyout(myDistance);
84   SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
85
86   // Update variable aspect parameters (depending on viewer scale)
87   double aTextSize = 0.0;
88   GetValueString(aTextSize);
89   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
90
91   // Update text visualization: parameter value or parameter text
92   myStyleListener->updateDimensions(this, myHasParameters, myValue);
93
94   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
95
96   if (!aReadyToDisplay)
97     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
98                               "An empty AIS presentation: SketcherPrs_LengthDimension");
99 }
100
101 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
102                                                  const std::shared_ptr<GeomAPI_Ax3>& thePlane,
103                                                  gp_Pnt& thePnt1, gp_Pnt& thePnt2)
104 {
105   DataPtr aData = theConstraint->data();
106   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
107     // The constraint is length
108     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
109       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
110       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
111     if (!anAttr)
112       return false;
113
114     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
115     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
116       return false;
117
118     // Get geometry of the object
119     DataPtr aLineData = aFeature->data();
120     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
121       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
122       (aLineData->attribute(SketchPlugin_Line::START_ID()));
123     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
124       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
125       (aLineData->attribute(SketchPlugin_Line::END_ID()));
126     thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
127     thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
128     return true;
129
130   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
131     // The constraint is distance
132     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
133         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
134     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
135         aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
136
137     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
138     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
139     if (aPoint_A && aPoint_B) {
140       // Both objects are points
141       aPnt_A = aPoint_A->pnt();
142       aPnt_B = aPoint_B->pnt();
143     } else if (!aPoint_A && aPoint_B) {
144       // First object is line
145       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
146           aData, SketchPlugin_Constraint::ENTITY_A());
147       if (aLine) {
148         aPnt_B = aPoint_B->pnt();
149         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
150       }
151     } else if (aPoint_A && !aPoint_B) {
152       // Second object is line
153       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
154           aData, SketchPlugin_Constraint::ENTITY_B());
155       if (aLine) {
156         aPnt_A = aPoint_A->pnt();
157         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
158       }
159     }
160     if (!aPnt_A || !aPnt_B) // Objects not found
161       return false;
162
163     // Get points from these object
164     std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
165     std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
166     thePnt1 = aPoint1->impl<gp_Pnt>();
167     thePnt2 = aPoint2->impl<gp_Pnt>();
168     return true;
169   }
170   return false;
171 }
172
173
174
175 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176                                                    const Standard_Integer theMode)
177 {
178   // Map the application selection modes to standard ones
179   Standard_Integer aMode;
180   switch (theMode) {
181   case 0: // we should use selection of all objects
182     aMode = 0;
183     break;
184   case SketcherPrs_Tools::Sel_Dimension_All:
185     aMode = 0;
186     break;
187   case SketcherPrs_Tools::Sel_Dimension_Line:
188     aMode = 1;
189     break;
190   case SketcherPrs_Tools::Sel_Dimension_Text:
191     aMode = 2;
192     break;
193   default: {
194     // there are own selection modes, so the others should be ignored
195     // otherwise, the text selection appears in the viewer
196     return;
197   }
198   }
199   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
200   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
201 }