]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp
Salome HOME
fc2df7237bdaff1f5d4e2bf46c68e13c9aa7b8aa
[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   myFlyoutUpdate = false;
28 }
29
30 void SketchPlugin_ConstraintAngle::initAttributes()
31 {
32   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
34   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
35   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
36 }
37
38 void SketchPlugin_ConstraintAngle::execute()
39 {
40   std::shared_ptr<ModelAPI_Data> aData = data();
41   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
42       aData->attribute(SketchPlugin_Constraint::VALUE()));
43
44   if(anAttrValue->isInitialized())
45     return;
46
47   double anAngle = calculateAngle();
48   anAttrValue->setValue(anAngle);
49 }
50
51 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
52 {
53   if (!sketch())
54     return thePrevious;
55
56   AISObjectPtr anAIS = thePrevious;
57   if (!anAIS) {
58     anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
59   }
60
61   // Set color from preferences
62   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
63                                                     SKETCH_DIMENSION_COLOR);
64   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
65   return anAIS;
66 }
67
68 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
69 {
70   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
71       theID == SketchPlugin_Constraint::ENTITY_B()) {
72     std::shared_ptr<ModelAPI_Data> aData = data();
73     if (!aData)
74       return;
75   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
76   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
77     if (!aLineA || !aLineB)
78       return;
79
80     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
81         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
82     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
83       double anAngle = calculateAngle();
84       aValueAttr->setValue(anAngle);
85     }
86   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
87     // Recalculate flyout point in local coordinates
88     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
89         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
90   }
91 }
92
93 double SketchPlugin_ConstraintAngle::calculateAngle()
94 {
95   double anAngle = 0.0;
96
97   std::shared_ptr<ModelAPI_Data> aData = data();
98   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
99   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
100   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
101
102   // Start and end points of lines
103   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104       aLineA->attribute(SketchPlugin_Line::START_ID()));
105   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106       aLineA->attribute(SketchPlugin_Line::END_ID()));
107
108   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109       aLineB->attribute(SketchPlugin_Line::START_ID()));
110   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111       aLineB->attribute(SketchPlugin_Line::END_ID()));
112
113   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
114   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
115   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
116   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
117   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
118     return anAngle;
119
120   // Lines and their intersection point
121   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
122   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
123   std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
124   if (!anInter)
125     return anAngle;
126
127   // Directions of lines
128   if (anInter->distance(aEndA) < tolerance)
129     aEndA = aStartA;
130   if (anInter->distance(aEndB) < tolerance)
131     aEndB = aStartB;
132   std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
133   std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
134
135   anAngle = aDirA->angle(aDirB) * 180.0 / PI;
136   return anAngle;
137 }
138
139 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
140 {
141   std::shared_ptr<ModelAPI_Data> aData = data();
142   if (!aData->isValid())
143     return;
144
145   myFlyoutUpdate = true;
146   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
147       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
148   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
149   myFlyoutUpdate = false;
150 }