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