1 #include <SketchSolver_Constraint.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4 #include <SketchSolver_Manager.h>
6 #include <SketchPlugin_Arc.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_ConstraintEqual.h>
15 #include <SketchPlugin_ConstraintHorizontal.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintMirror.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintRadius.h>
21 #include <SketchPlugin_ConstraintRigid.h>
22 #include <SketchPlugin_ConstraintTangent.h>
23 #include <SketchPlugin_ConstraintVertical.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_ResultConstruction.h>
33 SketchSolver_Constraint::SketchSolver_Constraint(
34 ConstraintPtr theConstraint)
35 : myBaseConstraint(theConstraint),
36 myGroupID(GID_UNKNOWN),
37 myType(CONSTRAINT_UNKNOWN)
41 void SketchSolver_Constraint::process(StoragePtr theStorage,
42 const GroupID& theGroupID,
43 const EntityID& theSketchID)
45 myStorage = theStorage;
46 myGroupID = theGroupID;
47 mySketchID = theSketchID;
48 // Process constraint according to its type
53 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
55 const std::string& aType = theConstraint->getKind();
56 if (aType == SketchPlugin_ConstraintCoincidence::ID())
57 return CONSTRAINT_COINCIDENCE;
58 else if (aType == SketchPlugin_ConstraintRigid::ID())
59 return CONSTRAINT_FIXED;
60 else if (aType == SketchPlugin_ConstraintHorizontal::ID())
61 return CONSTRAINT_HORIZONTAL;
62 else if (aType == SketchPlugin_ConstraintVertical::ID())
63 return CONSTRAINT_VERTICAL;
64 else if (aType == SketchPlugin_ConstraintAngle::ID())
65 return CONSTRAINT_ANGLE;
66 else if (aType == SketchPlugin_ConstraintDistance::ID())
67 return CONSTRAINT_DISTANCE;
68 else if (aType == SketchPlugin_ConstraintEqual::ID())
69 return CONSTRAINT_EQUAL;
70 else if (aType == SketchPlugin_ConstraintLength::ID())
71 return CONSTRAINT_PT_PT_DISTANCE;
72 else if (aType == SketchPlugin_ConstraintMirror::ID())
73 return CONSTRAINT_SYMMETRIC;
74 else if (aType == SketchPlugin_ConstraintParallel::ID())
75 return CONSTRAINT_PARALLEL;
76 else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
77 return CONSTRAINT_PERPENDICULAR;
78 else if (aType == SketchPlugin_ConstraintRadius::ID())
79 return CONSTRAINT_RADIUS;
80 else if (aType == SketchPlugin_ConstraintTangent::ID())
81 return CONSTRAINT_TANGENT;
82 return CONSTRAINT_UNKNOWN;
85 void SketchSolver_Constraint::process()
88 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
89 // Not enough parameters are assigned
93 SketchSolver_ConstraintType aConstrType = getType();
95 std::vector<EntityWrapperPtr> anAttributes;
96 getAttributes(aValue, anAttributes);
97 if (!myErrorMsg.empty())
99 if (anAttributes.empty()) {
100 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
103 if (aConstrType == CONSTRAINT_UNKNOWN)
104 aConstrType = getType();
106 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
107 std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
108 myBaseConstraint, myGroupID, mySketchID, aConstrType,
109 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
110 myStorage->addConstraint(myBaseConstraint, aNewConstraints);
115 void SketchSolver_Constraint::update()
119 std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
120 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
121 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
123 std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
124 for (; aWIt != aWrapper.end(); ++aWIt)
125 if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
126 (*aWIt)->setValue(aValueAttr->value());
127 myStorage->setNeedToResolve(true);
130 myStorage->addConstraint(myBaseConstraint, aWrapper);
135 bool SketchSolver_Constraint::remove()
138 return myStorage->removeConstraint(myBaseConstraint);
141 void SketchSolver_Constraint::getAttributes(
143 std::vector<EntityWrapperPtr>& theAttributes)
145 static const int anInitNbOfAttr = 4;
146 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
148 DataPtr aData = myBaseConstraint->data();
149 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
151 myType = TYPE(myBaseConstraint);
153 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
154 aData->attribute(SketchPlugin_Constraint::VALUE()));
155 theValue = aValueAttr ? aValueAttr->value() : 0.0;
157 int aPtInd = 0; // index of first point in the list of attributes
158 int aEntInd = 2; // index of first entity in the list of attributes
159 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
160 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
161 for (; anIter != aConstrAttrs.end(); anIter++) {
162 AttributeRefAttrPtr aRefAttr =
163 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
164 if (!aRefAttr || !aRefAttr->isInitialized()) {
165 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
169 myStorage->update(*anIter/*, myGroupID*/);
170 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
172 SketchSolver_EntityType aType = anEntity->type();
173 if (aType == ENTITY_UNKNOWN)
175 else if (aType == ENTITY_POINT)
176 theAttributes[aPtInd++] = anEntity; // the point is created
177 else { // another entity (not a point) is created
178 if (aEntInd < anInitNbOfAttr)
179 theAttributes[aEntInd] = anEntity;
181 theAttributes.push_back(anEntity);
187 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
189 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
190 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
191 for (; aCIt != aCList.end(); ++aCIt)
192 if ((*aCIt)->isUsed(theFeature))
195 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
196 std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
197 for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
198 if (isUsed(*anAttrIt))
204 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
206 AttributePtr anAttribute = theAttribute;
207 AttributeRefAttrPtr aRefAttr =
208 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
210 if (aRefAttr->isObject())
211 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
213 anAttribute = aRefAttr->attr();
216 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
217 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
218 for (; aCIt != aCList.end(); ++aCIt)
219 if ((*aCIt)->isUsed(theAttribute))