X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchSolver%2FSketchSolver_Constraint.cpp;h=e44e1390b8b17e078077cd03cf0297fc827c3d23;hb=4224f4dbe7ceaefe74b5d6b79a5840a9f5df2d7a;hp=67a177038088fffb546b35af925fa8483aab67e6;hpb=8dfbb935d2eac7b77029d1f090b84840ff27d612;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_Constraint.cpp b/src/SketchSolver/SketchSolver_Constraint.cpp index 67a177038..e44e1390b 100644 --- a/src/SketchSolver/SketchSolver_Constraint.cpp +++ b/src/SketchSolver/SketchSolver_Constraint.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -17,9 +18,29 @@ #include #include +#include +#include +#include #include #include +#include +#include + +/// Possible types of attributes (used to determine constraint type) +enum AttrType +{ + UNKNOWN, // Something wrong during type determination + POINT2D, + POINT3D, + LINE, + CIRCLE, + ARC +}; + +/// Calculate type of the attribute +static AttrType typeOfAttribute(boost::shared_ptr theAttribute); + SketchSolver_Constraint::SketchSolver_Constraint() @@ -50,7 +71,7 @@ const int& SketchSolver_Constraint::getType(boost::shared_ptrgetKind(); // Constraint for coincidence of two points - if (aConstraintKind.compare("SketchConstraintCoincidence") == 0) + if (aConstraintKind.compare(SketchPlugin_ConstraintCoincidence::ID()) == 0) { int anAttrPos = 0; // Verify the constraint has only two attributes and they are points @@ -58,31 +79,23 @@ const int& SketchSolver_Constraint::getType(boost::shared_ptr anAttr = - boost::dynamic_pointer_cast( - theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr]) - ); + boost::shared_ptr anAttr = + theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); if (!anAttr) continue; - // Verify the attribute is a 2D point - boost::shared_ptr aPoint2D = - boost::dynamic_pointer_cast(anAttr->attr()); - if (aPoint2D) + switch (typeOfAttribute(anAttr)) { + case POINT2D: // the attribute is a 2D point aPt2d |= (1 << indAttr); - myAttributesList[anAttrPos++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } - // Verify the attribute is a 3D point - boost::shared_ptr aPoint3D = - boost::dynamic_pointer_cast(anAttr->attr()); - if (aPoint3D) - { + myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); + break; + case POINT3D: // the attribute is a 3D point aPt3d |= (1 << indAttr); - myAttributesList[anAttrPos++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; + myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); + break; + default: + // Attribute neither 2D nor 3D point is not supported by this type of constraint + return getType(); } - // Attribute neither 2D nor 3D point is not supported by this type of constraint - return getType(); } // The constrained points should be in first and second positions, // so the expected value of aPt2d or aPt3d is 3 @@ -93,78 +106,45 @@ const int& SketchSolver_Constraint::getType(boost::shared_ptr anAttr = - boost::dynamic_pointer_cast( - theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr]) - ); - if (!anAttr) continue; - if (anAttr->isFeature() && anAttr->feature()) - { // verify posiible entities - const std::string& aKind = anAttr->feature()->getKind(); - if (aKind.compare(SKETCH_POINT_KIND) == 0) - { - myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } - else if(aKind.compare(SKETCH_LINE_KIND) == 0) - { - // entities are placed starting from CONSTRAINT_ATTR_ENTITY_C attribute - myAttributesList[2 + aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr]; + boost::shared_ptr anAttr = + theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); + switch (typeOfAttribute(anAttr)) + { + case POINT2D: + case POINT3D: + myAttributesList[aNbPoints++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); + break; + case LINE: + // entities are placed starting from SketchPlugin_Constraint::ENTITY_C() attribute + myAttributesList[2 + aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); myType = SLVS_C_PT_LINE_DISTANCE; - continue; - } - } - else - { // verify points - // Verify the attribute is a 2D point - boost::shared_ptr aPoint2D = - boost::dynamic_pointer_cast(anAttr->attr()); - if (aPoint2D) - { - myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } - // Verify the attribute is a 3D point - boost::shared_ptr aPoint3D = - boost::dynamic_pointer_cast(anAttr->attr()); - if (aPoint3D) - { - myAttributesList[aNbPoints++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } + break; } } // Verify the correctness of constraint arguments if (aNbPoints == 2 && aNbEntities ==0) myType = SLVS_C_PT_PT_DISTANCE; - else if (aNbPoints == 1 && aNbEntities == 1) + else if (aNbPoints != 1 || aNbEntities != 1) myType = SLVS_C_UNKNOWN; return getType(); } // Constraint for the given length of a line - if (aConstraintKind.compare(SKETCH_CONSTRAINT_LENGTH_KIND) == 0) + if (aConstraintKind.compare(SketchPlugin_ConstraintLength::ID()) == 0) { int aNbLines = 0; for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) { - boost::shared_ptr anAttr = - boost::dynamic_pointer_cast( - theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr]) - ); - if (!anAttr) continue; - if (anAttr->isFeature() && anAttr->feature() && - anAttr->feature()->getKind().compare(SKETCH_LINE_KIND) == 0) - { - myAttributesList[aNbLines++] = CONSTRAINT_ATTRIBUTES[indAttr]; - break; - } + boost::shared_ptr anAttr = + theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); + if (typeOfAttribute(anAttr) == LINE) + myAttributesList[aNbLines++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); } if (aNbLines == 1) myType = SLVS_C_PT_PT_DISTANCE; @@ -172,24 +152,17 @@ const int& SketchSolver_Constraint::getType(boost::shared_ptr anAttr = - boost::dynamic_pointer_cast( - theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr]) - ); - if (!anAttr || !anAttr->isFeature() || !anAttr->feature()) continue; - const std::string& aKind = anAttr->feature()->getKind(); - if (aKind.compare(SKETCH_LINE_KIND) == 0) - { - myAttributesList[aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } + boost::shared_ptr anAttr = + theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); + if (typeOfAttribute(anAttr) == LINE) + myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); } if (aNbEntities == 4) myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR; @@ -197,29 +170,64 @@ const int& SketchSolver_Constraint::getType(boost::shared_ptr anAttr = - boost::dynamic_pointer_cast( - theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr]) - ); - if (!anAttr || !anAttr->isFeature() || !anAttr->feature()) continue; - const std::string& aKind = anAttr->feature()->getKind(); - if (aKind.compare(SKETCH_CIRCLE_KIND) == 0 || aKind.compare(SKETCH_ARC_KIND) == 0) - { - myAttributesList[aNbEntities++] = CONSTRAINT_ATTRIBUTES[indAttr]; - continue; - } + boost::shared_ptr anAttr = + theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr)); + AttrType aType = typeOfAttribute(anAttr); + if (aType == CIRCLE || aType == ARC) + myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr); } if (aNbEntities == 3) myType = SLVS_C_DIAMETER; return getType(); } - /// \todo Implement other kind of constrtaints + /// \todo Implement other kind of constraints return getType(); } + + +// ================= Auxiliary functions ============================== +AttrType typeOfAttribute(boost::shared_ptr theAttribute) +{ + boost::shared_ptr anAttrRef = + boost::dynamic_pointer_cast(theAttribute); + if (!anAttrRef) return UNKNOWN; + + if (anAttrRef->isObject()) + { + ResultConstructionPtr aRC = + boost::dynamic_pointer_cast(anAttrRef->object()); + if (!aRC) return UNKNOWN; + + if (aRC->shape()->isVertex()) + return POINT3D; + else if (aRC->shape()->isEdge()) + { + boost::shared_ptr anEdge = + boost::dynamic_pointer_cast(aRC->shape()); + if (anEdge->isLine()) + return LINE; + else if (anEdge->isCircle()) + return CIRCLE; + else if (anEdge->isArc()) + return ARC; + } + } + else + { + const std::string aType = anAttrRef->attr()->attributeType(); + if (aType == GeomDataAPI_Point2D::type()) + return POINT2D; + if (aType == GeomDataAPI_Point2D::type()) + return POINT2D; + } + + return UNKNOWN; +} +