]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Radius.cpp
Salome HOME
Bug #1279: Bad display of distance constraint in the sktech
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Radius.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Radius.cpp
4 // Created:     26 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Radius.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <SketchPlugin_ConstraintRadius.h>
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_Circle.h>
13 #include <SketchPlugin_Arc.h>
14
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomAPI_XYZ.h>
19 #include <ModelAPI_AttributeDouble.h>
20
21 #include <gp_Circ.hxx>
22
23 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
24
25 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
26 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
27
28 static const Standard_ExtCharacter MyEmptySymbol(' ');
29 static const Standard_ExtCharacter MySummSymbol(0x03A3);
30
31 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
32                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
33 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
34 {
35   // Set default values of the presentation
36   myAspect = new Prs3d_DimensionAspect();
37   myAspect->MakeArrows3d(false);
38   myAspect->MakeText3d(false);
39   myAspect->MakeTextShaded(false);
40   myAspect->MakeUnitsDisplayed(false);
41   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
43   
44   SetDimensionAspect(myAspect);
45   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
46 }
47
48 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
49                                  const Handle(Prs3d_Presentation)& thePresentation, 
50                                  const Standard_Integer theMode)
51 {
52   DataPtr aData = myConstraint->data();
53
54   // Flyout point
55   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
56     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
57     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
58   if (!aFlyoutAttr->isInitialized())
59     return; // can not create a good presentation
60
61   // Get circle
62   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
63     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
64     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
65   if (!anAttr) return;
66
67   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
68   double aRadius = 1;
69   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
70   // it is possible that circle result becomes zero, in this case the presentation should disappear
71   // for example, it happens when circle radius is set to zero
72   if (!aCyrcFeature)
73     return;
74   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
75     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
76         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
77     AttributeDoublePtr aCircRadius = 
78       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
79       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
80     aRadius = aCircRadius->value();
81   } else { // arc
82     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
83         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
84     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
85       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
86       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
87     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
88   }
89   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
90   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
91
92   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
93     
94   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
95
96   gp_Circ aCirc = aCircle.impl<gp_Circ>();
97   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
98   // anchor point should not coincide to the location point of the circle
99   // OCCT does not process this case.
100   if (anAncorPnt.Distance(aCirc.Location()) < 1e-7)
101     return;
102   SetMeasuredGeometry(aCirc, anAncorPnt);
103   SetCustomValue(aRadius);
104
105   // Update variable aspect parameters (depending on viewer scale)
106   double anArrowLength = myAspect->ArrowAspect()->Length();
107    // This is not realy correct way to get viewer scale.
108   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
109   double aDimensionValue = GetValue();
110   double aTextSize = 0.0;
111   GetValueString(aTextSize);
112
113   if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
114     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
115     myAspect->SetArrowOrientation(Prs3d_DAO_External);
116     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
117   } else {
118     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
119     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
120   }
121   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
122
123   // The value of vertical aligment is sometimes changed
124   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
125
126   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
127   std::set<std::string> aParams = aValue->usedParameters();
128   if (aParams.size() > 0) {
129     SetSpecialSymbol(MySummSymbol);
130     SetDisplaySpecialSymbol(AIS_DSS_Before);
131   }
132   else {
133     SetSpecialSymbol(MyEmptySymbol);
134     SetDisplaySpecialSymbol(AIS_DSS_Before);
135   }
136
137   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
138 }
139
140 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
141                                                    const Standard_Integer theMode)
142 {
143   // Map the application selection modes to standard ones
144   Standard_Integer aMode;
145   switch (theMode) {
146   case 0: // we should use selection of all objects
147     aMode = 0;
148     break;
149   case SketcherPrs_Tools::Sel_Dimension_All:
150     aMode = 0;
151     break;
152   case SketcherPrs_Tools::Sel_Dimension_Line:
153     aMode = 1;
154     break;
155   case SketcherPrs_Tools::Sel_Dimension_Text:
156     aMode = 2;
157     break;
158   default: {
159     // there are own selection modes, so the others should be ignored
160     // otherwise, the text selection appears in the viewer
161     return; 
162   }
163   }
164   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
165 }