1 // Copyright (C) 2014-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_ConstraintAngle.h"
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Tools.h>
23 #include <SketcherPrs_Tools.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_EventReentrantMessage.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
31 #include <Locale_Convert.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Angle2d.h>
36 #include <GeomAPI_Dir2d.h>
37 #include <GeomAPI_Lin2d.h>
38 #include <GeomAPI_Pnt2d.h>
39 #include <GeomAPI_XY.h>
41 #include <SketcherPrs_Factory.h>
42 #include <SketcherPrs_Tools.h>
49 const double tolerance = 1.e-7;
50 #define PI 3.1415926535897932
52 // To support old types of GCC (less than 4.9), check the regular expressions are working
53 #if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L) && \
54 (__cplusplus >= 201402L || !defined(__GLIBCXX__) || \
55 (defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
56 defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
57 (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4)))
58 #define HAVE_WORKING_REGEX 1
60 #define HAVE_WORKING_REGEX 0
64 /// \brief Calculate intersection point of two lines
65 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
68 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
70 myFlyoutUpdate = false;
73 void SketchPlugin_ConstraintAngle::initAttributes()
75 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
77 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
78 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
79 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
80 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
82 data()->addAttribute(ANGLE_VALUE_ID(), ModelAPI_AttributeDouble::typeId());
83 data()->addAttribute(TYPE_ID(), ModelAPI_AttributeInteger::typeId());
85 data()->addAttribute(ANGLE_REVERSED_FIRST_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
86 data()->addAttribute(ANGLE_REVERSED_SECOND_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
88 data()->addAttribute(LOCATION_TYPE_ID(), ModelAPI_AttributeInteger::typeId());
89 aValidators->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
91 data()->addAttribute(PREV_TYPE_ID(), ModelAPI_AttributeInteger::typeId());
92 data()->attribute(PREV_TYPE_ID())->setIsArgument(false);
93 aValidators->registerNotObligatory(getKind(), PREV_TYPE_ID());
94 if (attribute(TYPE_ID())->isInitialized())
95 integer(PREV_TYPE_ID())->setValue(integer(TYPE_ID())->value());
97 data()->addAttribute(SELECTED_FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
98 data()->attribute(SELECTED_FIRST_POINT_ID())->setIsArgument(false);
99 aValidators->registerNotObligatory(getKind(), SELECTED_FIRST_POINT_ID());
101 data()->addAttribute(SELECTED_SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
102 data()->attribute(SELECTED_SECOND_POINT_ID())->setIsArgument(false);
103 aValidators->registerNotObligatory(getKind(), SELECTED_SECOND_POINT_ID());
105 AttributeIntegerPtr aVerAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(
106 data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId()));
107 aVerAttr->setIsArgument(false);
108 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
109 if (!aVerAttr->isInitialized()) {
110 // this is a newly created feature (not read from file),
111 // so, initialize the latest version
112 aVerAttr->setValue(THE_VERSION_1);
116 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
117 std::string& theDefault)
119 theSection = "Visualization";
120 theName = "sketch_dimension_color";
121 theDefault = SKETCH_DIMENSION_COLOR;
124 void SketchPlugin_ConstraintAngle::execute()
126 std::shared_ptr<ModelAPI_Data> aData = data();
128 AttributeRefAttrPtr anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
129 AttributeRefAttrPtr anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
130 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
133 AttributeIntegerPtr aVersion = integer(VERSION_ID());
134 if (!aVersion->isInitialized() || aVersion->value() < THE_VERSION_1)
137 AttributeDoublePtr anAttrValue = real(ANGLE_VALUE_ID());
138 if (!anAttrValue->isInitialized())
141 // the value should to be computed here, not in the
142 // getAISObject in order to change the model value
143 // inside the object transaction. This is important for creating a constraint by preselection.
144 // The display of the presentation in this case happens after the transaction commit
145 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
146 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
147 if(!aFlyOutAttr->isInitialized())
148 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
151 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
156 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch(),
158 if (anAIS.get() && !thePrevious.get())
159 SketchPlugin_Tools::setDimensionColor(anAIS);
164 std::string SketchPlugin_ConstraintAngle::processEvent(
165 const std::shared_ptr<Events_Message>& theMessage)
167 std::string aFilledAttributeName;
169 std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
170 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
171 if (aReentrantMessage.get()) {
172 aFilledAttributeName = ENTITY_A();
173 refattr(ENTITY_A())->setObject(aReentrantMessage->selectedObject());
174 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SELECTED_FIRST_POINT_ID()))
175 ->setValue(aReentrantMessage->clickedPoint());
177 return aFilledAttributeName;
181 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
186 std::shared_ptr<ModelAPI_Data> aData = data();
190 if (theID == TYPE_ID())
193 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_A());
194 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_B());
195 if (!aLineA || !aLineB)
198 AttributeIntegerPtr aVersion = integer(VERSION_ID());
199 if (!aVersion->isInitialized() || aVersion->value() < THE_VERSION_1)
202 if (theID == ENTITY_A() || theID == ENTITY_B() ||
203 theID == TYPE_ID() || theID == ANGLE_VALUE_ID()) {
205 } else if (theID == FLYOUT_VALUE_PNT()) {
210 void SketchPlugin_ConstraintAngle::calculateAngle()
212 // update *_REVERSED_* flags
213 calculateAnglePosition();
215 std::shared_ptr<ModelAPI_Data> aData = data();
216 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
217 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_A());
218 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_B());
220 GeomPnt2dPtr aStartA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::START_ID());
221 GeomPnt2dPtr aEndA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::END_ID());
222 GeomPnt2dPtr aStartB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::START_ID());
223 GeomPnt2dPtr aEndB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::END_ID());
225 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA, aEndA));
226 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB, aEndB));
228 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
229 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
231 AttributeDoublePtr anAngleValueAttr = real(ANGLE_VALUE_ID());
232 if (!anAngleValueAttr->isInitialized()) {
233 std::shared_ptr<GeomAPI_Angle2d> anAng(
234 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
235 anAngleValueAttr->setValue(getAngleForType(fabs(anAng->angleDegree())));
238 std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(aLine1, false, aLine2, false));
239 double anAngle = anAng->angleDegree();
241 anAngle /= fabs(anAngle);
242 anAngle *= getAngleForType(anAngleValueAttr->value(), isReversed1, isReversed2);
244 // update value of the constraint to be passed to the solver
245 real(SketchPlugin_Constraint::VALUE())->setValue(anAngle);
248 void SketchPlugin_ConstraintAngle::calculateAnglePosition()
250 if (attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() &&
251 attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
252 return; // already calculated
254 DataPtr aData = data();
255 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_A());
256 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_B());
258 GeomPnt2dPtr aStartA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::START_ID());
259 GeomPnt2dPtr aEndA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::END_ID());
260 GeomPnt2dPtr aStartB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::START_ID());
261 GeomPnt2dPtr aEndB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::END_ID());
263 bool isReversed1 = false;
264 bool isReversed2 = false;
266 GeomPnt2dPtr aSelected1 = SketcherPrs_Tools::getPoint(this, SELECTED_FIRST_POINT_ID());
267 GeomPnt2dPtr aSelected2 = SketcherPrs_Tools::getPoint(this, SELECTED_SECOND_POINT_ID());
268 if (aSelected1 && aSelected2) {
269 GeomPnt2dPtr anInterPnt = intersect(aLineA, aLineB);
272 std::shared_ptr<GeomAPI_XY> anInterXY = anInterPnt->xy();
273 isReversed1 = aSelected1->xy()->decreased(anInterXY)->dot(
274 aEndA->xy()->decreased(aStartA->xy())) < -tolerance;
275 isReversed2 = aSelected2->xy()->decreased(anInterXY)->dot(
276 aEndB->xy()->decreased(aStartB->xy())) < -tolerance;
279 // no point is selected (document opened or Python script is loaded),
280 // calculate basing on the value
281 std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(aStartA, aEndA, aStartB, aEndB));
282 isReversed1 = anAng->isReversed(0);
283 isReversed2 = anAng->isReversed(1);
286 // adjust reversed flags according to the angle type
287 AttributeIntegerPtr aTypeAttr = integer(TYPE_ID());
288 if (aTypeAttr && aTypeAttr->isInitialized() &&
289 (SketcherPrs_Tools::AngleType)(aTypeAttr->value()) == SketcherPrs_Tools::ANGLE_COMPLEMENTARY)
290 isReversed1 = !isReversed1;
292 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(isReversed1);
293 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(isReversed2);
296 // Convert angle value from the DIRECT to any given type.
297 static double angleForType(const double theAngle, const int theType)
299 double anAngle = theAngle;
300 switch ((SketcherPrs_Tools::AngleType)theType) {
301 case SketcherPrs_Tools::ANGLE_DIRECT:
304 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
305 anAngle = 180.0 - theAngle;
307 case SketcherPrs_Tools::ANGLE_BACKWARD:
308 anAngle = 360.0 - theAngle;
316 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle,
320 double anAngle = angleForType(theAngle, integer(TYPE_ID())->value());
321 if (isReversed1 != isReversed2)
322 anAngle = 180.0 - anAngle;
326 #if !HAVE_WORKING_REGEX
327 static bool parseString(const std::wstring& theString, std::wostringstream* theResult)
329 // skip leading spaces
330 size_t aLength = theString.size();
331 size_t aPos = theString.find_first_not_of(L' ');
332 if (aPos == std::wstring::npos)
334 // first should be a value
335 if (theString[aPos] == L'-' || theString[aPos] == L'+')
336 theResult[1] << theString[aPos++];
337 while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
338 theResult[1] << theString[aPos++];
339 if (theString[aPos] != L' ') {
340 if (theString[aPos] != L'.')
342 theResult[1] << theString[aPos++];
343 while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
344 theResult[1] << theString[aPos++];
347 // next, find the sign
348 aPos = theString.find_first_not_of(L' ', aPos);
349 if (aPos == std::wstring::npos)
351 if (theString[aPos] == L'-' || theString[aPos] == L'+')
352 theResult[2] << theString[aPos++];
354 // a variable should be at the end
355 aPos = theString.find_first_not_of(L' ', aPos);
356 if (aPos == std::wstring::npos)
358 if (theString[aPos] != L'(' || theString.back() != L')')
360 theResult[3] << theString.substr(aPos + 1, aLength - aPos - 2);
366 // Convert angle value or a text expression from one angle type to another
367 static void convertAngle(AttributeDoublePtr theAngle,
368 const int thePrevType, const int theNewType)
370 if (theAngle->isInitialized()) {
371 if (theAngle->text().empty()) {
372 // calculate value related to the type twice:
373 // the first time - to return to direct angle,
374 // the second time - to apply new type
375 double aValue = angleForType(theAngle->value(), thePrevType);
376 aValue = angleForType(aValue, theNewType);
377 theAngle->setValue(aValue);
380 // process the parametric value
381 std::wstring anAngleText = theAngle->text();
382 #if HAVE_WORKING_REGEX
383 std::wregex anAngleRegex(L"\\s*([-+]?[0-9]*\\.?[0-9]*)\\s*([-+])\\s*\\((.*)\\)$",
384 std::regex_constants::ECMAScript);
387 double anAnglePrefix = 0.0;
388 static const wchar_t aSignPrefix[2] = { L'-', L'+' };
391 #if HAVE_WORKING_REGEX
392 std::wsmatch aResult;
393 if (std::regex_search(anAngleText, aResult, anAngleRegex)) {
395 // workaround to support old versions of GCC (less than 4.9)
396 std::wostringstream aResult[4];
397 if (parseString(anAngleText, aResult)) {
399 anAnglePrefix = std::atof(Locale::Convert::toString(aResult[1].str()).c_str());
400 aSignInd = aResult[2].str()[0] == aSignPrefix[0] ? 0 : 1;
401 anAngleText = aResult[3].str();
404 if (thePrevType != SketcherPrs_Tools::ANGLE_DIRECT)
405 aSignInd = 1 - aSignInd;
406 anAnglePrefix = angleForType(anAnglePrefix, thePrevType);
408 if (theNewType != SketcherPrs_Tools::ANGLE_DIRECT)
409 aSignInd = 1 - aSignInd;
410 anAnglePrefix = angleForType(anAnglePrefix, theNewType);
412 std::wostringstream aText;
413 bool isPrintSign = true;
414 if (fabs(anAnglePrefix) > tolerance)
415 aText << anAnglePrefix;
417 isPrintSign = aSignInd == 0;
419 aText << L" " << aSignPrefix[aSignInd] << L" (";
420 aText << anAngleText << (isPrintSign ? L")" : L"");
421 theAngle->setText(aText.str());
426 void SketchPlugin_ConstraintAngle::updateAngleValue()
428 AttributeIntegerPtr anAngleType = integer(TYPE_ID());
429 AttributeIntegerPtr aPrevAngleType = integer(PREV_TYPE_ID());
430 convertAngle(real(ANGLE_VALUE_ID()), aPrevAngleType->value(), anAngleType->value());
431 aPrevAngleType->setValue(anAngleType->value());
434 static GeomPnt2dPtr lineBoundary(const FeaturePtr& theLine, const bool theReversed,
435 const GeomPnt2dPtr& thePointToAvoid)
437 GeomPnt2dPtr aPoint = SketcherPrs_Tools::getPoint(theLine.get(),
438 theReversed ? SketchPlugin_Line::START_ID() : SketchPlugin_Line::END_ID());
439 if (aPoint->distance(thePointToAvoid) < tolerance) {
440 // extremity is equal to the intersection point,
441 // thus recalculate it using another boundary point
442 aPoint = SketcherPrs_Tools::getPoint(theLine.get(),
443 theReversed ? SketchPlugin_Line::END_ID() : SketchPlugin_Line::START_ID());
444 aPoint->setX(thePointToAvoid->x() * 2.0 - aPoint->x());
445 aPoint->setY(thePointToAvoid->y() * 2.0 - aPoint->y());
450 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
452 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
457 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
458 GeomDataAPI_Point2D>(attribute(theAttributeId));
460 DataPtr aData = data();
461 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
462 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_A());
463 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, ENTITY_B());
465 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
468 // Intersection of lines
469 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
473 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
474 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
476 int anAngleType = integer(TYPE_ID())->value();
478 bool isSupplementary = anAngleType == (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
480 // point on lines to compose an angle
481 GeomPnt2dPtr aPointA = lineBoundary(aLineA, isReversed1 ^ isSupplementary, anInter);
482 GeomPnt2dPtr aPointB = lineBoundary(aLineB, isReversed2, anInter);
484 myFlyoutUpdate = true;
485 if (aFlyOutAttr->isInitialized()) {
486 std::shared_ptr<GeomAPI_XY> aFlyoutPoint = aFlyOutAttr->pnt()->xy();
487 std::shared_ptr<GeomAPI_XY> anInterXY = anInter->xy();
488 std::shared_ptr<GeomAPI_XY> aDirIF = aFlyoutPoint->decreased(anInterXY);
489 std::shared_ptr<GeomAPI_XY> aDirIA = aPointA->xy()->decreased(anInterXY);
490 std::shared_ptr<GeomAPI_XY> aDirIB = aPointB->xy()->decreased(anInterXY);
491 double aSign = aDirIA->cross(aDirIB);
492 aSign /= fabs(aSign);
493 if (anAngleType == (int)SketcherPrs_Tools::ANGLE_BACKWARD)
496 double cross1 = aSign * aDirIA->cross(aDirIF);
497 if (cross1 < -tolerance)
498 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(!isReversed2);
499 double cross2 = aSign * aDirIF->cross(aDirIB);
500 if (cross2 < -tolerance)
501 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(!isReversed1);
503 // the direction is reversed only once
504 if ((cross1 + tolerance) * (cross2 + tolerance) < 0.0) {
505 if (anAngleType == (int)SketcherPrs_Tools::ANGLE_BACKWARD) {
506 convertAngle(real(ANGLE_VALUE_ID()), (int)SketcherPrs_Tools::ANGLE_BACKWARD,
507 (int)SketcherPrs_Tools::ANGLE_DIRECT);
509 convertAngle(real(ANGLE_VALUE_ID()), (int)SketcherPrs_Tools::ANGLE_DIRECT,
510 (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY);
511 if (anAngleType == (int)SketcherPrs_Tools::ANGLE_BACKWARD) {
512 convertAngle(real(ANGLE_VALUE_ID()), (int)SketcherPrs_Tools::ANGLE_DIRECT,
513 (int)SketcherPrs_Tools::ANGLE_BACKWARD);
520 // default position of the presentation
521 double aX = (aPointA->x() + aPointB->x() + anInter->x()) / 3.;
522 double aY = (aPointA->y() + aPointB->y() + anInter->y()) / 3.;
523 aFlyOutAttr->setValue(aX, aY);
525 myFlyoutUpdate = false;
530 void SketchPlugin_ConstraintAngle::updateVersion()
532 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
534 // Calculate angle value by the old algorithm and
535 // update the corresponding attribute to meet the new requirements.
536 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(data(), ENTITY_A());
537 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(data(), ENTITY_B());
539 GeomPnt2dPtr aStartA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::START_ID());
540 GeomPnt2dPtr aEndA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::END_ID());
541 GeomPnt2dPtr aStartB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::START_ID());
542 GeomPnt2dPtr aEndB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::END_ID());
544 std::shared_ptr<GeomAPI_Angle2d> anAng;
546 if (boolean(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() &&
547 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized()) {
548 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
549 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
551 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA, aEndA));
552 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB, aEndB));
553 anAng.reset(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
556 anAng.reset(new GeomAPI_Angle2d(aStartA, aEndA, aStartB, aEndB));
558 bool isReversed1 = anAng->isReversed(0);
559 bool isReversed2 = anAng->isReversed(1);
561 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(isReversed1);
562 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(isReversed2);
564 double anAngleValue = anAng->angleDegree();
565 double aConstValue = real(ANGLE_VALUE_ID())->value();
567 AttributeIntegerPtr aType = integer(TYPE_ID());
568 switch ((SketcherPrs_Tools::AngleType)aType->value()) {
569 case SketcherPrs_Tools::ANGLE_DIRECT:
570 if (anAngleValue < 0.0 && aConstValue > 180.0)
571 convertAngle(real(ANGLE_VALUE_ID()), SketcherPrs_Tools::ANGLE_BACKWARD,
572 SketcherPrs_Tools::ANGLE_DIRECT);
574 case SketcherPrs_Tools::ANGLE_BACKWARD:
575 if (anAngleValue < 0.0 && aConstValue < 180.0)
576 convertAngle(real(ANGLE_VALUE_ID()), SketcherPrs_Tools::ANGLE_DIRECT,
577 SketcherPrs_Tools::ANGLE_BACKWARD);
582 data()->blockSendAttributeUpdated(aWasBlocked, false);
583 integer(VERSION_ID())->setValue(THE_VERSION_1);
587 // =============== Auxiliary functions ==================================
588 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
590 // Start and end points of lines
591 const std::string& aLineStartAttr = SketchPlugin_Line::START_ID();
592 const std::string& aLineEndAttr = SketchPlugin_Line::END_ID();
593 GeomPnt2dPtr aStartA = SketcherPrs_Tools::getPoint(theLine1.get(), aLineStartAttr);
594 GeomPnt2dPtr aEndA = SketcherPrs_Tools::getPoint(theLine1.get(), aLineEndAttr);
595 GeomPnt2dPtr aStartB = SketcherPrs_Tools::getPoint(theLine2.get(), aLineStartAttr);
596 GeomPnt2dPtr aEndB = SketcherPrs_Tools::getPoint(theLine2.get(), aLineEndAttr);
597 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
598 std::shared_ptr<GeomAPI_Pnt2d>();
600 // Lines and their intersection point
601 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
602 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
603 return aLA->intersect(aLB);