Salome HOME
Provide special symbol for parametrized dimensions
[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 #include <ModelAPI_AttributeDouble.h>
25
26 #include <AIS_DisplaySpecialSymbol.hxx>
27
28
29 static const gp_Pnt MyDefStart(0,0,0);
30 static const gp_Pnt MyDefEnd(1,0,0);
31 static const gp_Pln MyDefPln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
32
33 static const Standard_ExtCharacter MySummSymbol(0x2211);
34
35 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_LengthDimension, AIS_LengthDimension);
36 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_LengthDimension, AIS_LengthDimension);
37
38 SketcherPrs_LengthDimension::SketcherPrs_LengthDimension(ModelAPI_Feature* theConstraint, 
39                         const std::shared_ptr<GeomAPI_Ax3>& thePlane)
40 : AIS_LengthDimension(MyDefStart, MyDefEnd, MyDefPln), 
41 myConstraint(theConstraint), myPlane(thePlane)
42 {
43   myAspect = new Prs3d_DimensionAspect();
44   myAspect->MakeArrows3d(false);
45   myAspect->MakeText3d(false);
46   myAspect->MakeTextShaded(false);
47   myAspect->MakeUnitsDisplayed(false);
48   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
49   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
50
51   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
52   SetDimensionAspect(myAspect);
53 }
54
55
56 void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
57                                  const Handle(Prs3d_Presentation)& thePresentation, 
58                                  const Standard_Integer theMode)
59 {
60   gp_Pnt aPnt1, aPnt2;
61   if (!getPoints(aPnt1, aPnt2))
62     return;
63
64   // compute flyout distance
65   double aD = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
66   SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
67   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
68
69   // Update variable aspect parameters (depending on viewer scale)
70   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
71   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
72   // The value of vertical aligment is sometimes changed
73   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
74
75   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
76   std::set<std::string> aParams = aValue->usedParameters();
77   if (aParams.size() > 0) {
78     SetSpecialSymbol(MySummSymbol);
79     SetDisplaySpecialSymbol(AIS_DSS_Before);
80   }
81
82   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
83 }
84
85 bool SketcherPrs_LengthDimension::getPoints(gp_Pnt& thePnt1, gp_Pnt& thePnt2)
86 {
87   DataPtr aData = myConstraint->data();
88   if (myConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
89     // The constraint is length
90     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
91       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
92       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
93     if (!anAttr)
94       return false;
95
96     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
97     if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
98       return false;
99
100     // Get geometry of the object
101     DataPtr aLineData = aFeature->data();
102     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
103       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
104       (aLineData->attribute(SketchPlugin_Line::START_ID()));
105     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = 
106       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
107       (aLineData->attribute(SketchPlugin_Line::END_ID()));
108     thePnt1 = myPlane->to3D(aStartPoint->x(), aStartPoint->y())->impl<gp_Pnt>();
109     thePnt2 = myPlane->to3D(aEndPoint->x(), aEndPoint->y())->impl<gp_Pnt>();
110     return true;
111
112   } else if (myConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
113     // The constraint is distance
114     std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
115         aData, SketchPlugin_Constraint::ENTITY_A(), myPlane);
116     std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
117         aData, SketchPlugin_Constraint::ENTITY_B(), myPlane);
118
119     std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
120     std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
121     if (aPoint_A && aPoint_B) {
122       // Both objects are points
123       aPnt_A = aPoint_A->pnt();
124       aPnt_B = aPoint_B->pnt();
125     } else if (!aPoint_A && aPoint_B) {
126       // First object is line
127       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
128           aData, SketchPlugin_Constraint::ENTITY_A());
129       if (aLine) {
130         aPnt_B = aPoint_B->pnt();
131         aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
132       }
133     } else if (aPoint_A && !aPoint_B) {
134       // Second object is line
135       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
136           aData, SketchPlugin_Constraint::ENTITY_B());
137       if (aLine) {
138         aPnt_A = aPoint_A->pnt();
139         aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
140       }
141     }
142     if (!aPnt_A || !aPnt_B) // Objects not found
143       return false;
144
145     // Get points from these objects
146     std::shared_ptr<GeomAPI_Pnt> aPoint1 = myPlane->to3D(aPnt_A->x(), aPnt_A->y());
147     std::shared_ptr<GeomAPI_Pnt> aPoint2 = myPlane->to3D(aPnt_B->x(), aPnt_B->y());
148     thePnt1 = aPoint1->impl<gp_Pnt>();
149     thePnt2 = aPoint2->impl<gp_Pnt>();
150     return true;
151   }
152   return false;
153 }
154
155
156
157 void SketcherPrs_LengthDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
158                                                    const Standard_Integer theMode)
159 {
160   // Map the application selection modes to standard ones
161   Standard_Integer aMode;
162   switch (theMode) {
163   case 0: // we should use selection of all objects
164     aMode = 0;
165     break;
166   case SketcherPrs_Tools::Sel_Dimension_All:
167     aMode = 0;
168     break;
169   case SketcherPrs_Tools::Sel_Dimension_Line:
170     aMode = 1;
171     break;
172   case SketcherPrs_Tools::Sel_Dimension_Text:
173     aMode = 2;
174     break;
175   default: {
176     // there are own selection modes, so the others should be ignored
177     // otherwise, the text selection appears in the viewer
178     return;
179   }
180   }
181   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
182   AIS_LengthDimension::ComputeSelection(aSelection, aMode);
183 }