From: azv Date: Tue, 26 Dec 2017 06:46:33 +0000 (+0300) Subject: SketchPlugin refactoring X-Git-Tag: V_2.10.1~19 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=12d2b665d46564704a8d0e94e3250abcf4b19dc1;p=modules%2Fshaper.git SketchPlugin refactoring --- diff --git a/src/SketchPlugin/SketchPlugin_Fillet.cpp b/src/SketchPlugin/SketchPlugin_Fillet.cpp index 36e6714ab..ab0dedbea 100644 --- a/src/SketchPlugin/SketchPlugin_Fillet.cpp +++ b/src/SketchPlugin/SketchPlugin_Fillet.cpp @@ -75,9 +75,6 @@ static void calculateFilletCenter(FeaturePtr theFilletFeatures[2], std::shared_ptr& theTangentA, std::shared_ptr& theTangentB); -/// Get coincide edges for fillet -static std::set getCoincides(const FeaturePtr& theConstraintCoincidence); - static std::set findFeaturesToRemove(const FeaturePtr theFeature, const AttributePtr theAttribute); @@ -102,33 +99,8 @@ void SketchPlugin_Fillet::execute() // set flag here to avoid building Fillet presentation if "Redisplay" event appears myFilletCreated = true; - // Calculate Fillet parameters if does not yet - if (!myBaseFeatures[0] || !myBaseFeatures[1]) - calculateFilletParameters(); - - // Create arc feature. - FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID()); - - // Set arc attributes. - bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true); - std::dynamic_pointer_cast( - aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(), - myCenterXY->y()); - std::shared_ptr aStartPoint = - std::dynamic_pointer_cast( - aFilletArc->attribute(SketchPlugin_Arc::START_ID())); - std::shared_ptr aEndPoint = - std::dynamic_pointer_cast( - aFilletArc->attribute(SketchPlugin_Arc::END_ID())); - if(aStartPoint->isInitialized() && aEndPoint->isInitialized() - && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance - || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) { - std::dynamic_pointer_cast(aFilletArc)->setReversed(false); - } - aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y()); - aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y()); - aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked); - aFilletArc->execute(); + // create feature for fillet arc + FeaturePtr aFilletArc = createFilletArc(); // Delete features with refs to points of edges. std::shared_ptr aStartPoint1; @@ -157,37 +129,26 @@ void SketchPlugin_Fillet::execute() recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(), myBaseFeatures[aFeatInd2], myFeatAttributes[anAttrInd2]); + FeaturePtr aConstraint; + // Create coincidence features. - FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID()); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::START_ID())); - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setAttr(myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1])); - aConstraint->execute(); + aConstraint = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), + aFilletArc->attribute(SketchPlugin_Arc::START_ID()), + myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1])); ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent); - aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID()); - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::END_ID())); - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setAttr(myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2])); - aConstraint->execute(); + aConstraint = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), + aFilletArc->attribute(SketchPlugin_Arc::END_ID()), + myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2])); ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent); // Create tangent features. for (int i = 0; i < 2; i++) { - aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID()); - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setObject(aFilletArc->lastResult()); - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - bool isArc = myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID(); - aRefAttr->setObject(isArc ? myBaseFeatures[i]->lastResult() : - myBaseFeatures[i]->firstResult()); + aConstraint = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintTangent::ID(), + aFilletArc->lastResult(), + myBaseFeatures[i]->lastResult()); aConstraint->execute(); ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent); } @@ -242,11 +203,17 @@ bool SketchPlugin_Fillet::calculateFilletParameters() if (!aFilletPoint2D.get()) return false; - if (!findFeaturesContainingFilletPoint(aFilletPoint2D)) { + std::set aFilletFeatures = + SketchPlugin_Tools::findFeaturesCoincidentToPoint(aFilletPoint2D); + if (aFilletFeatures.size() != 2) { setError("Error: Selected point does not have two suitable edges for fillet."); return false; } + std::set::iterator aFIt = aFilletFeatures.begin(); + myBaseFeatures[0] = *aFIt; + myBaseFeatures[1] = *(++aFIt); + std::shared_ptr aFilletPnt2d = aFilletPoint2D->pnt(); double aRadius = calculateFilletRadius(myBaseFeatures); @@ -326,49 +293,37 @@ bool SketchPlugin_Fillet::calculateFilletParameters() return true; } -bool SketchPlugin_Fillet::findFeaturesContainingFilletPoint( - std::shared_ptr theFilletPoint) +FeaturePtr SketchPlugin_Fillet::createFilletArc() { - // Obtain constraint coincidence for the fillet point. - FeaturePtr aConstraintCoincidence; - const std::set& aRefsList = theFilletPoint->owner()->data()->refsToMe(); - for(std::set::const_iterator anIt = aRefsList.cbegin(); - anIt != aRefsList.cend(); - ++anIt) { - std::shared_ptr anAttr = (*anIt); - FeaturePtr aFeature = std::dynamic_pointer_cast(anAttr->owner()); - if(aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { - AttributeRefAttrPtr anAttrRefA = - aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A()); - AttributeRefAttrPtr anAttrRefB = - aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B()); - if(anAttrRefA.get() && !anAttrRefA->isObject()) { - AttributePtr anAttrA = anAttrRefA->attr(); - if(theFilletPoint == anAttrA) { - aConstraintCoincidence = aFeature; - break; - } - } - if(anAttrRefB.get() && !anAttrRefB->isObject()) { - AttributePtr anAttrB = anAttrRefB->attr(); - if(theFilletPoint == anAttrB) { - aConstraintCoincidence = aFeature; - break; - } - } - } - } + // Calculate Fillet parameters if does not yet + if (!myBaseFeatures[0] || !myBaseFeatures[1]) + calculateFilletParameters(); - if(!aConstraintCoincidence.get()) - return false; + // Create arc feature. + FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID()); - // Get coincide edges. - std::set anEdgeFeatures = getCoincides(aConstraintCoincidence); - std::set::iterator aLinesIt = anEdgeFeatures.begin(); - for (int i = 0; aLinesIt != anEdgeFeatures.end() && i < 2; ++aLinesIt, ++i) - myBaseFeatures[i] = *aLinesIt; + // Set arc attributes. + bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true); + std::dynamic_pointer_cast( + aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(), + myCenterXY->y()); + std::shared_ptr aStartPoint = + std::dynamic_pointer_cast( + aFilletArc->attribute(SketchPlugin_Arc::START_ID())); + std::shared_ptr aEndPoint = + std::dynamic_pointer_cast( + aFilletArc->attribute(SketchPlugin_Arc::END_ID())); + if(aStartPoint->isInitialized() && aEndPoint->isInitialized() + && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance + || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) { + std::dynamic_pointer_cast(aFilletArc)->setReversed(false); + } + aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y()); + aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y()); + aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked); + aFilletArc->execute(); - return myBaseFeatures[0] && myBaseFeatures[1]; + return aFilletArc; } // ========= Auxiliary functions ================= @@ -465,59 +420,6 @@ double calculateFilletRadius(FeaturePtr theFilletFeatures[2]) return std::min(aLengths[0], aLengths[1]) / 6.0; } -std::set getCoincides(const FeaturePtr& theConstraintCoincidence) -{ - std::set aCoincides; - - std::shared_ptr aFilletPnt = - SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence); - - SketchPlugin_Tools::findCoincidences(theConstraintCoincidence, - SketchPlugin_ConstraintCoincidence::ENTITY_A(), - aCoincides, - true); - SketchPlugin_Tools::findCoincidences(theConstraintCoincidence, - SketchPlugin_ConstraintCoincidence::ENTITY_B(), - aCoincides, - true); - - // Remove points from set of coincides. - std::set aNewSetOfCoincides; - for(std::set::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) { - std::shared_ptr aSketchEntity = - std::dynamic_pointer_cast(*anIt); - if(aSketchEntity.get() && (aSketchEntity->isCopy() || aSketchEntity->isExternal())) { - continue; - } - if((*anIt)->getKind() == SketchPlugin_Line::ID()) { - aNewSetOfCoincides.insert(*anIt); - } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) { - AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID()); - std::shared_ptr aPointCenter2D = - std::dynamic_pointer_cast(anAttrCenter); - if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) { - continue; - } - aNewSetOfCoincides.insert(*anIt); - } - } - aCoincides = aNewSetOfCoincides; - - // If we still have more than two coincides remove auxilary entities from set of coincides. - if(aCoincides.size() > 2) { - aNewSetOfCoincides.clear(); - for(std::set::iterator - anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) { - if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) { - aNewSetOfCoincides.insert(*anIt); - } - } - aCoincides = aNewSetOfCoincides; - } - - return aCoincides; -} - std::set findFeaturesToRemove(const FeaturePtr theFeature, const AttributePtr theAttribute) { std::set aFeaturesToBeRemoved; diff --git a/src/SketchPlugin/SketchPlugin_Fillet.h b/src/SketchPlugin/SketchPlugin_Fillet.h index d178933e8..dfd4c4b0c 100644 --- a/src/SketchPlugin/SketchPlugin_Fillet.h +++ b/src/SketchPlugin/SketchPlugin_Fillet.h @@ -83,6 +83,11 @@ class SketchPlugin_Fillet: public SketchPlugin_SketchEntity, public GeomAPI_IPre private: bool calculateFilletParameters(); + /// Create new feature presenting a fillet arc and initialize its parameters + FeaturePtr createFilletArc(); + + void createCoincidenceWithFilletArc(); + bool findFeaturesContainingFilletPoint(std::shared_ptr theFilletPoint); private: diff --git a/src/SketchPlugin/SketchPlugin_MacroArc.cpp b/src/SketchPlugin/SketchPlugin_MacroArc.cpp index cae5ca0f4..8016c3353 100644 --- a/src/SketchPlugin/SketchPlugin_MacroArc.cpp +++ b/src/SketchPlugin/SketchPlugin_MacroArc.cpp @@ -309,40 +309,40 @@ void SketchPlugin_MacroArc::execute() // Create constraints. std::string anArcType = string(ARC_TYPE())->value(); if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) { - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, CENTER_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, START_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::START_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, END_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::END_ID()), ObjectPtr(), false); } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) { - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, START_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::START_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, END_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::END_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, PASSED_POINT_REF_ID(), AttributePtr(), anArcFeature->lastResult(), true); } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) { // constraints for tangent arc - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, TANGENT_POINT_ID(), anArcFeature->attribute(SketchPlugin_Arc::START_ID()), ObjectPtr(), @@ -355,7 +355,7 @@ void SketchPlugin_MacroArc::execute() AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B()); aRefAttrB->setObject(anArcFeature->lastResult()); // constraint for end point - SketchPlugin_Tools::createConstraint(this, + SketchPlugin_Tools::createCoincidenceOrTangency(this, END_POINT_REF_ID(), anArcFeature->attribute(SketchPlugin_Arc::END_ID()), ObjectPtr(), diff --git a/src/SketchPlugin/SketchPlugin_MacroCircle.cpp b/src/SketchPlugin/SketchPlugin_MacroCircle.cpp index 2b6829c72..b584a004e 100644 --- a/src/SketchPlugin/SketchPlugin_MacroCircle.cpp +++ b/src/SketchPlugin/SketchPlugin_MacroCircle.cpp @@ -186,11 +186,11 @@ std::string SketchPlugin_MacroCircle::processEvent( void SketchPlugin_MacroCircle::constraintsForCircleByCenterAndPassed(FeaturePtr theCircleFeature) { // Create constraints. - SketchPlugin_Tools::createConstraint( + SketchPlugin_Tools::createCoincidenceOrTangency( this, CENTER_POINT_REF_ID(), theCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint( + SketchPlugin_Tools::createCoincidenceOrTangency( this, PASSED_POINT_REF_ID(), AttributePtr(), theCircleFeature->lastResult(), true); } @@ -203,8 +203,10 @@ void SketchPlugin_MacroCircle::constraintsForCircleByThreePoints(FeaturePtr theC // Create constraints. ResultPtr aCircleResult = theCircleFeature->lastResult(); - for (int i = 0; i < 3; ++i) - SketchPlugin_Tools::createConstraint(this, aPointRef[i], AttributePtr(), aCircleResult, true); + for (int i = 0; i < 3; ++i) { + SketchPlugin_Tools::createCoincidenceOrTangency( + this, aPointRef[i], AttributePtr(), aCircleResult, true); + } } FeaturePtr SketchPlugin_MacroCircle::createCircleFeature() diff --git a/src/SketchPlugin/SketchPlugin_MacroEllipse.cpp b/src/SketchPlugin/SketchPlugin_MacroEllipse.cpp index 37ea7aa31..ac07fa664 100644 --- a/src/SketchPlugin/SketchPlugin_MacroEllipse.cpp +++ b/src/SketchPlugin/SketchPlugin_MacroEllipse.cpp @@ -193,14 +193,14 @@ std::string SketchPlugin_MacroEllipse::processEvent( void SketchPlugin_MacroEllipse::constraintsForEllipse(FeaturePtr theEllipseFeature) { // Create constraints. - SketchPlugin_Tools::createConstraint( + SketchPlugin_Tools::createCoincidenceOrTangency( this, CENTER_POINT_REF_ID(), theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()), ObjectPtr(), false); - SketchPlugin_Tools::createConstraint( + SketchPlugin_Tools::createCoincidenceOrTangency( this, MAJOR_AXIS_POINT_REF_ID(), AttributePtr(), theEllipseFeature->lastResult(), true); - SketchPlugin_Tools::createConstraint( + SketchPlugin_Tools::createCoincidenceOrTangency( this, PASSED_POINT_REF_ID(), AttributePtr(), theEllipseFeature->lastResult(), true); } diff --git a/src/SketchPlugin/SketchPlugin_Split.cpp b/src/SketchPlugin/SketchPlugin_Split.cpp index b254d63d4..7da9ca15f 100644 --- a/src/SketchPlugin/SketchPlugin_Split.cpp +++ b/src/SketchPlugin/SketchPlugin_Split.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include @@ -64,7 +65,6 @@ //#define CREATE_CONSTRAINTS -//#define DEBUG_SPLIT #ifdef DEBUG_SPLIT #include #endif @@ -914,7 +914,8 @@ void SketchPlugin_Split::updateCoincidenceConstraintsToFeature( } if (aFeaturePointAttribute.get()) { // create new constraint and remove the current - aCoincFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aCoincFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr()); theFeaturesToDelete.insert(aCIt->first); // create new coincidences to split feature points @@ -923,7 +924,7 @@ void SketchPlugin_Split::updateCoincidenceConstraintsToFeature( for (; aSFIt != aSFLast; aSFIt++) { AttributePoint2DPtr aSFAttribute = *aSFIt; if (aCoincPnt->isEqual(aSFAttribute->pnt())) { - createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(), aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr()); } } @@ -1081,7 +1082,8 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature, aFeature->attribute(SketchPlugin_Line::END_ID()))); anNewFeature = aFeature; } - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theSplitFeature->attribute(SketchPlugin_Line::END_ID()), aFeature->attribute(SketchPlugin_Line::START_ID())); theCreatedFeatures.insert(aConstraintFeature); @@ -1110,7 +1112,8 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature, fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()), aFirstPointAttrOfSplit); theBaseFeatureModified->execute(); // to update result - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()), theSplitFeature->attribute(SketchPlugin_Line::START_ID())); theCreatedFeatures.insert(aConstraintFeature); @@ -1126,14 +1129,16 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature, #ifdef CREATE_CONSTRAINTS // additional constraints between split and base features - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(theSplitFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintParallel::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(theSplitFeature)); theCreatedFeatures.insert(aConstraintFeature); if (theAfterFeature.get()) { - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(theAfterFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintParallel::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(theAfterFeature)); theCreatedFeatures.insert(aConstraintFeature); } #endif @@ -1220,7 +1225,8 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature, aFeature->attribute(SketchPlugin_Arc::END_ID()))); anNewFeature = aFeature; } - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theSplitFeature->attribute(SketchPlugin_Arc::END_ID()), aFeature->attribute(SketchPlugin_Arc::START_ID())); theCreatedFeatures.insert(aConstraintFeature); @@ -1249,7 +1255,8 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature, fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()), aFirstPointAttrOfSplit); theBaseFeatureModified->execute(); // to update result - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()), theSplitFeature->attribute(SketchPlugin_Arc::START_ID())); theCreatedFeatures.insert(aConstraintFeature); @@ -1265,22 +1272,26 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature, // additional constraints between split and base features #ifdef CREATE_CONSTRAINTS - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(theSplitFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintEqual::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(theSplitFeature)); theCreatedFeatures.insert(aConstraintFeature); - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), - getFeatureResult(theSplitFeature), - getFeatureResult(aBaseFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintTangent::ID(), + getFeatureResult(theSplitFeature), + getFeatureResult(aBaseFeature)); theCreatedFeatures.insert(aConstraintFeature); if (theAfterFeature.get()) { - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(theAfterFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintEqual::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(theAfterFeature)); theCreatedFeatures.insert(aConstraintFeature); - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), - getFeatureResult(theSplitFeature), - getFeatureResult(theAfterFeature)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintTangent::ID(), + getFeatureResult(theSplitFeature), + getFeatureResult(theAfterFeature)); theCreatedFeatures.insert(aConstraintFeature); } #endif @@ -1344,19 +1355,22 @@ FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature, (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()))); // additional constraints between split and base features - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()), theSplitFeature->attribute(SketchPlugin_Arc::END_ID())); theCreatedFeatures.insert(aConstraintFeature); - aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(), + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintCoincidence::ID(), theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()), theSplitFeature->attribute(SketchPlugin_Arc::START_ID())); theCreatedFeatures.insert(aConstraintFeature); #ifdef CREATE_CONSTRAINTS - aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), - getFeatureResult(theSplitFeature), - getFeatureResult(theBaseFeatureModified)); + aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(), + SketchPlugin_ConstraintTangent::ID(), + getFeatureResult(theSplitFeature), + getFeatureResult(theBaseFeatureModified)); theCreatedFeatures.insert(aConstraintFeature); #endif return anNewFeature; @@ -1503,39 +1517,6 @@ FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature return aFeature; } -FeaturePtr SketchPlugin_Split::createConstraint(const std::string& theConstraintId, - const AttributePtr& theFirstAttribute, - const AttributePtr& theSecondAttribute) -{ - FeaturePtr aConstraint = sketch()->addFeature(theConstraintId); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setAttr(theFirstAttribute); - - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setAttr(theSecondAttribute); - - return aConstraint; -} - -FeaturePtr SketchPlugin_Split::createConstraintForObjects( - const std::string& theConstraintId, - const ObjectPtr& theFirstObject, - const ObjectPtr& theSecondObject) -{ - FeaturePtr aConstraint = sketch()->addFeature(theConstraintId); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setObject(theFirstObject); - - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setObject(theSecondObject); - - return aConstraint; -} - void SketchPlugin_Split::updateFeaturesAfterSplit( const std::set& theFeaturesToUpdate) { diff --git a/src/SketchPlugin/SketchPlugin_Split.h b/src/SketchPlugin/SketchPlugin_Split.h index 37f1e1659..363db3645 100644 --- a/src/SketchPlugin/SketchPlugin_Split.h +++ b/src/SketchPlugin/SketchPlugin_Split.h @@ -297,22 +297,6 @@ private: const AttributePtr& theFirstPointAttr, const AttributePtr& theSecondPointAttr); - /// Add feature coincidence constraint between given attributes - /// \param theConstraintId a constraint index - /// \param theFirstAttribute an attribute of further coincidence - /// \param theSecondAttribute an attribute of further coincidence - std::shared_ptr createConstraint(const std::string& theConstraintId, - const std::shared_ptr& theFirstAttribute, - const std::shared_ptr& theSecondAttribute); - - /// Add feature coincidence constraint between given attributes - /// \param theConstraintId a constraint index - /// \param theFirstAttribute an attribute of further coincidence - /// \param theFirstAttribute an attribute of further coincidence - std::shared_ptr createConstraintForObjects(const std::string& theConstraintId, - const std::shared_ptr& theFirstObject, - const std::shared_ptr& theSecondObject); - /// Add feature coincidence constraint between given attributes /// \param theFeaturesToUpdate a constraint index void updateFeaturesAfterSplit(const std::set& theFeaturesToUpdate); diff --git a/src/SketchPlugin/SketchPlugin_Tools.cpp b/src/SketchPlugin/SketchPlugin_Tools.cpp index c798ea4a8..7a3895a34 100644 --- a/src/SketchPlugin/SketchPlugin_Tools.cpp +++ b/src/SketchPlugin/SketchPlugin_Tools.cpp @@ -32,6 +32,10 @@ #include #include +#ifdef DEBUG_TRIM +#include +#endif + namespace SketchPlugin_Tools { void clearExpressions(AttributeDoublePtr theAttribute) @@ -252,11 +256,11 @@ void resetAttribute(SketchPlugin_Feature* theFeature, } } -void createConstraint(SketchPlugin_Feature* theFeature, - const std::string& theId, - const AttributePtr theAttr, - const ObjectPtr theObject, - const bool theIsCanBeTangent) +void createCoincidenceOrTangency(SketchPlugin_Feature* theFeature, + const std::string& theId, + const AttributePtr theAttr, + const ObjectPtr theObject, + const bool theIsCanBeTangent) { AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId); if(aRefAttr.get() && aRefAttr->isInitialized()) { @@ -313,4 +317,77 @@ void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr& theRefA thePassingPoint = std::dynamic_pointer_cast(anAttr)->pnt(); } + +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const AttributePtr& theFirstAttribute, + const AttributePtr& theSecondAttribute) +{ + FeaturePtr aConstraint = theSketch->addFeature(theConstraintId); + AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); + aRefAttr->setAttr(theFirstAttribute); + + aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); + aRefAttr->setAttr(theSecondAttribute); + +#if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT) + std::cout << " :" + << " first attribute - " << theFirstAttribute->id() + << " second attribute - " << theSecondAttribute->id() + << std::endl; +#endif + + return aConstraint; +} + +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const AttributePtr& theFirstAttribute, + const ObjectPtr& theSecondObject) +{ + FeaturePtr aConstraint = theSketch->addFeature(theConstraintId); + AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); + aRefAttr->setAttr(theFirstAttribute); + + aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); + aRefAttr->setObject(theSecondObject); + +#if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT) + std::cout << " :" + << " first attribute - " << theFirstAttribute->id() + << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind() + << std::endl; +#endif + + return aConstraint; +} + +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const ObjectPtr& theFirstObject, + const ObjectPtr& theSecondObject) +{ + FeaturePtr aConstraint = theSketch->addFeature(theConstraintId); + AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); + aRefAttr->setObject(theFirstObject); + + aRefAttr = std::dynamic_pointer_cast( + aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); + aRefAttr->setObject(theSecondObject); + +#if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT) + std::cout << " :" + << " first object - " << ModelAPI_Feature::feature(theFirstObject)->getKind() + << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind() + << std::endl; +#endif + + return aConstraint; +} + } // namespace SketchPlugin_Tools diff --git a/src/SketchPlugin/SketchPlugin_Tools.h b/src/SketchPlugin/SketchPlugin_Tools.h index 07ea391df..6653b44ff 100644 --- a/src/SketchPlugin/SketchPlugin_Tools.h +++ b/src/SketchPlugin/SketchPlugin_Tools.h @@ -29,6 +29,7 @@ #include class SketchPlugin_Feature; +class SketchPlugin_Sketch; namespace SketchPlugin_Tools { @@ -60,17 +61,47 @@ std::set findPointsCoincidentToPoint(const AttributePoint2D void resetAttribute(SketchPlugin_Feature* theFeature, const std::string& theId); +/// Create new constraint between given attributes +/// \param[in] theSketch a sketch where the constraint will be created +/// \param[in] theConstraintId a constraint identifier +/// \param[in] theFirstAttribute an attribute of further constraint +/// \param[in] theSecondAttribute an attribute of further constraint +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const AttributePtr& theFirstAttribute, + const AttributePtr& theSecondAttribute); + +/// Create new constraint between given attribute and object +/// \param[in] theSketch a sketch where the constraint will be created +/// \param[in] theConstraintId a constraint identifier +/// \param[in] theFirstAttribute an attribute of further constraint +/// \param[in] theSecondObject an attribute of further constraint +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const AttributePtr& theFirstAttribute, + const ObjectPtr& theSecondObject); + +/// Create new constraint between given objects +/// \param[in] theSketch a sketch where the constraint will be created +/// \param[in] theConstraintId a constraint identifier +/// \param[in] theFirstObject an attribute of further constraint +/// \param[in] theSecondObject an attribute of further constraint +FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch, + const std::string& theConstraintId, + const ObjectPtr& theFirstObject, + const ObjectPtr& theSecondObject); + /// Creates coincidence or tangent constraint. /// \param[in] theFeature to get selected attribute or object /// \param[in] theId ID of attribute where selection is. /// \param[in] theObject object for constraint /// \param[in] theIsCanBeTangent if true constraint can be tangent or coincidence, depending on /// the selection in the attribute with passed ID. -void createConstraint(SketchPlugin_Feature* theFeature, - const std::string& theId, - const AttributePtr theAttr, - const ObjectPtr theObject, - const bool theIsCanBeTangent); +void createCoincidenceOrTangency(SketchPlugin_Feature* theFeature, + const std::string& theId, + const AttributePtr theAttr, + const ObjectPtr theObject, + const bool theIsCanBeTangent); /// Creates passing point or tangent curve basing on the given attributes are initialized. /// \param[in] theRefAttr prefered attribute to be converted diff --git a/src/SketchPlugin/SketchPlugin_Trim.cpp b/src/SketchPlugin/SketchPlugin_Trim.cpp index 74be0dc1f..ec57b9393 100644 --- a/src/SketchPlugin/SketchPlugin_Trim.cpp +++ b/src/SketchPlugin/SketchPlugin_Trim.cpp @@ -45,7 +45,6 @@ #include #include #include -//#include #include #include #include @@ -55,6 +54,7 @@ #include #include #include +#include #include @@ -68,9 +68,6 @@ #include -//#define DEBUG_TRIM_METHODS -//#define DEBUG_TRIM - #ifdef DEBUG_TRIM #include #endif @@ -362,8 +359,8 @@ void SketchPlugin_Trim::execute() const std::list& anObjects = anInfo.second; for (std::list::const_iterator anObjectIt = anObjects.begin(); anObjectIt != anObjects.end(); anObjectIt++) { - createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute, - *anObjectIt); + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(), + aPointAttribute, *anObjectIt); } } @@ -553,7 +550,8 @@ bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribu std::shared_ptr aPoint2d = aPointAttribute->pnt(); if (aPoint2d->isEqual(aRefPnt2d)) { // create new coincidence and then remove the old one - createConstraint(SketchPlugin_ConstraintCoincidence::ID(), aRefPointAttr, aPointAttribute); + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(), + aRefPointAttr, aPointAttribute); theFeaturesToDelete.insert(aFeature); } } @@ -964,10 +962,9 @@ FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr& the (aBaseFeature->attribute(aModifiedAttribute))); // Collinear constraint for lines - createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(anNewFeature)); - + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCollinear::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(anNewFeature)); } return anNewFeature; } @@ -1049,13 +1046,13 @@ FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr& theS (aBaseFeature->attribute(aModifiedAttribute))); // equal Radius constraint for arcs - createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), - getFeatureResult(aBaseFeature), - getFeatureResult(anNewFeature)); + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintEqual::ID(), + getFeatureResult(aBaseFeature), + getFeatureResult(anNewFeature)); // coincident centers constraint - createConstraint(SketchPlugin_ConstraintCoincidence::ID(), - aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()), - anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())); + SketchPlugin_Tools::createConstraint(sketch(), SketchPlugin_ConstraintCoincidence::ID(), + aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()), + anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())); #ifdef DEBUG_TRIM std::cout << "Created arc on points:" << std::endl; @@ -1292,69 +1289,6 @@ FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature, return aFeature; } -FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId, - const AttributePtr& theFirstAttribute, - const AttributePtr& theSecondAttribute) -{ - FeaturePtr aConstraint = sketch()->addFeature(theConstraintId); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setAttr(theFirstAttribute); - - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setAttr(theSecondAttribute); - -#ifdef DEBUG_TRIM - std::cout << " :" - << "first attribute - " << theFirstAttribute->id() - << "second attribute - " << theSecondAttribute->id() - << std::endl; -#endif - - return aConstraint; -} - -FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId, - const AttributePtr& theFirstAttribute, - const ObjectPtr& theSecondObject) -{ - FeaturePtr aConstraint = sketch()->addFeature(theConstraintId); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setAttr(theFirstAttribute); - - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setObject(theSecondObject); - -#ifdef DEBUG_TRIM - std::cout << " :" - << "first attribute - " << theFirstAttribute->id() - << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind() - << std::endl; -#endif - - return aConstraint; -} - -FeaturePtr SketchPlugin_Trim::createConstraintForObjects( - const std::string& theConstraintId, - const ObjectPtr& theFirstObject, - const ObjectPtr& theSecondObject) -{ - FeaturePtr aConstraint = sketch()->addFeature(theConstraintId); - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - aRefAttr->setObject(theFirstObject); - - aRefAttr = std::dynamic_pointer_cast( - aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); - aRefAttr->setObject(theSecondObject); - - return aConstraint; -} - std::shared_ptr SketchPlugin_Trim::getFeatureResult( const std::shared_ptr& theFeature) { diff --git a/src/SketchPlugin/SketchPlugin_Trim.h b/src/SketchPlugin/SketchPlugin_Trim.h index 6ad6a58c4..40090b40e 100644 --- a/src/SketchPlugin/SketchPlugin_Trim.h +++ b/src/SketchPlugin/SketchPlugin_Trim.h @@ -255,30 +255,6 @@ private: const std::shared_ptr& theFirstPoint, const std::shared_ptr& theSecondPoint); - /// Add feature coincidence constraint between given attributes - /// \param theConstraintId a constraint index - /// \param theFirstAttribute an attribute of further coincidence - /// \param theSecondAttribute an attribute of further coincidence - std::shared_ptr createConstraint(const std::string& theConstraintId, - const std::shared_ptr& theFirstAttribute, - const std::shared_ptr& theSecondAttribute); - - /// Add feature coincidence constraint between given attributes - /// \param theConstraintId a constraint index - /// \param theFirstAttribute an attribute of further coincidence - /// \param theSecondObject an object of further coincidence - std::shared_ptr createConstraintToObject(const std::string& theConstraintId, - const std::shared_ptr& theFirstAttribute, - const std::shared_ptr& theSecondObject); - - /// Add feature coincidence constraint between given attributes - /// \param theConstraintId a constraint index - /// \param theFirstAttribute an attribute of further coincidence - /// \param theFirstAttribute an attribute of further coincidence - std::shared_ptr createConstraintForObjects(const std::string& theConstraintId, - const std::shared_ptr& theFirstObject, - const std::shared_ptr& theSecondObject); - /// Result result of the feature to build constraint with. For arc, circle it is an edge result. /// \param theFeature a feature /// \return result object