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