1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_Constraint.h>
4 #include <SketchSolver_Group.h>
5 #include <SketchSolver_Error.h>
6 #include <SketchSolver_Manager.h>
8 #include <SketchPlugin_Arc.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Point.h>
13 #include <SketchPlugin_ConstraintAngle.h>
14 #include <SketchPlugin_ConstraintCoincidence.h>
15 #include <SketchPlugin_ConstraintCollinear.h>
16 #include <SketchPlugin_ConstraintDistance.h>
17 #include <SketchPlugin_ConstraintEqual.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintMirror.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintVertical.h>
29 #include <GeomAPI_Dir2d.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
37 SketchSolver_Constraint::SketchSolver_Constraint(
38 ConstraintPtr theConstraint)
39 : myBaseConstraint(theConstraint),
40 myGroupID(GID_UNKNOWN),
41 myType(CONSTRAINT_UNKNOWN)
45 void SketchSolver_Constraint::process(StoragePtr theStorage,
46 const GroupID& theGroupID,
47 const EntityID& theSketchID)
49 myStorage = theStorage;
50 myGroupID = theGroupID;
51 mySketchID = theSketchID;
52 // Process constraint according to its type
57 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
59 const std::string& aType = theConstraint->getKind();
60 if (aType == SketchPlugin_ConstraintCoincidence::ID())
61 return CONSTRAINT_COINCIDENCE;
62 else if (aType == SketchPlugin_ConstraintRigid::ID())
63 return CONSTRAINT_FIXED;
64 else if (aType == SketchPlugin_ConstraintHorizontal::ID())
65 return CONSTRAINT_HORIZONTAL;
66 else if (aType == SketchPlugin_ConstraintVertical::ID())
67 return CONSTRAINT_VERTICAL;
68 else if (aType == SketchPlugin_ConstraintAngle::ID())
69 return CONSTRAINT_ANGLE;
70 else if (aType == SketchPlugin_ConstraintDistance::ID())
71 return CONSTRAINT_DISTANCE;
72 else if (aType == SketchPlugin_ConstraintEqual::ID())
73 return CONSTRAINT_EQUAL;
74 else if (aType == SketchPlugin_ConstraintLength::ID())
75 return CONSTRAINT_PT_PT_DISTANCE;
76 else if (aType == SketchPlugin_ConstraintMirror::ID())
77 return CONSTRAINT_SYMMETRIC;
78 else if (aType == SketchPlugin_ConstraintParallel::ID())
79 return CONSTRAINT_PARALLEL;
80 else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
81 return CONSTRAINT_PERPENDICULAR;
82 else if (aType == SketchPlugin_ConstraintRadius::ID())
83 return CONSTRAINT_RADIUS;
84 else if (aType == SketchPlugin_ConstraintTangent::ID())
85 return CONSTRAINT_TANGENT;
86 else if (aType == SketchPlugin_ConstraintCollinear::ID())
87 return CONSTRAINT_COLLINEAR;
88 else if (aType == SketchPlugin_ConstraintMiddle::ID())
89 return CONSTRAINT_MIDDLE_POINT;
90 return CONSTRAINT_UNKNOWN;
93 void SketchSolver_Constraint::process()
96 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
97 // Not enough parameters are assigned
101 SketchSolver_ConstraintType aConstrType = getType();
103 std::vector<EntityWrapperPtr> anAttributes;
104 getAttributes(aValue, anAttributes);
105 if (!myErrorMsg.empty())
107 if (anAttributes.empty()) {
108 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
111 if (aConstrType == CONSTRAINT_UNKNOWN)
112 aConstrType = getType();
114 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
115 std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
116 myBaseConstraint, myGroupID, mySketchID, aConstrType,
117 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
118 myStorage->addConstraint(myBaseConstraint, aNewConstraints);
123 void SketchSolver_Constraint::update()
126 std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
127 std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
129 // Check if attributes of constraint are changed, rebuild constraint
130 std::set<AttributePtr> anAttributes;
131 std::set<AttributePtr>::iterator aFoundAttr;
132 std::set<FeaturePtr> aFeatures;
133 std::set<FeaturePtr>::iterator aFoundFeat;
134 for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
135 AttributePtr anAttr =
136 myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
140 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
142 if (aRefAttr->isObject()) {
143 FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
144 if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
145 aFeatures.insert(aFeat);
147 // Workaround for the Length constraint: add points of line, not line itself
148 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
149 anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
152 anAttributes.insert(aRefAttr->attr());
154 anAttributes.insert(anAttr);
156 bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
157 for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
158 const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
159 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
160 for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
161 if ((*aSIt)->baseAttribute()) {
162 aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
163 if (aFoundAttr != anAttributes.end())
164 anAttributes.erase(aFoundAttr);
166 aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
167 if (aFoundFeat != aFeatures.end())
168 aFeatures.erase(aFoundFeat);
170 hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
179 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
180 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
182 for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
183 if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
184 (*aWIt)->setValue(aValueAttr->value());
185 myStorage->setNeedToResolve(true);
188 myStorage->addConstraint(myBaseConstraint, aWrapper);
193 bool SketchSolver_Constraint::remove()
196 myType = CONSTRAINT_UNKNOWN;
197 return myStorage->removeConstraint(myBaseConstraint);
200 void SketchSolver_Constraint::getAttributes(
202 std::vector<EntityWrapperPtr>& theAttributes)
204 static const int anInitNbOfAttr = 4;
205 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
206 myAttributes.clear();
208 DataPtr aData = myBaseConstraint->data();
209 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
211 myType = TYPE(myBaseConstraint);
213 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
214 aData->attribute(SketchPlugin_Constraint::VALUE()));
215 theValue = aValueAttr ? aValueAttr->value() : 0.0;
217 int aPtInd = 0; // index of first point in the list of attributes
218 int aEntInd = 2; // index of first entity in the list of attributes
219 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
220 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
221 for (; anIter != aConstrAttrs.end(); anIter++) {
222 AttributeRefAttrPtr aRefAttr =
223 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
224 if (!aRefAttr || !aRefAttr->isInitialized()) {
225 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
229 myStorage->update(*anIter/*, myGroupID*/);
230 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
232 // Force creation of an entity
233 myStorage->update(*anIter, GID_UNKNOWN, true);
234 anEntity = myStorage->entity(*anIter);
236 myAttributes.push_back(anEntity);
238 SketchSolver_EntityType aType = anEntity->type();
239 if (aType == ENTITY_UNKNOWN)
241 else if (aType == ENTITY_POINT)
242 theAttributes[aPtInd++] = anEntity; // the point is created
243 else { // another entity (not a point) is created
244 if (aEntInd < anInitNbOfAttr)
245 theAttributes[aEntInd] = anEntity;
247 theAttributes.push_back(anEntity);
253 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
255 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
256 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
257 for (; aCIt != aCList.end(); ++aCIt)
258 if ((*aCIt)->isUsed(theFeature))
261 std::list<AttributePtr> anAttrList =
262 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
263 std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
264 for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
265 if (isUsed(*anAttrIt))
271 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
273 AttributePtr anAttribute = theAttribute;
274 AttributeRefAttrPtr aRefAttr =
275 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
277 if (aRefAttr->isObject())
278 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
280 anAttribute = aRefAttr->attr();
283 const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
284 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
285 for (; aCIt != aCList.end(); ++aCIt)
286 if ((*aCIt)->isUsed(theAttribute))