]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Angle.cpp
Salome HOME
Empty AIS in the viewer: throw c++ exception (to be finalized, currently do nothing)
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Angle.cpp
4 // Created:     20 August 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Angle.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <SketchPlugin_ConstraintAngle.h>
11 #include <SketchPlugin_Constraint.h>
12
13 #include <GeomAPI_Edge.h>
14 #include <GeomAPI_Lin.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeDouble.h>
19
20 #include <TopExp.hxx>
21 #include <BRep_Tool.hxx>
22
23 #include <Events_Error.h>
24
25 #define PI 3.1415926535897932
26
27 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
28 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
29
30 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
31                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
32 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
33 {
34   myAspect = new Prs3d_DimensionAspect();
35   myAspect->MakeArrows3d(false);
36   myAspect->MakeText3d(false);
37   myAspect->MakeTextShaded(false);
38   myAspect->MakeUnitsDisplayed(false);
39   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
40   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
41   
42   SetDimensionAspect(myAspect);
43   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
44 }
45
46 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
47                                 const Handle(Prs3d_Presentation)& thePresentation, 
48                                 const Standard_Integer theMode)
49 {
50   DataPtr aData = myConstraint->data();
51
52   // Flyout point
53   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
54     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
55     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
56   if (!aFlyoutAttr->isInitialized()) {
57     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
58     return; // can not create a good presentation
59   }
60   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
61
62   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
63   if (!anAttr1->isInitialized()) {
64     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
65     return;
66   }
67
68   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
69   if (!anAttr2->isInitialized()) {
70     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
71     return;
72   }
73   // Get angle edges
74   ObjectPtr aObj1 = anAttr1->object();
75   ObjectPtr aObj2 = anAttr2->object();
76
77   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
78   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
79
80   if (!aShape1 && !aShape2) {
81     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
82     return;
83   }
84
85   TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
86   TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
87
88   TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
89   TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
90   SetMeasuredGeometry(aEdge1, aEdge2);
91
92   const gp_Pnt& aCenter = CenterPoint();
93   const gp_Pnt& aFirst = FirstPoint();
94   const gp_Pnt& aSecond = SecondPoint();
95
96   gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
97
98   gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
99   gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
100   double aDist = aFlyDir.Dot(aBisector.XYZ());
101   SetFlyout(aDist);
102
103   // Angle value is in degrees
104   AttributeDoublePtr aVal = aData->real(SketchPlugin_Constraint::VALUE());
105   SetCustomValue(aVal->value() * PI / 180.0);
106
107   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
108   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
109
110   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
111   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
112
113   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
114 }
115
116 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
117                                                    const Standard_Integer theMode)
118 {
119   Standard_Integer aMode;
120   switch (theMode) {
121   case 0: // we should use selection of all objects
122     aMode = 0;
123     break;
124   case SketcherPrs_Tools::Sel_Dimension_All:
125     aMode = 0;
126     break;
127   case SketcherPrs_Tools::Sel_Dimension_Line:
128     aMode = 1;
129     break;
130   case SketcherPrs_Tools::Sel_Dimension_Text:
131     aMode = 2;
132     break;
133   default: {
134     // there are own selection modes, so the others should be ignored
135     // otherwise, the text selection appears in the viewer
136     return; 
137   }
138   }
139   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
140 }