1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintAngle.cpp
4 // Created: 19 August 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintAngle.h"
8 #include <SketchPlugin_Line.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeInteger.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_XY.h>
20 #include <SketcherPrs_Factory.h>
21 #include <SketcherPrs_Tools.h>
25 const double tolerance = 1.e-7;
26 #define PI 3.1415926535897932
28 /// \brief Calculate intersection point of two lines
29 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
32 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
34 myFlyoutUpdate = false;
37 void SketchPlugin_ConstraintAngle::initAttributes()
39 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
40 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
41 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
42 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
44 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
48 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
49 std::string& theDefault)
51 theSection = "Visualization";
52 theName = "sketch_dimension_color";
53 theDefault = SKETCH_DIMENSION_COLOR;
56 void SketchPlugin_ConstraintAngle::execute()
58 std::shared_ptr<ModelAPI_Data> aData = data();
60 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
61 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
62 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
65 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
66 aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
68 if (!anAttrValue->isInitialized()) {
69 double anAngle = calculateAngle();
70 anAttrValue->setValue(anAngle);
73 // the value should to be computed here, not in the getAISObject in order to change the model value
74 // inside the object transaction. This is important for creating a constraint by preselection.
75 // The display of the presentation in this case happens after the transaction commit
76 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
77 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
78 if(!aFlyOutAttr->isInitialized())
79 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
82 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
87 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
92 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
94 std::shared_ptr<ModelAPI_Data> aData = data();
97 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
98 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
99 if (!aLineA || !aLineB)
102 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
103 theID == SketchPlugin_Constraint::ENTITY_B()) {
104 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
105 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
106 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
107 double anAngle = calculateAngle();
108 aValueAttr->setValue(anAngle);
111 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
112 // Recalculate flyout point in local coordinates
113 // coordinates are calculated according to the center of shapes intersection
114 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
115 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
117 std::shared_ptr<ModelAPI_Data> aData = data();
118 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
119 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
120 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
122 // Intersection of lines
123 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
127 myFlyoutUpdate = true;
128 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
129 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
130 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
131 myFlyoutUpdate = false;
133 else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
134 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
135 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
136 double anAngle = calculateAngle();
137 aValueAttr->setValue(anAngle);
140 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
145 double SketchPlugin_ConstraintAngle::calculateAngle()
147 double anAngle = 0.0;
149 std::shared_ptr<ModelAPI_Data> aData = data();
150 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
151 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
152 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
154 // Intersection of lines
155 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
159 // Start and end points of lines
160 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aLineA->attribute(SketchPlugin_Line::START_ID()));
162 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
163 aLineA->attribute(SketchPlugin_Line::END_ID()));
165 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
166 aLineB->attribute(SketchPlugin_Line::START_ID()));
167 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168 aLineB->attribute(SketchPlugin_Line::END_ID()));
170 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
171 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
172 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
173 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
175 double aDist[2][2] = {
176 { anInter->distance(aStartA), anInter->distance(aEndA) },
177 { anInter->distance(aStartB), anInter->distance(aEndB) }
180 // Directions of lines
181 if (aDist[0][0] > aDist[0][1])
183 if (aDist[1][0] > aDist[1][1])
186 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
187 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
189 double aDirAngle = aDirA->angle(aDirB);
191 aDirAngle += 2.0 * PI;
192 anAngle = fabs(aDirAngle) * 180.0 / PI;
193 /// an angle value should be corrected by the current angle type
194 anAngle = getAngleForType(anAngle);
198 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle)
200 double anAngle = theAngle;
202 std::shared_ptr<ModelAPI_Data> aData = data();
203 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
204 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
205 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
206 switch (anAngleType) {
207 case SketcherPrs_Tools::ANGLE_DIRECT:
210 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
211 anAngle = fabs(180.0 - theAngle);
213 case SketcherPrs_Tools::ANGLE_BACKWARD:
214 anAngle = 360.0 - theAngle;
222 void SketchPlugin_ConstraintAngle::updateAngleValue()
224 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
225 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
226 double anAngle = aValueAttr->value();
228 /// an angle value should be corrected by the current angle type
229 anAngle = getAngleForType(anAngle);
230 aValueAttr = std::dynamic_pointer_cast<
231 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
232 aValueAttr->setValue(anAngle);
235 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
237 std::shared_ptr<ModelAPI_Data> aData = data();
238 if (!aData->isValid())
241 myFlyoutUpdate = true;
242 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
243 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
244 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
245 myFlyoutUpdate = false;
249 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
251 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
256 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
257 GeomDataAPI_Point2D>(attribute(theAttributeId));
258 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
261 DataPtr aData = data();
262 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
263 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
264 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
266 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
269 // Start and end points of lines
270 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
271 aLineA->attribute(SketchPlugin_Line::START_ID()));
272 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273 aLineA->attribute(SketchPlugin_Line::END_ID()));
275 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
276 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
277 if (aStartA->distance(aEndA) < tolerance)
280 myFlyoutUpdate = true;
281 double aX = (aStartA->x() + aEndA->x()) / 2.;
282 double aY = (aStartA->y() + aEndA->y()) / 2.;
284 aFlyOutAttr->setValue(aX, aY);
285 myFlyoutUpdate = false;
291 // =============== Auxiliary functions ==================================
292 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
294 // Start and end points of lines
295 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296 theLine1->attribute(SketchPlugin_Line::START_ID()));
297 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
298 theLine1->attribute(SketchPlugin_Line::END_ID()));
300 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
301 theLine2->attribute(SketchPlugin_Line::START_ID()));
302 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
303 theLine2->attribute(SketchPlugin_Line::END_ID()));
305 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
306 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
307 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
308 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
309 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
310 std::shared_ptr<GeomAPI_Pnt2d>();
312 // Lines and their intersection point
313 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
314 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
315 return aLA->intersect(aLB);