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