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