Salome HOME
Empty AIS in the viewer: throw c++ exception (to be finalized, currently do nothing)
[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 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
57                                  const Handle(Prs3d_Presentation)& thePresentation, 
58                                  const Standard_Integer theMode)
59 {
60   gp_Pnt aPnt1, aPnt2;
61   if (!getPoints(aPnt1, aPnt2)) {
62     Events_Error::throwException("An empty AIS presentation: SketcherPrs_LengthDimension");
63     return;
64   }
65
66   // compute flyout distance
67   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
68   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
69
70   // Update variable aspect parameters (depending on viewer scale)
71   double anArrowLength = myAspect->ArrowAspect()->Length();
72    // This is not realy correct way to get viewer scale.
73   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
74   double aDimensionValue = GetValue();
75   double aTextSize = 0.0;
76   GetValueString(aTextSize);
77
78   if(aTextSize > ((aDimensionValue - 3 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
79     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
80     myAspect->SetArrowOrientation(Prs3d_DAO_External);
81     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
82   } else {
83     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
84     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
85   }
86   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
87
88   // The value of vertical aligment is sometimes changed
89   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
90
91   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
92   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
93
94   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
95 }
96
97 bool SketcherPrs_LengthDimension::getPoints(gp_Pnt& thePnt1, gp_Pnt& thePnt2)
98 {
99   DataPtr aData = myConstraint->data();
100   if (myConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
101     // The constraint is length
102     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
103       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
104       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
105     if (!anAttr)
106       return false;
107
108     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
109     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
110       return false;
111
112     // Get geometry of the object
113     DataPtr aLineData = aFeature->data();
114     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
115       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
116       (aLineData->attribute(SketchPlugin_Line::START_ID()));
117     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
118       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
119       (aLineData->attribute(SketchPlugin_Line::END_ID()));
120     thePnt1 = myPlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
121     thePnt2 = myPlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
122     return true;
123
124   } else if (myConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
125     // The constraint is distance
126     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
127         aData, SketchPlugin_Constraint::ENTITY_A(), myPlane);
128     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
129         aData, SketchPlugin_Constraint::ENTITY_B(), myPlane);
130
131     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
132     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
133     if (aPoint_A && aPoint_B) {
134       // Both objects are points
135       aPnt_A = aPoint_A->pnt();
136       aPnt_B = aPoint_B->pnt();
137     } else if (!aPoint_A && aPoint_B) {
138       // First object is line
139       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
140           aData, SketchPlugin_Constraint::ENTITY_A());
141       if (aLine) {
142         aPnt_B = aPoint_B->pnt();
143         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
144       }
145     } else if (aPoint_A && !aPoint_B) {
146       // Second object is line
147       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
148           aData, SketchPlugin_Constraint::ENTITY_B());
149       if (aLine) {
150         aPnt_A = aPoint_A->pnt();
151         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
152       }
153     }
154     if (!aPnt_A || !aPnt_B) // Objects not found
155       return false;
156
157     // Get points from these objects
158     std::shared_ptr<GeomAPI_Pnt> aPoint1 = myPlane->to3D(aPnt_A->x(), aPnt_A->y());
159     std::shared_ptr<GeomAPI_Pnt> aPoint2 = myPlane->to3D(aPnt_B->x(), aPnt_B->y());
160     thePnt1 = aPoint1->impl<gp_Pnt>();
161     thePnt2 = aPoint2->impl<gp_Pnt>();
162     return true;
163   }
164   return false;
165 }
166
167
168
169 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
170                                                    const Standard_Integer theMode)
171 {
172   // Map the application selection modes to standard ones
173   Standard_Integer aMode;
174   switch (theMode) {
175   case 0: // we should use selection of all objects
176     aMode = 0;
177     break;
178   case SketcherPrs_Tools::Sel_Dimension_All:
179     aMode = 0;
180     break;
181   case SketcherPrs_Tools::Sel_Dimension_Line:
182     aMode = 1;
183     break;
184   case SketcherPrs_Tools::Sel_Dimension_Text:
185     aMode = 2;
186     break;
187   default: {
188     // there are own selection modes, so the others should be ignored
189     // otherwise, the text selection appears in the viewer
190     return;
191   }
192   }
193   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
194   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
195 }