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