X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchPlugin%2FSketchPlugin_ConstraintRadius.cpp;h=906802732176716dfe7d9d7def0879547d2078f4;hb=e466624749375b6ead3e8a8ded26bf29ea9325ae;hp=6f4d17670498c8bd11569408487bd4d75480541e;hpb=f92ef94e9723207fc5b8421f4644e281c5e69d42;p=modules%2Fshaper.git diff --git a/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp b/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp index 6f4d17670..906802732 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp @@ -1,175 +1,214 @@ -// File: SketchPlugin_ConstraintRadius.cpp -// Created: 26 May 2014 -// Author: Artem ZHIDKOV +// Copyright (C) 2014-2024 CEA, EDF +// +// 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_ConstraintRadius.h" #include #include #include +#include + +#include +#include #include +#include #include +#include +#include #include #include +#include +#include +#include +#include #include #include -#include -#include +#include + +const double tolerance = 1.e-7; SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius() { + myFlyoutUpdate = false; } void SketchPlugin_ConstraintRadius::initAttributes() { - data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type()); - data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type()); - data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type()); + data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId()); + data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId()); + + data()->addAttribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID(), + ModelAPI_AttributeInteger::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID()); +} + +void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName, + std::string& theDefault) +{ + theSection = "Visualization"; + theName = "sketch_dimension_color"; + theDefault = SKETCH_DIMENSION_COLOR; } void SketchPlugin_ConstraintRadius::execute() { - if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() && - !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) { - - boost::shared_ptr aRef = - boost::dynamic_pointer_cast(data()->attribute(CONSTRAINT_ATTR_ENTITY_A)); - FeaturePtr aFeature = aRef->feature(); - if (aFeature) { - double aRadius = 0; - boost::shared_ptr aData = aFeature->data(); - if (aFeature->getKind() == SKETCH_CIRCLE_KIND) { - AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast - (aData->attribute(CIRCLE_ATTR_RADIUS)); - if (anAttribute) - aRadius = anAttribute->value(); - } - else if (aFeature->getKind() == SKETCH_ARC_KIND) { - boost::shared_ptr aCenterAttr = - boost::dynamic_pointer_cast(aData->attribute(ARC_ATTR_CENTER)); - boost::shared_ptr aStartAttr = - boost::dynamic_pointer_cast(aData->attribute(ARC_ATTR_START)); - if (aCenterAttr && aStartAttr) - aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt()); - } - boost::shared_ptr aValueAttr = - boost::dynamic_pointer_cast(data()->attribute(CONSTRAINT_ATTR_VALUE)); - aValueAttr->setValue(aRadius); - } + std::shared_ptr aRef = std::dynamic_pointer_cast< + ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A())); + FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object()); + if (aFeature) { + // 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< + GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + if (!aFlyoutAttr->isInitialized()) + compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()); + } +} + +bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId) +{ + if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT()) + return false; + + std::shared_ptr aCyrcFeature; + double aRadius = circleRadius(aCyrcFeature); + if (aRadius < 0) + return false; + + // Flyout point + std::shared_ptr aFlyoutAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(data()->attribute(theAttributeId)); + // Prepare a circle + if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle + std::shared_ptr aCenterAttr = + std::dynamic_pointer_cast( + aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID())); + double aShift = aRadius * 1.1; + std::shared_ptr aPnt = aCenterAttr->pnt(); + std::shared_ptr aFPnt = + std::shared_ptr(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift)); + aFlyoutAttr->setValue(aFPnt); + } else { // arc + std::shared_ptr aStartAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID())); + aFlyoutAttr->setValue(aStartAttr->pnt()); } + return true; } -Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape( - Handle_AIS_InteractiveObject thePrevious) +double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr& theCirc) { - Handle(AIS_InteractiveObject) anAIS = thePrevious; + static const double kErrorResult = -1.; if (!sketch()) - return anAIS; + return kErrorResult; - boost::shared_ptr aData = data(); - boost::shared_ptr anAttr = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_A)); + std::shared_ptr aData = data(); + std::shared_ptr anAttr = std::dynamic_pointer_cast< + ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A())); if (!anAttr) - return anAIS; - FeaturePtr aFeature = anAttr->feature(); - std::string aKind = aFeature ? aFeature->getKind() : ""; - if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND) - return anAIS; - - boost::shared_ptr aValueAttr = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_VALUE)); - double aValue = aValueAttr->value(); - - // an anchor point - boost::shared_ptr aAnchorAttr = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT)); - if (!aAnchorAttr->isInitialized()) - return anAIS; - boost::shared_ptr anAnchor2D = aAnchorAttr->pnt(); - boost::shared_ptr anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y()); - - aData = aFeature->data(); - boost::shared_ptr aCenterAttr; - double aRadius; - if (aKind == SKETCH_CIRCLE_KIND) { - aCenterAttr = boost::dynamic_pointer_cast(aData->attribute(CIRCLE_ATTR_CENTER)); - AttributeDoublePtr aCircRadius = - boost::dynamic_pointer_cast(aData->attribute(CIRCLE_ATTR_RADIUS)); - aRadius = aCircRadius->value(); + return kErrorResult; + theCirc = ModelAPI_Feature::feature(anAttr->object()); + std::string aKind = theCirc ? theCirc->getKind() : ""; + if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID()) + return kErrorResult; + + DataPtr aCircData = theCirc->data(); + std::shared_ptr aCenterAttr; + if (aKind == SketchPlugin_Circle::ID()) { + AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast( + aCircData->attribute(SketchPlugin_Circle::RADIUS_ID())); + return aCircRadius->value(); + } else { + aCenterAttr = std::dynamic_pointer_cast( + aCircData->attribute(SketchPlugin_Arc::CENTER_ID())); + std::shared_ptr aStartAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID())); + return aCenterAttr->pnt()->distance(aStartAttr->pnt()); } - else if (aKind == SKETCH_ARC_KIND) { - aCenterAttr = boost::dynamic_pointer_cast(aData->attribute(ARC_ATTR_CENTER)); - boost::shared_ptr aStartAttr = - boost::dynamic_pointer_cast(aData->attribute(ARC_ATTR_START)); - aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt()); - } - - // a circle - boost::shared_ptr aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y()); - - boost::shared_ptr aNDir = - boost::dynamic_pointer_cast(sketch()->data()->attribute(SKETCH_ATTR_NORM)); - boost::shared_ptr aNormal = aNDir->dir(); - - boost::shared_ptr aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius)); - - anAnchor = aCircle->project(anAnchor); - // TODO: a bug in AIS_RadiusDimension: - // The anchor point can't be myCirc.Location() - an exception is raised. - // But we need exactly this case... - // We want to show a radius dimension starting from the circle centre and - // ending at the user-defined point. - // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all. - gp_Pnt anAPnt = anAnchor->impl(); - double aDelta = 1/1000.0; - if (anAnchor->x() != 0) - anAnchor->setX(anAnchor->x() + aDelta); - if (anAnchor->y() != 0) - anAnchor->setY(anAnchor->y() + aDelta); - if (anAnchor->z() != 0) - anAnchor->setZ(anAnchor->z() + aDelta); - - if (anAIS.IsNull()) - { - Handle(AIS_RadiusDimension) aDimAIS = - new AIS_RadiusDimension(aCircle->impl(), anAnchor->impl()); - aDimAIS->SetCustomValue(aValue); - - Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect(); - anAspect->MakeArrows3d (Standard_False); - anAspect->MakeText3d(false); - anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT); - anAspect->MakeTextShaded(false); - aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false); - aDimAIS->SetDimensionAspect (anAspect); - aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE); - - anAIS = aDimAIS; - } - else - { - // update presentation - Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS); - if (!aDimAIS.IsNull()) - { - aDimAIS->SetMeasuredGeometry(aCircle->impl(), anAnchor->impl()); - aDimAIS->SetCustomValue(aValue); - aDimAIS->Redisplay(Standard_True); - } - } - return anAIS; + return kErrorResult; } -void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY) +AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious) { - boost::shared_ptr aData = data(); - if (!aData->isValid()) - return; + if (!sketch()) + return thePrevious; - boost::shared_ptr aPoint1 = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT)); - aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY); + AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch(), + thePrevious); + if (anAIS.get() && !thePrevious.get()) + SketchPlugin_Tools::setDimensionColor(anAIS); + return anAIS; +} + +void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) { + if (theID == SketchPlugin_Constraint::ENTITY_A()) { + 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 + std::shared_ptr aCyrcFeature; + double aRadius = circleRadius(aCyrcFeature); + if (aRadius > 0) { // set as value the radius of updated reference to another circle + aValueAttr->setValue(aRadius); + } + } + } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) { + // Recalculate flyout point in local coordinates of the circle (or arc): + // coordinates are calculated according to center of the shape + std::shared_ptr aFlyoutAttr = + std::dynamic_pointer_cast( + attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + + AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( + attribute(SketchPlugin_Constraint::ENTITY_A())); + if (!aRefAttr || !aRefAttr->isObject()) + return; + FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object()); + if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() && + aFeature->getKind() != SketchPlugin_Circle::ID())) + return; + + std::string aCenterAttrName; + if (aFeature->getKind() == SketchPlugin_Circle::ID()) + aCenterAttrName = SketchPlugin_Circle::CENTER_ID(); + else if (aFeature->getKind() == SketchPlugin_Arc::ID()) + aCenterAttrName = SketchPlugin_Arc::CENTER_ID(); + std::shared_ptr aCenterAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName)); + std::shared_ptr aCenter = aCenterAttr->pnt(); + std::shared_ptr aRadius = std::dynamic_pointer_cast< + ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE())); + + std::shared_ptr aFlyoutPnt = aFlyoutAttr->pnt(); + double aDist = aFlyoutPnt->distance(aCenter); + if (aDist < tolerance) + return; + + myFlyoutUpdate = true; + std::shared_ptr aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy()); + aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y()); + myFlyoutUpdate = false; + } }