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