]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Radius.cpp
Salome HOME
Empty AIS in the viewer: throw c++ exception (to be finalized, currently do nothing)
[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 <Events_Error.h>
16
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Circ.h>
20 #include <GeomAPI_XYZ.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <gp_Circ.hxx>
24
25 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
26
27 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
28 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
29
30 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
31                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
32 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
33 {
34   // Set default values of the presentation
35   myAspect = new Prs3d_DimensionAspect();
36   myAspect->MakeArrows3d(false);
37   myAspect->MakeText3d(false);
38   myAspect->MakeTextShaded(false);
39   myAspect->MakeUnitsDisplayed(false);
40   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
41   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
42   
43   SetDimensionAspect(myAspect);
44   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
45 }
46
47 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
48                                  const Handle(Prs3d_Presentation)& thePresentation, 
49                                  const Standard_Integer theMode)
50 {
51   DataPtr aData = myConstraint->data();
52
53   // Flyout point
54   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
55     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
56     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
57   if (!aFlyoutAttr->isInitialized()) {
58     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
59     return; // can not create a good presentation
60   }
61
62   // Get circle
63   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
64     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
65     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
66   if (!anAttr) return;
67
68   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
69   double aRadius = 1;
70   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
71   // it is possible that circle result becomes zero, in this case the presentation should disappear
72   // for example, it happens when circle radius is set to zero
73   if (!aCyrcFeature)
74     return;
75   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
76     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
77         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
78     AttributeDoublePtr aCircRadius = 
79       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
81     aRadius = aCircRadius->value();
82   } else { // arc
83     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
84         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
85     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
86       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
87       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
88     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
89   }
90   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
91   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
92
93   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
94     
95   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
96
97   gp_Circ aCirc = aCircle.impl<gp_Circ>();
98   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
99   // anchor point should not coincide to the location point of the circle
100   // OCCT does not process this case.
101   if (anAncorPnt.Distance(aCirc.Location()) < 1e-7)
102     return;
103   SetMeasuredGeometry(aCirc, anAncorPnt);
104   SetCustomValue(aRadius);
105
106   // Update variable aspect parameters (depending on viewer scale)
107   double anArrowLength = myAspect->ArrowAspect()->Length();
108    // This is not realy correct way to get viewer scale.
109   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
110   double aDimensionValue = GetValue();
111   double aTextSize = 0.0;
112   GetValueString(aTextSize);
113
114   if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
115     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
116     myAspect->SetArrowOrientation(Prs3d_DAO_External);
117     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
118   } else {
119     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
120     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
121   }
122   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
123
124   // The value of vertical aligment is sometimes changed
125   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
126
127   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
128   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
129
130   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
131 }
132
133 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
134                                                    const Standard_Integer theMode)
135 {
136   // Map the application selection modes to standard ones
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   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
158 }