1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Builder.h"
8 #include <SketchSolver_Constraint.h>
9 #include <SketchSolver_ConstraintAngle.h>
10 #include <SketchSolver_ConstraintCoincidence.h>
11 #include <SketchSolver_ConstraintDistance.h>
12 #include <SketchSolver_ConstraintEqual.h>
13 #include <SketchSolver_ConstraintFixed.h>
14 #include <SketchSolver_ConstraintFixedArcRadius.h>
15 #include <SketchSolver_ConstraintLength.h>
16 #include <SketchSolver_ConstraintMirror.h>
17 #include <SketchSolver_ConstraintTangent.h>
18 #include <SketchSolver_ConstraintMultiRotation.h>
19 #include <SketchSolver_ConstraintMultiTranslation.h>
20 #include <SketchSolver_ConstraintMovement.h>
23 #include <Events_Error.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <SketchSolver_Error.h>
29 #include <SketchPlugin_ConstraintAngle.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_ConstraintDistance.h>
32 #include <SketchPlugin_ConstraintEqual.h>
33 #include <SketchPlugin_ConstraintLength.h>
34 #include <SketchPlugin_ConstraintMirror.h>
35 #include <SketchPlugin_ConstraintRigid.h>
36 #include <SketchPlugin_ConstraintTangent.h>
37 #include <SketchPlugin_MultiRotation.h>
38 #include <SketchPlugin_MultiTranslation.h>
42 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint) const
44 SolverConstraintPtr aResult;
45 DataPtr aData = theConstraint->data();
46 if (!aData || !aData->isValid())
50 // Verify attributes of constraint and generate errors
51 std::list<AttributePtr> anAttrList = aData->attributes(std::string());
52 std::list<AttributePtr>::iterator anIter = anAttrList.begin();
53 for (; anIter != anAttrList.end(); anIter++) {
54 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
56 if (aRefAttr->isObject() && aRefAttr->object()) {
57 ResultConstructionPtr aRC =
58 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
60 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
64 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
66 std::list<ObjectPtr> aList = aRefList->list();
67 std::list<ObjectPtr>::iterator aListIter = aList.begin();
68 for (; aListIter != aList.end(); aListIter++) {
69 ResultConstructionPtr aRC =
70 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
71 if (*aListIter && !aRC)
72 Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
78 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
79 return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
80 } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
81 return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
82 } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
83 return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
84 } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
85 return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
86 } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
87 return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
88 } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
89 return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
90 } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
91 return SolverConstraintPtr(new SketchSolver_ConstraintFixed(theConstraint));
92 } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
93 return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
94 } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
95 return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
96 } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) {
97 return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint));
99 // All other types of constraints
100 return SolverConstraintPtr(new SketchSolver_Constraint(theConstraint));
103 SolverConstraintPtr SketchSolver_Builder::createFixedConstraint(FeaturePtr theFixedFeature) const
105 DataPtr aData = theFixedFeature->data();
106 if (!aData || !aData->isValid())
107 return SolverConstraintPtr();
108 return SolverConstraintPtr(new SketchSolver_ConstraintFixed(theFixedFeature));
111 SolverConstraintPtr SketchSolver_Builder::createFixedArcRadiusConstraint(FeaturePtr theArc) const
113 DataPtr aData = theArc->data();
114 if (!aData || !aData->isValid())
115 return SolverConstraintPtr();
116 return SolverConstraintPtr(new SketchSolver_ConstraintFixedArcRadius(theArc));
119 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature) const
121 DataPtr aData = theFixedFeature->data();
122 if (!aData || !aData->isValid())
123 return SolverConstraintPtr();
124 return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
127 std::shared_ptr<GeomAPI_Pnt2d> SketchSolver_Builder::point(EntityWrapperPtr theEntity) const
129 if (theEntity->type() != ENTITY_POINT)
130 return std::shared_ptr<GeomAPI_Pnt2d>();
131 if (theEntity->subEntities().size() == 1) // SketchPlugin_Point wrapper
132 return point(theEntity->subEntities().front());
135 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
136 std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
137 for (int i = 0; i < 2 && anIt != aParams.end(); ++i, ++anIt)
138 aXY[i] = (*anIt)->value();
139 if (anIt != aParams.end())
140 return std::shared_ptr<GeomAPI_Pnt2d>();
142 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aXY[0], aXY[1]));
145 std::shared_ptr<GeomAPI_Lin2d> SketchSolver_Builder::line(EntityWrapperPtr theEntity) const
147 if (theEntity->type() != ENTITY_LINE)
148 return std::shared_ptr<GeomAPI_Lin2d>();
150 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
151 std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
152 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
153 for (int i = 0; i < 2 && anIt != aSubs.end(); ++i, ++anIt)
154 aPoints[i] = point(*anIt);
155 if (anIt != aSubs.end())
156 return std::shared_ptr<GeomAPI_Lin2d>();
158 return std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0], aPoints[1]));