- return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
- }
- return aResult;
-}
-
-SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
-{
- DataPtr aData = theFixedFeature->data();
- if (!aData || !aData->isValid())
- return SolverConstraintPtr();
- return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
-}
-
-
-
-bool SketchSolver_Builder::createWorkplane(
- CompositeFeaturePtr theSketch,
- std::vector<Slvs_Entity>& theEntities,
- std::vector<Slvs_Param>& theParameters)
-{
- DataPtr aSketchData = theSketch->data();
- if (!aSketchData || !aSketchData->isValid())
- return false; // the sketch is incorrect
-
- // Get parameters of workplane
- std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
- SketchPlugin_Sketch::DIRX_ID());
- std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
- SketchPlugin_Sketch::NORM_ID());
- std::shared_ptr<ModelAPI_Attribute> 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;
-}
-
-bool SketchSolver_Builder::createEntity(
- AttributePtr theAttribute,
- std::vector<Slvs_Entity>& theEntities,
- std::vector<Slvs_Param>& 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<GeomDataAPI_Point> aPoint =
- std::dynamic_pointer_cast<GeomDataAPI_Point>(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<GeomDataAPI_Point2D> aPoint2D =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<ModelAPI_AttributeDouble>(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<Slvs_Entity>& theEntities,
- std::vector<Slvs_Param>& theParameters)
-{
- if (!theFeature->data()->isValid())
- return false;
-
- // SketchPlugin features
- std::shared_ptr<SketchPlugin_Feature> 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));