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(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
48 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
49 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
52 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
53 std::string& theDefault)
55 theSection = "Visualization";
56 theName = "sketch_dimension_color";
57 theDefault = SKETCH_DIMENSION_COLOR;
60 void SketchPlugin_ConstraintAngle::execute()
62 std::shared_ptr<ModelAPI_Data> aData = data();
64 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
65 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
66 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
69 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
70 aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
72 if (!anAttrValue->isInitialized()) {
73 double anAngle = calculateAngle();
74 anAttrValue->setValue(anAngle);
75 updateConstraintValueByAngleValue();
77 // the value should to be computed here, not in the getAISObject in order to change the model value
78 // inside the object transaction. This is important for creating a constraint by preselection.
79 // The display of the presentation in this case happens after the transaction commit
80 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
81 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
82 if(!aFlyOutAttr->isInitialized())
83 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
86 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
91 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
96 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
98 std::shared_ptr<ModelAPI_Data> aData = data();
101 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
102 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
103 if (!aLineA || !aLineB)
106 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
107 theID == SketchPlugin_Constraint::ENTITY_B()) {
108 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
109 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
110 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
111 double anAngle = calculateAngle();
112 aValueAttr->setValue(anAngle);
113 updateConstraintValueByAngleValue();
115 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
116 // Recalculate flyout point in local coordinates
117 // coordinates are calculated according to the center of shapes intersection
118 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
119 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
121 std::shared_ptr<ModelAPI_Data> aData = data();
122 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
123 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
124 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
126 // Intersection of lines
127 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
131 myFlyoutUpdate = true;
132 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
133 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
134 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
135 myFlyoutUpdate = false;
137 else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
138 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
139 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
140 double anAngle = calculateAngle();
141 aValueAttr->setValue(anAngle);
143 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
144 updateConstraintValueByAngleValue();
148 double SketchPlugin_ConstraintAngle::calculateAngle()
150 std::shared_ptr<ModelAPI_Data> aData = data();
151 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
152 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
153 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
155 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
156 aLineA->attribute(SketchPlugin_Line::START_ID()));
157 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158 aLineA->attribute(SketchPlugin_Line::END_ID()));
159 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160 aLineB->attribute(SketchPlugin_Line::START_ID()));
161 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162 aLineB->attribute(SketchPlugin_Line::END_ID()));
164 std::shared_ptr<GeomAPI_Angle2d> anAng;
165 if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
166 !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
167 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
168 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
170 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
171 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
172 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
173 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
174 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
176 double anAngle = anAng->angleDegree();
177 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
178 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
179 if (!aValueAttr->isInitialized())
180 aValueAttr->setValue(anAngle);
181 /// an angle value should be corrected by the current angle type
182 anAngle = getAngleForType(anAngle);
183 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
184 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
188 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
190 double anAngle = theAngle;
192 std::shared_ptr<ModelAPI_Data> aData = data();
193 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
194 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
195 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
196 switch (anAngleType) {
197 case SketcherPrs_Tools::ANGLE_DIRECT:
200 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
201 if (theAngle > 180 || isPreviousValueObtuse)
202 anAngle = theAngle - 180.0;
204 anAngle = 180.0 - theAngle;
210 case SketcherPrs_Tools::ANGLE_BACKWARD:
211 anAngle = 360.0 - theAngle;
219 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
221 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
222 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
223 double anAngle = aValueAttr->value();
225 /// an angle value should be corrected by the current angle type
226 aValueAttr = std::dynamic_pointer_cast<
227 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
228 anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
229 aValueAttr->setValue(anAngle);
232 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
234 std::shared_ptr<ModelAPI_Data> aData = data();
235 if (!aData->isValid())
238 myFlyoutUpdate = true;
239 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
240 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
241 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
242 myFlyoutUpdate = false;
246 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
248 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
253 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
254 GeomDataAPI_Point2D>(attribute(theAttributeId));
255 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
258 DataPtr aData = data();
259 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
260 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
261 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
263 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
266 // Start and end points of lines
267 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
268 aLineA->attribute(SketchPlugin_Line::START_ID()));
269 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270 aLineA->attribute(SketchPlugin_Line::END_ID()));
272 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
273 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
274 if (aStartA->distance(aEndA) < tolerance)
277 myFlyoutUpdate = true;
278 double aX = (aStartA->x() + aEndA->x()) / 2.;
279 double aY = (aStartA->y() + aEndA->y()) / 2.;
281 aFlyOutAttr->setValue(aX, aY);
282 myFlyoutUpdate = false;
288 // =============== Auxiliary functions ==================================
289 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
291 // Start and end points of lines
292 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
293 theLine1->attribute(SketchPlugin_Line::START_ID()));
294 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
295 theLine1->attribute(SketchPlugin_Line::END_ID()));
297 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
298 theLine2->attribute(SketchPlugin_Line::START_ID()));
299 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
300 theLine2->attribute(SketchPlugin_Line::END_ID()));
302 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
303 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
304 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
305 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
306 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
307 std::shared_ptr<GeomAPI_Pnt2d>();
309 // Lines and their intersection point
310 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
311 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
312 return aLA->intersect(aLB);