]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_LengthDimension.cpp
Salome HOME
Porting to SALOME_8.2.0
[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_DimensionStyleListener.h"
10
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_ConstraintLength.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_Line.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Circle.h>
17
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_XYZ.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Lin2d.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDouble.h>
26
27 #include <AIS_DisplaySpecialSymbol.hxx>
28
29
30 static const gp_Pnt MyDefStart(0,0,0);
31 static const gp_Pnt MyDefEnd(1,0,0);
32 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
33
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   mySketcherPlane(thePlane),
41   myFirstPoint(MyDefStart),
42   mySecondPoint(MyDefEnd),
43   myPlane(MyDefPln),
44   myDistance(1),
45   myValue(0., false, "")
46 {
47   // PORTING_TO_SALOME_8
48   // SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
49   myStyleListener = new SketcherPrs_DimensionStyleListener();
50 }
51
52 SketcherPrs_LengthDimension::~SketcherPrs_LengthDimension()
53 {
54   delete myStyleListener;
55 }
56
57 bool SketcherPrs_LengthDimension::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
58                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
59 {
60   gp_Pnt aPnt1, aPnt2;
61   return readyToDisplay(theConstraint, thePlane, aPnt1, aPnt2);
62 }
63
64 void SketcherPrs_LengthDimension::Compute(
65   const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
66   const Handle(Prs3d_Presentation)& thePresentation,
67   const Standard_Integer theMode)
68 {
69   gp_Pnt aPnt1, aPnt2;
70   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPnt1, aPnt2);
71   if (aReadyToDisplay) {
72     myFirstPoint = aPnt1;
73     mySecondPoint = aPnt2;
74
75     myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
76     myPlane = gp_Pln(mySketcherPlane->impl<gp_Ax3>());
77
78     DataPtr aData = myConstraint->data();
79     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
80     myValue.init(anAttributeValue);
81   }
82
83   // compute flyout distance
84   SetFlyout(myDistance);
85   SetMeasuredGeometry(myFirstPoint, mySecondPoint, myPlane);
86
87   // Update variable aspect parameters (depending on viewer scale)
88   double aTextSize = 0.0;
89   GetValueString(aTextSize);
90
91   // PORTING_TO_SALOME_8
92   //SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
93
94   // Update text visualization: parameter value or parameter text
95   myStyleListener->updateDimensions(this, myValue);
96
97   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
98
99   if (!aReadyToDisplay)
100     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
101                               "An empty AIS presentation: SketcherPrs_LengthDimension");
102 }
103
104 bool SketcherPrs_LengthDimension::readyToDisplay(ModelAPI_Feature* theConstraint,
105                                                  const std::shared_ptr<GeomAPI_Ax3>& thePlane,
106                                                  gp_Pnt& thePnt1, gp_Pnt& thePnt2)
107 {
108   DataPtr aData = theConstraint->data();
109   if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
110     // The constraint is length
111     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
112       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
113       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
114     if (!anAttr)
115       return false;
116
117     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
118     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
119       return false;
120
121     // Get geometry of the object
122     DataPtr aLineData = aFeature->data();
123     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
124       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
125       (aLineData->attribute(SketchPlugin_Line::START_ID()));
126     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
127       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
128       (aLineData->attribute(SketchPlugin_Line::END_ID()));
129     thePnt1 = thePlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
130     thePnt2 = thePlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
131     return true;
132
133   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
134     // The constraint is distance
135     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
136         aData, SketchPlugin_Constraint::ENTITY_A(), thePlane);
137     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
138         aData, SketchPlugin_Constraint::ENTITY_B(), thePlane);
139
140     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
141     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
142     if (aPoint_A && aPoint_B) {
143       // Both objects are points
144       aPnt_A = aPoint_A->pnt();
145       aPnt_B = aPoint_B->pnt();
146     } else if (!aPoint_A && aPoint_B) {
147       // First object is line
148       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
149           aData, SketchPlugin_Constraint::ENTITY_A());
150       if (aLine) {
151         aPnt_B = aPoint_B->pnt();
152         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
153       }
154     } else if (aPoint_A && !aPoint_B) {
155       // Second object is line
156       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
157           aData, SketchPlugin_Constraint::ENTITY_B());
158       if (aLine) {
159         aPnt_A = aPoint_A->pnt();
160         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
161       }
162     }
163     if (!aPnt_A || !aPnt_B) // Objects not found
164       return false;
165
166     // Get points from these object
167     std::shared_ptr<GeomAPI_Pnt> aPoint1 = thePlane->to3D(aPnt_A->x(), aPnt_A->y());
168     std::shared_ptr<GeomAPI_Pnt> aPoint2 = thePlane->to3D(aPnt_B->x(), aPnt_B->y());
169     thePnt1 = aPoint1->impl<gp_Pnt>();
170     thePnt2 = aPoint2->impl<gp_Pnt>();
171     return true;
172   }
173   return false;
174 }
175
176
177
178 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
179                                                    const Standard_Integer theMode)
180 {
181   // Map the application selection modes to standard ones
182   Standard_Integer aMode;
183   switch (theMode) {
184   case 0: // we should use selection of all objects
185     aMode = 0;
186     break;
187   case SketcherPrs_Tools::Sel_Dimension_All:
188     aMode = 0;
189     break;
190   case SketcherPrs_Tools::Sel_Dimension_Line:
191     aMode = 1;
192     break;
193   case SketcherPrs_Tools::Sel_Dimension_Text:
194     aMode = 2;
195     break;
196   default: {
197     // there are own selection modes, so the others should be ignored
198     // otherwise, the text selection appears in the viewer
199     return;
200   }
201   }
202   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
203   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
204 }