Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_LengthDimension.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketcherPrs_LengthDimension.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_DimensionStyleListener.h"
23
24 #include <SketchPlugin_Constraint.h>
25 #include <SketchPlugin_ConstraintLength.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Circle.h>
32
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_XYZ.h>
36 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_Lin2d.h>
38
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_AttributeInteger.h>
42
43 #include <AIS_DisplaySpecialSymbol.hxx>
44
45 #define OCCT_28850_FIXED
46
47 /// Creates an aspect to be shown in length/radius dimension presentations
48 /// \return an instance of aspect
49 Handle(Prs3d_DimensionAspect) createDimensionAspect()
50 {
51   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
52   anAspect->MakeArrows3d(false);
53   anAspect->MakeText3d(false);
54   anAspect->MakeTextShaded(false);
55   anAspect->MakeUnitsDisplayed(false);
56   anAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
57   anAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
58
59   return anAspect;
60 }
61
62 /// Update variable aspect parameters (depending on viewer scale)
63 /// \param theDimAspect an aspect to be changed
64 /// \param theDimValue an arrow value
65 /// \param theTextSize an arrow value
66 void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
67   double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType)
68 {
69   if (theLocationType == SketcherPrs_Tools::LOCATION_AUTOMATIC) {
70     double anArrowLength = theDimAspect->ArrowAspect()->Length();
71      // This is not realy correct way to get viewer scale.
72     double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
73
74     if(theTextSize > ((theDimValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
75       theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
76       theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
77       theDimAspect->SetExtensionSize(
78         (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
79     } else {
80       theDimAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
81       theDimAspect->SetArrowOrientation(Prs3d_DAO_Internal);
82     }
83   }
84   else if (theLocationType == SketcherPrs_Tools::LOCATION_RIGHT ||
85            theLocationType == SketcherPrs_Tools::LOCATION_LEFT) {
86     theDimAspect->SetTextHorizontalPosition(
87       theLocationType == SketcherPrs_Tools::LOCATION_LEFT ? Prs3d_DTHP_Left : Prs3d_DTHP_Right);
88     theDimAspect->SetArrowOrientation(Prs3d_DAO_External);
89
90     double anArrowLength = theDimAspect->ArrowAspect()->Length();
91     // This is not realy correct way to get viewer scale.
92     double aViewerScale = (double) SketcherPrs_Tools::getConfigArrowSize() / anArrowLength;
93     theDimAspect->SetExtensionSize(
94         (theTextSize / aViewerScale + SketcherPrs_Tools::getArrowSize()) / 2.0);
95   }
96   theDimAspect->TextAspect()->SetFont(Config_PropManager::string("Visualization",
97                                       "dimension_font").c_str());
98
99   theDimAspect->SetArrowTailSize(theDimAspect->ArrowAspect()->Length());
100   // The value of vertical aligment is sometimes changed
101   theDimAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
102 }
103
104
105 static const gp_Pnt MyDefStart(0,0,0);
106 static const gp_Pnt MyDefEnd(1,0,0);
107 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
108
109 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
110
111 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
112   SketchPlugin_Sketch* theSketcher)
113 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
114   myConstraint(theConstraint),
115   mySketcher(theSketcher),
116   myFirstPoint(MyDefStart),
117   mySecondPoint(MyDefEnd),
118   myPlane(MyDefPln),
119   myDistance(1),
120   myValue(0., false, "")
121 {
122   SetDimensionAspect(createDimensionAspect());
123   myStyleListener = new SketcherPrs_DimensionStyleListener();
124
125 #ifdef OCCT_28850_FIXED
126   if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
127     SetDirection(plane()->dirX()->impl<gp_Dir>(), true);
128   else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
129     SetDirection(plane()->dirY()->impl<gp_Dir>(), true);
130 #endif
131 }
132
133 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
134 {
135   delete myStyleListener;
136 }
137
138 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
139                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
140 {
141   gp_Pnt aPnt1, aPnt2;
142   return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
143 }
144
145 void SketcherPrs_LengthDimension::Compute(
146   const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
147   const Handle(Prs3d_Presentation)& thePresentation,
148   const Standard_Integer theMode)
149 {
150   gp_Pnt aPnt1, aPnt2;
151   bool aReadyToDisplay = readyToDisplay(myConstraint, plane(), aPnt1, aPnt2);
152   if (aReadyToDisplay) {
153     myFirstPoint = aPnt1;
154     mySecondPoint = aPnt2;
155
156     myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
157     myPlane = gp_Pln(plane()->impl<gp_Ax3>());
158
159     DataPtr aData = myConstraint->data();
160     AttributeDoublePtr anAttributeValue;
161     if (myConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
162         myConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
163       anAttributeValue = aData->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID());
164     else
165       anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
166     myValue.init(anAttributeValue);
167   }
168
169   // compute flyout distance
170   SetFlyout(myDistance);
171   SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
172
173   // Update variable aspect parameters (depending on viewer scale)
174   double aTextSize = 0.0;
175   GetValueString(aTextSize);
176
177   std::string aLocationAttribute;
178   std::string aConstraintKind = myConstraint->getKind();
179   if (aConstraintKind == SketchPlugin_ConstraintLength::ID())
180     aLocationAttribute = SketchPlugin_ConstraintLength::LOCATION_TYPE_ID();
181   else if (aConstraintKind == SketchPlugin_ConstraintDistance::ID())
182     aLocationAttribute = SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID();
183   else if (aConstraintKind == SketchPlugin_ConstraintDistanceHorizontal::ID())
184     aLocationAttribute = SketchPlugin_ConstraintDistanceHorizontal::LOCATION_TYPE_ID();
185   else if (aConstraintKind == SketchPlugin_ConstraintDistanceVertical::ID())
186     aLocationAttribute = SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID();
187
188   AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
189     (myConstraint->data()->attribute(aLocationAttribute));
190   SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
191     (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
192   updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
193
194   // Update text visualization: parameter value or parameter text
195   myStyleListener->updateDimensions(this, myValue);
196
197   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
198
199   if (!aReadyToDisplay)
200     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
201                               "An empty AIS presentation: SketcherPrs_LengthDimension");
202 }
203
204 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
205                                                  const std::shared_ptr<GeomAPI_Ax3>& thePlane,
206                                                  gp_Pnt& thePnt1, gp_Pnt& thePnt2)
207 {
208   if (!thePlane)
209     return false;
210
211   DataPtr aData = theConstraint->data();
212   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
213     // The constraint is length
214     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
215       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
216       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
217     if (!anAttr)
218       return false;
219
220     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
221     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
222       return false;
223
224     // Get geometry of the object
225     DataPtr aLineData = aFeature->data();
226     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
227       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
228       (aLineData->attribute(SketchPlugin_Line::START_ID()));
229     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
230       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
231       (aLineData->attribute(SketchPlugin_Line::END_ID()));
232     thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
233     thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
234     return true;
235
236   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID() ||
237              theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
238              theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
239     // The constraint is distance
240     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
241         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
242     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
243         aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
244
245     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
246     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
247     if (aPoint_A && aPoint_B) {
248       // Both objects are points
249       aPnt_A = aPoint_A->pnt();
250       aPnt_B = aPoint_B->pnt();
251     } else if (!aPoint_A && aPoint_B) {
252       // First object is line
253       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
254           aData, SketchPlugin_Constraint::ENTITY_A());
255       if (aLine) {
256         aPnt_B = aPoint_B->pnt();
257         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
258       }
259     } else if (aPoint_A && !aPoint_B) {
260       // Second object is line
261       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
262           aData, SketchPlugin_Constraint::ENTITY_B());
263       if (aLine) {
264         aPnt_A = aPoint_A->pnt();
265         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
266       }
267     }
268     if (!aPnt_A || !aPnt_B) // Objects not found
269       return false;
270
271     if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID()) {
272       if (fabs(aPnt_A->x() - aPnt_B->x()) < Precision::Confusion())
273         return false;
274     }
275     else if (theConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID()) {
276       if (fabs(aPnt_A->y() - aPnt_B->y()) < Precision::Confusion())
277         return false;
278     }
279
280     // Get points from these object
281     std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
282     std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
283     thePnt1 = aPoint1->impl<gp_Pnt>();
284     thePnt2 = aPoint2->impl<gp_Pnt>();
285     return true;
286   }
287   return false;
288 }
289
290
291
292 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
293                                                    const Standard_Integer theMode)
294 {
295   // Map the application selection modes to standard ones
296   Standard_Integer aMode;
297   switch (theMode) {
298   case 0: // we should use selection of all objects
299     aMode = 0;
300     break;
301   case SketcherPrs_Tools::Sel_Dimension_All:
302     aMode = 0;
303     break;
304   case SketcherPrs_Tools::Sel_Dimension_Line:
305     aMode = 1;
306     break;
307   case SketcherPrs_Tools::Sel_Dimension_Text:
308     aMode = 2;
309     break;
310   default: {
311     // there are own selection modes, so the others should be ignored
312     // otherwise, the text selection appears in the viewer
313     return;
314   }
315   }
316   SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
317   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
318 }