-// Copyright (C) 2014-2019 CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
#include <ModelAPI_AttributeDouble.h>
#include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_EventReentrantMessage.h>
#include <ModelAPI_Session.h>
#include <ModelAPI_Validator.h>
+#include <Locale_Convert.h>
+
#include <GeomDataAPI_Point2D.h>
#include <GeomAPI_Angle2d.h>
#include <cmath>
#include <regex>
#include <sstream>
+#include <vector>
const double tolerance = 1.e-7;
#define PI 3.1415926535897932
+// To support old types of GCC (less than 4.9), check the regular expressions are working
+#if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L) && \
+ (__cplusplus >= 201402L || !defined(__GLIBCXX__) || \
+ (defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
+ defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
+ (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4)))
+#define HAVE_WORKING_REGEX 1
+#else
+#define HAVE_WORKING_REGEX 0
+#endif
+
+
/// \brief Calculate intersection point of two lines
static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
data()->addAttribute(SELECTED_SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
data()->attribute(SELECTED_SECOND_POINT_ID())->setIsArgument(false);
aValidators->registerNotObligatory(getKind(), SELECTED_SECOND_POINT_ID());
+
+ AttributeIntegerPtr aVerAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(
+ data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId()));
+ aVerAttr->setIsArgument(false);
+ ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
+ if (!aVerAttr->isInitialized()) {
+ // this is a newly created feature (not read from file),
+ // so, initialize the latest version
+ aVerAttr->setValue(THE_VERSION_1);
+ }
}
void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
return;
+ AttributeIntegerPtr aVersion = integer(VERSION_ID());
+ if (!aVersion->isInitialized() || aVersion->value() < THE_VERSION_1)
+ updateVersion();
+
AttributeDoublePtr anAttrValue = real(ANGLE_VALUE_ID());
if (!anAttrValue->isInitialized())
calculateAngle();
return anAIS;
}
+// LCOV_EXCL_START
+std::string SketchPlugin_ConstraintAngle::processEvent(
+ const std::shared_ptr<Events_Message>& theMessage)
+{
+ std::string aFilledAttributeName;
+
+ std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
+ std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
+ if (aReentrantMessage.get()) {
+ aFilledAttributeName = ENTITY_A();
+ refattr(ENTITY_A())->setObject(aReentrantMessage->selectedObject());
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SELECTED_FIRST_POINT_ID()))
+ ->setValue(aReentrantMessage->clickedPoint());
+ }
+ return aFilledAttributeName;
+}
+// LCOV_EXCL_STOP
+
void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
{
if (myFlyoutUpdate)
if (!aLineA || !aLineB)
return;
+ AttributeIntegerPtr aVersion = integer(VERSION_ID());
+ if (!aVersion->isInitialized() || aVersion->value() < THE_VERSION_1)
+ updateVersion();
+
if (theID == ENTITY_A() || theID == ENTITY_B() ||
theID == TYPE_ID() || theID == ANGLE_VALUE_ID()) {
calculateAngle();
return anAngle;
}
+#if !HAVE_WORKING_REGEX
+static bool parseString(const std::wstring& theString, std::wostringstream* theResult)
+{
+ // skip leading spaces
+ size_t aLength = theString.size();
+ size_t aPos = theString.find_first_not_of(L' ');
+ if (aPos == std::wstring::npos)
+ return false;
+ // first should be a value
+ if (theString[aPos] == L'-' || theString[aPos] == L'+')
+ theResult[1] << theString[aPos++];
+ while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
+ theResult[1] << theString[aPos++];
+ if (theString[aPos] != L' ') {
+ if (theString[aPos] != L'.')
+ return false;
+ theResult[1] << theString[aPos++];
+ while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
+ theResult[1] << theString[aPos++];
+ }
+
+ // next, find the sign
+ aPos = theString.find_first_not_of(L' ', aPos);
+ if (aPos == std::wstring::npos)
+ return false;
+ if (theString[aPos] == L'-' || theString[aPos] == L'+')
+ theResult[2] << theString[aPos++];
+
+ // a variable should be at the end
+ aPos = theString.find_first_not_of(L' ', aPos);
+ if (aPos == std::wstring::npos)
+ return false;
+ if (theString[aPos] != L'(' || theString.back() != L')')
+ return false;
+ theResult[3] << theString.substr(aPos + 1, aLength - aPos - 2);
+
+ return true;
+}
+#endif
+
// Convert angle value or a text expression from one angle type to another
-static void convertAngle(AttributeDoublePtr& theAngle,
+static void convertAngle(AttributeDoublePtr theAngle,
const int thePrevType, const int theNewType)
{
if (theAngle->isInitialized()) {
}
else {
// process the parametric value
- std::string anAngleText = theAngle->text();
- std::regex anAngleRegex("\\s*([-+]?[0-9]*\\.?[0-9]*)\\s*([-+])\\s*\\((.*)\\)",
- std::regex_constants::ECMAScript);
+ std::wstring anAngleText = theAngle->text();
+#if HAVE_WORKING_REGEX
+ std::wregex anAngleRegex(L"\\s*([-+]?[0-9]*\\.?[0-9]*)\\s*([-+])\\s*\\((.*)\\)$",
+ std::regex_constants::ECMAScript);
+#endif
double anAnglePrefix = 0.0;
- static const char aSignPrefix[2] = { '-', '+' };
+ static const wchar_t aSignPrefix[2] = { L'-', L'+' };
int aSignInd = 1;
- std::smatch aResult;
+#if HAVE_WORKING_REGEX
+ std::wsmatch aResult;
if (std::regex_search(anAngleText, aResult, anAngleRegex)) {
- anAnglePrefix = std::atof(aResult[1].str().c_str());
+#else
+ // workaround to support old versions of GCC (less than 4.9)
+ std::wostringstream aResult[4];
+ if (parseString(anAngleText, aResult)) {
+#endif
+ anAnglePrefix = std::atof(Locale::Convert::toString(aResult[1].str()).c_str());
aSignInd = aResult[2].str()[0] == aSignPrefix[0] ? 0 : 1;
anAngleText = aResult[3].str();
}
aSignInd = 1 - aSignInd;
anAnglePrefix = angleForType(anAnglePrefix, theNewType);
- std::ostringstream aText;
+ std::wostringstream aText;
bool isPrintSign = true;
if (fabs(anAnglePrefix) > tolerance)
aText << anAnglePrefix;
else
isPrintSign = aSignInd == 0;
if (isPrintSign)
- aText << " " << aSignPrefix[aSignInd] << " (";
- aText << anAngleText << (isPrintSign ? ")" : "");
+ aText << L" " << aSignPrefix[aSignInd] << L" (";
+ aText << anAngleText << (isPrintSign ? L")" : L"");
theAngle->setText(aText.str());
}
}
aPrevAngleType->setValue(anAngleType->value());
}
+static GeomPnt2dPtr lineBoundary(const FeaturePtr& theLine, const bool theReversed,
+ const GeomPnt2dPtr& thePointToAvoid)
+{
+ GeomPnt2dPtr aPoint = SketcherPrs_Tools::getPoint(theLine.get(),
+ theReversed ? SketchPlugin_Line::START_ID() : SketchPlugin_Line::END_ID());
+ if (aPoint->distance(thePointToAvoid) < tolerance) {
+ // extremity is equal to the intersection point,
+ // thus recalculate it using another boundary point
+ aPoint = SketcherPrs_Tools::getPoint(theLine.get(),
+ theReversed ? SketchPlugin_Line::END_ID() : SketchPlugin_Line::START_ID());
+ aPoint->setX(thePointToAvoid->x() * 2.0 - aPoint->x());
+ aPoint->setY(thePointToAvoid->y() * 2.0 - aPoint->y());
+ }
+ return aPoint;
+}
+
bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
{
if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
bool isSupplementary = anAngleType == (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
// point on lines to compose an angle
- GeomPnt2dPtr aPointA = SketcherPrs_Tools::getPoint(aLineA.get(),
- (isReversed1 ^ isSupplementary) ?
- SketchPlugin_Line::START_ID() : SketchPlugin_Line::END_ID());
- GeomPnt2dPtr aPointB = SketcherPrs_Tools::getPoint(aLineB.get(),
- isReversed2 ? SketchPlugin_Line::START_ID() : SketchPlugin_Line::END_ID());
+ GeomPnt2dPtr aPointA = lineBoundary(aLineA, isReversed1 ^ isSupplementary, anInter);
+ GeomPnt2dPtr aPointB = lineBoundary(aLineB, isReversed2, anInter);
myFlyoutUpdate = true;
if (aFlyOutAttr->isInitialized()) {
return true;
}
+void SketchPlugin_ConstraintAngle::updateVersion()
+{
+ bool aWasBlocked = data()->blockSendAttributeUpdated(true);
+
+ // Calculate angle value by the old algorithm and
+ // update the corresponding attribute to meet the new requirements.
+ FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(data(), ENTITY_A());
+ FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(data(), ENTITY_B());
+
+ GeomPnt2dPtr aStartA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::START_ID());
+ GeomPnt2dPtr aEndA = SketcherPrs_Tools::getPoint(aLineA.get(), SketchPlugin_Line::END_ID());
+ GeomPnt2dPtr aStartB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::START_ID());
+ GeomPnt2dPtr aEndB = SketcherPrs_Tools::getPoint(aLineB.get(), SketchPlugin_Line::END_ID());
+
+ std::shared_ptr<GeomAPI_Angle2d> anAng;
+
+ if (boolean(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() &&
+ boolean(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized()) {
+ bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
+ bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
+
+ std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA, aEndA));
+ std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB, aEndB));
+ anAng.reset(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
+ }
+ else {
+ anAng.reset(new GeomAPI_Angle2d(aStartA, aEndA, aStartB, aEndB));
+
+ bool isReversed1 = anAng->isReversed(0);
+ bool isReversed2 = anAng->isReversed(1);
+
+ boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(isReversed1);
+ boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(isReversed2);
+ }
+ double anAngleValue = anAng->angleDegree();
+ double aConstValue = real(ANGLE_VALUE_ID())->value();
+
+ AttributeIntegerPtr aType = integer(TYPE_ID());
+ switch ((SketcherPrs_Tools::AngleType)aType->value()) {
+ case SketcherPrs_Tools::ANGLE_DIRECT:
+ if (anAngleValue < 0.0 && aConstValue > 180.0)
+ convertAngle(real(ANGLE_VALUE_ID()), SketcherPrs_Tools::ANGLE_BACKWARD,
+ SketcherPrs_Tools::ANGLE_DIRECT);
+ break;
+ case SketcherPrs_Tools::ANGLE_BACKWARD:
+ if (anAngleValue < 0.0 && aConstValue < 180.0)
+ convertAngle(real(ANGLE_VALUE_ID()), SketcherPrs_Tools::ANGLE_DIRECT,
+ SketcherPrs_Tools::ANGLE_BACKWARD);
+ break;
+ default:
+ break;
+ }
+ data()->blockSendAttributeUpdated(aWasBlocked, false);
+ integer(VERSION_ID())->setValue(THE_VERSION_1);
+}
+
// =============== Auxiliary functions ==================================
std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)