Salome HOME
Minor updates in SketchSolver plugin
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 #include <SketchSolver_Constraint.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4 #include <SketchSolver_Manager.h>
5
6 #include <SketchPlugin_Arc.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10
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>
24
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>
30
31 #include <math.h>
32
33 SketchSolver_Constraint::SketchSolver_Constraint(
34     ConstraintPtr  theConstraint)
35   : myBaseConstraint(theConstraint),
36     myGroupID(GID_UNKNOWN),
37     myType(CONSTRAINT_UNKNOWN)
38 {
39 }
40
41 void SketchSolver_Constraint::process(StoragePtr theStorage,
42                                       const GroupID& theGroupID,
43                                       const EntityID& theSketchID)
44 {
45   myStorage = theStorage;
46   myGroupID = theGroupID;
47   mySketchID = theSketchID;
48   // Process constraint according to its type
49   process();
50 }
51
52
53 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
54 {
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;
83 }
84
85 void SketchSolver_Constraint::process()
86 {
87   cleanErrorMsg();
88   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
89     // Not enough parameters are assigned
90     return;
91   }
92
93   SketchSolver_ConstraintType aConstrType = getType();
94   double aValue;
95   std::vector<EntityWrapperPtr> anAttributes;
96   getAttributes(aValue, anAttributes);
97   if (!myErrorMsg.empty())
98     return;
99   if (anAttributes.empty()) {
100     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
101     return;
102   }
103   if (aConstrType == CONSTRAINT_UNKNOWN)
104     aConstrType = getType();
105
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);
111
112   adjustConstraint();
113 }
114
115 void SketchSolver_Constraint::update()
116 {
117   cleanErrorMsg();
118
119   std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
120   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
121       myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
122   if (aValueAttr) {
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);
128       }
129   }
130   myStorage->addConstraint(myBaseConstraint, aWrapper);
131
132   adjustConstraint();
133 }
134
135 bool SketchSolver_Constraint::remove()
136 {
137   cleanErrorMsg();
138   return myStorage->removeConstraint(myBaseConstraint);
139 }
140
141 void SketchSolver_Constraint::getAttributes(
142     double& theValue,
143     std::vector<EntityWrapperPtr>& theAttributes)
144 {
145   static const int anInitNbOfAttr = 4;
146   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
147
148   DataPtr aData = myBaseConstraint->data();
149   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
150
151   myType = TYPE(myBaseConstraint);
152
153   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
154       aData->attribute(SketchPlugin_Constraint::VALUE()));
155   theValue = aValueAttr ? aValueAttr->value() : 0.0;
156
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();
166       return;
167     }
168
169     myStorage->update(*anIter/*, myGroupID*/);
170     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
171
172     SketchSolver_EntityType aType = anEntity->type();
173     if (aType == ENTITY_UNKNOWN)
174       continue;
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;
180       else
181         theAttributes.push_back(anEntity);
182       aEntInd++;
183     }
184   }
185 }
186
187 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
188 {
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))
193       return true;
194
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))
199       return true;
200
201   return false;
202 }
203
204 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
205 {
206   AttributePtr anAttribute = theAttribute;
207   AttributeRefAttrPtr aRefAttr =
208       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
209   if (aRefAttr) {
210     if (aRefAttr->isObject())
211       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
212     else
213       anAttribute = aRefAttr->attr();
214   }
215
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))
220       return true;
221   return false;
222 }