Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.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
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
25
26 static const gp_Pnt MyDefStart(0,0,0);
27 static const gp_Pnt MyDefEnd(1,0,0);
28 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
29
30 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
31 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
32
33 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint, 
34                         const std::shared_ptr<GeomAPI_Ax3>& thePlane)
35 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln), 
36 myConstraint(theConstraint), myPlane(thePlane)
37 {
38   myAspect = new Prs3d_DimensionAspect();
39   myAspect->MakeArrows3d(false);
40   myAspect->MakeText3d(false);
41   myAspect->MakeTextShaded(false);
42   myAspect->MakeUnitsDisplayed(false);
43   myAspect->TextAspect()->SetHeight(MyTextHeight);
44   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
45
46   SetSelToleranceForText2d(MyTextHeight);
47   SetDimensionAspect(myAspect);
48 }
49
50
51 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
52                                  const Handle(Prs3d_Presentation)& thePresentation, 
53                                  const Standard_Integer theMode)
54 {
55   gp_Pnt aPnt1, aPnt2;
56   if (!getPoints(aPnt1, aPnt2))
57     return;
58   //DataPtr aData = myConstraint->data();
59
60   //AttributePtr aFlyOutAttribute = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
61   //if (!aFlyOutAttribute->isInitialized()) {
62   //  return; // not possible to show length because points are not defined
63   //}
64   //std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
65   //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFlyOutAttribute);
66   //std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
67   //gp_Pnt aFlyPnt = aFlyoutPnt->impl<gp_Pnt>();
68
69   //double aDistance = aPnt1.Distance(aPnt2);
70
71   //double aFlyout = 0;
72   //double aDist = 0.0;
73   //if (aDistance < Precision::Confusion())
74   //  aDist = aPnt1.Distance(aFlyPnt);
75   //else {
76   //  gp_Lin aLine(aPnt1, gp_Vec(aPnt1, aPnt2));
77   //  aDist = aLine.Distance(aFlyPnt);
78   //}
79
80   //gp_XYZ aLineDir = aPnt2.XYZ().Subtracted(aPnt1.XYZ());
81   //gp_XYZ aFOutDir = aFlyPnt.XYZ().Subtracted(aPnt1.XYZ());
82   //gp_XYZ aNorm = myPlane->norm()->xyz()->impl<gp_XYZ>();
83   //if (aLineDir.Crossed(aFOutDir).Dot(aNorm) < 0)
84   //  aDist = -aDist;
85   //aFlyout = aDist;
86
87   //SetFlyout(aFlyout);
88   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint, myPlane));
89   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
90   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
91 }
92
93 bool SketcherPrs_LengthDimension::getPoints(gp_Pnt& thePnt1, gp_Pnt& thePnt2) const
94 {
95   DataPtr aData = myConstraint->data();
96   if (myConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
97     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
98       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
99       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
100     if (!anAttr)
101       return false;
102
103     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
104     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
105       return false;
106
107     DataPtr aLineData = aFeature->data();
108     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
109       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
110       (aLineData->attribute(SketchPlugin_Line::START_ID()));
111     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
112       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
113       (aLineData->attribute(SketchPlugin_Line::END_ID()));
114     thePnt1 = myPlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
115     thePnt2 = myPlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
116     return true;
117
118   } else if (myConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
119     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
120         aData, SketchPlugin_Constraint::ENTITY_A());
121     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
122         aData, SketchPlugin_Constraint::ENTITY_B());
123
124     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
125     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
126     if (aPoint_A && aPoint_B) {
127       aPnt_A = aPoint_A->pnt();
128       aPnt_B = aPoint_B->pnt();
129     } else if (!aPoint_A && aPoint_B) {
130       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
131           aData, SketchPlugin_Constraint::ENTITY_A());
132       if (aLine) {
133         aPnt_B = aPoint_B->pnt();
134         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
135       }
136     } else if (aPoint_A && !aPoint_B) {
137       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
138           aData, SketchPlugin_Constraint::ENTITY_B());
139       if (aLine) {
140         aPnt_A = aPoint_A->pnt();
141         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
142       }
143     }
144     if (!aPnt_A || !aPnt_B)
145       return false;
146
147     std::shared_ptr<GeomAPI_Pnt> aPoint1 = myPlane->to3D(aPnt_A->x(), aPnt_A->y());
148     std::shared_ptr<GeomAPI_Pnt> aPoint2 = myPlane->to3D(aPnt_B->x(), aPnt_B->y());
149     thePnt1 = aPoint1->impl<gp_Pnt>();
150     thePnt2 = aPoint2->impl<gp_Pnt>();
151     return true;
152   }
153   return false;
154 }
155
156
157
158 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
159                                                    const Standard_Integer theMode)
160 {
161   Standard_Integer aMode;
162   switch (theMode) {
163   case SketcherPrs_Tools::Sel_Dimension_All:
164     aMode = 0;
165     break;
166   case SketcherPrs_Tools::Sel_Dimension_Line:
167     aMode = 1;
168     break;
169   case SketcherPrs_Tools::Sel_Dimension_Text:
170     aMode = 2;
171     break;
172   default:
173     aMode = theMode;
174   }
175   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
176 }