1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SolveSpaceSolver_Builder.cpp
4 // Created: 25 Mar 2015
5 // Author: Artem ZHIDKOV
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>
15 #include <SketchSolver_Manager.h>
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>
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_Circle.h>
28 #include <SketchPlugin_Line.h>
29 #include <SketchPlugin_Point.h>
30 #include <SketchPlugin_IntersectionPoint.h>
35 static EntityWrapperPtr createLine(FeaturePtr theFeature,
36 const std::list<EntityWrapperPtr>& theAttributes,
37 const GroupID& theGroupID,
38 const EntityID& theSketchID);
39 static EntityWrapperPtr createCircle(FeaturePtr theFeature,
40 const std::list<EntityWrapperPtr>& theAttributes,
41 const GroupID& theGroupID,
42 const EntityID& theSketchID);
43 static EntityWrapperPtr createArc(FeaturePtr theFeature,
44 const std::list<EntityWrapperPtr>& theAttributes,
45 const GroupID& theGroupID,
46 const EntityID& theSketchID);
48 /// \brief Set flags of constraint to identify which points are coincident in the Tangency
49 /// (for more information, see SolveSpace documentation)
50 static void adjustTangency(ConstraintWrapperPtr theConstraint);
51 /// \brief Set flags for angle constraint
52 static void adjustAngle(ConstraintWrapperPtr theConstraint);
53 /// \brief Update mirror points
54 static void adjustMirror(ConstraintWrapperPtr theConstraint);
55 /// \brief Update a sign of the point-line distance constraint
56 static void adjustPtLineDistance(ConstraintWrapperPtr theConstraint);
58 /// \brief Transform points to be symmetric regarding to the mirror line
59 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
60 EntityWrapperPtr theMirrored,
61 EntityWrapperPtr theMirrorLine);
65 // Initialization of constraint builder self pointer
66 BuilderPtr SolveSpaceSolver_Builder::mySelf = SolveSpaceSolver_Builder::getInstance();
68 BuilderPtr SolveSpaceSolver_Builder::getInstance()
71 mySelf = BuilderPtr(new SolveSpaceSolver_Builder);
72 SketchSolver_Manager::instance()->setBuilder(mySelf);
77 StoragePtr SolveSpaceSolver_Builder::createStorage(const GroupID& theGroup) const
79 return StoragePtr(new SolveSpaceSolver_Storage(theGroup));
82 SolverPtr SolveSpaceSolver_Builder::createSolver() const
84 return SolverPtr(new SolveSpaceSolver_Solver);
88 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createConstraint(
89 ConstraintPtr theConstraint,
90 const GroupID& theGroupID,
91 const EntityID& theSketchID,
92 const SketchSolver_ConstraintType& theType,
93 const double& theValue,
94 const EntityWrapperPtr& thePoint1,
95 const EntityWrapperPtr& thePoint2,
96 const EntityWrapperPtr& theEntity1,
97 const EntityWrapperPtr& theEntity2) const
99 if (theType == CONSTRAINT_SYMMETRIC)
100 return createMirror(theConstraint, theGroupID, theSketchID,
101 thePoint1, thePoint2, theEntity1);
102 else if (theType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
103 // replace by distance from center of circle to the line
104 const std::list<EntityWrapperPtr>& aSubs = theEntity1->subEntities();
105 EntityWrapperPtr aCenter = aSubs.front();
106 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
107 aSubs.back()->baseAttribute());
108 return createConstraint(theConstraint, theGroupID, theSketchID,
109 CONSTRAINT_PT_LINE_DISTANCE, aRadius->value(), aCenter, EntityWrapperPtr(), theEntity2);
111 else if (theType == CONSTRAINT_COLLINEAR) {
112 // replace by two constraints point-on-line
113 std::list<ConstraintWrapperPtr> aConstraints;
114 const std::list<EntityWrapperPtr>& aSubs1 = theEntity1->subEntities();
115 const std::list<EntityWrapperPtr>& aSubs2 = theEntity2->subEntities();
116 std::list<EntityWrapperPtr>::const_iterator anIt1, anIt2;
117 for (anIt2 = aSubs2.begin(); anIt2 != aSubs2.end(); ++anIt2) {
118 for (anIt1 = aSubs1.begin(); anIt1 != aSubs1.end(); ++anIt1)
119 if ((*anIt1)->id() == (*anIt2)->id())
121 if (anIt1 != aSubs1.end())
122 continue; // the lines have coincident point
124 std::list<ConstraintWrapperPtr> aC = createConstraint(theConstraint, theGroupID,
125 theSketchID, CONSTRAINT_PT_ON_LINE, theValue, *anIt2, EntityWrapperPtr(), theEntity1);
126 aConstraints.insert(aConstraints.end(), aC.begin(), aC.end());
131 int aType = ConstraintType::toSolveSpace(theType);
132 if (aType == SLVS_C_UNKNOWN)
133 return std::list<ConstraintWrapperPtr>();
135 Slvs_hEntity aSlvsEntities[4] = {SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN};
136 EntityWrapperPtr anOriginal[4] = {thePoint1, thePoint2, theEntity1, theEntity2};
137 std::list<EntityWrapperPtr> aConstrAttrList; // to be filled
138 for (int i = 0; i < 4; ++i) {
141 aSlvsEntities[i] = (Slvs_hEntity)anOriginal[i]->id();
142 if (aSlvsEntities[i] == SLVS_E_UNKNOWN)
143 return std::list<ConstraintWrapperPtr>(); // entity is not added into a storage, constraint can not be created
144 aConstrAttrList.push_back(anOriginal[i]);
147 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
148 SLVS_C_UNKNOWN, (Slvs_hGroup)theGroupID, aType, (Slvs_hEntity)theSketchID,
149 theValue, aSlvsEntities[0], aSlvsEntities[1], aSlvsEntities[2], aSlvsEntities[3]);
150 ConstraintWrapperPtr aResult(new SolveSpaceSolver_ConstraintWrapper(theConstraint, aConstraint));
151 aResult->setGroup(theGroupID);
152 aResult->setValue(theValue);
153 aResult->setEntities(aConstrAttrList);
154 adjustConstraint(aResult);
156 return std::list<ConstraintWrapperPtr>(1, aResult);
159 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createConstraint(
160 ConstraintPtr theConstraint,
161 const GroupID& theGroupID,
162 const EntityID& theSketchID,
163 const SketchSolver_ConstraintType& theType,
164 const double& theValue,
165 const bool theFullValue,
166 const EntityWrapperPtr& thePoint1,
167 const EntityWrapperPtr& thePoint2,
168 const std::list<EntityWrapperPtr>& theTrsfEnt) const
170 if (theType != CONSTRAINT_MULTI_ROTATION && theType != CONSTRAINT_MULTI_TRANSLATION)
171 return std::list<ConstraintWrapperPtr>();
173 int aType = ConstraintType::toSolveSpace(theType);
174 if (aType == SLVS_C_UNKNOWN)
175 return std::list<ConstraintWrapperPtr>();
177 Slvs_Constraint aConstraint =
178 Slvs_MakeConstraint(SLVS_C_UNKNOWN, (Slvs_hGroup)theGroupID, aType, (Slvs_hEntity)theSketchID,
179 theValue, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
181 std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
183 aConstrAttrList.push_front(thePoint2);
184 aConstrAttrList.push_front(thePoint1);
186 ConstraintWrapperPtr aResult(new SolveSpaceSolver_ConstraintWrapper(theConstraint, aConstraint));
187 aResult->setGroup(theGroupID);
188 aResult->setValue(theValue);
189 aResult->setIsFullValue(theFullValue);
190 aResult->setEntities(aConstrAttrList);
191 return std::list<ConstraintWrapperPtr>(1, aResult);
195 std::list<ConstraintWrapperPtr> SolveSpaceSolver_Builder::createMirror(
196 ConstraintPtr theConstraint,
197 const GroupID& theGroupID,
198 const EntityID& theSketchID,
199 const EntityWrapperPtr& theEntity1,
200 const EntityWrapperPtr& theEntity2,
201 const EntityWrapperPtr& theMirrorLine) const
203 Slvs_Constraint aConstraint;
204 std::list<ConstraintWrapperPtr> aResult;
205 std::list<EntityWrapperPtr> aConstrAttrList;
206 if (theEntity1->type() == ENTITY_POINT) {
207 if (theEntity2->group() == theGroupID) // theEntity2 is not fixed
208 makeMirrorPoints(theEntity1, theEntity2, theMirrorLine);
210 aConstraint = Slvs_MakeConstraint(
211 SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID, SLVS_C_SYMMETRIC_LINE, (Slvs_hEntity)theSketchID,
212 0.0, (Slvs_hEntity)theEntity1->id(), (Slvs_hEntity)theEntity2->id(),
213 (Slvs_hEntity)theMirrorLine->id(), SLVS_E_UNKNOWN);
215 aConstrAttrList.push_back(theEntity1);
216 aConstrAttrList.push_back(theEntity2);
217 aConstrAttrList.push_back(theMirrorLine);
219 ConstraintWrapperPtr aWrapper(new SolveSpaceSolver_ConstraintWrapper(
220 theConstraint, aConstraint));
221 aWrapper->setGroup(theGroupID);
222 aWrapper->setEntities(aConstrAttrList);
223 aResult.push_back(aWrapper);
225 else if (theEntity1->type() == ENTITY_LINE) {
226 const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
227 const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
228 std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
229 std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
230 for (; anIt1 != aPoints1.end() && anIt2 != aPoints2.end(); ++anIt1, ++anIt2) {
231 std::list<ConstraintWrapperPtr> aMrrList =
232 createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
233 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
236 else if (theEntity1->type() == ENTITY_CIRCLE) {
237 const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
238 std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
239 for (; anIt1 != aPoints1.end(); ++anIt1)
240 if ((*anIt1)->type() == ENTITY_POINT)
242 const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
243 std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
244 for (; anIt2 != aPoints2.end(); ++anIt2)
245 if ((*anIt2)->type() == ENTITY_POINT)
248 std::list<ConstraintWrapperPtr> aMrrList =
249 createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
250 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
252 // Additional constraint for equal radii
253 aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
254 0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
255 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
257 else if (theEntity1->type() == ENTITY_ARC) {
258 // Do not allow mirrored arc recalculate its position until coordinated of all points recalculated
259 FeaturePtr aMirrArc = theEntity2->baseFeature();
260 aMirrArc->data()->blockSendAttributeUpdated(true);
262 std::list<ConstraintWrapperPtr> aMrrList;
263 std::list<EntityWrapperPtr>::const_iterator anIt1 = theEntity1->subEntities().begin();
264 std::list<EntityWrapperPtr>::const_iterator anIt2 = theEntity2->subEntities().begin();
265 if ((*anIt2)->group() == theGroupID) // mirrored point is not fixed
266 makeMirrorPoints(*anIt1, *anIt2, theMirrorLine);
268 // Workaround to avoid problems in SolveSpace.
269 // The symmetry of two arcs will be done using symmetry of three points on these arcs:
270 // start point, end point, and any other point on the arc
271 std::list<EntityWrapperPtr> aBaseArcPoints(++anIt1, theEntity1->subEntities().end());
272 std::list<EntityWrapperPtr> aMirrorArcPoints(++anIt2, theEntity2->subEntities().end());
273 // indices of points of arc, center corresponds center, first point corresponds last point
274 aMirrorArcPoints.reverse();
276 anIt1 = aBaseArcPoints.begin();
277 anIt2 = aMirrorArcPoints.begin();
278 for (; anIt1 != aBaseArcPoints.end(); ++anIt1, ++anIt2) {
279 aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
280 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
282 // Restore event sending
283 aMirrArc->data()->blockSendAttributeUpdated(false);
288 void SolveSpaceSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
290 SketchSolver_ConstraintType aType = theConstraint->type();
291 // Update flags in constraints
292 if (aType == CONSTRAINT_TANGENT_ARC_ARC || aType == CONSTRAINT_TANGENT_ARC_LINE)
293 adjustTangency(theConstraint);
294 else if (aType == CONSTRAINT_ANGLE)
295 adjustAngle(theConstraint);
296 else if (aType == CONSTRAINT_SYMMETRIC)
297 adjustMirror(theConstraint);
298 else if (aType == CONSTRAINT_PT_LINE_DISTANCE)
299 adjustPtLineDistance(theConstraint);
302 EntityWrapperPtr SolveSpaceSolver_Builder::createFeature(
303 FeaturePtr theFeature,
304 const std::list<EntityWrapperPtr>& theAttributes,
305 const GroupID& theGroupID,
306 const EntityID& theSketchID) const
308 static EntityWrapperPtr aDummy;
309 if (!theFeature->data()->isValid())
313 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
315 return createSketchEntity(aSketch, theGroupID);
317 // SketchPlugin features
318 std::shared_ptr<SketchPlugin_Feature> aFeature =
319 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
323 // Verify the feature by its kind
324 const std::string& aFeatureKind = aFeature->getKind();
326 if (aFeatureKind == SketchPlugin_Line::ID())
327 return createLine(theFeature, theAttributes, theGroupID, theSketchID);
329 else if (aFeatureKind == SketchPlugin_Circle::ID())
330 return createCircle(theFeature, theAttributes, theGroupID, theSketchID);
332 else if (aFeatureKind == SketchPlugin_Arc::ID())
333 return createArc(theFeature, theAttributes, theGroupID, theSketchID);
334 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
335 else if (aFeatureKind == SketchPlugin_Point::ID() ||
336 aFeatureKind == SketchPlugin_IntersectionPoint::ID()) {
337 AttributePtr aPoint = theFeature->attribute(SketchPlugin_Point::COORD_ID());
338 if (!aPoint->isInitialized())
340 EntityWrapperPtr aSub = theAttributes.empty() ? createAttribute(aPoint, theGroupID, theSketchID) :
341 theAttributes.front();
345 const Slvs_Entity& aSubEnt =
346 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(aSub)->entity();
347 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(theFeature, aPoint, aSubEnt));
348 aResult->setSubEntities(theAttributes);
356 EntityWrapperPtr SolveSpaceSolver_Builder::createAttribute(
357 AttributePtr theAttribute,
358 const GroupID& theGroupID,
359 const EntityID& theSketchID) const
361 AttributePtr anAttribute = theAttribute;
362 AttributeRefAttrPtr aRefAttr =
363 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
365 if (aRefAttr->isObject()) {
366 // do not create features here
367 return EntityWrapperPtr();
369 anAttribute = aRefAttr->attr();
372 std::list<ParameterWrapperPtr> aParameters;
373 Slvs_Entity anEntity;
377 std::shared_ptr<GeomDataAPI_Point> aPoint =
378 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
380 aParameters.push_back(createParameter(theGroupID, aPoint->x(), !aPoint->textX().empty()));
381 aParameters.push_back(createParameter(theGroupID, aPoint->y(), !aPoint->textY().empty()));
382 aParameters.push_back(createParameter(theGroupID, aPoint->z(), !aPoint->textZ().empty()));
383 // Create entity (parameters are not filled)
384 anEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
385 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
388 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
389 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
391 aParameters.push_back(createParameter(theGroupID, aPoint2D->x(), !aPoint2D->textX().empty()));
392 aParameters.push_back(createParameter(theGroupID, aPoint2D->y(), !aPoint2D->textY().empty()));
393 // Create entity (parameters are not filled)
394 anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
395 (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
397 // Scalar value (used for the distance entities)
398 AttributeDoublePtr aScalar =
399 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
401 aParameters.push_back(createParameter(theGroupID, aScalar->value(), !aScalar->text().empty()));
402 // Create entity (parameter is not filled)
403 anEntity = Slvs_MakeDistance(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
404 (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN);
409 if (anEntity.type == 0) {
410 // unknown attribute type
411 return EntityWrapperPtr();
414 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(theAttribute, anEntity));
415 aResult->setParameters(aParameters);
421 EntityWrapperPtr SolveSpaceSolver_Builder::createSketchEntity(
422 CompositeFeaturePtr theSketch,
423 const GroupID& theGroupID) const
425 DataPtr aSketchData = theSketch->data();
426 if (!aSketchData || !aSketchData->isValid())
427 return EntityWrapperPtr(); // the sketch is incorrect
429 // Get parameters of workplane
430 AttributePtr aDirX = aSketchData->attribute(SketchPlugin_Sketch::DIRX_ID());
431 AttributePtr aNorm = aSketchData->attribute(SketchPlugin_Sketch::NORM_ID());
432 AttributePtr anOrigin = aSketchData->attribute(SketchPlugin_Sketch::ORIGIN_ID());
433 if (!anOrigin->isInitialized() || !aNorm->isInitialized() || !aDirX->isInitialized())
434 return EntityWrapperPtr();
436 EntityWrapperPtr aNewEnt;
437 std::list<EntityWrapperPtr> aSubs;
439 // Create SolveSpace entity corresponding to the sketch origin
440 aNewEnt = createAttribute(anOrigin, theGroupID);
442 return EntityWrapperPtr();
443 aSubs.push_back(aNewEnt);
445 // Create SolveSpace entity corresponding the the sketch normal
446 aNewEnt = createNormal(aNorm, aDirX, theGroupID);
448 return EntityWrapperPtr();
449 aSubs.push_back(aNewEnt);
452 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
453 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
455 aNewEnt = EntityWrapperPtr(
456 new SolveSpaceSolver_EntityWrapper(FeaturePtr(theSketch), aWorkplane));
457 aNewEnt->setSubEntities(aSubs);
461 EntityWrapperPtr SolveSpaceSolver_Builder::createNormal(
462 AttributePtr theNormal,
463 AttributePtr theDirX,
464 const GroupID& theGroupID) const
466 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
467 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
468 if (!aDirX || !aNorm ||
469 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
470 !aNorm->isInitialized())
471 return EntityWrapperPtr();
472 // calculate Y direction
473 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
475 // quaternion parameters of normal vector
476 double qw, qx, qy, qz;
477 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
479 double aNormCoord[4] = { qw, qx, qy, qz };
481 // Create parameters of the normal
482 std::list<ParameterWrapperPtr> aParameters;
483 for (int i = 0; i < 4; i++)
484 aParameters.push_back(createParameter(theGroupID, aNormCoord[i]));
486 // Create a normal with empty parameters
487 Slvs_Entity aNormalEnt = Slvs_MakeNormal3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
488 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
489 EntityWrapperPtr aNormal(new SolveSpaceSolver_EntityWrapper(theNormal, aNormalEnt));
490 aNormal->setParameters(aParameters);
494 ParameterWrapperPtr SolveSpaceSolver_Builder::createParameter(
495 const GroupID& theGroup, const double theValue, const bool theExpr) const
497 Slvs_Param aParam = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroup, theValue);
498 ParameterWrapperPtr aWrapper(new SolveSpaceSolver_ParameterWrapper(aParam));
499 aWrapper->setIsParametric(theExpr);
507 // ================ Auxiliary functions ==========================
508 EntityWrapperPtr createLine(FeaturePtr theFeature,
509 const std::list<EntityWrapperPtr>& theAttributes,
510 const GroupID& theGroupID,
511 const EntityID& theSketchID)
513 EntityWrapperPtr aNewEntity;
514 std::list<EntityWrapperPtr> aSubs;
516 AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
517 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
518 if (!aStart->isInitialized() || !aEnd->isInitialized())
521 EntityWrapperPtr aStartEnt, aEndEnt;
522 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
523 for (; anIt != theAttributes.end(); ++anIt) {
524 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
525 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
526 if (aSlvsEntity->isBase(aStart))
527 aStartEnt = aSlvsEntity;
528 else if (aSlvsEntity->isBase(aEnd))
529 aEndEnt = aSlvsEntity;
531 if (!aStartEnt || !aEndEnt)
534 aSubs.push_back(aStartEnt);
535 aSubs.push_back(aEndEnt);
536 Slvs_Entity anEntity = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
537 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
539 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
540 aNewEntity->setSubEntities(aSubs);
544 EntityWrapperPtr createCircle(FeaturePtr theFeature,
545 const std::list<EntityWrapperPtr>& theAttributes,
546 const GroupID& theGroupID,
547 const EntityID& theSketchID)
549 EntityWrapperPtr aNewEntity;
550 std::list<EntityWrapperPtr> aSubs;
552 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
553 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
554 if (!aCenter->isInitialized() || !aRadius->isInitialized())
557 EntityWrapperPtr aCenterEnt, aRadiusEnt, aNormalEnt;
558 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
559 for (; anIt != theAttributes.end(); ++anIt) {
560 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
561 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
562 if (aSlvsEntity->isBase(aCenter))
563 aCenterEnt = aSlvsEntity;
564 else if (aSlvsEntity->isBase(aRadius))
565 aRadiusEnt = aSlvsEntity;
566 else if (aSlvsEntity->type() == ENTITY_NORMAL)
567 aNormalEnt = aSlvsEntity;
569 if (!aCenterEnt || !aRadiusEnt || !aNormalEnt)
572 aSubs.push_back(aCenterEnt);
573 aSubs.push_back(aRadiusEnt);
574 Slvs_Entity anEntity = Slvs_MakeCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
575 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aCenterEnt->id(),
576 (Slvs_hEntity)aNormalEnt->id(), (Slvs_hEntity)aRadiusEnt->id());
578 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
579 aNewEntity->setSubEntities(aSubs);
583 EntityWrapperPtr createArc(FeaturePtr theFeature,
584 const std::list<EntityWrapperPtr>& theAttributes,
585 const GroupID& theGroupID,
586 const EntityID& theSketchID)
588 EntityWrapperPtr aNewEntity;
589 std::list<EntityWrapperPtr> aSubs;
591 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Arc::CENTER_ID());
592 AttributePtr aStart = theFeature->attribute(SketchPlugin_Arc::START_ID());
593 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Arc::END_ID());
594 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
597 EntityWrapperPtr aCenterEnt, aStartEnt, aEndEnt, aNormalEnt;
598 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
599 for (; anIt != theAttributes.end(); ++anIt) {
600 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
601 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
602 if (aSlvsEntity->isBase(aCenter))
603 aCenterEnt = aSlvsEntity;
604 else if (aSlvsEntity->isBase(aStart))
605 aStartEnt = aSlvsEntity;
606 else if (aSlvsEntity->isBase(aEnd))
607 aEndEnt = aSlvsEntity;
608 else if (aSlvsEntity->type() == ENTITY_NORMAL)
609 aNormalEnt = aSlvsEntity;
611 if (!aCenterEnt || !aStartEnt || !aEndEnt || !aNormalEnt)
614 aSubs.push_back(aCenterEnt);
615 aSubs.push_back(aStartEnt);
616 aSubs.push_back(aEndEnt);
617 Slvs_Entity anEntity = Slvs_MakeArcOfCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
618 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aNormalEnt->id(),
619 (Slvs_hEntity)aCenterEnt->id(), (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
621 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
622 aNewEntity->setSubEntities(aSubs);
627 void adjustTangency(ConstraintWrapperPtr theConstraint)
629 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
631 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
632 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
634 // Collect start, end points of entities
635 std::shared_ptr<GeomAPI_Pnt2d> aStartEntPoints[2][2];
636 bool isCoinc[2][2] = {false};
637 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
638 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
639 for (int i = 0; aSIt != aSubs.end(); ++aSIt, ++i) {
640 const std::list<EntityWrapperPtr>& aPoints = (*aSIt)->subEntities();
641 std::list<EntityWrapperPtr>::const_iterator aPIt = aPoints.begin();
642 if ((*aSIt)->type() == ENTITY_ARC)
644 for (int j = 0; aPIt != aPoints.end(); ++aPIt, ++j) {
645 aStartEntPoints[i][j] = aBuilder->point(*aPIt);
646 if (i > 0) { // check coincidence
647 for (int k = 0; k < 2; ++k)
648 if (aStartEntPoints[i][j]->distance(aStartEntPoints[0][k]) < tolerance)
649 isCoinc[0][k] = isCoinc[i][j] = true;
654 Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
655 if (isCoinc[0][0] == false && isCoinc[0][1] == true)
656 aSlvsConstraint.other = 1;
657 else aSlvsConstraint.other = 0;
658 if (isCoinc[1][0] == false && isCoinc[1][1] == true)
659 aSlvsConstraint.other2 = 1;
660 else aSlvsConstraint.other2 = 0;
663 void adjustAngle(ConstraintWrapperPtr theConstraint)
665 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
667 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
668 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
671 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
672 const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
673 std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
674 for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
675 const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
676 std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
677 for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt) {
678 isFixed[i][j] = ((*aLPIt)->group() != theConstraint->group());
679 aPoints[i][j] = aBuilder->point(*aLPIt);
683 if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
684 return; // both lines are fixed => no need to update them
686 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
687 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
688 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
690 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
694 for (int i = 0; i < 2; i++) {
695 for (int j = 0; j < 2; j++) {
696 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
697 if (fabs(aDist[i][j]) <= tolerance)
700 if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
701 aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
702 // the intersection point is an inner point of the line,
703 // we change the sign of distance till start point to calculate correct coordinates
708 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
709 for (int i = 0; i < 2; i++) {
710 if (aDist[i][1] > fabs(aDist[i][0]))
711 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
712 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
714 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
715 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
716 // main direction is opposite => change signs
717 if (aDist[i][0] < 0.0) {
724 Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
725 aSlvsConstraint.other = false;
726 for (int i = 0; i < 2; i++)
727 if (aLine[i]->direction()->dot(aDir[i]) < 0.0)
728 aSlvsConstraint.other = !aSlvsConstraint.other;
730 // Recalculate positions of lines to avoid conflicting constraints
731 // while changing angle value several times
732 double cosA = cos(aConstraint->value() * PI / 180.0);
733 double sinA = sin(aConstraint->value() * PI / 180.0);
734 //if (aDir[0]->cross(aDir[1]) < 0.0)
737 if (isFixed[1][0] && isFixed[1][1]) {
741 double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
742 double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
744 std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
745 for (int i = 0; i < 2; i++) {
746 aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
747 new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
748 anIntersection->y() + y * aDist[aLineToUpd][i]));
751 std::shared_ptr<GeomAPI_XY> aDelta;
752 if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
753 aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
754 else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
755 aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
757 for (int i = 0; i < 2; i++) {
758 aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
759 aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
763 // Update positions of points
764 std::list<EntityWrapperPtr>::const_iterator anUpdLine = aConstrLines.begin();
765 if (aLineToUpd > 0) ++anUpdLine;
766 const std::list<EntityWrapperPtr>& anUpdPoints = (*anUpdLine)->subEntities();
767 std::list<EntityWrapperPtr>::const_iterator aPIt = anUpdPoints.begin();
768 for (int i = 0; aPIt != anUpdPoints.end(); ++aPIt, ++i) {
769 std::shared_ptr<GeomDataAPI_Point2D> aPnt2D =
770 std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aPIt)->baseAttribute());
771 aPnt2D->setValue(aNewPoints[i]);
775 void adjustMirror(ConstraintWrapperPtr theConstraint)
777 std::vector<EntityWrapperPtr> aPoints;
778 EntityWrapperPtr aMirrorLine;
780 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
781 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
782 for (; anIt != aSubs.end(); ++anIt) {
783 if ((*anIt)->type() == ENTITY_POINT)
784 aPoints.push_back(*anIt);
785 else if ((*anIt)->type() == ENTITY_LINE)
789 makeMirrorPoints(aPoints[0], aPoints[1], aMirrorLine);
792 void makeMirrorPoints(EntityWrapperPtr theOriginal,
793 EntityWrapperPtr theMirrored,
794 EntityWrapperPtr theMirrorLine)
796 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
798 std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
799 std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
800 // orthogonal direction
801 aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
803 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
804 std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
805 double aDist = aVec->dot(aMLDir->xy());
806 aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
807 double aCoord[2] = {aVec->x(), aVec->y()};
808 std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
809 for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
810 (*aMIt)->setValue(aCoord[i]);
812 // update corresponding attribute
813 AttributePtr anAttr = std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMirrored)->baseAttribute();
815 std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
816 aMirroredPnt->setValue(aCoord[0], aCoord[1]);
820 void adjustPtLineDistance(ConstraintWrapperPtr theConstraint)
822 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
824 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
825 std::shared_ptr<GeomAPI_Lin2d> aLine;
826 std::list<EntityWrapperPtr> aSubs = theConstraint->entities();
827 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
828 for (; aSIt != aSubs.end(); ++aSIt) {
829 if ((*aSIt)->type() == ENTITY_POINT)
830 aPoint = aBuilder->point(*aSIt);
831 else if ((*aSIt)->type() == ENTITY_LINE)
832 aLine = aBuilder->line(*aSIt);
835 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
836 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
837 if (aPtLineVec->cross(aLineVec) * theConstraint->value() < 0.0)
838 theConstraint->setValue(theConstraint->value() * (-1.0));