Salome HOME
Correct processing arcs in SketchSolver with SolveSpace (issue #1144)
[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       (*aWIt)->setValue(aValueAttr->value());
126   }
127   myStorage->addConstraint(myBaseConstraint, aWrapper);
128
129   adjustConstraint();
130 }
131
132 bool SketchSolver_Constraint::remove()
133 {
134   cleanErrorMsg();
135   return myStorage->removeConstraint(myBaseConstraint);
136 }
137
138 void SketchSolver_Constraint::getAttributes(
139     double& theValue,
140     std::vector<EntityWrapperPtr>& theAttributes)
141 {
142   static const int anInitNbOfAttr = 4;
143   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
144
145   DataPtr aData = myBaseConstraint->data();
146   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
147
148   myType = TYPE(myBaseConstraint);
149
150   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
151       aData->attribute(SketchPlugin_Constraint::VALUE()));
152   theValue = aValueAttr ? aValueAttr->value() : 0.0;
153
154   int aPtInd = 0; // index of first point in the list of attributes
155   int aEntInd = 2; // index of first entity in the list of attributes
156   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
157   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
158   for (; anIter != aConstrAttrs.end(); anIter++) {
159     AttributeRefAttrPtr aRefAttr =
160         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
161     if (!aRefAttr || !aRefAttr->isInitialized()) {
162       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
163       return;
164     }
165
166     myStorage->update(*anIter/*, myGroupID*/);
167     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
168
169     SketchSolver_EntityType aType = anEntity->type();
170     if (aType == ENTITY_UNKNOWN)
171       continue;
172     else if (aType == ENTITY_POINT)
173       theAttributes[aPtInd++] = anEntity; // the point is created
174     else { // another entity (not a point) is created
175       if (aEntInd < anInitNbOfAttr)
176         theAttributes[aEntInd] = anEntity;
177       else
178         theAttributes.push_back(anEntity);
179       aEntInd++;
180     }
181   }
182 }
183
184 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
185 {
186   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
187   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
188   for (; aCIt != aCList.end(); ++aCIt)
189     if ((*aCIt)->isUsed(theFeature))
190       return true;
191
192   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
193   std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
194   for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
195     if (isUsed(*anAttrIt))
196       return true;
197
198   return false;
199 }
200
201 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
202 {
203   AttributePtr anAttribute = theAttribute;
204   AttributeRefAttrPtr aRefAttr =
205       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
206   if (aRefAttr) {
207     if (aRefAttr->isObject())
208       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
209     else
210       anAttribute = aRefAttr->attr();
211   }
212
213   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
214   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
215   for (; aCIt != aCList.end(); ++aCIt)
216     if ((*aCIt)->isUsed(theAttribute))
217       return true;
218   return false;
219 }