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