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_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintHorizontal.h>
17 #include <SketchPlugin_ConstraintLength.h>
18 #include <SketchPlugin_ConstraintMiddle.h>
19 #include <SketchPlugin_ConstraintMirror.h>
20 #include <SketchPlugin_ConstraintParallel.h>
21 #include <SketchPlugin_ConstraintPerpendicular.h>
22 #include <SketchPlugin_ConstraintRadius.h>
23 #include <SketchPlugin_ConstraintRigid.h>
24 #include <SketchPlugin_ConstraintTangent.h>
25 #include <SketchPlugin_ConstraintVertical.h>
27 #include <GeomAPI_Dir2d.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_ResultConstruction.h>
35 SketchSolver_Constraint::SketchSolver_Constraint(
36 ConstraintPtr theConstraint)
37 : myBaseConstraint(theConstraint),
38 myGroupID(GID_UNKNOWN),
39 myType(CONSTRAINT_UNKNOWN)
43 void SketchSolver_Constraint::process(StoragePtr theStorage,
44 const GroupID& theGroupID,
45 const EntityID& theSketchID)
47 myStorage = theStorage;
48 myGroupID = theGroupID;
49 mySketchID = theSketchID;
50 // Process constraint according to its type
55 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
57 const std::string& aType = theConstraint->getKind();
58 if (aType == SketchPlugin_ConstraintCoincidence::ID())
59 return CONSTRAINT_COINCIDENCE;
60 else if (aType == SketchPlugin_ConstraintRigid::ID())
61 return CONSTRAINT_FIXED;
62 else if (aType == SketchPlugin_ConstraintHorizontal::ID())
63 return CONSTRAINT_HORIZONTAL;
64 else if (aType == SketchPlugin_ConstraintVertical::ID())
65 return CONSTRAINT_VERTICAL;
66 else if (aType == SketchPlugin_ConstraintAngle::ID())
67 return CONSTRAINT_ANGLE;
68 else if (aType == SketchPlugin_ConstraintDistance::ID())
69 return CONSTRAINT_DISTANCE;
70 else if (aType == SketchPlugin_ConstraintEqual::ID())
71 return CONSTRAINT_EQUAL;
72 else if (aType == SketchPlugin_ConstraintLength::ID())
73 return CONSTRAINT_PT_PT_DISTANCE;
74 else if (aType == SketchPlugin_ConstraintMirror::ID())
75 return CONSTRAINT_SYMMETRIC;
76 else if (aType == SketchPlugin_ConstraintParallel::ID())
77 return CONSTRAINT_PARALLEL;
78 else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
79 return CONSTRAINT_PERPENDICULAR;
80 else if (aType == SketchPlugin_ConstraintRadius::ID())
81 return CONSTRAINT_RADIUS;
82 else if (aType == SketchPlugin_ConstraintTangent::ID())
83 return CONSTRAINT_TANGENT;
84 else if (aType == SketchPlugin_ConstraintCollinear::ID())
85 return CONSTRAINT_COLLINEAR;
86 else if (aType == SketchPlugin_ConstraintMiddle::ID())
87 return CONSTRAINT_MIDDLE_POINT;
88 return CONSTRAINT_UNKNOWN;
91 void SketchSolver_Constraint::process()
94 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
95 // Not enough parameters are assigned
99 SketchSolver_ConstraintType aConstrType = getType();
101 std::vector<EntityWrapperPtr> anAttributes;
102 getAttributes(aValue, anAttributes);
103 if (!myErrorMsg.empty())
105 if (anAttributes.empty()) {
106 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
109 if (aConstrType == CONSTRAINT_UNKNOWN)
110 aConstrType = getType();
112 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
113 std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
114 myBaseConstraint, myGroupID, mySketchID, aConstrType,
115 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
116 myStorage->addConstraint(myBaseConstraint, aNewConstraints);
121 void SketchSolver_Constraint::update()
124 std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
125 std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
127 // Check if attributes of constraint are changed, rebuild constraint
128 std::set<AttributePtr> anAttributes;
129 std::set<AttributePtr>::iterator aFoundAttr;
130 std::set<FeaturePtr> aFeatures;
131 std::set<FeaturePtr>::iterator aFoundFeat;
132 for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
133 AttributePtr anAttr =
134 myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
138 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
140 if (aRefAttr->isObject()) {
141 FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
142 if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
143 aFeatures.insert(aFeat);
145 // Workaround for the Length constraint: add points of line, not line itself
146 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
147 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
150 anAttributes.insert(aRefAttr->attr());
152 anAttributes.insert(anAttr);
154 bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
155 for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
156 const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
157 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
158 for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
159 if ((*aSIt)->baseAttribute()) {
160 aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
161 if (aFoundAttr != anAttributes.end())
162 anAttributes.erase(aFoundAttr);
164 aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
165 if (aFoundFeat != aFeatures.end())
166 aFeatures.erase(aFoundFeat);
168 hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
177 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
178 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
180 for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
181 if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
182 (*aWIt)->setValue(aValueAttr->value());
183 myStorage->setNeedToResolve(true);
186 myStorage->addConstraint(myBaseConstraint, aWrapper);
191 bool SketchSolver_Constraint::remove()
194 myType = CONSTRAINT_UNKNOWN;
195 return myStorage->removeConstraint(myBaseConstraint);
198 void SketchSolver_Constraint::getAttributes(
200 std::vector<EntityWrapperPtr>& theAttributes)
202 static const int anInitNbOfAttr = 4;
203 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
204 myAttributes.clear();
206 DataPtr aData = myBaseConstraint->data();
207 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
209 myType = TYPE(myBaseConstraint);
211 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
212 aData->attribute(SketchPlugin_Constraint::VALUE()));
213 theValue = aValueAttr ? aValueAttr->value() : 0.0;
215 int aPtInd = 0; // index of first point in the list of attributes
216 int aEntInd = 2; // index of first entity in the list of attributes
217 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
218 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
219 for (; anIter != aConstrAttrs.end(); anIter++) {
220 AttributeRefAttrPtr aRefAttr =
221 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
222 if (!aRefAttr || !aRefAttr->isInitialized()) {
223 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
227 myStorage->update(*anIter/*, myGroupID*/);
228 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
229 myAttributes.push_back(anEntity);
231 SketchSolver_EntityType aType = anEntity->type();
232 if (aType == ENTITY_UNKNOWN)
234 else if (aType == ENTITY_POINT)
235 theAttributes[aPtInd++] = anEntity; // the point is created
236 else { // another entity (not a point) is created
237 if (aEntInd < anInitNbOfAttr)
238 theAttributes[aEntInd] = anEntity;
240 theAttributes.push_back(anEntity);
246 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
248 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
249 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
250 for (; aCIt != aCList.end(); ++aCIt)
251 if ((*aCIt)->isUsed(theFeature))
254 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
255 std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
256 for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
257 if (isUsed(*anAttrIt))
263 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
265 AttributePtr anAttribute = theAttribute;
266 AttributeRefAttrPtr aRefAttr =
267 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
269 if (aRefAttr->isObject())
270 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
272 anAttribute = aRefAttr->attr();
275 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
276 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
277 for (; aCIt != aCList.end(); ++aCIt)
278 if ((*aCIt)->isUsed(theAttribute))