Salome HOME
Fix solvespace version number in environment.
[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
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintLength.h>
12 #include <SketchPlugin_ConstraintDistance.h>
13 #include <SketchPlugin_Line.h>
14 #include <SketchPlugin_Point.h>
15 #include <SketchPlugin_Circle.h>
16
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt.h>
19 #include <GeomAPI_XYZ.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_Lin2d.h>
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_AttributeDouble.h>
25
26 #include <AIS_DisplaySpecialSymbol.hxx>
27
28
29 static const gp_Pnt MyDefStart(0,0,0);
30 static const gp_Pnt MyDefEnd(1,0,0);
31 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
32
33 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
34 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
35
36 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint,
37                                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
38 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln),
39   myConstraint(theConstraint),
40   myPlane(thePlane),
41   myAspect(new Prs3d_DimensionAspect())
42 {
43   myAspect->MakeArrows3d(false);
44   myAspect->MakeText3d(false);
45   myAspect->MakeTextShaded(false);
46   myAspect->MakeUnitsDisplayed(false);
47   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
48   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
49
50   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
51   SetDimensionAspect(myAspect);
52 }
53
54 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
55                                  const Handle(Prs3d_Presentation)& thePresentation, 
56                                  const Standard_Integer theMode)
57 {
58   gp_Pnt aPnt1, aPnt2;
59   if (!getPoints(aPnt1, aPnt2))
60     return;
61
62   // compute flyout distance
63   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
64   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
65
66   // Update variable aspect parameters (depending on viewer scale)
67   double anArrowLength = myAspect->ArrowAspect()->Length();
68    // This is not realy correct way to get viewer scale.
69   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
70   double aDimensionValue = GetValue();
71   double aTextSize = 0.0;
72   GetValueString(aTextSize);
73
74   if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
75     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
76     myAspect->SetArrowOrientation(Prs3d_DAO_External);
77     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
78   } else {
79     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
80     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
81   }
82   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
83
84   // The value of vertical aligment is sometimes changed
85   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
86
87   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
88   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
89
90   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
91 }
92
93 bool SketcherPrs_LengthDimension::getPoints(gp_Pnt& thePnt1, gp_Pnt& thePnt2)
94 {
95   DataPtr aData = myConstraint->data();
96   if (myConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
97     // The constraint is length
98     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
99       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
100       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
101     if (!anAttr)
102       return false;
103
104     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
105     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
106       return false;
107
108     // Get geometry of the object
109     DataPtr aLineData = aFeature->data();
110     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
111       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
112       (aLineData->attribute(SketchPlugin_Line::START_ID()));
113     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
114       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
115       (aLineData->attribute(SketchPlugin_Line::END_ID()));
116     thePnt1 = myPlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
117     thePnt2 = myPlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
118     return true;
119
120   } else if (myConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
121     // The constraint is distance
122     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
123         aData, SketchPlugin_Constraint::ENTITY_A(), myPlane);
124     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
125         aData, SketchPlugin_Constraint::ENTITY_B(), myPlane);
126
127     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
128     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
129     if (aPoint_A && aPoint_B) {
130       // Both objects are points
131       aPnt_A = aPoint_A->pnt();
132       aPnt_B = aPoint_B->pnt();
133     } else if (!aPoint_A && aPoint_B) {
134       // First object is line
135       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
136           aData, SketchPlugin_Constraint::ENTITY_A());
137       if (aLine) {
138         aPnt_B = aPoint_B->pnt();
139         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
140       }
141     } else if (aPoint_A && !aPoint_B) {
142       // Second object is line
143       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
144           aData, SketchPlugin_Constraint::ENTITY_B());
145       if (aLine) {
146         aPnt_A = aPoint_A->pnt();
147         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
148       }
149     }
150     if (!aPnt_A || !aPnt_B) // Objects not found
151       return false;
152
153     // Get points from these objects
154     std::shared_ptr<GeomAPI_Pnt> aPoint1 = myPlane->to3D(aPnt_A->x(), aPnt_A->y());
155     std::shared_ptr<GeomAPI_Pnt> aPoint2 = myPlane->to3D(aPnt_B->x(), aPnt_B->y());
156     thePnt1 = aPoint1->impl<gp_Pnt>();
157     thePnt2 = aPoint2->impl<gp_Pnt>();
158     return true;
159   }
160   return false;
161 }
162
163
164
165 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
166                                                    const Standard_Integer theMode)
167 {
168   // Map the application selection modes to standard ones
169   Standard_Integer aMode;
170   switch (theMode) {
171   case 0: // we should use selection of all objects
172     aMode = 0;
173     break;
174   case SketcherPrs_Tools::Sel_Dimension_All:
175     aMode = 0;
176     break;
177   case SketcherPrs_Tools::Sel_Dimension_Line:
178     aMode = 1;
179     break;
180   case SketcherPrs_Tools::Sel_Dimension_Text:
181     aMode = 2;
182     break;
183   default: {
184     // there are own selection modes, so the others should be ignored
185     // otherwise, the text selection appears in the viewer
186     return;
187   }
188   }
189   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
190   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
191 }