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