Salome HOME
Issue #805 In the sketch presentation, show both the parameter and its value: dimensi...
[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
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintLength.h>
12 #include <SketchPlugin_ConstraintDistance.h>
13 #include <SketchPlugin_Line.h>
14 #include <SketchPlugin_Point.h>
15 #include <SketchPlugin_Circle.h>
16
17 #include <SketcherPrs_Tools.h>
18 #include <SketcherPrs_DimensionStyleListener.h>
19
20 #include <Events_Error.h>
21 #include <Events_Loop.h>
22
23 #include <GeomDataAPI_Point2D.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_XYZ.h>
26 #include <GeomAPI_Pnt2d.h>
27 #include <GeomAPI_Lin2d.h>
28
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_AttributeDouble.h>
31
32 #include <AIS_DisplaySpecialSymbol.hxx>
33
34
35 static const gp_Pnt MyDefStart(0,0,0);
36 static const gp_Pnt MyDefEnd(1,0,0);
37 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
38
39 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
40 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
41
42 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
43                                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
44 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
45   myConstraint(theConstraint),
46   myPlane(thePlane),
47   myAspect(new Prs3d_DimensionAspect())
48 {
49   myAspect->MakeArrows3d(false);
50   myAspect->MakeText3d(false);
51   myAspect->MakeTextShaded(false);
52   myAspect->MakeUnitsDisplayed(false);
53   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
54   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
55
56   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
57   SetDimensionAspect(myAspect);
58
59   myStyleListener = new SketcherPrs_DimensionStyleListener();
60 }
61
62 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
63 {
64   delete myStyleListener;
65 }
66
67 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
68                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
69 {
70   bool aReadyToDisplay = false;
71
72   gp_Pnt aPnt1, aPnt2;
73   aReadyToDisplay = getPoints(theConstraint, thePlane, aPnt1, aPnt2);
74
75   return aReadyToDisplay;
76 }
77
78 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
79                                  const Handle(Prs3d_Presentation)& thePresentation, 
80                                  const Standard_Integer theMode)
81 {
82   if (!SketcherPrs_LengthDimension::IsReadyToDisplay(myConstraint, myPlane)) {
83     Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
84     return;
85   }
86
87   gp_Pnt aPnt1, aPnt2;
88   getPoints(myConstraint, myPlane, aPnt1, aPnt2);
89
90   // compute flyout distance
91   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
92   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
93
94   // Update variable aspect parameters (depending on viewer scale)
95   double anArrowLength = myAspect->ArrowAspect()->Length();
96    // This is not realy correct way to get viewer scale.
97   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
98   double aDimensionValue = GetValue();
99   double aTextSize = 0.0;
100   GetValueString(aTextSize);
101
102   if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
103     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
104     myAspect->SetArrowOrientation(Prs3d_DAO_External);
105     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
106   } else {
107     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
108     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
109   }
110   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
111
112   // The value of vertical aligment is sometimes changed
113   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
114
115   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
116
117   myStyleListener->updateDimensions(this, aValue);
118
119   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
120 }
121
122 bool SketcherPrs_LengthDimension::getPoints(ModelAPI_Feature* theConstraint,
123                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
124                                             gp_Pnt& thePnt1, gp_Pnt& thePnt2)
125 {
126   DataPtr aData = theConstraint->data();
127   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
128     // The constraint is length
129     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
130       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
131       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
132     if (!anAttr)
133       return false;
134
135     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
136     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
137       return false;
138
139     // Get geometry of the object
140     DataPtr aLineData = aFeature->data();
141     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
142       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
143       (aLineData->attribute(SketchPlugin_Line::START_ID()));
144     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
145       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
146       (aLineData->attribute(SketchPlugin_Line::END_ID()));
147     thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
148     thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
149     return true;
150
151   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
152     // The constraint is distance
153     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
154         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
155     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
156         aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
157
158     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
159     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
160     if (aPoint_A && aPoint_B) {
161       // Both objects are points
162       aPnt_A = aPoint_A->pnt();
163       aPnt_B = aPoint_B->pnt();
164     } else if (!aPoint_A && aPoint_B) {
165       // First object is line
166       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
167           aData, SketchPlugin_Constraint::ENTITY_A());
168       if (aLine) {
169         aPnt_B = aPoint_B->pnt();
170         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
171       }
172     } else if (aPoint_A && !aPoint_B) {
173       // Second object is line
174       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
175           aData, SketchPlugin_Constraint::ENTITY_B());
176       if (aLine) {
177         aPnt_A = aPoint_A->pnt();
178         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
179       }
180     }
181     if (!aPnt_A || !aPnt_B) // Objects not found
182       return false;
183
184     // Get points from these object
185     std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
186     std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
187     thePnt1 = aPoint1->impl<gp_Pnt>();
188     thePnt2 = aPoint2->impl<gp_Pnt>();
189     return true;
190   }
191   return false;
192 }
193
194
195
196 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
197                                                    const Standard_Integer theMode)
198 {
199   // Map the application selection modes to standard ones
200   Standard_Integer aMode;
201   switch (theMode) {
202   case 0: // we should use selection of all objects
203     aMode = 0;
204     break;
205   case SketcherPrs_Tools::Sel_Dimension_All:
206     aMode = 0;
207     break;
208   case SketcherPrs_Tools::Sel_Dimension_Line:
209     aMode = 1;
210     break;
211   case SketcherPrs_Tools::Sel_Dimension_Text:
212     aMode = 2;
213     break;
214   default: {
215     // there are own selection modes, so the others should be ignored
216     // otherwise, the text selection appears in the viewer
217     return;
218   }
219   }
220   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
221   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
222 }