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