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