Salome HOME
03c840b270a051812c87d0ca455c8427797d05e8
[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 #include <math.h>
22
23 const double tolerance = 1.e-7;
24 #define PI 3.1415926535897932
25
26
27 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
28 {
29   myFlyoutUpdate = false;
30 }
31
32 void SketchPlugin_ConstraintAngle::initAttributes()
33 {
34   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
37   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
38 }
39
40 void SketchPlugin_ConstraintAngle::execute()
41 {
42   std::shared_ptr<ModelAPI_Data> aData = data();
43
44   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
45   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
46   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
47     return;
48
49   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
50       aData->attribute(SketchPlugin_Constraint::VALUE()));
51
52   if (!anAttrValue->isInitialized()) {
53     double anAngle = calculateAngle();
54     anAttrValue->setValue(anAngle);
55   }
56   // the value should to be computed here, not in the getAISObject in order to change the model value
57   // inside the object transaction. This is important for creating a constraint by preselection.
58   // The display of the presentation in this case happens after the transaction commit
59   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
60       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
61   if(!aFlyOutAttr->isInitialized())
62     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
63 }
64
65 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
66 {
67   if (!sketch())
68     return thePrevious;
69
70   AISObjectPtr anAIS = thePrevious;
71   if (!anAIS) {
72     anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
73   }
74
75   // Set color from preferences
76   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
77                                                     SKETCH_DIMENSION_COLOR);
78   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
79   return anAIS;
80 }
81
82 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
83 {
84   std::shared_ptr<ModelAPI_Data> aData = data();
85   if (!aData)
86     return;
87   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
88   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
89   if (!aLineA || !aLineB)
90     return;
91
92   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
93       theID == SketchPlugin_Constraint::ENTITY_B()) {
94     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
95         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
96     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
97       double anAngle = calculateAngle();
98       aValueAttr->setValue(anAngle);
99     }
100   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
101     // Recalculate flyout point in local coordinates
102     // coordinates are calculated according to the center of shapes intersection
103     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
104         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
105   }
106 }
107
108 double SketchPlugin_ConstraintAngle::calculateAngle()
109 {
110   double anAngle = 0.0;
111
112   std::shared_ptr<ModelAPI_Data> aData = data();
113   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
114   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
115   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
116
117   // Start and end points of lines
118   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119       aLineA->attribute(SketchPlugin_Line::START_ID()));
120   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121       aLineA->attribute(SketchPlugin_Line::END_ID()));
122
123   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124       aLineB->attribute(SketchPlugin_Line::START_ID()));
125   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126       aLineB->attribute(SketchPlugin_Line::END_ID()));
127
128   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
129   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
130   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
131   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
132   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
133     return anAngle;
134
135   // Lines and their intersection point
136   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
137   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
138   std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
139   if (!anInter)
140     return anAngle;
141
142   // Directions of lines
143   if (anInter->distance(aEndA) < tolerance)
144     aEndA = aStartA;
145   if (anInter->distance(aEndB) < tolerance)
146     aEndB = aStartB;
147   std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
148   std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
149
150   anAngle = aDirA->angle(aDirB) * 180.0 / PI;
151   return anAngle;
152 }
153
154 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
155 {
156   std::shared_ptr<ModelAPI_Data> aData = data();
157   if (!aData->isValid())
158     return;
159
160   myFlyoutUpdate = true;
161   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
162       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
163   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
164   myFlyoutUpdate = false;
165 }
166
167
168 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
169 {
170   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
171     return false;
172   if (!sketch())
173     return false;
174
175   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
176                            GeomDataAPI_Point2D>(attribute(theAttributeId));
177   if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
178     return false;
179
180   DataPtr aData = data();
181   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
182   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
183   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
184
185   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
186     return false;
187
188   // Start and end points of lines
189   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190       aLineA->attribute(SketchPlugin_Line::START_ID()));
191   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192       aLineA->attribute(SketchPlugin_Line::END_ID()));
193
194   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
195   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
196   if (aStartA->distance(aEndA) < tolerance)
197     return false;
198
199   myFlyoutUpdate = true;
200   double aX = (aStartA->x() + aEndA->x()) / 2.;
201   double aY = (aStartA->y() + aEndA->y()) / 2.;
202
203   aFlyOutAttr->setValue(aX, aY);
204   myFlyoutUpdate = false;
205
206   return true;
207 }