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