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