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 aValueAttr->setValue(anAngle);
157 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
158 updateConstraintValueByAngleValue();
162 double SketchPlugin_ConstraintAngle::calculateAngle()
164 std::shared_ptr<ModelAPI_Data> aData = data();
165 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
166 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
167 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
169 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170 aLineA->attribute(SketchPlugin_Line::START_ID()));
171 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
172 aLineA->attribute(SketchPlugin_Line::END_ID()));
173 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
174 aLineB->attribute(SketchPlugin_Line::START_ID()));
175 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 aLineB->attribute(SketchPlugin_Line::END_ID()));
178 std::shared_ptr<GeomAPI_Angle2d> anAng;
179 if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
180 !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
181 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
182 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
184 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
185 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
186 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
187 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
188 anAng = std::shared_ptr<GeomAPI_Angle2d>(
189 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
191 double anAngle = anAng->angleDegree();
192 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
193 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
194 if (!aValueAttr->isInitialized())
195 aValueAttr->setValue(anAngle);
196 /// an angle value should be corrected by the current angle type
197 anAngle = getAngleForType(anAngle);
198 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
199 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
203 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
205 double anAngle = theAngle;
207 std::shared_ptr<ModelAPI_Data> aData = data();
208 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
209 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
210 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
211 switch (anAngleType) {
212 case SketcherPrs_Tools::ANGLE_DIRECT:
215 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
216 if (theAngle > 180 || isPreviousValueObtuse)
217 anAngle = theAngle - 180.0;
219 anAngle = 180.0 - theAngle;
225 case SketcherPrs_Tools::ANGLE_BACKWARD:
226 anAngle = 360.0 - theAngle;
234 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
236 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
237 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
238 double anAngle = aValueAttr->value();
240 /// an angle value should be corrected by the current angle type
241 aValueAttr = std::dynamic_pointer_cast<
242 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
243 if (!aValueAttr->isInitialized())
245 anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
246 aValueAttr->setValue(anAngle);
249 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
251 std::shared_ptr<ModelAPI_Data> aData = data();
252 if (!aData->isValid())
255 myFlyoutUpdate = true;
256 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
257 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
258 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
259 myFlyoutUpdate = false;
263 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
265 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
270 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
271 GeomDataAPI_Point2D>(attribute(theAttributeId));
272 if (aFlyOutAttr->isInitialized() &&
273 (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
276 DataPtr aData = data();
277 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
278 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
279 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
281 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
284 // Start and end points of lines
285 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
286 aLineA->attribute(SketchPlugin_Line::START_ID()));
287 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288 aLineA->attribute(SketchPlugin_Line::END_ID()));
290 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
291 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
292 if (aStartA->distance(aEndA) < tolerance)
295 myFlyoutUpdate = true;
296 double aX = (aStartA->x() + aEndA->x()) / 2.;
297 double aY = (aStartA->y() + aEndA->y()) / 2.;
299 aFlyOutAttr->setValue(aX, aY);
300 myFlyoutUpdate = false;
306 // =============== Auxiliary functions ==================================
307 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
309 // Start and end points of lines
310 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
311 theLine1->attribute(SketchPlugin_Line::START_ID()));
312 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
313 theLine1->attribute(SketchPlugin_Line::END_ID()));
315 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
316 theLine2->attribute(SketchPlugin_Line::START_ID()));
317 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
318 theLine2->attribute(SketchPlugin_Line::END_ID()));
320 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
321 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
322 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
323 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
324 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
325 std::shared_ptr<GeomAPI_Pnt2d>();
327 // Lines and their intersection point
328 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
329 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
330 return aLA->intersect(aLB);