From: azv Date: Wed, 29 Mar 2017 15:37:09 +0000 (+0300) Subject: Improve behavior of MacroCircle X-Git-Tag: V_2.7.0~141 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=aa1a5a776cd37e08a96806686f8f25aec477f196;p=modules%2Fshaper.git Improve behavior of MacroCircle --- diff --git a/src/SketchPlugin/SketchPlugin_MacroCircle.cpp b/src/SketchPlugin/SketchPlugin_MacroCircle.cpp index 2af73d693..da9625e23 100644 --- a/src/SketchPlugin/SketchPlugin_MacroCircle.cpp +++ b/src/SketchPlugin/SketchPlugin_MacroCircle.cpp @@ -46,7 +46,8 @@ namespace { SketchPlugin_MacroCircle::SketchPlugin_MacroCircle() -: SketchPlugin_SketchEntity() +: SketchPlugin_SketchEntity(), + myRadius(0.0) { } @@ -78,69 +79,58 @@ void SketchPlugin_MacroCircle::initAttributes() void SketchPlugin_MacroCircle::execute() { + FeaturePtr aCircle = createCircleFeature(); + std::string aType = string(CIRCLE_TYPE())->value(); if (aType == CIRCLE_TYPE_BY_CENTER_AND_PASSED_POINTS()) - createCircleByCenterAndPassed(); + constraintsForCircleByCenterAndPassed(aCircle); else if (aType == CIRCLE_TYPE_BY_THREE_POINTS()) - createCircleByThreePoints(); + constraintsForCircleByThreePoints(aCircle); } -void SketchPlugin_MacroCircle::createCircleByCenterAndPassed() +void SketchPlugin_MacroCircle::constraintsForCircleByCenterAndPassed(FeaturePtr theCircleFeature) { - // Create circle feature. - std::shared_ptr aCircle = shapeByCenterAndPassed(); - FeaturePtr aCircleFeature = createCircleFeature(aCircle); - // Create constraints. - SketchPlugin_Tools::createConstraint(this, - CENTER_POINT_REF_ID(), - aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()), - ObjectPtr(), - false); - SketchPlugin_Tools::createConstraint(this, - PASSED_POINT_REF_ID(), - AttributePtr(), - aCircleFeature->lastResult(), - true); + SketchPlugin_Tools::createConstraint( + this, CENTER_POINT_REF_ID(), + theCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()), + ObjectPtr(), false); + SketchPlugin_Tools::createConstraint( + this, PASSED_POINT_REF_ID(), AttributePtr(), + theCircleFeature->lastResult(), true); } -void SketchPlugin_MacroCircle::createCircleByThreePoints() +void SketchPlugin_MacroCircle::constraintsForCircleByThreePoints(FeaturePtr theCircleFeature) { std::string aPointRef[3] = { FIRST_POINT_REF_ID(), SECOND_POINT_REF_ID(), THIRD_POINT_REF_ID() }; - // Create circle feature. - std::shared_ptr aCircle = shapeByThreePoints(); - FeaturePtr aCircleFeature = createCircleFeature(aCircle); - ResultPtr aCircleResult = aCircleFeature->lastResult(); - // Create constraints. + ResultPtr aCircleResult = theCircleFeature->lastResult(); for (int i = 0; i < 3; ++i) SketchPlugin_Tools::createConstraint(this, aPointRef[i], AttributePtr(), aCircleResult, true); } -FeaturePtr SketchPlugin_MacroCircle::createCircleFeature( - const std::shared_ptr& theCircle) +FeaturePtr SketchPlugin_MacroCircle::createCircleFeature() { FeaturePtr aCircleFeature = sketch()->addFeature(SketchPlugin_Circle::ID()); - std::shared_ptr aCenter = theCircle->center(); std::dynamic_pointer_cast( - aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->setValue(aCenter->x(), - aCenter->y()); - aCircleFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(theCircle->radius()); + aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->setValue(myCenter->x(), + myCenter->y()); + aCircleFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(myRadius); aCircleFeature->boolean(SketchPlugin_Circle::AUXILIARY_ID()) ->setValue(boolean(AUXILIARY_ID())->value()); aCircleFeature->execute(); return aCircleFeature; } -std::shared_ptr SketchPlugin_MacroCircle::shapeByCenterAndPassed() +void SketchPlugin_MacroCircle::fillByCenterAndPassed() { AttributePtr aCenterAttr = attribute(CENTER_POINT_ID()); AttributePtr aPassedAttr = attribute(PASSED_POINT_ID()); if (!aCenterAttr->isInitialized() || !aPassedAttr->isInitialized()) - return std::shared_ptr(); + return; AttributeRefAttrPtr aPassedRef = refattr(PASSED_POINT_REF_ID()); // Calculate circle parameters @@ -158,10 +148,13 @@ std::shared_ptr SketchPlugin_MacroCircle::shapeByCenterAndPassed aCircle = std::shared_ptr(new GeomAPI_Circ2d(aCenter, aTangentCurve, anAxis)); } else aCircle = std::shared_ptr(new GeomAPI_Circ2d(aCenter, aPassedPoint)); - return aCircle; + if (aCircle->implPtr()) { + myCenter = aCircle->center(); + myRadius = aCircle->radius(); + } } -std::shared_ptr SketchPlugin_MacroCircle::shapeByThreePoints() +void SketchPlugin_MacroCircle::fillByThreePoints() { std::string aPointAttr[3] = { FIRST_POINT_ID(), SECOND_POINT_ID(), @@ -191,12 +184,13 @@ std::shared_ptr SketchPlugin_MacroCircle::shapeByThreePoints() std::shared_ptr anAxis = SketchPlugin_Sketch::plane(sketch()); std::shared_ptr aCircle = std::shared_ptr( new GeomAPI_Circ2d(aPassedEntities[0], aPassedEntities[1], aPassedEntities[2], anAxis)); - if (!aCircle->implPtr()) - return std::shared_ptr(); - return aCircle; + if (aCircle->implPtr()) { + myCenter = aCircle->center(); + myRadius = aCircle->radius(); + } } -std::shared_ptr SketchPlugin_MacroCircle::shapeByTwoPassedPoints() +void SketchPlugin_MacroCircle::fillByTwoPassedPoints() { std::string aPointAttr[2] = { FIRST_POINT_ID(), SECOND_POINT_ID() }; @@ -228,7 +222,7 @@ std::shared_ptr SketchPlugin_MacroCircle::shapeByTwoPassedPoints } } if (aPntIndex <= 1) - return std::shared_ptr(); + return; std::shared_ptr aCircle; if (aPassedEntities[2]) { @@ -242,45 +236,26 @@ std::shared_ptr SketchPlugin_MacroCircle::shapeByTwoPassedPoints (aPassedPoints[0]->y() + aPassedPoints[1]->y()) * 0.5)); aCircle = std::shared_ptr(new GeomAPI_Circ2d(aCenter, aPassedPoints[0])); } - if (!aCircle->implPtr()) - return std::shared_ptr(); - return aCircle; + if (aCircle->implPtr()) { + myCenter = aCircle->center(); + myRadius = aCircle->radius(); + } } AISObjectPtr SketchPlugin_MacroCircle::getAISObject(AISObjectPtr thePrevious) { SketchPlugin_Sketch* aSketch = sketch(); - if(!aSketch) { + if(!aSketch || !myCenter || myRadius == 0) { return AISObjectPtr(); } - // Create circle on the sketch plane - std::shared_ptr aCircleOnSketch; - std::string aType = string(CIRCLE_TYPE())->value(); - if (aType == CIRCLE_TYPE_BY_CENTER_AND_PASSED_POINTS()) - aCircleOnSketch = shapeByCenterAndPassed(); - else if (aType == CIRCLE_TYPE_BY_THREE_POINTS()) { - if (attribute(THIRD_POINT_ID())->isInitialized()) - aCircleOnSketch = shapeByThreePoints(); - else - aCircleOnSketch = shapeByTwoPassedPoints(); - } - - if (!aCircleOnSketch) - return AISObjectPtr(); - - std::shared_ptr aCenter2D = aCircleOnSketch->center(); - if(!aCenter2D.get()) - return AISObjectPtr(); - double aRadius = aCircleOnSketch->radius(); - // Compute a circle in 3D view. - std::shared_ptr aCenter(aSketch->to3D(aCenter2D->x(), aCenter2D->y())); + std::shared_ptr aCenter(aSketch->to3D(myCenter->x(), myCenter->y())); std::shared_ptr aNDir = std::dynamic_pointer_cast( aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID())); std::shared_ptr aNormal = aNDir->dir(); - GeomShapePtr aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius); + GeomShapePtr aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, myRadius); GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter); if(!aCircleShape.get() || !aCenterPointShape.get()) { return AISObjectPtr(); @@ -313,20 +288,12 @@ void SketchPlugin_MacroCircle::attributeChanged(const std::string& theID) { SketchPlugin_Tools::resetAttribute(this, SECOND_POINT_REF_ID()); SketchPlugin_Tools::resetAttribute(this, THIRD_POINT_ID()); SketchPlugin_Tools::resetAttribute(this, THIRD_POINT_REF_ID()); - } else if(theID == CENTER_POINT_ID() || theID == PASSED_POINT_ID()) { - std::shared_ptr aCenterPointAttr = - std::dynamic_pointer_cast(attribute(CENTER_POINT_ID())); - if(!aCenterPointAttr->isInitialized()) { - return; - } - std::shared_ptr aPassedPointAttr = - std::dynamic_pointer_cast(attribute(PASSED_POINT_ID())); - if(!aPassedPointAttr->isInitialized()) { - return; - } - - aRadius = aCenterPointAttr->pnt()->distance(aPassedPointAttr->pnt()); - } else if(theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) { + } else if(theID == CENTER_POINT_ID() || theID == PASSED_POINT_ID() || + theID == CENTER_POINT_REF_ID() || theID == PASSED_POINT_REF_ID()) + fillByCenterAndPassed(); + else if(theID == FIRST_POINT_ID() || theID == FIRST_POINT_REF_ID() || + theID == SECOND_POINT_ID() || theID == SECOND_POINT_REF_ID() || + theID == THIRD_POINT_ID() || theID == THIRD_POINT_REF_ID()) { std::shared_ptr aPoints[3]; int aNbInitialized = 0; for(int i = 1; i <= 3; ++i) { @@ -336,19 +303,16 @@ void SketchPlugin_MacroCircle::attributeChanged(const std::string& theID) { aPoints[aNbInitialized++] = aCurPnt->pnt(); } - std::shared_ptr aCircle; if(aNbInitialized == 1) return; else if(aNbInitialized == 2) - aCircle = shapeByTwoPassedPoints(); + fillByTwoPassedPoints(); else - aCircle = shapeByThreePoints(); - if (aCircle) - aRadius = aCircle->radius(); + fillByThreePoints(); } AttributeDoublePtr aRadiusAttr = real(CIRCLE_RADIUS_ID()); bool aWasBlocked = data()->blockSendAttributeUpdated(true); - aRadiusAttr->setValue(aRadius); + aRadiusAttr->setValue(myRadius); data()->blockSendAttributeUpdated(aWasBlocked, false); } diff --git a/src/SketchPlugin/SketchPlugin_MacroCircle.h b/src/SketchPlugin/SketchPlugin_MacroCircle.h index 27130a4e7..6bf46f36c 100644 --- a/src/SketchPlugin/SketchPlugin_MacroCircle.h +++ b/src/SketchPlugin/SketchPlugin_MacroCircle.h @@ -163,15 +163,19 @@ class SketchPlugin_MacroCircle: public SketchPlugin_SketchEntity, SketchPlugin_MacroCircle(); private: - std::shared_ptr shapeByCenterAndPassed(); - std::shared_ptr shapeByThreePoints(); - /// Creates shape if only two of three points is initialized - std::shared_ptr shapeByTwoPassedPoints(); + void fillByCenterAndPassed(); + void fillByThreePoints(); + /// set fields if only two of three points is initialized + void fillByTwoPassedPoints(); - void createCircleByCenterAndPassed(); - void createCircleByThreePoints(); + void constraintsForCircleByCenterAndPassed(FeaturePtr theCircleFeature); + void constraintsForCircleByThreePoints(FeaturePtr theCircleFeature); - FeaturePtr createCircleFeature(const std::shared_ptr& theCircle); + FeaturePtr createCircleFeature(); + +private: + std::shared_ptr myCenter; + double myRadius; }; #endif