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