X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchSolver%2FSketchSolver_Builder.cpp;h=c01e8817a418d2951a545b288dc6c7a3e3d5881d;hb=29d446f4dd2969d80087745fe44adb5638d13de7;hp=78bfc59bb19eff477c4c13b367ec68f81425fe58;hpb=b1b810161662f4976b2ed5aafe8cfdc1e6112002;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_Builder.cpp b/src/SketchSolver/SketchSolver_Builder.cpp index 78bfc59bb..c01e8817a 100644 --- a/src/SketchSolver/SketchSolver_Builder.cpp +++ b/src/SketchSolver/SketchSolver_Builder.cpp @@ -5,66 +5,40 @@ // 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 +#ifdef _DEBUG #include -#include -#include -#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 -// 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(); @@ -106,26 +80,14 @@ SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theCons 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_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()) { @@ -133,18 +95,19 @@ 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::createMovementConstraint(FeaturePtr theFixedFeature) const { DataPtr aData = theFixedFeature->data(); if (!aData || !aData->isValid()) @@ -152,185 +115,35 @@ SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr th return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature)); } -SolverConstraintPtr SketchSolver_Builder::createParametricConstraint(AttributePtr theAttribute) +std::shared_ptr SketchSolver_Builder::point(EntityWrapperPtr theEntity) const { - return SolverConstraintPtr(new SketchSolver_ConstraintParametric(theAttribute)); + if (theEntity->type() != ENTITY_POINT) + return std::shared_ptr(); + + 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::createWorkplane( - CompositeFeaturePtr theSketch, - std::vector& theEntities, - std::vector& theParameters) +std::shared_ptr SketchSolver_Builder::line(EntityWrapperPtr theEntity) 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; + 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::createEntity( - AttributePtr theAttribute, - std::vector& theEntities, - std::vector& theParameters) -{ - 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; -} - -bool SketchSolver_Builder::createEntity( - FeaturePtr theFeature, - std::vector& theEntities, - std::vector& theParameters) -{ - 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; -} - -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; -}