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(*anIt1, *anIt2, theMirrorLine);
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();
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());
250 // Restore event sending
251 aMirrArc->data()->blockSendAttributeUpdated(false);
256 void SolveSpaceSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
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);
272 EntityWrapperPtr SolveSpaceSolver_Builder::createFeature(
273 FeaturePtr theFeature,
274 const std::list<EntityWrapperPtr>& theAttributes,
275 const GroupID& theGroupID,
276 const EntityID& theSketchID) const
278 static EntityWrapperPtr aDummy;
279 if (!theFeature->data()->isValid())
283 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
285 return createSketchEntity(aSketch, theGroupID);
287 // SketchPlugin features
288 std::shared_ptr<SketchPlugin_Feature> aFeature =
289 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
293 // Verify the feature by its kind
294 const std::string& aFeatureKind = aFeature->getKind();
296 if (aFeatureKind == SketchPlugin_Line::ID())
297 return createLine(theFeature, theAttributes, theGroupID, theSketchID);
299 else if (aFeatureKind == SketchPlugin_Circle::ID())
300 return createCircle(theFeature, theAttributes,theGroupID, theSketchID);
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())
309 EntityWrapperPtr aSub = createAttribute(aPoint, theGroupID, theSketchID);
313 const Slvs_Entity& aSubEnt =
314 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(aSub)->entity();
315 return EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, aPoint, aSubEnt));
322 EntityWrapperPtr SolveSpaceSolver_Builder::createAttribute(
323 AttributePtr theAttribute,
324 const GroupID& theGroupID,
325 const EntityID& theSketchID) const
327 AttributePtr anAttribute = theAttribute;
328 AttributeRefAttrPtr aRefAttr =
329 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
331 if (aRefAttr->isObject()) {
332 // do not create features here
333 return EntityWrapperPtr();
335 anAttribute = aRefAttr->attr();
338 std::list<ParameterWrapperPtr> aParameters;
339 Slvs_Entity anEntity;
343 std::shared_ptr<GeomDataAPI_Point> aPoint =
344 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
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);
354 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
355 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
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);
363 // Scalar value (used for the distance entities)
364 AttributeDoublePtr aScalar =
365 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
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);
375 if (anEntity.type == 0) {
376 // unknown attribute type
377 return EntityWrapperPtr();
380 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(theAttribute, anEntity));
381 aResult->setParameters(aParameters);
387 EntityWrapperPtr SolveSpaceSolver_Builder::createSketchEntity(
388 CompositeFeaturePtr theSketch,
389 const GroupID& theGroupID) const
391 DataPtr aSketchData = theSketch->data();
392 if (!aSketchData || !aSketchData->isValid())
393 return EntityWrapperPtr(); // the sketch is incorrect
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();
402 EntityWrapperPtr aNewEnt;
403 std::list<EntityWrapperPtr> aSubs;
405 // Create SolveSpace entity corresponding to the sketch origin
406 aNewEnt = createAttribute(anOrigin, theGroupID);
408 return EntityWrapperPtr();
409 aSubs.push_back(aNewEnt);
411 // Create SolveSpace entity corresponding the the sketch normal
412 aNewEnt = createNormal(aNorm, aDirX, theGroupID);
414 return EntityWrapperPtr();
415 aSubs.push_back(aNewEnt);
418 Slvs_Entity aWorkplane = Slvs_MakeWorkplane(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroupID,
419 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
421 aNewEnt = EntityWrapperPtr(
422 new SolveSpaceSolver_EntityWrapper(FeaturePtr(theSketch), aWorkplane));
423 aNewEnt->setSubEntities(aSubs);
427 EntityWrapperPtr SolveSpaceSolver_Builder::createNormal(
428 AttributePtr theNormal,
429 AttributePtr theDirX,
430 const GroupID& theGroupID) const
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())));
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,
445 double aNormCoord[4] = { qw, qx, qy, qz };
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]));
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);
460 ParameterWrapperPtr SolveSpaceSolver_Builder::createParameter(
461 const GroupID& theGroup, const double theValue, const bool theExpr) const
463 Slvs_Param aParam = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)theGroup, theValue);
464 ParameterWrapperPtr aWrapper(new SolveSpaceSolver_ParameterWrapper(aParam));
465 aWrapper->setIsParametric(theExpr);
473 // ================ Auxiliary functions ==========================
474 EntityWrapperPtr createLine(FeaturePtr theFeature,
475 const std::list<EntityWrapperPtr>& theAttributes,
476 const GroupID& theGroupID,
477 const EntityID& theSketchID)
479 EntityWrapperPtr aNewEntity;
480 std::list<EntityWrapperPtr> aSubs;
482 AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
483 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
484 if (!aStart->isInitialized() || !aEnd->isInitialized())
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;
497 if (!aStartEnt || !aEndEnt)
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());
505 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
506 aNewEntity->setSubEntities(aSubs);
510 EntityWrapperPtr createCircle(FeaturePtr theFeature,
511 const std::list<EntityWrapperPtr>& theAttributes,
512 const GroupID& theGroupID,
513 const EntityID& theSketchID)
515 EntityWrapperPtr aNewEntity;
516 std::list<EntityWrapperPtr> aSubs;
518 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
519 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
520 if (!aCenter->isInitialized() || !aRadius->isInitialized())
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;
535 if (!aCenterEnt || !aRadiusEnt || !aNormalEnt)
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());
544 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
545 aNewEntity->setSubEntities(aSubs);
549 EntityWrapperPtr createArc(FeaturePtr theFeature,
550 const std::list<EntityWrapperPtr>& theAttributes,
551 const GroupID& theGroupID,
552 const EntityID& theSketchID)
554 EntityWrapperPtr aNewEntity;
555 std::list<EntityWrapperPtr> aSubs;
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())
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;
577 if (!aCenterEnt || !aStartEnt || !aEndEnt || !aNormalEnt)
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());
587 aNewEntity = EntityWrapperPtr(new SolveSpaceSolver_EntityWrapper(theFeature, anEntity));
588 aNewEntity->setSubEntities(aSubs);
593 void adjustTangency(ConstraintWrapperPtr theConstraint)
595 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
597 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
598 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
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)
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;
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;
629 void adjustAngle(ConstraintWrapperPtr theConstraint)
631 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
633 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
634 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
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);
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]))
650 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
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)
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
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())));
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) {
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;
691 void adjustMirror(ConstraintWrapperPtr theConstraint)
693 std::vector<EntityWrapperPtr> aPoints;
694 EntityWrapperPtr aMirrorLine;
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)
705 makeMirrorPoints(aPoints[0], aPoints[1], aMirrorLine);
708 void makeMirrorPoints(EntityWrapperPtr theOriginal,
709 EntityWrapperPtr theMirrored,
710 EntityWrapperPtr theMirrorLine)
712 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
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()));
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]);
728 // update corresponding attribute
729 AttributePtr anAttr = std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMirrored)->baseAttribute();
731 std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
732 aMirroredPnt->setValue(aCoord[0], aCoord[1]);
736 static void rotate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
737 std::shared_ptr<GeomAPI_Pnt2d> theCenter,
738 double theSin, double theCos)
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);
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);
760 FeaturePtr aDestFeature = aDest->baseFeature();
762 aDestFeature->data()->blockSendAttributeUpdated(true);
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);
773 aDestFeature->data()->blockSendAttributeUpdated(false);
776 static void translate(EntityWrapperPtr theSource, EntityWrapperPtr theDest,
777 std::shared_ptr<GeomAPI_XY> theDelta)
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);
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());
795 FeaturePtr aDestFeature = aDest->baseFeature();
797 aDestFeature->data()->blockSendAttributeUpdated(true);
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);
808 aDestFeature->data()->blockSendAttributeUpdated(false);
811 void adjustMultiRotation(ConstraintWrapperPtr theConstraint)
813 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
815 double anAngleRad = theConstraint->value() * PI / 180.0;
816 double aSin = sin(anAngleRad);
817 double aCos = cos(anAngleRad);
819 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
820 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
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);
828 void adjustMultiTranslation(ConstraintWrapperPtr theConstraint)
830 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
832 const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
833 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
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());
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);
846 std::list<EntityWrapperPtr>::const_iterator aPrevIt = aSIt++;
847 for (; aSIt != aSubs.end(); ++aPrevIt, ++aSIt)
848 translate(*aPrevIt, *aSIt, aDelta);