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