Salome HOME
Implementation of the Angle constraint (issue #788)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintAngle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintAngle.cpp
4 // Created: 19 August 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintAngle.h"
8 #include <SketchPlugin_Line.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <GeomDataAPI_Point2D.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Lin2d.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_XY.h>
17
18 #include <SketcherPrs_Factory.h>
19 #include <SketcherPrs_Tools.h>
20
21 const double tolerance = 1.e-7;
22 #define PI 3.1415926535897932
23
24
25 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
26 {
27 }
28
29 void SketchPlugin_ConstraintAngle::initAttributes()
30 {
31   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
34 }
35
36 void SketchPlugin_ConstraintAngle::execute()
37 {
38   std::shared_ptr<ModelAPI_Data> aData = data();
39   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
40       aData->attribute(SketchPlugin_Constraint::VALUE()));
41
42   if(anAttrValue->isInitialized())
43     return;
44
45   double anAngle = calculateAngle();
46   anAttrValue->setValue(anAngle);
47 }
48
49 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
50 {
51   if (!sketch())
52     return thePrevious;
53
54   AISObjectPtr anAIS = thePrevious;
55   if (!anAIS) {
56     anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
57   }
58   return anAIS;
59 }
60
61 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
62 {
63   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
64       theID == SketchPlugin_Constraint::ENTITY_B()) {
65     std::shared_ptr<ModelAPI_Data> aData = data();
66     if (!aData)
67       return;
68   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
69   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
70     if (!aLineA || !aLineB)
71       return;
72
73     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
74         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
75     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
76       double anAngle = calculateAngle();
77       aValueAttr->setValue(anAngle);
78     }
79   }
80 }
81
82 double SketchPlugin_ConstraintAngle::calculateAngle()
83 {
84   double anAngle = 0.0;
85
86   std::shared_ptr<ModelAPI_Data> aData = data();
87   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
88   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
89   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
90
91   // Start and end points of lines
92   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93       aLineA->attribute(SketchPlugin_Line::START_ID()));
94   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95       aLineA->attribute(SketchPlugin_Line::END_ID()));
96
97   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
98       aLineB->attribute(SketchPlugin_Line::START_ID()));
99   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100       aLineB->attribute(SketchPlugin_Line::END_ID()));
101
102   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
103   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
104   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
105   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
106   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
107     return anAngle;
108
109   // Lines and their intersection point
110   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
111   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
112   std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
113   if (!anInter)
114     return anAngle;
115
116   // Directions of lines
117   if (anInter->distance(aEndA) < tolerance)
118     aEndA = aStartA;
119   if (anInter->distance(aEndB) < tolerance)
120     aEndB = aStartB;
121   std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
122   std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
123
124   anAngle = aDirA->angle(aDirB) * 180.0 / PI;
125   return anAngle;
126 }