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_Angle2d.h>
16 #include <GeomAPI_Dir2d.h>
17 #include <GeomAPI_Lin2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
21 #include <SketcherPrs_Factory.h>
22 #include <SketcherPrs_Tools.h>
26 const double tolerance = 1.e-7;
27 #define PI 3.1415926535897932
29 /// \brief Calculate intersection point of two lines
30 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
33 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
35 myFlyoutUpdate = false;
38 void SketchPlugin_ConstraintAngle::initAttributes()
40 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
41 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
42 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
43 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
45 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(),
46 ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(),
48 ModelAPI_AttributeInteger::typeId());
50 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(),
51 ModelAPI_AttributeBoolean::typeId());
52 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(),
53 ModelAPI_AttributeBoolean::typeId());
56 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
57 std::string& theDefault)
59 theSection = "Visualization";
60 theName = "sketch_dimension_color";
61 theDefault = SKETCH_DIMENSION_COLOR;
64 void SketchPlugin_ConstraintAngle::execute()
66 std::shared_ptr<ModelAPI_Data> aData = data();
68 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA =
69 aData->refattr(SketchPlugin_Constraint::ENTITY_A());
70 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB =
71 aData->refattr(SketchPlugin_Constraint::ENTITY_B());
72 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
75 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
76 aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
78 if (!anAttrValue->isInitialized()) {
79 double anAngle = calculateAngle();
80 anAttrValue->setValue(anAngle);
81 updateConstraintValueByAngleValue();
83 // the value should to be computed here, not in the
84 // getAISObject in order to change the model value
85 // inside the object transaction. This is important for creating a constraint by preselection.
86 // The display of the presentation in this case happens after the transaction commit
87 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
88 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
89 if(!aFlyOutAttr->isInitialized())
90 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
93 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
98 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
103 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
105 std::shared_ptr<ModelAPI_Data> aData = data();
108 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
109 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
110 if (!aLineA || !aLineB)
113 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
114 theID == SketchPlugin_Constraint::ENTITY_B()) {
115 AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
116 AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
117 // only if one of attributes is not initialized, try to compute the current value
118 if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
119 if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized())
120 // initialize base value of constraint
121 updateConstraintValueByAngleValue();
122 double anAngle = calculateAngle();
123 aValueAttr->setValue(anAngle);
124 updateConstraintValueByAngleValue();
126 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
127 // Recalculate flyout point in local coordinates
128 // coordinates are calculated according to the center of shapes intersection
129 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
130 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
133 std::shared_ptr<ModelAPI_Data> aData = data();
134 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
136 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
138 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
140 // Intersection of lines
141 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
145 myFlyoutUpdate = true;
146 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
147 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
148 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
149 myFlyoutUpdate = false;
151 else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
152 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
153 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
154 double anAngle = calculateAngle();
155 if (aValueAttr->text().empty())
156 aValueAttr->setValue(anAngle);
158 aValueAttr = std::dynamic_pointer_cast<
159 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
160 aValueAttr->setValue(anAngle);
163 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
164 updateConstraintValueByAngleValue();
168 double SketchPlugin_ConstraintAngle::calculateAngle()
170 std::shared_ptr<ModelAPI_Data> aData = data();
171 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
172 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
173 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
175 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 aLineA->attribute(SketchPlugin_Line::START_ID()));
177 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178 aLineA->attribute(SketchPlugin_Line::END_ID()));
179 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
180 aLineB->attribute(SketchPlugin_Line::START_ID()));
181 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aLineB->attribute(SketchPlugin_Line::END_ID()));
184 std::shared_ptr<GeomAPI_Angle2d> anAng;
185 if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
186 !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
187 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
188 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
190 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
191 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
192 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
193 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
194 anAng = std::shared_ptr<GeomAPI_Angle2d>(
195 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
197 double anAngle = anAng->angleDegree();
198 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
199 ModelAPI_AttributeDouble>(data()->attribute(VALUE()));
200 std::shared_ptr<ModelAPI_AttributeDouble> anAngleValueAttr = std::dynamic_pointer_cast<
201 ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID()));
202 if (!aValueAttr->isInitialized())
203 aValueAttr->setValue(anAngle);
204 /// an angle value should be corrected by the current angle type
205 anAngle = getAngleForType(anAngleValueAttr->text().empty() ?
206 anAngle : anAngleValueAttr->value());
207 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
208 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
212 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
214 double anAngle = theAngle;
216 std::shared_ptr<ModelAPI_Data> aData = data();
217 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
218 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
219 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
220 switch (anAngleType) {
221 case SketcherPrs_Tools::ANGLE_DIRECT:
224 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
225 if (theAngle > 180 || isPreviousValueObtuse)
226 anAngle = theAngle - 180.0;
228 anAngle = 180.0 - theAngle;
234 case SketcherPrs_Tools::ANGLE_BACKWARD:
235 anAngle = 360.0 - theAngle;
243 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
245 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
246 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
247 double anAngle = aValueAttr->value();
249 /// an angle value should be corrected by the current angle type
250 aValueAttr = std::dynamic_pointer_cast<
251 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
252 if (!aValueAttr->isInitialized())
254 anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
255 aValueAttr->setValue(anAngle);
258 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
260 std::shared_ptr<ModelAPI_Data> aData = data();
261 if (!aData->isValid())
264 myFlyoutUpdate = true;
265 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
266 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
267 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
268 myFlyoutUpdate = false;
272 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
274 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
279 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
280 GeomDataAPI_Point2D>(attribute(theAttributeId));
281 if (aFlyOutAttr->isInitialized() &&
282 (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
285 DataPtr aData = data();
286 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
287 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
288 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
290 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
293 // Start and end points of lines
294 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
295 aLineA->attribute(SketchPlugin_Line::START_ID()));
296 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297 aLineA->attribute(SketchPlugin_Line::END_ID()));
299 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
300 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
301 if (aStartA->distance(aEndA) < tolerance)
304 myFlyoutUpdate = true;
305 double aX = (aStartA->x() + aEndA->x()) / 2.;
306 double aY = (aStartA->y() + aEndA->y()) / 2.;
308 aFlyOutAttr->setValue(aX, aY);
309 myFlyoutUpdate = false;
315 // =============== Auxiliary functions ==================================
316 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
318 // Start and end points of lines
319 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
320 theLine1->attribute(SketchPlugin_Line::START_ID()));
321 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
322 theLine1->attribute(SketchPlugin_Line::END_ID()));
324 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
325 theLine2->attribute(SketchPlugin_Line::START_ID()));
326 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
327 theLine2->attribute(SketchPlugin_Line::END_ID()));
329 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
330 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
331 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
332 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
333 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
334 std::shared_ptr<GeomAPI_Pnt2d>();
336 // Lines and their intersection point
337 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
338 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
339 return aLA->intersect(aLB);