Salome HOME
Add tools
[modules/shaper.git] / src / SketchSolver / SketchSolver_Builder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchSolver_Builder.h"
8 #include <SketchPlugin_ConstraintAngle.h>
9 #include <SketchSolver_ConstraintAngle.h>
10 #include <SketchSolver_ConstraintCoincidence.h>
11 #include <SketchSolver_ConstraintDistance.h>
12 #include <SketchSolver_ConstraintEqual.h>
13 #include <SketchSolver_ConstraintFillet.h>
14 #include <SketchSolver_ConstraintLength.h>
15 #include <SketchSolver_ConstraintMirror.h>
16 #include <SketchSolver_ConstraintRigid.h>
17 #include <SketchSolver_ConstraintTangent.h>
18 #include <SketchSolver_ConstraintMultiRotation.h>
19 #include <SketchSolver_ConstraintMultiTranslation.h>
20 #include <SketchSolver_ConstraintMovement.h>
21 #include <SketchSolver_ConstraintParametric.h>
22 #include <SketchSolver_Error.h>
23
24 #include <GeomAPI_Edge.h>
25 #include <GeomDataAPI_Dir.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <Events_Error.h>
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_ResultConstruction.h>
34
35 #include <SketchPlugin_Arc.h>
36 #include <SketchPlugin_Circle.h>
37 #include <SketchPlugin_Line.h>
38 #include <SketchPlugin_Point.h>
39 #include <SketchPlugin_ConstraintCoincidence.h>
40 #include <SketchPlugin_ConstraintDistance.h>
41 #include <SketchPlugin_ConstraintEqual.h>
42 #include <SketchPlugin_ConstraintFillet.h>
43 #include <SketchPlugin_ConstraintHorizontal.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintMirror.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 #include <SketchPlugin_ConstraintRigid.h>
50 #include <SketchPlugin_ConstraintTangent.h>
51 #include <SketchPlugin_ConstraintVertical.h>
52 #include <SketchPlugin_MultiRotation.h>
53 #include <SketchPlugin_MultiTranslation.h>
54
55 #include <math.h>
56
57 // Initialization of constraint builder self pointer
58 SketchSolver_Builder* SketchSolver_Builder::mySelf = 0;
59
60 SketchSolver_Builder* SketchSolver_Builder::getInstance()
61 {
62   if (!mySelf)
63     mySelf = new SketchSolver_Builder();
64   return mySelf;
65 }
66
67 SolverConstraintPtr SketchSolver_Builder::createConstraint(ConstraintPtr theConstraint)
68 {
69   SolverConstraintPtr aResult;
70   DataPtr aData = theConstraint->data();
71   if (!aData || !aData->isValid())
72     return aResult;
73
74 #ifdef _DEBUG
75   // Verify attributes of constraint and generate errors
76   std::list<AttributePtr> anAttrList = aData->attributes(std::string());
77   std::list<AttributePtr>::iterator anIter = anAttrList.begin();
78   for (; anIter != anAttrList.end(); anIter++) {
79     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
80     if (aRefAttr) {
81       if (aRefAttr->isObject() && aRefAttr->object()) {
82         ResultConstructionPtr aRC =
83             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aRefAttr->object());
84         if (!aRC)
85           Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
86       }
87       continue;
88     }
89     AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIter);
90     if (aRefList) {
91       std::list<ObjectPtr> aList = aRefList->list();
92       std::list<ObjectPtr>::iterator aListIter = aList.begin();
93       for (; aListIter != aList.end(); aListIter++) {
94         ResultConstructionPtr aRC =
95             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aListIter);
96         if (*aListIter && !aRC)
97           Events_Error::send(SketchSolver_Error::NEED_OBJECT_NOT_FEATURE(), this);
98       }
99     }
100   }
101 #endif
102
103   if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
104     return SolverConstraintPtr(new SketchSolver_ConstraintCoincidence(theConstraint));
105   } else if (theConstraint->getKind() == SketchPlugin_ConstraintDistance::ID()) {
106     return SolverConstraintPtr(new SketchSolver_ConstraintDistance(theConstraint));
107   } else if (theConstraint->getKind() == SketchPlugin_ConstraintEqual::ID()) {
108     return SolverConstraintPtr(new SketchSolver_ConstraintEqual(theConstraint));
109   } else if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
110     return SolverConstraintPtr(new SketchSolver_ConstraintFillet(theConstraint));
111   } else if (theConstraint->getKind() == SketchPlugin_ConstraintHorizontal::ID()) {
112     return SolverConstraintPtr(new SketchSolver_ConstraintHorizontal(theConstraint));
113   } else if (theConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
114     return SolverConstraintPtr(new SketchSolver_ConstraintLength(theConstraint));
115   } else if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
116     return SolverConstraintPtr(new SketchSolver_ConstraintMirror(theConstraint));
117   } else if (theConstraint->getKind() == SketchPlugin_ConstraintParallel::ID()) {
118     return SolverConstraintPtr(new SketchSolver_ConstraintParallel(theConstraint));
119   } else if (theConstraint->getKind() == SketchPlugin_ConstraintPerpendicular::ID()) {
120     return SolverConstraintPtr(new SketchSolver_ConstraintPerpendicular(theConstraint));
121   } else if (theConstraint->getKind() == SketchPlugin_ConstraintRadius::ID()) {
122     return SolverConstraintPtr(new SketchSolver_ConstraintRadius(theConstraint));
123   } else if (theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID()) {
124     return SolverConstraintPtr(new SketchSolver_ConstraintTangent(theConstraint));
125   } else if (theConstraint->getKind() == SketchPlugin_ConstraintVertical::ID()) {
126     return SolverConstraintPtr(new SketchSolver_ConstraintVertical(theConstraint));
127   } else if (theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID()) {
128     return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theConstraint));
129   } else if (theConstraint->getKind() == SketchPlugin_MultiTranslation::ID()) {
130     return SolverConstraintPtr(new SketchSolver_ConstraintMultiTranslation(theConstraint));
131   } else if (theConstraint->getKind() == SketchPlugin_MultiRotation::ID()) {
132     return SolverConstraintPtr(new SketchSolver_ConstraintMultiRotation(theConstraint));
133   } else if (theConstraint->getKind() == SketchPlugin_ConstraintAngle::ID()) {
134     return SolverConstraintPtr(new SketchSolver_ConstraintAngle(theConstraint));
135   }
136   return aResult;
137 }
138
139 SolverConstraintPtr SketchSolver_Builder::createRigidConstraint(FeaturePtr theFixedFeature)
140 {
141   DataPtr aData = theFixedFeature->data();
142   if (!aData || !aData->isValid())
143     return SolverConstraintPtr();
144   return SolverConstraintPtr(new SketchSolver_ConstraintRigid(theFixedFeature));
145 }
146
147 SolverConstraintPtr SketchSolver_Builder::createMovementConstraint(FeaturePtr theFixedFeature)
148 {
149   DataPtr aData = theFixedFeature->data();
150   if (!aData || !aData->isValid())
151     return SolverConstraintPtr();
152   return SolverConstraintPtr(new SketchSolver_ConstraintMovement(theFixedFeature));
153 }
154
155 SolverConstraintPtr SketchSolver_Builder::createParametricConstraint(AttributePtr theAttribute)
156 {
157   return SolverConstraintPtr(new SketchSolver_ConstraintParametric(theAttribute));
158 }
159
160
161
162 bool SketchSolver_Builder::createWorkplane(
163     CompositeFeaturePtr theSketch,
164     std::vector<Slvs_Entity>& theEntities,
165     std::vector<Slvs_Param>& theParameters)
166 {
167   DataPtr aSketchData = theSketch->data();
168   if (!aSketchData || !aSketchData->isValid())
169     return false; // the sketch is incorrect
170
171   // Get parameters of workplane
172   std::shared_ptr<ModelAPI_Attribute> aDirX = aSketchData->attribute(
173       SketchPlugin_Sketch::DIRX_ID());
174   std::shared_ptr<ModelAPI_Attribute> aNorm = aSketchData->attribute(
175       SketchPlugin_Sketch::NORM_ID());
176   std::shared_ptr<ModelAPI_Attribute> anOrigin = aSketchData->attribute(
177       SketchPlugin_Sketch::ORIGIN_ID());
178   // Create SolveSpace entity corresponding to the sketch origin
179   if (!createEntity(anOrigin, theEntities, theParameters))
180     return false;
181   Slvs_hEntity anOriginID = theEntities.back().h;
182   // Create SolveSpace entity corresponding the the sketch normal
183   if (!createNormal(aNorm, aDirX, theEntities, theParameters))
184     return false;
185   Slvs_hEntity aNormalID = theEntities.back().h;
186
187   // Create workplane
188   Slvs_hEntity aWorkplaneID = theEntities.back().h + 1;
189   Slvs_Entity aWorkplane = Slvs_MakeWorkplane(aWorkplaneID, SLVS_G_UNKNOWN, anOriginID, aNormalID);
190   theEntities.push_back(aWorkplane);
191   return true;
192 }
193
194 bool SketchSolver_Builder::createEntity(
195     AttributePtr theAttribute,
196     std::vector<Slvs_Entity>& theEntities,
197     std::vector<Slvs_Param>& theParameters)
198 {
199   Slvs_hEntity anEntID = theEntities.empty() ? 0 : theEntities.back().h;
200   Slvs_hParam aParamID = theParameters.empty() ? 0 : theParameters.back().h;
201
202   // Point in 3D
203   std::shared_ptr<GeomDataAPI_Point> aPoint =
204       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
205   if (aPoint) {
206     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->x()));
207     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->y()));
208     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint->z()));
209     theEntities.push_back(Slvs_MakePoint3d(++anEntID, SLVS_G_UNKNOWN,
210         aParamID-2, aParamID-1, aParamID));
211     return true;
212   }
213   // Point in 2D
214   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
215     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
216   if (aPoint2D) {
217     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->x()));
218     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aPoint2D->y()));
219     theEntities.push_back(Slvs_MakePoint2d(++anEntID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
220       aParamID-1, aParamID));
221     return true;
222   }
223   // Scalar value (used for the distance entities)
224   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
225   if (aScalar) {
226     theParameters.push_back(Slvs_MakeParam(++aParamID, SLVS_G_UNKNOWN, aScalar->value()));
227     theEntities.push_back(Slvs_MakeDistance(++anEntID, SLVS_G_UNKNOWN,
228       SLVS_E_UNKNOWN, aParamID));
229     return true;
230   }
231   // unknown attribute type
232   return false;
233 }
234
235 bool SketchSolver_Builder::createEntity(
236     FeaturePtr theFeature,
237     std::vector<Slvs_Entity>& theEntities,
238     std::vector<Slvs_Param>& theParameters)
239 {
240   if (!theFeature->data()->isValid())
241     return false;
242
243   // SketchPlugin features
244   std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
245       SketchPlugin_Feature>(theFeature);
246   if (!aFeature)
247     return false;
248
249   // Verify the feature by its kind
250   const std::string& aFeatureKind = aFeature->getKind();
251   DataPtr aData = aFeature->data();
252   // Line
253   if (aFeatureKind == SketchPlugin_Line::ID()) {
254     AttributePtr aStart = aData->attribute(SketchPlugin_Line::START_ID());
255     AttributePtr aEnd = aData->attribute(SketchPlugin_Line::END_ID());
256     if (!aStart->isInitialized() || !aEnd->isInitialized())
257       return false;
258     if (!createEntity(aStart, theEntities, theParameters) ||
259         !createEntity(aEnd, theEntities, theParameters))
260       return false;
261     Slvs_hEntity aLineID = theEntities.back().h + 1;
262     theEntities.push_back(Slvs_MakeLineSegment(aLineID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
263         aLineID-2, aLineID-1));
264   }
265   // Circle
266   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
267     AttributePtr aCenter = aData->attribute(SketchPlugin_Circle::CENTER_ID());
268     AttributePtr aRadius = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
269     if (!aCenter->isInitialized() || !aRadius->isInitialized())
270       return false;
271     if (!createEntity(aCenter, theEntities, theParameters) ||
272         !createEntity(aRadius, theEntities, theParameters))
273       return false;
274     Slvs_hEntity aCircID = theEntities.back().h;
275     theEntities.push_back(Slvs_MakeCircle(aCircID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN, aCircID-2,
276       SLVS_E_UNKNOWN, aCircID-1));
277   }
278   // Arc
279   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
280     AttributePtr aCenter = aData->attribute(SketchPlugin_Arc::CENTER_ID());
281     AttributePtr aStart = aData->attribute(SketchPlugin_Arc::START_ID());
282     AttributePtr aEnd = aData->attribute(SketchPlugin_Arc::END_ID());
283     if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
284       return false;
285     if (!createEntity(aCenter, theEntities, theParameters) ||
286         !createEntity(aStart, theEntities, theParameters) ||
287         !createEntity(aEnd, theEntities, theParameters))
288       return false;
289     Slvs_hEntity anArcID = theEntities.back().h;
290     theEntities.push_back(Slvs_MakeArcOfCircle(anArcID, SLVS_G_UNKNOWN, SLVS_E_UNKNOWN,
291         SLVS_E_UNKNOWN, anArcID-3, anArcID-2, anArcID-1));
292   }
293   // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
294   else if (aFeatureKind == SketchPlugin_Point::ID()) {
295     AttributePtr aPoint = aData->attribute(SketchPlugin_Point::COORD_ID());
296     if (!aPoint->isInitialized() ||
297         !createEntity(aPoint, theEntities, theParameters))
298       return false;
299     // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier.
300     // No need to add another entity.
301   }
302   return true;
303 }
304
305 bool SketchSolver_Builder::createNormal(
306     AttributePtr theNormal,
307     AttributePtr theDirX,
308     std::vector<Slvs_Entity>& theEntities,
309     std::vector<Slvs_Param>& theParameters)
310 {
311   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
312   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
313   if (!aDirX || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) || 
314       !aNorm->isInitialized())
315     return false;
316   // calculate Y direction
317   std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
318
319   // quaternion parameters of normal vector
320   double qw, qx, qy, qz;
321   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
322                       &qx, &qy, &qz);
323   double aNormCoord[4] = { qw, qx, qy, qz };
324
325   // Create parameters of the normal
326   Slvs_hParam aCurParam = theParameters.back().h;
327   for (int i = 0; i < 4; i++)
328     theParameters.push_back(Slvs_MakeParam(++aCurParam, SLVS_G_UNKNOWN, aNormCoord[i]));
329
330   // Create a normal
331   Slvs_hEntity aCurEntity = theEntities.back().h + 1;
332   Slvs_Entity aNormal = Slvs_MakeNormal3d(aCurEntity, SLVS_G_UNKNOWN,
333       aCurParam-3, aCurParam-2, aCurParam-1, aCurParam);
334   theEntities.push_back(aNormal);
335   return true;
336 }