Salome HOME
3ab9bc6be8b050906cf8d2e6b2679692841bf5aa
[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(SketcherPrs_Tools::getDefaultTextHeight());
44   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
45
46   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
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
59   double aD = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
60   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
61   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
62   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
63   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
64   // The value of vertical aligment is sometimes changed
65   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
66   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
67 }
68
69 bool SketcherPrs_LengthDimension::getPoints(gp_Pnt& thePnt1, gp_Pnt& thePnt2)
70 {
71   DataPtr aData = myConstraint->data();
72   if (myConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
73     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
74       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
75       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76     if (!anAttr)
77       return false;
78
79     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
80     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
81       return false;
82
83     DataPtr aLineData = aFeature->data();
84     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
85       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
86       (aLineData->attribute(SketchPlugin_Line::START_ID()));
87     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
88       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
89       (aLineData->attribute(SketchPlugin_Line::END_ID()));
90     thePnt1 = myPlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
91     thePnt2 = myPlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
92     return true;
93
94   } else if (myConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
95     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
96         aData, SketchPlugin_Constraint::ENTITY_A(), myPlane);
97     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
98         aData, SketchPlugin_Constraint::ENTITY_B(), myPlane);
99
100     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
101     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
102     if (aPoint_A && aPoint_B) {
103       aPnt_A = aPoint_A->pnt();
104       aPnt_B = aPoint_B->pnt();
105     } else if (!aPoint_A && aPoint_B) {
106       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
107           aData, SketchPlugin_Constraint::ENTITY_A());
108       if (aLine) {
109         aPnt_B = aPoint_B->pnt();
110         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
111       }
112     } else if (aPoint_A && !aPoint_B) {
113       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
114           aData, SketchPlugin_Constraint::ENTITY_B());
115       if (aLine) {
116         aPnt_A = aPoint_A->pnt();
117         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
118       }
119     }
120     if (!aPnt_A || !aPnt_B)
121       return false;
122
123     std::shared_ptr<GeomAPI_Pnt> aPoint1 = myPlane->to3D(aPnt_A->x(), aPnt_A->y());
124     std::shared_ptr<GeomAPI_Pnt> aPoint2 = myPlane->to3D(aPnt_B->x(), aPnt_B->y());
125     thePnt1 = aPoint1->impl<gp_Pnt>();
126     thePnt2 = aPoint2->impl<gp_Pnt>();
127     return true;
128   }
129   return false;
130 }
131
132
133
134 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
135                                                    const Standard_Integer theMode)
136 {
137   Standard_Integer aMode;
138   switch (theMode) {
139   case 0: // we should use selection of all objects
140     aMode = 0;
141     break;
142   case SketcherPrs_Tools::Sel_Dimension_All:
143     aMode = 0;
144     break;
145   case SketcherPrs_Tools::Sel_Dimension_Line:
146     aMode = 1;
147     break;
148   case SketcherPrs_Tools::Sel_Dimension_Text:
149     aMode = 2;
150     break;
151   default: {
152     // there are own selection modes, so the others should be ignored
153     // otherwise, the text selection appears in the viewer
154     return;
155   }
156   }
157   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
158   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
159 }