X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchPlugin%2FSketchPlugin_ConstraintAngle.cpp;h=2bfd5c34be1cdb0a4c8a782e840b44ec01fd823e;hb=bb4d0036dd0785056ab21fc237ec7fcc0870ae5f;hp=7874be39513f2c17c99082de801dbabbed27d366;hpb=22b8f7fe2eccd1eeee183f52834b191b1e904dc3;p=modules%2Fshaper.git diff --git a/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp b/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp index 7874be395..2bfd5c34b 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp @@ -1,15 +1,34 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> - -// File: SketchPlugin_ConstraintAngle.cpp -// Created: 19 August 2015 -// Author: Artem ZHIDKOV +// Copyright (C) 2014-2019 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 +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "SketchPlugin_ConstraintAngle.h" #include +#include #include +#include +#include +#include + #include +#include #include #include #include @@ -38,25 +57,51 @@ void SketchPlugin_ConstraintAngle::initAttributes() data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId()); data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId()); + + data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(), + ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), + ModelAPI_AttributeInteger::typeId()); + + data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(), + ModelAPI_AttributeBoolean::typeId()); + data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(), + ModelAPI_AttributeBoolean::typeId()); + + data()->addAttribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID(), + ModelAPI_AttributeInteger::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID()); +} + +void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName, + std::string& theDefault) +{ + theSection = "Visualization"; + theName = "sketch_dimension_color"; + theDefault = SKETCH_DIMENSION_COLOR; } void SketchPlugin_ConstraintAngle::execute() { std::shared_ptr aData = data(); - std::shared_ptr anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A()); - std::shared_ptr anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B()); + std::shared_ptr anAttrA = + aData->refattr(SketchPlugin_Constraint::ENTITY_A()); + std::shared_ptr anAttrB = + aData->refattr(SketchPlugin_Constraint::ENTITY_B()); if (!anAttrA->isInitialized() || !anAttrB->isInitialized()) return; AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast( - aData->attribute(SketchPlugin_Constraint::VALUE())); + aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID())); if (!anAttrValue->isInitialized()) { double anAngle = calculateAngle(); anAttrValue->setValue(anAngle); + updateConstraintValueByAngleValue(); } - // the value should to be computed here, not in the getAISObject in order to change the model value + // the value should to be computed here, not in the + // getAISObject in order to change the model value // inside the object transaction. This is important for creating a constraint by preselection. // The display of the presentation in this case happens after the transaction commit std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< @@ -70,15 +115,8 @@ AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious if (!sketch()) return thePrevious; - AISObjectPtr anAIS = thePrevious; - if (!anAIS) { - anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane()); - } - - // Set color from preferences - std::vector aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color", - SKETCH_DIMENSION_COLOR); - anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]); + AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch(), + thePrevious); return anAIS; } @@ -92,24 +130,32 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID) if (!aLineA || !aLineB) return; - if (theID == SketchPlugin_Constraint::ENTITY_A() || + if (theID == SketchPlugin_Constraint::ENTITY_A() || theID == SketchPlugin_Constraint::ENTITY_B()) { - std::shared_ptr aValueAttr = std::dynamic_pointer_cast< - ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE())); - if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value + AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()); + AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE()); + // only if one of attributes is not initialized, try to compute the current value + if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) { + if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized()) + // initialize base value of constraint + updateConstraintValueByAngleValue(); double anAngle = calculateAngle(); aValueAttr->setValue(anAngle); + updateConstraintValueByAngleValue(); } } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) { // Recalculate flyout point in local coordinates // coordinates are calculated according to the center of shapes intersection std::shared_ptr aFlyoutAttr = - std::dynamic_pointer_cast(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + std::dynamic_pointer_cast( + attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); std::shared_ptr aData = data(); std::shared_ptr aPlane = SketchPlugin_Sketch::plane(sketch()); - FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); - FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); + FeaturePtr aLineA = + SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); + FeaturePtr aLineB = + SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); // Intersection of lines std::shared_ptr anInter = intersect(aLineA, aLineB); @@ -122,78 +168,112 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID) aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y()); myFlyoutUpdate = false; } + else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) { + std::shared_ptr aValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID())); + double anAngle = calculateAngle(); + if (aValueAttr->text().empty()) + aValueAttr->setValue(anAngle); + else { + aValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE())); + aValueAttr->setValue(anAngle); + } + } + else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) { + updateConstraintValueByAngleValue(); + } } double SketchPlugin_ConstraintAngle::calculateAngle() { - double anAngle = 0.0; - std::shared_ptr aData = data(); std::shared_ptr aPlane = SketchPlugin_Sketch::plane(sketch()); FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); - // Intersection of lines - std::shared_ptr anInter = intersect(aLineA, aLineB); - if (!anInter) - return anAngle; - - // Start and end points of lines - std::shared_ptr aPointA1 = std::dynamic_pointer_cast( + std::shared_ptr aStartA = std::dynamic_pointer_cast( aLineA->attribute(SketchPlugin_Line::START_ID())); - std::shared_ptr aPointA2 = std::dynamic_pointer_cast( + std::shared_ptr aEndA = std::dynamic_pointer_cast( aLineA->attribute(SketchPlugin_Line::END_ID())); - - std::shared_ptr aPointB1 = std::dynamic_pointer_cast( + std::shared_ptr aStartB = std::dynamic_pointer_cast( aLineB->attribute(SketchPlugin_Line::START_ID())); - std::shared_ptr aPointB2 = std::dynamic_pointer_cast( + std::shared_ptr aEndB = std::dynamic_pointer_cast( aLineB->attribute(SketchPlugin_Line::END_ID())); - std::shared_ptr aStartA = aPointA1->pnt(); - std::shared_ptr aEndA = aPointA2->pnt(); - std::shared_ptr aStartB = aPointB1->pnt(); - std::shared_ptr aEndB = aPointB2->pnt(); - - double aDist[2][2] = { - { anInter->distance(aStartA), anInter->distance(aEndA) }, - { anInter->distance(aStartB), anInter->distance(aEndB) } - }; - - // Directions of lines - if (aDist[0][1] < tolerance) - aEndA = aStartA; - if (aDist[1][1] < tolerance) - aEndB = aStartB; - std::shared_ptr aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy()))); - std::shared_ptr aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy()))); - - anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI; - - // If the lines intersected inside one of them, the angle selected is less than 90 degrees - if ((aDist[0][0] >= tolerance && aDist[0][1] >= tolerance && - aDist[0][0] + aDist[0][1] < aStartA->distance(aEndA) + 2.0 * tolerance) || - (aDist[1][0] >= tolerance && aDist[1][1] >= tolerance && - aDist[1][0] + aDist[1][1] < aStartB->distance(aEndB) + 2.0 * tolerance)) { - if (anAngle > 90.0) - anAngle = 180.0 - anAngle; + std::shared_ptr anAng; + if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() || + !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized()) + anAng = std::shared_ptr(new GeomAPI_Angle2d( + aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt())); + else { + std::shared_ptr aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt())); + bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value(); + std::shared_ptr aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt())); + bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value(); + anAng = std::shared_ptr( + new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2)); } - + double anAngle = anAng->angleDegree(); + std::shared_ptr aValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(VALUE())); + std::shared_ptr anAngleValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID())); + if (!aValueAttr->isInitialized()) + aValueAttr->setValue(anAngle); + /// an angle value should be corrected by the current angle type + anAngle = getAngleForType(anAngleValueAttr->text().empty() ? + anAngle : anAngleValueAttr->value()); + boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0)); + boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1)); return anAngle; } -void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY) +double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse) { - std::shared_ptr aData = data(); - if (!aData->isValid()) - return; + double anAngle = theAngle; - myFlyoutUpdate = true; - std::shared_ptr aFlyoutAttr = std::dynamic_pointer_cast< - GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); - aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY); - myFlyoutUpdate = false; + std::shared_ptr aData = data(); + std::shared_ptr aTypeAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID())); + SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value()); + switch (anAngleType) { + case SketcherPrs_Tools::ANGLE_DIRECT: + anAngle = theAngle; + break; + case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: { + if (theAngle > 180 || isPreviousValueObtuse) + anAngle = theAngle - 180.0; + else + anAngle = 180.0 - theAngle; + + if (anAngle < 0.0) + anAngle += 360.0; + } + break; + case SketcherPrs_Tools::ANGLE_BACKWARD: + anAngle = 360.0 - theAngle; + break; + default: + break; + } + return anAngle; } +void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue() +{ + std::shared_ptr aValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID())); + double anAngle = aValueAttr->value(); + + /// an angle value should be corrected by the current angle type + aValueAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE())); + if (!aValueAttr->isInitialized()) + calculateAngle(); + anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0); + aValueAttr->setValue(anAngle); +} bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId) { @@ -204,7 +284,8 @@ bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId) std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< GeomDataAPI_Point2D>(attribute(theAttributeId)); - if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance) + if (aFlyOutAttr->isInitialized() && + (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)) return false; DataPtr aData = data();