Salome HOME
#1404 Random crash with Shaper: AIS presentations: coincidence/radius constraints...
[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   bool aReadyToDisplay = false;
60
61   gp_Pnt aPnt1, aPnt2;
62   aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
63
64   return aReadyToDisplay;
65 }
66
67 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
68                                           const Handle(Prs3d_Presentation)& thePresentation, 
69                                           const Standard_Integer theMode)
70 {
71   bool aReadyToDisplay = IsReadyToDisplay(myConstraint, mySketcherPlane);
72   if (aReadyToDisplay) {
73     myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
74     getPoints(myConstraint, mySketcherPlane, myFirstPoint, mySecondPoint);
75     myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
76
77     AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
78
79     myHasParameters = anAttributeValue->usedParameters().size() > 0;
80     myValue = anAttributeValue->text();
81   }
82
83   // compute flyout distance
84   SetFlyout(myDistance);
85   SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
86
87   // Update variable aspect parameters (depending on viewer scale)
88   double aTextSize = 0.0;
89   GetValueString(aTextSize);
90   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
91
92   // Update text visualization: parameter value or parameter text
93   myStyleListener->updateDimensions(this, myHasParameters, myValue);
94
95   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
96
97   if (!aReadyToDisplay)
98     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
99                               "An empty AIS presentation: SketcherPrs_LengthDimension");
100
101 }
102
103 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
104                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
105                                             gp_Pnt& thePnt1, gp_Pnt& thePnt2)
106 {
107   DataPtr aData = theConstraint->data();
108   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
109     // The constraint is length
110     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
111       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
112       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
113     if (!anAttr)
114       return false;
115
116     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
117     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
118       return false;
119
120     // Get geometry of the object
121     DataPtr aLineData = aFeature->data();
122     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
123       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
124       (aLineData->attribute(SketchPlugin_Line::START_ID()));
125     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
126       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
127       (aLineData->attribute(SketchPlugin_Line::END_ID()));
128     thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
129     thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
130     return true;
131
132   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
133     // The constraint is distance
134     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
135         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
136     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
137         aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
138
139     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
140     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
141     if (aPoint_A && aPoint_B) {
142       // Both objects are points
143       aPnt_A = aPoint_A->pnt();
144       aPnt_B = aPoint_B->pnt();
145     } else if (!aPoint_A && aPoint_B) {
146       // First object is line
147       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
148           aData, SketchPlugin_Constraint::ENTITY_A());
149       if (aLine) {
150         aPnt_B = aPoint_B->pnt();
151         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
152       }
153     } else if (aPoint_A && !aPoint_B) {
154       // Second object is line
155       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
156           aData, SketchPlugin_Constraint::ENTITY_B());
157       if (aLine) {
158         aPnt_A = aPoint_A->pnt();
159         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
160       }
161     }
162     if (!aPnt_A || !aPnt_B) // Objects not found
163       return false;
164
165     // Get points from these object
166     std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
167     std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
168     thePnt1 = aPoint1->impl<gp_Pnt>();
169     thePnt2 = aPoint2->impl<gp_Pnt>();
170     return true;
171   }
172   return false;
173 }
174
175
176
177 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
178                                                    const Standard_Integer theMode)
179 {
180   // Map the application selection modes to standard ones
181   Standard_Integer aMode;
182   switch (theMode) {
183   case 0: // we should use selection of all objects
184     aMode = 0;
185     break;
186   case SketcherPrs_Tools::Sel_Dimension_All:
187     aMode = 0;
188     break;
189   case SketcherPrs_Tools::Sel_Dimension_Line:
190     aMode = 1;
191     break;
192   case SketcherPrs_Tools::Sel_Dimension_Text:
193     aMode = 2;
194     break;
195   default: {
196     // there are own selection modes, so the others should be ignored
197     // otherwise, the text selection appears in the viewer
198     return;
199   }
200   }
201   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
202   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
203 }