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