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