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()
118 std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
119 std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
121 // Check if attributes of constraint are changed, rebuild constraint
122 std::set<AttributePtr> anAttributes;
123 std::set<AttributePtr>::iterator aFoundAttr;
124 std::set<FeaturePtr> aFeatures;
125 std::set<FeaturePtr>::iterator aFoundFeat;
126 for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
127 AttributePtr anAttr =
128 myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
132 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
134 if (aRefAttr->isObject()) {
135 FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
136 if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
137 aFeatures.insert(aFeat);
139 // Workaround for the Length constraint: add points of line, not line itself
140 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
141 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
144 anAttributes.insert(aRefAttr->attr());
146 anAttributes.insert(anAttr);
148 bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
149 for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
150 const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
151 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
152 for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
153 if ((*aSIt)->baseAttribute()) {
154 aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
155 if (aFoundAttr != anAttributes.end())
156 anAttributes.erase(aFoundAttr);
158 aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
159 if (aFoundFeat != aFeatures.end())
160 aFeatures.erase(aFoundFeat);
162 hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
171 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
172 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
174 for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
175 if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
176 (*aWIt)->setValue(aValueAttr->value());
177 myStorage->setNeedToResolve(true);
180 myStorage->addConstraint(myBaseConstraint, aWrapper);
185 bool SketchSolver_Constraint::remove()
188 myType = CONSTRAINT_UNKNOWN;
189 return myStorage->removeConstraint(myBaseConstraint);
192 void SketchSolver_Constraint::getAttributes(
194 std::vector<EntityWrapperPtr>& theAttributes)
196 static const int anInitNbOfAttr = 4;
197 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
199 DataPtr aData = myBaseConstraint->data();
200 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
202 myType = TYPE(myBaseConstraint);
204 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
205 aData->attribute(SketchPlugin_Constraint::VALUE()));
206 theValue = aValueAttr ? aValueAttr->value() : 0.0;
208 int aPtInd = 0; // index of first point in the list of attributes
209 int aEntInd = 2; // index of first entity in the list of attributes
210 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
211 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
212 for (; anIter != aConstrAttrs.end(); anIter++) {
213 AttributeRefAttrPtr aRefAttr =
214 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
215 if (!aRefAttr || !aRefAttr->isInitialized()) {
216 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
220 myStorage->update(*anIter/*, myGroupID*/);
221 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
223 SketchSolver_EntityType aType = anEntity->type();
224 if (aType == ENTITY_UNKNOWN)
226 else if (aType == ENTITY_POINT)
227 theAttributes[aPtInd++] = anEntity; // the point is created
228 else { // another entity (not a point) is created
229 if (aEntInd < anInitNbOfAttr)
230 theAttributes[aEntInd] = anEntity;
232 theAttributes.push_back(anEntity);
238 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
240 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
241 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
242 for (; aCIt != aCList.end(); ++aCIt)
243 if ((*aCIt)->isUsed(theFeature))
246 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
247 std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
248 for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
249 if (isUsed(*anAttrIt))
255 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
257 AttributePtr anAttribute = theAttribute;
258 AttributeRefAttrPtr aRefAttr =
259 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
261 if (aRefAttr->isObject())
262 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
264 anAttribute = aRefAttr->attr();
267 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
268 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
269 for (; aCIt != aCList.end(); ++aCIt)
270 if ((*aCIt)->isUsed(theAttribute))