]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SolveSpaceSolver/SolveSpaceSolver_Builder.cpp
Salome HOME
5e1b85015d27534de5c319239072282ecb0fbfba
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Builder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SolveSpaceSolver_Builder.h>
8 #include <SolveSpaceSolver_Solver.h>
9 #include <SolveSpaceSolver_Storage.h>
10 #include <SolveSpaceSolver_ParameterWrapper.h>
11 #include <SolveSpaceSolver_EntityWrapper.h>
12 #include <SolveSpaceSolver_ConstraintWrapper.h>
13 #include <SolveSpaceSolver_ConstraintType.h>
14
15 #include <SketchSolver_Manager.h>
16
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Point2D.h>
23 #include <ModelAPI_Attribute.h>
24 #include <ModelAPI_AttributeRefAttr.h>
25
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_Circle.h>
28 #include <SketchPlugin_Line.h>
29 #include <SketchPlugin_Point.h>
30
31 #include <math.h>
32
33
34 static EntityWrapperPtr createLine(FeaturePtr theFeature,
35                                    const std::list<EntityWrapperPtr>& theAttributes,
36                                    const GroupID& theGroupID,
37                                    const EntityID& theSketchID);
38 static EntityWrapperPtr createCircle(FeaturePtr theFeature,
39                                      const std::list<EntityWrapperPtr>& theAttributes,
40                                      const GroupID& theGroupID,
41                                      const EntityID& theSketchID);
42 static EntityWrapperPtr createArc(FeaturePtr theFeature,
43                                   const std::list<EntityWrapperPtr>& theAttributes,
44                                   const GroupID& theGroupID,
45                                   const EntityID& theSketchID);
46
47 /// \brief Set flags of constraint to identify which points are coincident in the Tangency
48 ///        (for more information, see SolveSpace documentation)
49 static void adjustTangency(ConstraintWrapperPtr theConstraint);
50 /// \brief Set flags for angle constraint
51 static void adjustAngle(ConstraintWrapperPtr theConstraint);
52 /// \brief Update mirror points
53 static void adjustMirror(ConstraintWrapperPtr theConstraint);
54 /// \brief Update positions of rotated features
55 static void adjustMultiRotation(ConstraintWrapperPtr theConstraint);
56 /// \brief Update positions of translated features
57 static void adjustMultiTranslation(ConstraintWrapperPtr theConstraint);
58
59 /// \brief Transform points to be symmetric regarding to the mirror line
60 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
61                              EntityWrapperPtr theMirrored,
62                              EntityWrapperPtr theMirrorLine);
63
64
65
66 // Initialization of constraint builder self pointer
67 BuilderPtr SolveSpaceSolver_Builder::mySelf = SolveSpaceSolver_Builder::getInstance();
68
69 BuilderPtr SolveSpaceSolver_Builder::getInstance()
70 {
71   if (!mySelf) {
72     mySelf = BuilderPtr(new SolveSpaceSolver_Builder);
73     SketchSolver_Manager::instance()->setBuilder(mySelf);
74   }
75   return mySelf;
76 }
77
78 StoragePtr SolveSpaceSolver_Builder::createStorage(const GroupID& theGroup) const
79 {
80   return StoragePtr(new SolveSpaceSolver_Storage(theGroup));
81 }
82
83 SolverPtr SolveSpaceSolver_Builder::createSolver() const
84 {
85   return SolverPtr(new SolveSpaceSolver_Solver);
86 }
87
88
89 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createConstraint(
90     ConstraintPtr theConstraint,
91     const GroupID& theGroupID,
92     const EntityID& theSketchID,
93     const SketchSolver_ConstraintType& theType,
94     const double& theValue,
95     const EntityWrapperPtr& thePoint1,
96     const EntityWrapperPtr& thePoint2,
97     const EntityWrapperPtr& theEntity1,
98     const EntityWrapperPtr& theEntity2) const
99 {
100   if (theType == CONSTRAINT_SYMMETRIC)
101     return createMirror(theConstraint, theGroupID, theSketchID,
102                         thePoint1, thePoint2, theEntity1);
103
104   int aType = ConstraintType::toSolveSpace(theType);
105   if (aType == SLVS_C_UNKNOWN)
106     return std::list<ConstraintWrapperPtr>();
107
108   Slvs_hEntity aSlvsEntities[4] = {SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN};
109   EntityWrapperPtr anOriginal[4] = {thePoint1, thePoint2, theEntity1, theEntity2};
110   std::list<EntityWrapperPtr> aConstrAttrList; // to be filled
111   for (int i = 0; i < 4; ++i) {
112     if (!anOriginal[i])
113       continue;
114     aSlvsEntities[i] = (Slvs_hEntity)anOriginal[i]->id();
115     if (aSlvsEntities[i] == SLVS_E_UNKNOWN)
116       return std::list<ConstraintWrapperPtr>(); // entity is not added into a storage, constraint can not be created
117     aConstrAttrList.push_back(anOriginal[i]);
118   }
119
120   Slvs_Constraint aConstraint = Slvs_MakeConstraint(
121       SLVS_C_UNKNOWN, (Slvs_hGroup)theGroupID, aType, (Slvs_hEntity)theSketchID,
122       theValue, aSlvsEntities[0], aSlvsEntities[1], aSlvsEntities[2], aSlvsEntities[3]);
123   ConstraintWrapperPtr aResult(new SolveSpaceSolver_ConstraintWrapper(theConstraint, aConstraint));
124   aResult->setValue(theValue);
125   aResult->setEntities(aConstrAttrList);
126   adjustConstraint(aResult);
127
128   return std::list<ConstraintWrapperPtr>(1, aResult);
129 }
130
131 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createConstraint(
132     ConstraintPtr theConstraint,
133     const GroupID& theGroupID,
134     const EntityID& theSketchID,
135     const SketchSolver_ConstraintType& theType,
136     const double& theValue,
137     const EntityWrapperPtr& thePoint1,
138     const EntityWrapperPtr& thePoint2,
139     const std::list<EntityWrapperPtr>& theTrsfEnt) const
140 {
141   if (theType != CONSTRAINT_MULTI_ROTATION && theType != CONSTRAINT_MULTI_TRANSLATION)
142     return std::list<ConstraintWrapperPtr>();
143
144   int aType = ConstraintType::toSolveSpace(theType);
145   if (aType == SLVS_C_UNKNOWN)
146     return std::list<ConstraintWrapperPtr>();
147
148   Slvs_Constraint aConstraint =
149       Slvs_MakeConstraint(SLVS_C_UNKNOWN, (Slvs_hGroup)theGroupID, aType, (Slvs_hEntity)theSketchID,
150       theValue, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
151
152   std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
153   if (thePoint2)
154     aConstrAttrList.push_front(thePoint2);
155   aConstrAttrList.push_front(thePoint1);
156
157   ConstraintWrapperPtr aResult(new SolveSpaceSolver_ConstraintWrapper(theConstraint, aConstraint));
158   aResult->setValue(theValue);
159   aResult->setEntities(aConstrAttrList);
160   return std::list<ConstraintWrapperPtr>(1, aResult);
161 }
162
163
164 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createMirror(
165     ConstraintPtr theConstraint,
166     const GroupID& theGroupID,
167     const EntityID& theSketchID,
168     const EntityWrapperPtr& theEntity1,
169     const EntityWrapperPtr& theEntity2,
170     const EntityWrapperPtr& theMirrorLine) const
171 {
172   Slvs_Constraint aConstraint;
173   std::list<ConstraintWrapperPtr> aResult;
174   std::list<EntityWrapperPtr> aConstrAttrList;
175   if (theEntity1->type() == ENTITY_POINT) {
176     if (theEntity2->group() == theGroupID) // theEntity2 is not fixed
177       makeMirrorPoints(theEntity1, theEntity2, theMirrorLine);
178
179     aConstraint = Slvs_MakeConstraint(
180         SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID, SLVS_C_SYMMETRIC_LINE, (Slvs_hEntity)theSketchID,
181         0.0, (Slvs_hEntity)theEntity1->id(), (Slvs_hEntity)theEntity2->id(),
182         (Slvs_hEntity)theMirrorLine->id(), SLVS_E_UNKNOWN);
183
184     aConstrAttrList.push_back(theEntity1);
185     aConstrAttrList.push_back(theEntity2);
186     aConstrAttrList.push_back(theMirrorLine);
187
188     ConstraintWrapperPtr aWrapper(new SolveSpaceSolver_ConstraintWrapper(
189         theConstraint, aConstraint));
190     aWrapper->setEntities(aConstrAttrList);
191     aResult.push_back(aWrapper);
192   }
193   else if (theEntity1->type() == ENTITY_LINE) {
194     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
195     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
196     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
197     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
198     for (; anIt1 != aPoints1.end() && anIt2 != aPoints2.end(); ++anIt1, ++anIt2) {
199       std::list<ConstraintWrapperPtr> aMrrList =
200           createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
201       aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
202     }
203   }
204   else if (theEntity1->type() == ENTITY_CIRCLE) {
205     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
206     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
207     for (; anIt1 != aPoints1.end(); ++anIt1)
208       if ((*anIt1)->type() == ENTITY_POINT)
209         break;
210     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
211     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
212     for (; anIt2 != aPoints2.end(); ++anIt2)
213       if ((*anIt2)->type() == ENTITY_POINT)
214         break;
215
216     std::list<ConstraintWrapperPtr> aMrrList =
217         createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
218     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
219
220     // Additional constraint for equal radii
221     aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
222         0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
223     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
224   }
225   else if (theEntity1->type() == ENTITY_ARC) {
226     // Do not allow mirrored arc recalculate its position until coordinated of all points recalculated
227     FeaturePtr aMirrArc = theEntity2->baseFeature();
228     aMirrArc->data()->blockSendAttributeUpdated(true);
229
230     std::list<ConstraintWrapperPtr> aMrrList;
231     std::list<EntityWrapperPtr>::const_iterator anIt1 = theEntity1->subEntities().begin();
232     std::list<EntityWrapperPtr>::const_iterator anIt2 = theEntity2->subEntities().begin();
233     if ((*anIt2)->group() == theGroupID) // mirrored point is not fixed
234       makeMirrorPoints(*anIt1, *anIt2, theMirrorLine);
235
236     // Workaround to avoid problems in SolveSpace.
237     // The symmetry of two arcs will be done using symmetry of three points on these arcs:
238     // start point, end point, and any other point on the arc
239     std::list<EntityWrapperPtr> aBaseArcPoints(++anIt1, theEntity1->subEntities().end());
240     std::list<EntityWrapperPtr> aMirrorArcPoints(++anIt2, theEntity2->subEntities().end());
241     // indices of points of arc, center corresponds center, first point corresponds last point
242     aMirrorArcPoints.reverse();
243
244     anIt1 = aBaseArcPoints.begin();
245     anIt2 = aMirrorArcPoints.begin();
246     for (; anIt1 != aBaseArcPoints.end(); ++anIt1, ++anIt2) {
247       aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
248       aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
249     }
250     // Restore event sending
251     aMirrArc->data()->blockSendAttributeUpdated(false);
252   }
253   return aResult;
254 }
255
256 void SolveSpaceSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
257 {
258   SketchSolver_ConstraintType aType = theConstraint->type();
259   // Update flags in constraints
260   if (aType == CONSTRAINT_TANGENT_ARC_ARC || aType == CONSTRAINT_TANGENT_ARC_LINE)
261     adjustTangency(theConstraint);
262   else if (aType == CONSTRAINT_ANGLE)
263     adjustAngle(theConstraint);
264   else if (aType == CONSTRAINT_SYMMETRIC)
265     adjustMirror(theConstraint);
266   else if (aType == CONSTRAINT_MULTI_ROTATION)
267     adjustMultiRotation(theConstraint);
268   else if (aType == CONSTRAINT_MULTI_TRANSLATION)
269     adjustMultiTranslation(theConstraint);
270 }
271
272 EntityWrapperPtr SolveSpaceSolver_Builder::createFeature(
273     FeaturePtr theFeature,
274     const std::list<EntityWrapperPtr>& theAttributes,
275     const GroupID& theGroupID,
276     const EntityID& theSketchID) const
277 {
278   static EntityWrapperPtr aDummy;
279   if (!theFeature->data()->isValid())
280     return aDummy;
281
282   // Sketch
283   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
284   if (aSketch)
285     return createSketchEntity(aSketch, theGroupID);
286
287   // SketchPlugin features
288   std::shared_ptr<SketchPlugin_Feature> aFeature =
289       std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
290   if (!aFeature)
291     return aDummy;
292
293   // Verify the feature by its kind
294   const std::string& aFeatureKind = aFeature->getKind();
295   // Line
296   if (aFeatureKind == SketchPlugin_Line::ID())
297     return createLine(theFeature, theAttributes, theGroupID, theSketchID);
298   // Circle
299   else if (aFeatureKind == SketchPlugin_Circle::ID())
300     return createCircle(theFeature, theAttributes,theGroupID, theSketchID);
301   // Arc
302   else if (aFeatureKind == SketchPlugin_Arc::ID())
303     return createArc(theFeature, theAttributes,theGroupID, theSketchID);
304   // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
305   else if (aFeatureKind == SketchPlugin_Point::ID()) {
306     AttributePtr aPoint = theFeature->attribute(SketchPlugin_Point::COORD_ID());
307     if (!aPoint->isInitialized())
308       return aDummy;
309     EntityWrapperPtr aSub = createAttribute(aPoint, theGroupID, theSketchID);
310     if (!aSub)
311       return aDummy;
312
313     const Slvs_Entity& aSubEnt =
314         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(aSub)->entity();
315     return EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, aPoint, aSubEnt));
316   }
317
318   // wrong entity
319   return aDummy;
320 }
321
322 EntityWrapperPtr SolveSpaceSolver_Builder::createAttribute(
323     AttributePtr theAttribute,
324     const GroupID& theGroupID,
325     const EntityID& theSketchID) const
326 {
327   AttributePtr anAttribute = theAttribute;
328   AttributeRefAttrPtr aRefAttr =
329       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
330   if (aRefAttr) {
331     if (aRefAttr->isObject()) {
332       // do not create features here
333       return EntityWrapperPtr();
334     } else
335       anAttribute = aRefAttr->attr();
336   }
337
338   std::list<ParameterWrapperPtr> aParameters;
339   Slvs_Entity anEntity;
340   anEntity.type = 0;
341
342   // Point in 3D
343   std::shared_ptr<GeomDataAPI_Point> aPoint =
344       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
345   if (aPoint) {
346     aParameters.push_back(createParameter(theGroupID, aPoint->x(), !aPoint->textX().empty()));
347     aParameters.push_back(createParameter(theGroupID, aPoint->y(), !aPoint->textY().empty()));
348     aParameters.push_back(createParameter(theGroupID, aPoint->z(), !aPoint->textZ().empty()));
349     // Create entity (parameters are not filled)
350     anEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
351         SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
352   } else {
353     // Point in 2D
354     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
355       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
356     if (aPoint2D) {
357       aParameters.push_back(createParameter(theGroupID, aPoint2D->x(), !aPoint2D->textX().empty()));
358       aParameters.push_back(createParameter(theGroupID, aPoint2D->y(), !aPoint2D->textY().empty()));
359       // Create entity (parameters are not filled)
360       anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
361           (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
362     } else {
363       // Scalar value (used for the distance entities)
364       AttributeDoublePtr aScalar =
365           std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
366       if (aScalar) {
367         aParameters.push_back(createParameter(theGroupID, aScalar->value(), !aScalar->text().empty()));
368         // Create entity (parameter is not filled)
369         anEntity = Slvs_MakeDistance(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
370           (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN);
371       }
372     }
373   }
374
375   if (anEntity.type == 0) {
376     // unknown attribute type
377     return EntityWrapperPtr();
378   }
379
380   EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(theAttribute, anEntity));
381   aResult->setParameters(aParameters);
382   return aResult;
383 }
384
385
386
387 EntityWrapperPtr SolveSpaceSolver_Builder::createSketchEntity(
388     CompositeFeaturePtr theSketch,
389     const GroupID& theGroupID) const
390 {
391   DataPtr aSketchData = theSketch->data();
392   if (!aSketchData || !aSketchData->isValid())
393     return EntityWrapperPtr(); // the sketch is incorrect
394
395   // Get parameters of workplane
396   AttributePtr aDirX    = aSketchData->attribute(SketchPlugin_Sketch::DIRX_ID());
397   AttributePtr aNorm    = aSketchData->attribute(SketchPlugin_Sketch::NORM_ID());
398   AttributePtr anOrigin = aSketchData->attribute(SketchPlugin_Sketch::ORIGIN_ID());
399   if (!anOrigin->isInitialized() || !aNorm->isInitialized() || !aDirX->isInitialized())
400     return EntityWrapperPtr();
401
402   EntityWrapperPtr aNewEnt;
403   std::list<EntityWrapperPtr> aSubs;
404
405   // Create SolveSpace entity corresponding to the sketch origin
406   aNewEnt = createAttribute(anOrigin, theGroupID);
407   if (!aNewEnt)
408     return EntityWrapperPtr();
409   aSubs.push_back(aNewEnt);
410
411   // Create SolveSpace entity corresponding the the sketch normal
412   aNewEnt = createNormal(aNorm, aDirX, theGroupID);
413   if (!aNewEnt)
414     return EntityWrapperPtr();
415   aSubs.push_back(aNewEnt);
416
417   // Create workplane
418   Slvs_Entity aWorkplane = Slvs_MakeWorkplane(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
419       SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
420
421   aNewEnt = EntityWrapperPtr(
422       new SolveSpaceSolver_EntityWrapper(FeaturePtr(theSketch), aWorkplane));
423   aNewEnt->setSubEntities(aSubs);
424   return aNewEnt;
425 }
426
427 EntityWrapperPtr SolveSpaceSolver_Builder::createNormal(
428     AttributePtr theNormal,
429     AttributePtr theDirX,
430     const GroupID& theGroupID) const
431 {
432   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
433   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
434   if (!aDirX || !aNorm ||
435       (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) || 
436       !aNorm->isInitialized())
437     return EntityWrapperPtr();
438   // calculate Y direction
439   std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
440
441   // quaternion parameters of normal vector
442   double qw, qx, qy, qz;
443   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
444                       &qx, &qy, &qz);
445   double aNormCoord[4] = { qw, qx, qy, qz };
446
447   // Create parameters of the normal
448   std::list<ParameterWrapperPtr> aParameters;
449   for (int i = 0; i < 4; i++)
450     aParameters.push_back(createParameter(theGroupID, aNormCoord[i]));
451
452   // Create a normal with empty parameters
453   Slvs_Entity aNormalEnt = Slvs_MakeNormal3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
454       SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
455   EntityWrapperPtr aNormal(new SolveSpaceSolver_EntityWrapper(theNormal, aNormalEnt));
456   aNormal->setParameters(aParameters);
457   return aNormal;
458 }
459
460 ParameterWrapperPtr SolveSpaceSolver_Builder::createParameter(
461     const GroupID& theGroup, const double theValue, const bool theExpr) const
462 {
463   Slvs_Param aParam = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroup, theValue);
464   ParameterWrapperPtr aWrapper(new SolveSpaceSolver_ParameterWrapper(aParam));
465   aWrapper->setIsParametric(theExpr);
466   return aWrapper;
467 }
468
469
470
471
472
473 // ================   Auxiliary functions   ==========================
474 EntityWrapperPtr createLine(FeaturePtr theFeature,
475                             const std::list<EntityWrapperPtr>& theAttributes,
476                             const GroupID& theGroupID,
477                             const EntityID& theSketchID)
478 {
479   EntityWrapperPtr aNewEntity;
480   std::list<EntityWrapperPtr> aSubs;
481
482   AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
483   AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
484   if (!aStart->isInitialized() || !aEnd->isInitialized())
485     return aNewEntity;
486
487   EntityWrapperPtr aStartEnt, aEndEnt;
488   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
489   for (; anIt != theAttributes.end(); ++anIt) {
490     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity = 
491         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
492     if (aSlvsEntity->isBase(aStart))
493       aStartEnt = aSlvsEntity;
494     else if (aSlvsEntity->isBase(aEnd))
495       aEndEnt = aSlvsEntity;
496   }
497   if (!aStartEnt || !aEndEnt)
498     return aNewEntity;
499
500   aSubs.push_back(aStartEnt);
501   aSubs.push_back(aEndEnt);
502   Slvs_Entity anEntity = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
503       (Slvs_hEntity)theSketchID, (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
504
505   aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
506   aNewEntity->setSubEntities(aSubs);
507   return aNewEntity;
508 }
509
510 EntityWrapperPtr createCircle(FeaturePtr theFeature,
511                               const std::list<EntityWrapperPtr>& theAttributes,
512                               const GroupID& theGroupID,
513                               const EntityID& theSketchID)
514 {
515   EntityWrapperPtr aNewEntity;
516   std::list<EntityWrapperPtr> aSubs;
517
518   AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
519   AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
520   if (!aCenter->isInitialized() || !aRadius->isInitialized())
521     return aNewEntity;
522
523   EntityWrapperPtr aCenterEnt, aRadiusEnt, aNormalEnt;
524   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
525   for (; anIt != theAttributes.end(); ++anIt) {
526     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity = 
527         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
528     if (aSlvsEntity->isBase(aCenter))
529       aCenterEnt = aSlvsEntity;
530     else if (aSlvsEntity->isBase(aRadius))
531       aRadiusEnt = aSlvsEntity;
532     else if (aSlvsEntity->type() == ENTITY_NORMAL)
533       aNormalEnt = aSlvsEntity;
534   }
535   if (!aCenterEnt || !aRadiusEnt || !aNormalEnt)
536     return aNewEntity;
537
538   aSubs.push_back(aCenterEnt);
539   aSubs.push_back(aRadiusEnt);
540   Slvs_Entity anEntity = Slvs_MakeCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
541       (Slvs_hEntity)theSketchID, (Slvs_hEntity)aCenterEnt->id(),
542       (Slvs_hEntity)aNormalEnt->id(), (Slvs_hEntity)aRadiusEnt->id());
543
544   aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
545   aNewEntity->setSubEntities(aSubs);
546   return aNewEntity;
547 }
548
549 EntityWrapperPtr createArc(FeaturePtr theFeature,
550                            const std::list<EntityWrapperPtr>& theAttributes,
551                            const GroupID& theGroupID,
552                            const EntityID& theSketchID)
553 {
554   EntityWrapperPtr aNewEntity;
555   std::list<EntityWrapperPtr> aSubs;
556
557   AttributePtr aCenter = theFeature->attribute(SketchPlugin_Arc::CENTER_ID());
558   AttributePtr aStart = theFeature->attribute(SketchPlugin_Arc::START_ID());
559   AttributePtr aEnd = theFeature->attribute(SketchPlugin_Arc::END_ID());
560   if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
561     return aNewEntity;
562
563   EntityWrapperPtr aCenterEnt, aStartEnt, aEndEnt, aNormalEnt;
564   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
565   for (; anIt != theAttributes.end(); ++anIt) {
566     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity = 
567         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
568     if (aSlvsEntity->isBase(aCenter))
569       aCenterEnt = aSlvsEntity;
570     else if (aSlvsEntity->isBase(aStart))
571       aStartEnt = aSlvsEntity;
572     else if (aSlvsEntity->isBase(aEnd))
573       aEndEnt = aSlvsEntity;
574     else if (aSlvsEntity->type() == ENTITY_NORMAL)
575       aNormalEnt = aSlvsEntity;
576   }
577   if (!aCenterEnt || !aStartEnt || !aEndEnt || !aNormalEnt)
578     return aNewEntity;
579
580   aSubs.push_back(aCenterEnt);
581   aSubs.push_back(aStartEnt);
582   aSubs.push_back(aEndEnt);
583   Slvs_Entity anEntity = Slvs_MakeArcOfCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
584       (Slvs_hEntity)theSketchID, (Slvs_hEntity)aNormalEnt->id(),
585       (Slvs_hEntity)aCenterEnt->id(), (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
586
587   aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
588   aNewEntity->setSubEntities(aSubs);
589   return aNewEntity;
590 }
591
592
593 void adjustTangency(ConstraintWrapperPtr theConstraint)
594 {
595   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
596
597   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
598     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
599
600   // Collect start, end points of entities
601   std::shared_ptr<GeomAPI_Pnt2d> aStartEntPoints[2][2];
602   bool isCoinc[2][2] = {false};
603   const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
604   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
605   for (int i = 0; aSIt != aSubs.end(); ++aSIt, ++i) {
606     const std::list<EntityWrapperPtr>& aPoints = (*aSIt)->subEntities();
607     std::list<EntityWrapperPtr>::const_iterator aPIt = aPoints.begin();
608     if ((*aSIt)->type() == ENTITY_ARC)
609       ++aPIt;
610     for (int j = 0; aPIt != aPoints.end(); ++aPIt, ++j) {
611       aStartEntPoints[i][j] = aBuilder->point(*aPIt);
612       if (i > 0) { // check coincidence
613         for (int k = 0; k < 2; ++k)
614           if (aStartEntPoints[i][j]->distance(aStartEntPoints[0][k]) < tolerance)
615             isCoinc[0][k] = isCoinc[i][j] = true;
616       }
617     }
618   }
619
620   Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
621   if (isCoinc[0][0] == false && isCoinc[0][1] == true)
622     aSlvsConstraint.other = 1;
623   else aSlvsConstraint.other = 0;
624   if (isCoinc[1][0] == false && isCoinc[1][1] == true)
625     aSlvsConstraint.other2 = 1;
626   else aSlvsConstraint.other2 = 0;
627 }
628
629 void adjustAngle(ConstraintWrapperPtr theConstraint)
630 {
631   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
632
633   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
634     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
635
636   std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
637   const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
638   std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
639   for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
640     const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
641     std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
642     for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt)
643       aPoints[i][j] = aBuilder->point(*aLPIt);
644   }
645
646   std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
647     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
648     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
649   };
650   std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
651   if (!anIntersection)
652     return;
653   double aDist[2][2];
654   for (int i = 0; i < 2; i++) {
655     for (int j = 0; j < 2; j++) {
656       aDist[i][j] = anIntersection->distance(aPoints[i][j]);
657       if (fabs(aDist[i][j]) <= tolerance)
658         aDist[i][j] = 0.0;
659     }
660     if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
661         aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
662       // the intersection point is an inner point of the line,
663       // we change the sign of distance till start point to calculate correct coordinates
664       // after rotation
665       aDist[i][0] *= -1.0;
666     }
667   }
668   std::shared_ptr<GeomAPI_Dir2d> aDir[2];
669   for (int i = 0; i < 2; i++) {
670     if (aDist[i][1] > fabs(aDist[i][0]))
671       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
672           aPoints[i][1]->xy()->decreased(anIntersection->xy())));
673     else {
674       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
675           aPoints[i][0]->xy()->decreased(anIntersection->xy())));
676       // main direction is opposite => change signs
677       if (aDist[i][0] < 0.0) {
678         aDist[i][0] *= -1.0;
679         aDist[i][1] *= -1.0;
680       }
681     }
682   }
683
684   Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
685   aSlvsConstraint.other = false;
686   for (int i = 0; i < 2; i++)
687     if (aLine[i]->direction()->dot(aDir[i]) < 0.0)
688       aSlvsConstraint.other = !aSlvsConstraint.other;
689 }
690
691 void adjustMirror(ConstraintWrapperPtr theConstraint)
692 {
693   std::vector<EntityWrapperPtr> aPoints;
694   EntityWrapperPtr aMirrorLine;
695
696   const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
697   std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
698   for (; anIt != aSubs.end(); ++anIt) {
699     if ((*anIt)->type() == ENTITY_POINT)
700       aPoints.push_back(*anIt);
701     else if ((*anIt)->type() == ENTITY_LINE)
702       aMirrorLine = *anIt;
703   }
704
705   makeMirrorPoints(aPoints[0], aPoints[1], aMirrorLine);
706 }
707
708 void makeMirrorPoints(EntityWrapperPtr theOriginal,
709                       EntityWrapperPtr theMirrored,
710                       EntityWrapperPtr theMirrorLine)
711 {
712   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
713
714   std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
715   std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
716   // orthogonal direction
717   aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
718
719   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
720   std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
721   double aDist = aVec->dot(aMLDir->xy());
722   aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
723   double aCoord[2] = {aVec->x(), aVec->y()};
724   std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
725   for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
726     (*aMIt)->setValue(aCoord[i]);
727
728   // update corresponding attribute
729   AttributePtr anAttr = std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMirrored)->baseAttribute();
730   if (anAttr) {
731     std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
732     aMirroredPnt->setValue(aCoord[0], aCoord[1]);
733   }
734 }
735
736 static void rotate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
737                    std::shared_ptr<GeomAPI_Pnt2d> theCenter,
738                    double theSin, double theCos)
739 {
740   std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSource =
741       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSource);
742   std::shared_ptr<SolveSpaceSolver_EntityWrapper> aDest =
743       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theDest);
744
745   if (theSource->type() == ENTITY_POINT) {
746     // Rotate single point
747     std::shared_ptr<GeomDataAPI_Point2D> aSrcAttr =
748         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSource->baseAttribute());
749     std::shared_ptr<GeomDataAPI_Point2D> aDstAttr =
750         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aDest->baseAttribute());
751     if (aSrcAttr && aDstAttr) {
752       std::shared_ptr<GeomAPI_XY> aVec = aSrcAttr->pnt()->xy()->decreased(theCenter->xy());
753       double aNewX = aVec->x() * theCos - aVec->y() * theSin;
754       double aNewY = aVec->x() * theSin + aVec->y() * theCos;
755       aDstAttr->setValue(theCenter->x() + aNewX, theCenter->y() + aNewY);
756     }
757     return;
758   }
759
760   FeaturePtr aDestFeature = aDest->baseFeature();
761   if (aDestFeature)
762     aDestFeature->data()->blockSendAttributeUpdated(true);
763
764   // Rotate points of the feature
765   const std::list<EntityWrapperPtr>& aSrcSubs = theSource->subEntities();
766   const std::list<EntityWrapperPtr>& aDstSubs = theDest->subEntities();
767   std::list<EntityWrapperPtr>::const_iterator aSrcIt, aDstIt;
768   for (aSrcIt = aSrcSubs.begin(), aDstIt = aDstSubs.begin();
769        aSrcIt != aSrcSubs.end() && aDstIt != aDstSubs.end(); ++aSrcIt, ++aDstIt)
770     rotate(*aSrcIt, *aDstIt, theCenter, theSin, theCos);
771
772   if (aDestFeature)
773     aDestFeature->data()->blockSendAttributeUpdated(false);
774 }
775
776 static void translate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
777                       std::shared_ptr<GeomAPI_XY> theDelta)
778 {
779   std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSource =
780       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSource);
781   std::shared_ptr<SolveSpaceSolver_EntityWrapper> aDest =
782       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theDest);
783
784   if (theSource->type() == ENTITY_POINT) {
785     // Translate single point
786     std::shared_ptr<GeomDataAPI_Point2D> aSrcAttr =
787         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSource->baseAttribute());
788     std::shared_ptr<GeomDataAPI_Point2D> aDstAttr =
789         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aDest->baseAttribute());
790     if (aSrcAttr && aDstAttr)
791       aDstAttr->setValue(aSrcAttr->x() + theDelta->x(), aSrcAttr->y() + theDelta->y());
792     return;
793   }
794
795   FeaturePtr aDestFeature = aDest->baseFeature();
796   if (aDestFeature)
797     aDestFeature->data()->blockSendAttributeUpdated(true);
798
799   // Translate points of the feature
800   const std::list<EntityWrapperPtr>& aSrcSubs = theSource->subEntities();
801   const std::list<EntityWrapperPtr>& aDstSubs = theDest->subEntities();
802   std::list<EntityWrapperPtr>::const_iterator aSrcIt, aDstIt;
803   for (aSrcIt = aSrcSubs.begin(), aDstIt = aDstSubs.begin();
804        aSrcIt != aSrcSubs.end() && aDstIt != aDstSubs.end(); ++aSrcIt, ++aDstIt)
805     translate(*aSrcIt, *aDstIt, theDelta);
806
807   if (aDestFeature)
808     aDestFeature->data()->blockSendAttributeUpdated(false);
809 }
810
811 void adjustMultiRotation(ConstraintWrapperPtr theConstraint)
812 {
813   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
814
815   double anAngleRad = theConstraint->value() * PI / 180.0;
816   double aSin = sin(anAngleRad);
817   double aCos = cos(anAngleRad);
818
819   const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
820   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
821
822   std::shared_ptr<GeomAPI_Pnt2d> aCenter = aBuilder->point(*aSIt++);
823   std::list<EntityWrapperPtr>::const_iterator aPrevIt = aSIt++;
824   for (; aSIt != aSubs.end(); ++aPrevIt, ++aSIt)
825     rotate(*aPrevIt, *aSIt, aCenter, aSin, aCos);
826 }
827
828 void adjustMultiTranslation(ConstraintWrapperPtr theConstraint)
829 {
830   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
831
832   const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
833   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
834
835   std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aBuilder->point(*aSIt++);
836   std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aBuilder->point(*aSIt++);
837   std::shared_ptr<GeomAPI_XY> aDelta = aEndPnt->xy()->decreased(aStartPnt->xy());
838
839   bool isFullValue = theConstraint->isFullValue();
840   int aNbObjects = aSubs.size()-3;
841   if (isFullValue && aNbObjects > 0) {
842     aDelta->setX(aDelta->x()/aNbObjects);
843     aDelta->setY(aDelta->y()/aNbObjects);
844   }
845
846   std::list<EntityWrapperPtr>::const_iterator aPrevIt = aSIt++;
847   for (; aSIt != aSubs.end(); ++aPrevIt, ++aSIt)
848     translate(*aPrevIt, *aSIt, aDelta);
849 }