X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchSolver%2FSketchSolver_Builder.cpp;h=451e2141ff9f5778f1d3f0814ed51bd47586086f;hb=ca1d1d90c5b1f6cb15db1bc8b66cb8e73991ef0f;hp=8e27a4b4156d83a5191b8e2fd0cc7c1c751c1b7a;hpb=4df3d12284892e126685f42a4e3c3c7f63f3b502;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_Builder.cpp b/src/SketchSolver/SketchSolver_Builder.cpp index 8e27a4b41..451e2141f 100644 --- a/src/SketchSolver/SketchSolver_Builder.cpp +++ b/src/SketchSolver/SketchSolver_Builder.cpp @@ -5,64 +5,45 @@ // Author: Artem ZHIDKOV #include "SketchSolver_Builder.h" -#include +#include +#include #include +#include #include #include -#include +#include +#include #include +#include #include -#include #include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#ifdef _DEBUG +#include #include #include +#include +#endif -#include -#include -#include -#include +#include #include +#include #include #include -#include -#include #include +#include #include -#include -#include -#include #include #include -#include #include #include #include -// Initialization of constraint builder self pointer -SketchSolver_Builder* SketchSolver_Builder::mySelf = 0; - -SketchSolver_Builder* SketchSolver_Builder::getInstance() -{ - if (!mySelf) - mySelf = new SketchSolver_Builder(); - return mySelf; -} - -SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint) +SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint) const { SolverConstraintPtr aResult; DataPtr aData = theConstraint->data(); @@ -80,7 +61,7 @@ SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theCons ResultConstructionPtr aRC = std::dynamic_pointer_cast(aRefAttr->object()); if (!aRC) - Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this); + Events_InfoMessage("SketchSolver_Builder", SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this).send(); } continue; } @@ -92,7 +73,7 @@ SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theCons ResultConstructionPtr aRC = std::dynamic_pointer_cast(*aListIter); if (*aListIter && !aRC) - Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this); + Events_InfoMessage("SketchSolver_Builder", SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this).send(); } } } @@ -100,30 +81,22 @@ SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theCons if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint)); + } else if (theConstraint->getKind() == SketchPlugin_ConstraintCollinear::ID()) { + return SolverConstraintPtr(new SketchSolver_ConstraintCollinear(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint)); + } else if (theConstraint->getKind() == SketchPlugin_ConstraintMiddle::ID()) { + return SolverConstraintPtr(new SketchSolver_ConstraintMiddle(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint)); - } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) { - return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint)); + return SolverConstraintPtr(new SketchSolver_ConstraintFixed(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint)); } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) { @@ -131,199 +104,65 @@ SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theCons } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) { return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint)); } - return aResult; + // All other types of constraints + return SolverConstraintPtr(new SketchSolver_Constraint(theConstraint)); } -SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature) +SolverConstraintPtr SketchSolver_Builder::createFixedConstraint(FeaturePtr theFixedFeature) const { DataPtr aData = theFixedFeature->data(); if (!aData || !aData->isValid()) return SolverConstraintPtr(); - return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature)); + return SolverConstraintPtr(new SketchSolver_ConstraintFixed(theFixedFeature)); } -SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature) +SolverConstraintPtr SketchSolver_Builder::createFixedArcRadiusConstraint(FeaturePtr theArc) const { - DataPtr aData = theFixedFeature->data(); + DataPtr aData = theArc->data(); if (!aData || !aData->isValid()) return SolverConstraintPtr(); - return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature)); + return SolverConstraintPtr(new SketchSolver_ConstraintFixedArcRadius(theArc)); } - - -bool SketchSolver_Builder::createWorkplane( - CompositeFeaturePtr theSketch, - std::vector& theEntities, - std::vector& theParameters) +SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature) const { - DataPtr aSketchData = theSketch->data(); - if (!aSketchData || !aSketchData->isValid()) - return false; // the sketch is incorrect - - // Get parameters of workplane - std::shared_ptr aDirX = aSketchData->attribute( - SketchPlugin_Sketch::DIRX_ID()); - std::shared_ptr aNorm = aSketchData->attribute( - SketchPlugin_Sketch::NORM_ID()); - std::shared_ptr anOrigin = aSketchData->attribute( - SketchPlugin_Sketch::ORIGIN_ID()); - // Create SolveSpace entity corresponding to the sketch origin - if (!createEntity(anOrigin, theEntities, theParameters)) - return false; - Slvs_hEntity anOriginID = theEntities.back().h; - // Create SolveSpace entity corresponding the the sketch normal - if (!createNormal(aNorm, aDirX, theEntities, theParameters)) - return false; - Slvs_hEntity aNormalID = theEntities.back().h; - - // Create workplane - Slvs_hEntity aWorkplaneID = theEntities.back().h + 1; - Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID); - theEntities.push_back(aWorkplane); - return true; + DataPtr aData = theFixedFeature->data(); + if (!aData || !aData->isValid()) + return SolverConstraintPtr(); + return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature)); } -bool SketchSolver_Builder::createEntity( - AttributePtr theAttribute, - std::vector& theEntities, - std::vector& theParameters) +std::shared_ptr SketchSolver_Builder::point(EntityWrapperPtr theEntity) const { - Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h; - Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h; - - // Point in 3D - std::shared_ptr aPoint = - std::dynamic_pointer_cast(theAttribute); - if (aPoint) { - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x())); - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y())); - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z())); - theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN, - aParamID-2, aParamID-1, aParamID)); - return true; - } - // Point in 2D - std::shared_ptr aPoint2D = - std::dynamic_pointer_cast(theAttribute); - if (aPoint2D) { - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x())); - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y())); - theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, - aParamID-1, aParamID)); - return true; - } - // Scalar value (used for the distance entities) - AttributeDoublePtr aScalar = std::dynamic_pointer_cast(theAttribute); - if (aScalar) { - theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value())); - theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN, - SLVS_E_UNKNOWN, aParamID)); - return true; - } - // unknown attribute type - return false; + if (theEntity->type() != ENTITY_POINT) + return std::shared_ptr(); + if (theEntity->subEntities().size() == 1) // SketchPlugin_Point wrapper + return point(theEntity->subEntities().front()); + + double aXY[2]; + std::list aParams = theEntity->parameters(); + std::list::const_iterator anIt = aParams.begin(); + for (int i = 0; i < 2 && anIt != aParams.end(); ++i, ++anIt) + aXY[i] = (*anIt)->value(); + if (anIt != aParams.end()) + return std::shared_ptr(); + + return std::shared_ptr(new GeomAPI_Pnt2d(aXY[0], aXY[1])); } -bool SketchSolver_Builder::createEntity( - FeaturePtr theFeature, - std::vector& theEntities, - std::vector& theParameters) +std::shared_ptr SketchSolver_Builder::line(EntityWrapperPtr theEntity) const { - if (!theFeature->data()->isValid()) - return false; - - // SketchPlugin features - std::shared_ptr aFeature = std::dynamic_pointer_cast< - SketchPlugin_Feature>(theFeature); - if (!aFeature) - return false; - - // Verify the feature by its kind - const std::string& aFeatureKind = aFeature->getKind(); - DataPtr aData = aFeature->data(); - // Line - if (aFeatureKind == SketchPlugin_Line::ID()) { - AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID()); - AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID()); - if (!aStart->isInitialized() || !aEnd->isInitialized()) - return false; - if (!createEntity(aStart, theEntities, theParameters) || - !createEntity(aEnd, theEntities, theParameters)) - return false; - Slvs_hEntity aLineID = theEntities.back().h + 1; - theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, - aLineID-2, aLineID-1)); - } - // Circle - else if (aFeatureKind == SketchPlugin_Circle::ID()) { - AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID()); - AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID()); - if (!aCenter->isInitialized() || !aRadius->isInitialized()) - return false; - if (!createEntity(aCenter, theEntities, theParameters) || - !createEntity(aRadius, theEntities, theParameters)) - return false; - Slvs_hEntity aCircID = theEntities.back().h; - theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2, - SLVS_E_UNKNOWN, aCircID-1)); - } - // Arc - else if (aFeatureKind == SketchPlugin_Arc::ID()) { - AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID()); - AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID()); - AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID()); - if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized()) - return false; - if (!createEntity(aCenter, theEntities, theParameters) || - !createEntity(aStart, theEntities, theParameters) || - !createEntity(aEnd, theEntities, theParameters)) - return false; - Slvs_hEntity anArcID = theEntities.back().h; - theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, - SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1)); - } - // Point (it has low probability to be an attribute of constraint, so it is checked at the end) - else if (aFeatureKind == SketchPlugin_Point::ID()) { - AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID()); - if (!aPoint->isInitialized() || - !createEntity(aPoint, theEntities, theParameters)) - return false; - // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier. - // No need to add another entity. - } - return true; + if (theEntity->type() != ENTITY_LINE) + return std::shared_ptr(); + + std::shared_ptr aPoints[2]; + std::list aSubs = theEntity->subEntities(); + std::list::const_iterator anIt = aSubs.begin(); + for (int i = 0; i < 2 && anIt != aSubs.end(); ++i, ++anIt) + aPoints[i] = point(*anIt); + if (anIt != aSubs.end()) + return std::shared_ptr(); + + return std::shared_ptr(new GeomAPI_Lin2d(aPoints[0], aPoints[1])); } -bool SketchSolver_Builder::createNormal( - AttributePtr theNormal, - AttributePtr theDirX, - std::vector& theEntities, - std::vector& theParameters) -{ - std::shared_ptr aNorm = std::dynamic_pointer_cast(theNormal); - std::shared_ptr aDirX = std::dynamic_pointer_cast(theDirX); - if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) || - !aNorm->isInitialized()) - return false; - // calculate Y direction - std::shared_ptr aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir()))); - - // quaternion parameters of normal vector - double qw, qx, qy, qz; - Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw, - &qx, &qy, &qz); - double aNormCoord[4] = { qw, qx, qy, qz }; - - // Create parameters of the normal - Slvs_hParam aCurParam = theParameters.back().h; - for (int i = 0; i < 4; i++) - theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i])); - - // Create a normal - Slvs_hEntity aCurEntity = theEntities.back().h + 1; - Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN, - aCurParam-3, aCurParam-2, aCurParam-1, aCurParam); - theEntities.push_back(aNormal); - return true; -}