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>
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);
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);
59 /// \brief Transform points to be symmetric regarding to the mirror line
60 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
61 EntityWrapperPtr theMirrored,
62 EntityWrapperPtr theMirrorLine);
66 // Initialization of constraint builder self pointer
67 BuilderPtr SolveSpaceSolver_Builder::mySelf = SolveSpaceSolver_Builder::getInstance();
69 BuilderPtr SolveSpaceSolver_Builder::getInstance()
72 mySelf = BuilderPtr(new SolveSpaceSolver_Builder);
73 SketchSolver_Manager::instance()->setBuilder(mySelf);
78 StoragePtr SolveSpaceSolver_Builder::createStorage(const GroupID& theGroup) const
80 return StoragePtr(new SolveSpaceSolver_Storage(theGroup));
83 SolverPtr SolveSpaceSolver_Builder::createSolver() const
85 return SolverPtr(new SolveSpaceSolver_Solver);
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
100 if (theType == CONSTRAINT_SYMMETRIC)
101 return createMirror(theConstraint, theGroupID, theSketchID,
102 thePoint1, thePoint2, theEntity1);
104 int aType = ConstraintType::toSolveSpace(theType);
105 if (aType == SLVS_C_UNKNOWN)
106 return std::list<ConstraintWrapperPtr>();
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) {
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]);
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);
128 return std::list<ConstraintWrapperPtr>(1, aResult);
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
141 if (theType != CONSTRAINT_MULTI_ROTATION && theType != CONSTRAINT_MULTI_TRANSLATION)
142 return std::list<ConstraintWrapperPtr>();
144 int aType = ConstraintType::toSolveSpace(theType);
145 if (aType == SLVS_C_UNKNOWN)
146 return std::list<ConstraintWrapperPtr>();
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);
152 std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
154 aConstrAttrList.push_front(thePoint2);
155 aConstrAttrList.push_front(thePoint1);
157 ConstraintWrapperPtr aResult(new SolveSpaceSolver_ConstraintWrapper(theConstraint, aConstraint));
158 aResult->setValue(theValue);
159 aResult->setEntities(aConstrAttrList);
160 return std::list<ConstraintWrapperPtr>(1, aResult);
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
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);
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);
184 aConstrAttrList.push_back(theEntity1);
185 aConstrAttrList.push_back(theEntity2);
186 aConstrAttrList.push_back(theMirrorLine);
188 ConstraintWrapperPtr aWrapper(new SolveSpaceSolver_ConstraintWrapper(
189 theConstraint, aConstraint));
190 aWrapper->setEntities(aConstrAttrList);
191 aResult.push_back(aWrapper);
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());
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)
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)
216 std::list<ConstraintWrapperPtr> aMrrList =
217 createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
218 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
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());
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);
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(theEntity1->subEntities().front(),
235 theEntity2->subEntities().front(), theMirrorLine);
237 // Workaround to avoid problems in SolveSpace.
238 // The symmetry of two arcs will be done using symmetry of three points on these arcs:
239 // start point, end point, and any other point on the arc
240 std::list<EntityWrapperPtr> aBaseArcPoints(++anIt1, theEntity1->subEntities().end());
241 std::list<EntityWrapperPtr> aMirrorArcPoints(++anIt2, theEntity2->subEntities().end());
242 // indices of points of arc, center corresponds center, first point corresponds last point
243 aMirrorArcPoints.reverse();
245 anIt1 = aBaseArcPoints.begin();
246 anIt2 = aMirrorArcPoints.begin();
247 for (; anIt1 != aBaseArcPoints.end(); ++anIt1, ++anIt2) {
248 aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
249 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
251 // Restore event sending
252 aMirrArc->data()->blockSendAttributeUpdated(false);
257 void SolveSpaceSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
259 SketchSolver_ConstraintType aType = theConstraint->type();
260 // Update flags in constraints
261 if (aType == CONSTRAINT_TANGENT_ARC_ARC || aType == CONSTRAINT_TANGENT_ARC_LINE)
262 adjustTangency(theConstraint);
263 else if (aType == CONSTRAINT_ANGLE)
264 adjustAngle(theConstraint);
265 else if (aType == CONSTRAINT_SYMMETRIC)
266 adjustMirror(theConstraint);
267 else if (aType == CONSTRAINT_MULTI_ROTATION)
268 adjustMultiRotation(theConstraint);
269 else if (aType == CONSTRAINT_MULTI_TRANSLATION)
270 adjustMultiTranslation(theConstraint);
273 EntityWrapperPtr SolveSpaceSolver_Builder::createFeature(
274 FeaturePtr theFeature,
275 const std::list<EntityWrapperPtr>& theAttributes,
276 const GroupID& theGroupID,
277 const EntityID& theSketchID) const
279 static EntityWrapperPtr aDummy;
280 if (!theFeature->data()->isValid())
284 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
286 return createSketchEntity(aSketch, theGroupID);
288 // SketchPlugin features
289 std::shared_ptr<SketchPlugin_Feature> aFeature =
290 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
294 // Verify the feature by its kind
295 const std::string& aFeatureKind = aFeature->getKind();
297 if (aFeatureKind == SketchPlugin_Line::ID())
298 return createLine(theFeature, theAttributes, theGroupID, theSketchID);
300 else if (aFeatureKind == SketchPlugin_Circle::ID())
301 return createCircle(theFeature, theAttributes,theGroupID, theSketchID);
303 else if (aFeatureKind == SketchPlugin_Arc::ID())
304 return createArc(theFeature, theAttributes,theGroupID, theSketchID);
305 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
306 else if (aFeatureKind == SketchPlugin_Point::ID()) {
307 AttributePtr aPoint = theFeature->attribute(SketchPlugin_Point::COORD_ID());
308 if (!aPoint->isInitialized())
310 EntityWrapperPtr aSub = createAttribute(aPoint, theGroupID, theSketchID);
314 std::shared_ptr<SketchPlugin_Point> aPointFeature =
315 std::dynamic_pointer_cast<SketchPlugin_Point>(theFeature);
317 const Slvs_Entity& aSubEnt =
318 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(aSub)->entity();
319 return EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(aPointFeature, aPoint, aSubEnt));
326 EntityWrapperPtr SolveSpaceSolver_Builder::createAttribute(
327 AttributePtr theAttribute,
328 const GroupID& theGroupID,
329 const EntityID& theSketchID) const
331 AttributePtr anAttribute = theAttribute;
332 AttributeRefAttrPtr aRefAttr =
333 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
335 if (aRefAttr->isObject()) {
336 // do not create features here
337 return EntityWrapperPtr();
339 anAttribute = aRefAttr->attr();
342 std::list<ParameterWrapperPtr> aParameters;
343 Slvs_Entity anEntity;
347 std::shared_ptr<GeomDataAPI_Point> aPoint =
348 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
350 aParameters.push_back(createParameter(theGroupID, aPoint->x(), !aPoint->textX().empty()));
351 aParameters.push_back(createParameter(theGroupID, aPoint->y(), !aPoint->textY().empty()));
352 aParameters.push_back(createParameter(theGroupID, aPoint->z(), !aPoint->textZ().empty()));
353 // Create entity (parameters are not filled)
354 anEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
355 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
358 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
359 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
361 aParameters.push_back(createParameter(theGroupID, aPoint2D->x(), !aPoint2D->textX().empty()));
362 aParameters.push_back(createParameter(theGroupID, aPoint2D->y(), !aPoint2D->textY().empty()));
363 // Create entity (parameters are not filled)
364 anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
365 (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
367 // Scalar value (used for the distance entities)
368 AttributeDoublePtr aScalar =
369 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
371 aParameters.push_back(createParameter(theGroupID, aScalar->value(), !aScalar->text().empty()));
372 // Create entity (parameter is not filled)
373 anEntity = Slvs_MakeDistance(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
374 (Slvs_hEntity)theSketchID, SLVS_E_UNKNOWN);
379 if (anEntity.type == 0) {
380 // unknown attribute type
381 return EntityWrapperPtr();
384 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(theAttribute, anEntity));
385 aResult->setParameters(aParameters);
391 EntityWrapperPtr SolveSpaceSolver_Builder::createSketchEntity(
392 CompositeFeaturePtr theSketch,
393 const GroupID& theGroupID) const
395 DataPtr aSketchData = theSketch->data();
396 if (!aSketchData || !aSketchData->isValid())
397 return EntityWrapperPtr(); // the sketch is incorrect
399 // Get parameters of workplane
400 AttributePtr aDirX = aSketchData->attribute(SketchPlugin_Sketch::DIRX_ID());
401 AttributePtr aNorm = aSketchData->attribute(SketchPlugin_Sketch::NORM_ID());
402 AttributePtr anOrigin = aSketchData->attribute(SketchPlugin_Sketch::ORIGIN_ID());
403 if (!anOrigin->isInitialized() || !aNorm->isInitialized() || !aDirX->isInitialized())
404 return EntityWrapperPtr();
406 EntityWrapperPtr aNewEnt;
407 std::list<EntityWrapperPtr> aSubs;
409 // Create SolveSpace entity corresponding to the sketch origin
410 aNewEnt = createAttribute(anOrigin, theGroupID);
412 return EntityWrapperPtr();
413 aSubs.push_back(aNewEnt);
415 // Create SolveSpace entity corresponding the the sketch normal
416 aNewEnt = createNormal(aNorm, aDirX, theGroupID);
418 return EntityWrapperPtr();
419 aSubs.push_back(aNewEnt);
422 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
423 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
425 aNewEnt = EntityWrapperPtr(
426 new SolveSpaceSolver_EntityWrapper(FeaturePtr(theSketch), aWorkplane));
427 aNewEnt->setSubEntities(aSubs);
431 EntityWrapperPtr SolveSpaceSolver_Builder::createNormal(
432 AttributePtr theNormal,
433 AttributePtr theDirX,
434 const GroupID& theGroupID) const
436 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theNormal);
437 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
438 if (!aDirX || !aNorm ||
439 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
440 !aNorm->isInitialized())
441 return EntityWrapperPtr();
442 // calculate Y direction
443 std::shared_ptr<GeomAPI_Dir> aDirY(new GeomAPI_Dir(aNorm->dir()->cross(aDirX->dir())));
445 // quaternion parameters of normal vector
446 double qw, qx, qy, qz;
447 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
449 double aNormCoord[4] = { qw, qx, qy, qz };
451 // Create parameters of the normal
452 std::list<ParameterWrapperPtr> aParameters;
453 for (int i = 0; i < 4; i++)
454 aParameters.push_back(createParameter(theGroupID, aNormCoord[i]));
456 // Create a normal with empty parameters
457 Slvs_Entity aNormalEnt = Slvs_MakeNormal3d(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
458 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
459 EntityWrapperPtr aNormal(new SolveSpaceSolver_EntityWrapper(theNormal, aNormalEnt));
460 aNormal->setParameters(aParameters);
464 ParameterWrapperPtr SolveSpaceSolver_Builder::createParameter(
465 const GroupID& theGroup, const double theValue, const bool theExpr) const
467 Slvs_Param aParam = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroup, theValue);
468 ParameterWrapperPtr aWrapper(new SolveSpaceSolver_ParameterWrapper(aParam));
469 aWrapper->setIsParametric(theExpr);
477 // ================ Auxiliary functions ==========================
478 EntityWrapperPtr createLine(FeaturePtr theFeature,
479 const std::list<EntityWrapperPtr>& theAttributes,
480 const GroupID& theGroupID,
481 const EntityID& theSketchID)
483 EntityWrapperPtr aNewEntity;
484 std::list<EntityWrapperPtr> aSubs;
486 AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
487 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
488 if (!aStart->isInitialized() || !aEnd->isInitialized())
491 EntityWrapperPtr aStartEnt, aEndEnt;
492 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
493 for (; anIt != theAttributes.end(); ++anIt) {
494 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
495 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
496 if (aSlvsEntity->isBase(aStart))
497 aStartEnt = aSlvsEntity;
498 else if (aSlvsEntity->isBase(aEnd))
499 aEndEnt = aSlvsEntity;
501 if (!aStartEnt || !aEndEnt)
504 aSubs.push_back(aStartEnt);
505 aSubs.push_back(aEndEnt);
506 Slvs_Entity anEntity = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
507 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
509 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
510 aNewEntity->setSubEntities(aSubs);
514 EntityWrapperPtr createCircle(FeaturePtr theFeature,
515 const std::list<EntityWrapperPtr>& theAttributes,
516 const GroupID& theGroupID,
517 const EntityID& theSketchID)
519 EntityWrapperPtr aNewEntity;
520 std::list<EntityWrapperPtr> aSubs;
522 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
523 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
524 if (!aCenter->isInitialized() || !aRadius->isInitialized())
527 EntityWrapperPtr aCenterEnt, aRadiusEnt, aNormalEnt;
528 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
529 for (; anIt != theAttributes.end(); ++anIt) {
530 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
531 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
532 if (aSlvsEntity->isBase(aCenter))
533 aCenterEnt = aSlvsEntity;
534 else if (aSlvsEntity->isBase(aRadius))
535 aRadiusEnt = aSlvsEntity;
536 else if (aSlvsEntity->type() == ENTITY_NORMAL)
537 aNormalEnt = aSlvsEntity;
539 if (!aCenterEnt || !aRadiusEnt || !aNormalEnt)
542 aSubs.push_back(aCenterEnt);
543 aSubs.push_back(aRadiusEnt);
544 Slvs_Entity anEntity = Slvs_MakeCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
545 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aCenterEnt->id(),
546 (Slvs_hEntity)aNormalEnt->id(), (Slvs_hEntity)aRadiusEnt->id());
548 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
549 aNewEntity->setSubEntities(aSubs);
553 EntityWrapperPtr createArc(FeaturePtr theFeature,
554 const std::list<EntityWrapperPtr>& theAttributes,
555 const GroupID& theGroupID,
556 const EntityID& theSketchID)
558 EntityWrapperPtr aNewEntity;
559 std::list<EntityWrapperPtr> aSubs;
561 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Arc::CENTER_ID());
562 AttributePtr aStart = theFeature->attribute(SketchPlugin_Arc::START_ID());
563 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Arc::END_ID());
564 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
567 EntityWrapperPtr aCenterEnt, aStartEnt, aEndEnt, aNormalEnt;
568 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
569 for (; anIt != theAttributes.end(); ++anIt) {
570 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSlvsEntity =
571 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
572 if (aSlvsEntity->isBase(aCenter))
573 aCenterEnt = aSlvsEntity;
574 else if (aSlvsEntity->isBase(aStart))
575 aStartEnt = aSlvsEntity;
576 else if (aSlvsEntity->isBase(aEnd))
577 aEndEnt = aSlvsEntity;
578 else if (aSlvsEntity->type() == ENTITY_NORMAL)
579 aNormalEnt = aSlvsEntity;
581 if (!aCenterEnt || !aStartEnt || !aEndEnt || !aNormalEnt)
584 aSubs.push_back(aCenterEnt);
585 aSubs.push_back(aStartEnt);
586 aSubs.push_back(aEndEnt);
587 Slvs_Entity anEntity = Slvs_MakeArcOfCircle(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
588 (Slvs_hEntity)theSketchID, (Slvs_hEntity)aNormalEnt->id(),
589 (Slvs_hEntity)aCenterEnt->id(), (Slvs_hEntity)aStartEnt->id(), (Slvs_hEntity)aEndEnt->id());
591 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
592 aNewEntity->setSubEntities(aSubs);
597 void adjustTangency(ConstraintWrapperPtr theConstraint)
599 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
601 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
602 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
604 // Collect start, end points of entities
605 std::shared_ptr<GeomAPI_Pnt2d> aStartEntPoints[2][2];
606 bool isCoinc[2][2] = {false};
607 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
608 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
609 for (int i = 0; aSIt != aSubs.end(); ++aSIt, ++i) {
610 const std::list<EntityWrapperPtr>& aPoints = (*aSIt)->subEntities();
611 std::list<EntityWrapperPtr>::const_iterator aPIt = aPoints.begin();
612 if ((*aSIt)->type() == ENTITY_ARC)
614 for (int j = 0; aPIt != aPoints.end(); ++aPIt, ++j) {
615 aStartEntPoints[i][j] = aBuilder->point(*aPIt);
616 if (i > 0) { // check coincidence
617 for (int k = 0; k < 2; ++k)
618 if (aStartEntPoints[i][j]->distance(aStartEntPoints[0][k]) < tolerance)
619 isCoinc[0][k] = isCoinc[i][j] = true;
624 Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
625 if (isCoinc[0][0] == false && isCoinc[0][1] == true)
626 aSlvsConstraint.other = 1;
627 else aSlvsConstraint.other = 0;
628 if (isCoinc[1][0] == false && isCoinc[1][1] == true)
629 aSlvsConstraint.other2 = 1;
630 else aSlvsConstraint.other2 = 0;
633 void adjustAngle(ConstraintWrapperPtr theConstraint)
635 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
637 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
638 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
640 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
641 const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
642 std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
643 for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
644 const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
645 std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
646 for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt)
647 aPoints[i][j] = aBuilder->point(*aLPIt);
650 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
651 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
652 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
654 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
658 for (int i = 0; i < 2; i++) {
659 for (int j = 0; j < 2; j++) {
660 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
661 if (fabs(aDist[i][j]) <= tolerance)
664 if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
665 aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
666 // the intersection point is an inner point of the line,
667 // we change the sign of distance till start point to calculate correct coordinates
672 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
673 for (int i = 0; i < 2; i++) {
674 if (aDist[i][1] > fabs(aDist[i][0]))
675 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
676 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
678 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
679 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
680 // main direction is opposite => change signs
681 if (aDist[i][0] < 0.0) {
688 Slvs_Constraint& aSlvsConstraint = aConstraint->changeConstraint();
689 aSlvsConstraint.other = false;
690 for (int i = 0; i < 2; i++)
691 if (aLine[i]->direction()->dot(aDir[i]) < 0.0)
692 aSlvsConstraint.other = !aSlvsConstraint.other;
695 void adjustMirror(ConstraintWrapperPtr theConstraint)
697 std::vector<EntityWrapperPtr> aPoints;
698 EntityWrapperPtr aMirrorLine;
700 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
701 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
702 for (; anIt != aSubs.end(); ++anIt) {
703 if ((*anIt)->type() == ENTITY_POINT)
704 aPoints.push_back(*anIt);
705 else if ((*anIt)->type() == ENTITY_LINE)
709 makeMirrorPoints(aPoints[0], aPoints[1], aMirrorLine);
712 void makeMirrorPoints(EntityWrapperPtr theOriginal,
713 EntityWrapperPtr theMirrored,
714 EntityWrapperPtr theMirrorLine)
716 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
718 std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
719 std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
720 // orthogonal direction
721 aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
723 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
724 std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
725 double aDist = aVec->dot(aMLDir->xy());
726 aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
727 double aCoord[2] = {aVec->x(), aVec->y()};
728 std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
729 for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
730 (*aMIt)->setValue(aCoord[i]);
732 // update corresponding attribute
733 AttributePtr anAttr = std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMirrored)->baseAttribute();
735 std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
736 aMirroredPnt->setValue(aCoord[0], aCoord[1]);
740 static void rotate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
741 std::shared_ptr<GeomAPI_Pnt2d> theCenter,
742 double theSin, double theCos)
744 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSource =
745 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSource);
746 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aDest =
747 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theDest);
749 if (theSource->type() == ENTITY_POINT) {
750 // Rotate single point
751 std::shared_ptr<GeomDataAPI_Point2D> aSrcAttr =
752 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSource->baseAttribute());
753 std::shared_ptr<GeomDataAPI_Point2D> aDstAttr =
754 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aDest->baseAttribute());
755 if (aSrcAttr && aDstAttr) {
756 std::shared_ptr<GeomAPI_XY> aVec = aSrcAttr->pnt()->xy()->decreased(theCenter->xy());
757 double aNewX = aVec->x() * theCos - aVec->y() * theSin;
758 double aNewY = aVec->x() * theSin + aVec->y() * theCos;
759 aDstAttr->setValue(theCenter->x() + aNewX, theCenter->y() + aNewY);
764 FeaturePtr aDestFeature = aDest->baseFeature();
766 aDestFeature->data()->blockSendAttributeUpdated(true);
768 // Rotate points of the feature
769 const std::list<EntityWrapperPtr>& aSrcSubs = theSource->subEntities();
770 const std::list<EntityWrapperPtr>& aDstSubs = theDest->subEntities();
771 std::list<EntityWrapperPtr>::const_iterator aSrcIt, aDstIt;
772 for (aSrcIt = aSrcSubs.begin(), aDstIt = aDstSubs.begin();
773 aSrcIt != aSrcSubs.end() && aDstIt != aDstSubs.end(); ++aSrcIt, ++aDstIt)
774 rotate(*aSrcIt, *aDstIt, theCenter, theSin, theCos);
777 aDestFeature->data()->blockSendAttributeUpdated(false);
780 static void translate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
781 std::shared_ptr<GeomAPI_XY> theDelta)
783 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSource =
784 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSource);
785 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aDest =
786 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theDest);
788 if (theSource->type() == ENTITY_POINT) {
789 // Translate single point
790 std::shared_ptr<GeomDataAPI_Point2D> aSrcAttr =
791 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aSource->baseAttribute());
792 std::shared_ptr<GeomDataAPI_Point2D> aDstAttr =
793 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aDest->baseAttribute());
794 if (aSrcAttr && aDstAttr)
795 aDstAttr->setValue(aSrcAttr->x() + theDelta->x(), aSrcAttr->y() + theDelta->y());
799 FeaturePtr aDestFeature = aDest->baseFeature();
801 aDestFeature->data()->blockSendAttributeUpdated(true);
803 // Translate points of the feature
804 const std::list<EntityWrapperPtr>& aSrcSubs = theSource->subEntities();
805 const std::list<EntityWrapperPtr>& aDstSubs = theDest->subEntities();
806 std::list<EntityWrapperPtr>::const_iterator aSrcIt, aDstIt;
807 for (aSrcIt = aSrcSubs.begin(), aDstIt = aDstSubs.begin();
808 aSrcIt != aSrcSubs.end() && aDstIt != aDstSubs.end(); ++aSrcIt, ++aDstIt)
809 translate(*aSrcIt, *aDstIt, theDelta);
812 aDestFeature->data()->blockSendAttributeUpdated(false);
815 void adjustMultiRotation(ConstraintWrapperPtr theConstraint)
817 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
819 double anAngleRad = theConstraint->value() * PI / 180.0;
820 double aSin = sin(anAngleRad);
821 double aCos = cos(anAngleRad);
823 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
824 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
826 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aBuilder->point(*aSIt++);
827 std::list<EntityWrapperPtr>::const_iterator aPrevIt = aSIt++;
828 for (; aSIt != aSubs.end(); ++aPrevIt, ++aSIt)
829 rotate(*aPrevIt, *aSIt, aCenter, aSin, aCos);
832 void adjustMultiTranslation(ConstraintWrapperPtr theConstraint)
834 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
836 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
837 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
839 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aBuilder->point(*aSIt++);
840 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aBuilder->point(*aSIt++);
841 std::shared_ptr<GeomAPI_XY> aDelta = aEndPnt->xy()->decreased(aStartPnt->xy());
843 std::list<EntityWrapperPtr>::const_iterator aPrevIt = aSIt++;
844 for (; aSIt != aSubs.end(); ++aPrevIt, ++aSIt)
845 translate(*aPrevIt, *aSIt, aDelta);