1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Builder.cpp
4 // Created: 14 Dec 2015
5 // Author: Artem ZHIDKOV
7 #include <PlaneGCSSolver_Builder.h>
8 #include <PlaneGCSSolver_Solver.h>
9 #include <PlaneGCSSolver_Storage.h>
10 #include <PlaneGCSSolver_ParameterWrapper.h>
11 #include <PlaneGCSSolver_AngleWrapper.h>
12 #include <PlaneGCSSolver_EntityWrapper.h>
13 #include <PlaneGCSSolver_PointWrapper.h>
14 #include <PlaneGCSSolver_ScalarWrapper.h>
15 #include <PlaneGCSSolver_ConstraintWrapper.h>
17 #include <SketchSolver_Manager.h>
19 #include <GeomAPI_Dir2d.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_XY.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomDataAPI_Point.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_Attribute.h>
26 #include <ModelAPI_AttributeRefAttr.h>
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
32 #include <SketchPlugin_ConstraintAngle.h>
37 #define GCS_ENTITY_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(x)
38 #define GCS_POINT_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(x)
39 #define GCS_PARAMETER_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(x)
42 /// \brief Converts a value to SolveSpace parameter
43 /// \param theGroup [in] group to store parameter
44 /// \param theValue [in] value of parameter
45 /// \param theExpr [in] shows the parameter is given by expression
46 /// \return Created parameter's wrapper
47 static ParameterWrapperPtr createParameter(const GroupID& theGroup,
48 const double theValue = 0.0,
49 const bool theExpr = false);
51 static ParameterWrapperPtr createParamAngle(const GroupID& theGroup,
52 const double& theValue = 0.0);
54 static std::shared_ptr<PlaneGCSSolver_ScalarWrapper>
55 createScalar(const GroupID& theGroupID,
56 AttributeDoublePtr theDoubleAttr = AttributeDoublePtr());
58 static EntityWrapperPtr createLine(FeaturePtr theFeature,
59 const std::list<EntityWrapperPtr>& theAttributes,
60 const GroupID& theGroupID);
61 static EntityWrapperPtr createCircle(FeaturePtr theFeature,
62 const std::list<EntityWrapperPtr>& theAttributes,
63 const GroupID& theGroupID);
64 static EntityWrapperPtr createArc(FeaturePtr theFeature,
65 const std::list<EntityWrapperPtr>& theAttributes,
66 const GroupID& theGroupID);
69 static ConstraintWrapperPtr
70 createConstraintCoincidence(ConstraintPtr theConstraint,
71 const GroupID& theGroupID,
72 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
73 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
74 static ConstraintWrapperPtr
75 createConstraintPointOnEntity(ConstraintPtr theConstraint,
76 const GroupID& theGroupID,
77 const SketchSolver_ConstraintType& theType,
78 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
79 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
80 static ConstraintWrapperPtr
81 createConstraintDistancePointPoint(ConstraintPtr theConstraint,
82 const GroupID& theGroupID,
83 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
84 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
85 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
86 static ConstraintWrapperPtr
87 createConstraintDistancePointLine(ConstraintPtr theConstraint,
88 const GroupID& theGroupID,
89 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
90 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
91 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
92 static ConstraintWrapperPtr
93 createConstraintRadius(ConstraintPtr theConstraint,
94 const GroupID& theGroupID,
95 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
96 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
97 static ConstraintWrapperPtr
98 createConstraintAngle(ConstraintPtr theConstraint,
99 const GroupID& theGroupID,
100 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
101 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
102 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
103 static ConstraintWrapperPtr
104 createConstraintHorizVert(ConstraintPtr theConstraint,
105 const GroupID& theGroupID,
106 const SketchSolver_ConstraintType& theType,
107 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
108 static ConstraintWrapperPtr
109 createConstraintParallel(ConstraintPtr theConstraint,
110 const GroupID& theGroupID,
111 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
112 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
113 static ConstraintWrapperPtr
114 createConstraintPerpendicular(ConstraintPtr theConstraint,
115 const GroupID& theGroupID,
116 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
117 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
118 static ConstraintWrapperPtr
119 createConstraintEqual(ConstraintPtr theConstraint,
120 const GroupID& theGroupID,
121 const SketchSolver_ConstraintType& theType,
122 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
123 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
124 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed);
125 static ConstraintWrapperPtr
126 createConstraintTangent(ConstraintPtr theConstraint,
127 const GroupID& theGroupID,
128 const SketchSolver_ConstraintType& theType,
129 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
130 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
131 static ConstraintWrapperPtr
132 createConstraintCollinear(ConstraintPtr theConstraint,
133 const GroupID& theGroupID,
134 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
135 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
136 static ConstraintWrapperPtr
137 createConstraintMiddlePoint(ConstraintPtr theConstraint,
138 const GroupID& theGroupID,
139 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
140 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
144 /// \brief Set flags for angle constraint
145 static void adjustAngle(ConstraintWrapperPtr theConstraint);
146 /// \brief Update mirror points
147 static void adjustMirror(ConstraintWrapperPtr theConstraint);
148 /// \brief Update a sign of the point-line distance constraint
149 static void adjustPtLineDistance(ConstraintWrapperPtr theConstraint);
151 /// \brief Transform points to be symmetric regarding to the mirror line
152 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
153 EntityWrapperPtr theMirrored,
154 EntityWrapperPtr theMirrorLine);
158 // Initialization of constraint builder self pointer
159 BuilderPtr PlaneGCSSolver_Builder::mySelf = PlaneGCSSolver_Builder::getInstance();
161 BuilderPtr PlaneGCSSolver_Builder::getInstance()
164 mySelf = BuilderPtr(new PlaneGCSSolver_Builder);
165 SketchSolver_Manager::instance()->setBuilder(mySelf);
170 StoragePtr PlaneGCSSolver_Builder::createStorage(const GroupID& theGroup) const
172 return StoragePtr(new PlaneGCSSolver_Storage(theGroup));
175 SolverPtr PlaneGCSSolver_Builder::createSolver() const
177 return SolverPtr(new PlaneGCSSolver_Solver);
181 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
182 ConstraintPtr theConstraint,
183 const GroupID& theGroupID,
184 const EntityID& theSketchID,
185 const SketchSolver_ConstraintType& theType,
186 const double& theValue,
187 const EntityWrapperPtr& thePoint1,
188 const EntityWrapperPtr& thePoint2,
189 const EntityWrapperPtr& theEntity1,
190 const EntityWrapperPtr& theEntity2) const
192 ConstraintWrapperPtr aResult;
193 ParameterWrapperPtr anIntermediate;
195 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(thePoint1);
196 if (!aPoint1 && thePoint1 && thePoint1->type() == ENTITY_POINT)
197 aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint1)->subEntities().front() );
198 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(thePoint2);
199 if (!aPoint2 && thePoint2 && thePoint2->type() == ENTITY_POINT)
200 aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint2)->subEntities().front() );
203 case CONSTRAINT_PT_PT_COINCIDENT:
204 aResult = createConstraintCoincidence(theConstraint, theGroupID, aPoint1, aPoint2);
206 case CONSTRAINT_PT_ON_LINE:
207 case CONSTRAINT_PT_ON_CIRCLE:
208 aResult = createConstraintPointOnEntity(theConstraint, theGroupID, theType,
209 aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
211 case CONSTRAINT_MIDDLE_POINT:
212 aResult = createConstraintMiddlePoint(theConstraint, theGroupID,
213 aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
215 case CONSTRAINT_PT_PT_DISTANCE:
216 aResult = createConstraintDistancePointPoint(theConstraint, theGroupID,
217 GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
220 case CONSTRAINT_PT_LINE_DISTANCE:
221 aResult = createConstraintDistancePointLine(theConstraint, theGroupID,
222 GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
223 aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
225 case CONSTRAINT_RADIUS:
226 aResult = createConstraintRadius(theConstraint, theGroupID,
227 GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
228 GCS_ENTITY_WRAPPER(theEntity1));
230 case CONSTRAINT_ANGLE:
231 aResult = createConstraintAngle(theConstraint, theGroupID,
232 GCS_PARAMETER_WRAPPER(createParamAngle(GID_OUTOFGROUP, theValue)),
233 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
235 case CONSTRAINT_FIXED:
237 case CONSTRAINT_HORIZONTAL:
238 case CONSTRAINT_VERTICAL:
239 aResult = createConstraintHorizVert(theConstraint, theGroupID, theType,
240 GCS_ENTITY_WRAPPER(theEntity1));
242 case CONSTRAINT_PARALLEL:
243 aResult = createConstraintParallel(theConstraint, theGroupID,
244 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
246 case CONSTRAINT_PERPENDICULAR:
247 aResult = createConstraintPerpendicular(theConstraint, theGroupID,
248 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
250 case CONSTRAINT_EQUAL_LINES:
251 anIntermediate = createParameter(theGroupID);
252 case CONSTRAINT_EQUAL_LINE_ARC:
253 case CONSTRAINT_EQUAL_RADIUS:
254 aResult = createConstraintEqual(theConstraint, theGroupID, theType,
255 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2),
256 GCS_PARAMETER_WRAPPER(anIntermediate));
258 case CONSTRAINT_TANGENT_ARC_LINE:
259 case CONSTRAINT_TANGENT_CIRCLE_LINE:
260 case CONSTRAINT_TANGENT_ARC_ARC:
261 aResult = createConstraintTangent(theConstraint, theGroupID, theType,
262 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
264 case CONSTRAINT_COLLINEAR:
265 aResult = createConstraintCollinear(theConstraint, theGroupID,
266 GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
268 case CONSTRAINT_MULTI_TRANSLATION:
269 case CONSTRAINT_MULTI_ROTATION:
271 case CONSTRAINT_SYMMETRIC:
272 return createMirror(theConstraint, theGroupID, theSketchID,
273 thePoint1, thePoint2, theEntity1);
279 return std::list<ConstraintWrapperPtr>();
280 adjustConstraint(aResult);
281 return std::list<ConstraintWrapperPtr>(1, aResult);
284 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
285 ConstraintPtr theConstraint,
286 const GroupID& theGroupID,
287 const EntityID& theSketchID,
288 const SketchSolver_ConstraintType& theType,
289 const double& theValue,
290 const bool theFullValue,
291 const EntityWrapperPtr& thePoint1,
292 const EntityWrapperPtr& thePoint2,
293 const std::list<EntityWrapperPtr>& theTrsfEnt) const
295 ParameterWrapperPtr anAngleParam;
296 if (theType == CONSTRAINT_MULTI_ROTATION)
297 anAngleParam = createParamAngle(theGroupID, theValue);
298 else if (theType != CONSTRAINT_MULTI_TRANSLATION)
299 return std::list<ConstraintWrapperPtr>();
301 std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
303 aConstrAttrList.push_front(thePoint2);
304 aConstrAttrList.push_front(thePoint1);
306 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
307 new PlaneGCSSolver_ConstraintWrapper(theConstraint, std::list<GCSConstraintPtr>(), theType));
308 aResult->setGroup(theGroupID);
309 aResult->setEntities(aConstrAttrList);
311 aResult->setValueParameter(anAngleParam);
312 aResult->setIsFullValue(theFullValue);
313 return std::list<ConstraintWrapperPtr>(1, aResult);
317 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createMirror(
318 ConstraintPtr theConstraint,
319 const GroupID& theGroupID,
320 const EntityID& theSketchID,
321 const EntityWrapperPtr& theEntity1,
322 const EntityWrapperPtr& theEntity2,
323 const EntityWrapperPtr& theMirrorLine) const
325 std::list<ConstraintWrapperPtr> aResult;
326 std::list<EntityWrapperPtr> aConstrAttrList;
327 if (theEntity1->type() == ENTITY_POINT) {
328 if (theEntity2->group() == theGroupID) // theEntity2 is not fixed
329 makeMirrorPoints(theEntity1, theEntity2, theMirrorLine);
331 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(theEntity1);
332 if (!aPoint1 && theEntity1->type() == ENTITY_POINT)
333 aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity1)->subEntities().front() );
334 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(theEntity2);
335 if (!aPoint2 && theEntity2->type() == ENTITY_POINT)
336 aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity2)->subEntities().front() );
338 std::shared_ptr<PlaneGCSSolver_EntityWrapper> aMirrorLine =
339 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMirrorLine);
340 std::shared_ptr<GCS::Line> aLine =
341 std::dynamic_pointer_cast<GCS::Line>(aMirrorLine->entity());
343 std::list<GCSConstraintPtr> aConstrList;
344 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintPerpendicular(
345 *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
346 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintMidpointOnLine(
347 *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
349 ConstraintWrapperPtr aSubResult(new PlaneGCSSolver_ConstraintWrapper(
350 theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
351 aSubResult->setGroup(theGroupID);
352 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
353 aSubs.push_back(theEntity2);
354 aSubs.push_back(theMirrorLine);
355 aSubResult->setEntities(aSubs);
356 aResult.push_back(aSubResult);
358 else if (theEntity1->type() == ENTITY_LINE) {
359 const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
360 const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
361 std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
362 std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
363 for (; anIt1 != aPoints1.end() && anIt2 != aPoints2.end(); ++anIt1, ++anIt2) {
364 std::list<ConstraintWrapperPtr> aMrrList =
365 createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
366 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
369 else if (theEntity1->type() == ENTITY_CIRCLE) {
370 const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
371 std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
372 for (; anIt1 != aPoints1.end(); ++anIt1)
373 if ((*anIt1)->type() == ENTITY_POINT)
375 const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
376 std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
377 for (; anIt2 != aPoints2.end(); ++anIt2)
378 if ((*anIt2)->type() == ENTITY_POINT)
381 std::list<ConstraintWrapperPtr> aMrrList =
382 createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
383 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
385 // Additional constraint for equal radii
386 aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
387 0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
388 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
390 else if (theEntity1->type() == ENTITY_ARC) {
391 // Do not allow mirrored arc recalculate its position until coordinated of all points recalculated
392 FeaturePtr aMirrArc = theEntity2->baseFeature();
393 aMirrArc->data()->blockSendAttributeUpdated(true);
395 // Make mirror for center and start point of original arc
396 std::list<ConstraintWrapperPtr> aMrrList;
397 std::list<EntityWrapperPtr>::const_iterator anIt1 = theEntity1->subEntities().begin();
398 std::list<EntityWrapperPtr>::const_iterator anIt2 = theEntity2->subEntities().begin();
399 aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
400 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
404 aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
405 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
407 // make symmetric last point of original arc and first point of mirrored arc without additional constraint
410 makeMirrorPoints(*anIt1, *anIt2, theMirrorLine);
412 // Additionally, make equal radii...
413 aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
414 0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
415 aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
416 // ... and make parametric length of arcs the same
417 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt1 =
418 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity1);
419 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt2 =
420 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity2);
421 std::shared_ptr<GCS::Arc> anArc1 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt1->entity());
422 std::shared_ptr<GCS::Arc> anArc2 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt2->entity());
423 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> anIntermed =
424 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(
425 createParameter(theGroupID, *(anArc1->endAngle) - *(anArc1->startAngle)));
426 // By the way, recalculate start and end angles of mirrored arc
427 std::shared_ptr<GeomAPI_Dir2d> anOX(new GeomAPI_Dir2d(1.0, 0.0));
428 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
429 *(anArc2->start.x) - *(anArc2->center.x), *(anArc2->start.y) - *(anArc2->center.y)));
430 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
431 *(anArc2->end.x) - *(anArc2->center.x), *(anArc2->end.y) - *(anArc2->center.y)));
432 *anArc2->startAngle = anOX->angle(aStartDir);
433 *anArc2->endAngle = anOX->angle(aEndDir);
435 std::list<GCSConstraintPtr> aConstrList;
436 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
437 anArc1->endAngle, anArc1->startAngle, anIntermed->parameter())));
438 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
439 anArc2->endAngle, anArc2->startAngle, anIntermed->parameter())));
441 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aSubResult(
442 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
443 aSubResult->setGroup(theGroupID);
444 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
445 aSubs.push_back(theEntity2);
446 aSubs.push_back(theMirrorLine);
447 aSubResult->setEntities(aSubs);
448 aSubResult->setValueParameter(anIntermed);
449 aResult.push_back(aSubResult);
451 // Restore event sending
452 aMirrArc->data()->blockSendAttributeUpdated(false);
457 void PlaneGCSSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
459 SketchSolver_ConstraintType aType = theConstraint->type();
460 // Update flags and parameters in constraints
461 if (aType == CONSTRAINT_ANGLE)
462 adjustAngle(theConstraint);
463 else if (aType == CONSTRAINT_PT_LINE_DISTANCE)
464 adjustPtLineDistance(theConstraint);
467 EntityWrapperPtr PlaneGCSSolver_Builder::createFeature(
468 FeaturePtr theFeature,
469 const std::list<EntityWrapperPtr>& theAttributes,
470 const GroupID& theGroupID,
471 const EntityID& /*theSketchID*/) const
473 static EntityWrapperPtr aDummy;
474 if (!theFeature->data()->isValid())
478 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
480 return createSketchEntity(aSketch, theGroupID);
482 // SketchPlugin features
483 std::shared_ptr<SketchPlugin_Feature> aFeature =
484 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
488 // Verify the feature by its kind
489 const std::string& aFeatureKind = aFeature->getKind();
491 if (aFeatureKind == SketchPlugin_Line::ID())
492 return createLine(theFeature, theAttributes, theGroupID);
494 else if (aFeatureKind == SketchPlugin_Circle::ID())
495 return createCircle(theFeature, theAttributes, theGroupID);
497 else if (aFeatureKind == SketchPlugin_Arc::ID())
498 return createArc(theFeature, theAttributes, theGroupID);
499 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
500 else if (aFeatureKind == SketchPlugin_Point::ID()) {
501 EntityWrapperPtr aSub;
502 if (theAttributes.size() == 1)
503 aSub = theAttributes.front();
505 AttributePtr aPoint = theFeature->attribute(SketchPlugin_Point::COORD_ID());
506 if (aPoint->isInitialized())
507 aSub = createAttribute(aPoint, theGroupID);
512 GCSPointPtr aSubEnt =
513 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aSub)->point();
514 EntityWrapperPtr aNewEntity(new PlaneGCSSolver_EntityWrapper(theFeature));
515 aNewEntity->setSubEntities(std::list<EntityWrapperPtr>(1, aSub));
523 EntityWrapperPtr PlaneGCSSolver_Builder::createAttribute(
524 AttributePtr theAttribute,
525 const GroupID& theGroupID,
526 const EntityID& theSketchID) const
528 AttributePtr anAttribute = theAttribute;
529 AttributeRefAttrPtr aRefAttr =
530 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
532 if (aRefAttr->isObject()) {
533 // do not create features here
534 return EntityWrapperPtr();
536 anAttribute = aRefAttr->attr();
539 std::list<ParameterWrapperPtr> aParameters;
540 EntityWrapperPtr aResult;
543 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
544 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
546 aParameters.push_back(::createParameter(theGroupID, aPoint2D->x(), !aPoint2D->textX().empty()));
547 aParameters.push_back(::createParameter(theGroupID, aPoint2D->y(), !aPoint2D->textY().empty()));
548 GCSPointPtr aGCSPoint(new GCS::Point);
549 aGCSPoint->x = std::dynamic_pointer_cast<
550 PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
551 aGCSPoint->y = std::dynamic_pointer_cast<
552 PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
553 // Create entity (parameters are not filled)
554 aResult = EntityWrapperPtr(new PlaneGCSSolver_PointWrapper(theAttribute, aGCSPoint));
556 // Scalar value (used for the distance entities)
557 AttributeDoublePtr aScalar =
558 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
560 return createScalar(theGroupID, aScalar);
564 // unknown attribute type
565 return EntityWrapperPtr();
568 aResult->setGroup(theGroupID);
569 aResult->setParameters(aParameters);
573 ParameterWrapperPtr PlaneGCSSolver_Builder::createParameter(
574 const GroupID& theGroupID, double theValue) const
576 return ::createParameter(theGroupID, theValue);
580 EntityWrapperPtr PlaneGCSSolver_Builder::createSketchEntity(
581 CompositeFeaturePtr theSketch,
582 const GroupID& theGroupID) const
584 DataPtr aSketchData = theSketch->data();
585 if (!aSketchData || !aSketchData->isValid())
586 return EntityWrapperPtr(); // the sketch is incorrect
588 // Create dummy wrapper representing workplane
589 std::shared_ptr<PlaneGCSSolver_EntityWrapper> aSketchEnt(
590 new PlaneGCSSolver_EntityWrapper(FeaturePtr(theSketch)));
591 aSketchEnt->setGroup(theGroupID);
592 aSketchEnt->setId(EID_SKETCH);
600 // ================ Auxiliary functions ==========================
601 ParameterWrapperPtr createParameter(
602 const GroupID& theGroup, const double theValue, const bool theExpr)
604 double* aParam = new double(theValue);
605 ParameterWrapperPtr aWrapper(new PlaneGCSSolver_ParameterWrapper(aParam));
606 aWrapper->setGroup(theGroup);
607 aWrapper->setIsParametric(theExpr);
611 ParameterWrapperPtr createParamAngle(const GroupID& theGroup, const double& theValue)
613 double* aParam = new double(theValue);
614 ParameterWrapperPtr aWrapper(new PlaneGCSSolver_AngleWrapper(aParam));
615 aWrapper->setGroup(theGroup);
619 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> createScalar(
620 const GroupID& theGroupID,
621 AttributeDoublePtr theDoubleAttr)
623 ParameterWrapperPtr aParam = createParameter(theGroupID, theDoubleAttr ? theDoubleAttr->value() : 0.0);
624 return std::shared_ptr<PlaneGCSSolver_ScalarWrapper>(
625 new PlaneGCSSolver_ScalarWrapper(theDoubleAttr, aParam));
628 EntityWrapperPtr createLine(FeaturePtr theFeature,
629 const std::list<EntityWrapperPtr>& theAttributes,
630 const GroupID& theGroupID)
632 EntityWrapperPtr aNewEntity;
633 std::list<EntityWrapperPtr> aSubs;
635 AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
636 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
637 if (!aStart->isInitialized() || !aEnd->isInitialized())
640 std::shared_ptr<PlaneGCSSolver_PointWrapper> aStartEnt, aEndEnt;
641 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
642 for (; anIt != theAttributes.end(); ++anIt) {
643 std::shared_ptr<PlaneGCSSolver_PointWrapper> aWrapper =
644 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
647 if (aWrapper->isBase(aStart))
648 aStartEnt = aWrapper;
649 else if (aWrapper->isBase(aEnd))
652 if (!aStartEnt || !aEndEnt)
655 aSubs.push_back(aStartEnt);
656 aSubs.push_back(aEndEnt);
658 std::shared_ptr<GCS::Line> aLine(new GCS::Line);
659 aLine->p1 = *(aStartEnt->point());
660 aLine->p2 = *(aEndEnt->point());
662 aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, aLine));
663 aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
664 aNewEntity->setSubEntities(aSubs);
668 EntityWrapperPtr createCircle(FeaturePtr theFeature,
669 const std::list<EntityWrapperPtr>& theAttributes,
670 const GroupID& theGroupID)
672 EntityWrapperPtr aNewEntity;
673 std::list<EntityWrapperPtr> aSubs;
675 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
676 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
677 if (!aCenter->isInitialized() || !aRadius->isInitialized())
680 std::shared_ptr<PlaneGCSSolver_PointWrapper> aCenterEnt;
681 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aRadiusEnt;
682 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
683 for (; anIt != theAttributes.end(); ++anIt) {
684 if ((*anIt)->isBase(aCenter))
685 aCenterEnt = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
686 else if ((*anIt)->isBase(aRadius))
687 aRadiusEnt = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*anIt);
689 if (!aCenterEnt || !aRadiusEnt)
692 aSubs.push_back(aCenterEnt);
693 aSubs.push_back(aRadiusEnt);
695 std::shared_ptr<GCS::Circle> aCircle(new GCS::Circle);
696 aCircle->center = *(aCenterEnt->point());
697 aCircle->rad = aRadiusEnt->scalar();
699 aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, aCircle));
700 aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
701 aNewEntity->setSubEntities(aSubs);
705 EntityWrapperPtr createArc(FeaturePtr theFeature,
706 const std::list<EntityWrapperPtr>& theAttributes,
707 const GroupID& theGroupID)
709 EntityWrapperPtr aNewEntity;
710 std::list<EntityWrapperPtr> aSubs;
712 AttributePtr aCenter = theFeature->attribute(SketchPlugin_Arc::CENTER_ID());
713 AttributePtr aStart = theFeature->attribute(SketchPlugin_Arc::START_ID());
714 AttributePtr aEnd = theFeature->attribute(SketchPlugin_Arc::END_ID());
715 if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
718 std::shared_ptr<PlaneGCSSolver_PointWrapper> aCenterEnt, aStartEnt, aEndEnt;
719 std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
720 for (; anIt != theAttributes.end(); ++anIt) {
721 std::shared_ptr<PlaneGCSSolver_PointWrapper> aWrapper =
722 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
725 if (aWrapper->isBase(aCenter))
726 aCenterEnt = aWrapper;
727 else if (aWrapper->isBase(aStart))
728 aStartEnt = aWrapper;
729 else if (aWrapper->isBase(aEnd))
732 if (!aCenterEnt || !aStartEnt || !aEndEnt)
735 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aStartAng, aEndAng, aRadius;
736 aStartAng = createScalar(theGroupID);
737 aEndAng = createScalar(theGroupID);
738 aRadius = createScalar(theGroupID);
740 aSubs.push_back(aCenterEnt);
741 aSubs.push_back(aStartEnt);
742 aSubs.push_back(aEndEnt);
743 aSubs.push_back(aStartAng);
744 aSubs.push_back(aEndAng);
745 aSubs.push_back(aRadius);
747 std::shared_ptr<GCS::Arc> anArc(new GCS::Arc);
748 anArc->center = *(aCenterEnt->point());
749 anArc->start = *(aStartEnt->point());
750 anArc->end = *(aEndEnt->point());
751 anArc->startAngle = aStartAng->scalar();
752 anArc->endAngle = aEndAng->scalar();
753 anArc->rad = aRadius->scalar();
755 aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, anArc));
756 aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
757 aNewEntity->setSubEntities(aSubs);
763 ConstraintWrapperPtr createConstraintCoincidence(
764 ConstraintPtr theConstraint,
765 const GroupID& theGroupID,
766 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
767 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2)
769 // Create equality constraint for corresponding attributes of the points
770 std::list<GCSConstraintPtr> aConstrList;
771 std::list<ParameterWrapperPtr>::const_iterator anIt1 = thePoint1->parameters().begin();
772 std::list<ParameterWrapperPtr>::const_iterator anIt2 = thePoint2->parameters().begin();
773 for (; anIt1 != thePoint1->parameters().end(); ++anIt1, ++anIt2) {
774 if (*anIt1 == *anIt2)
775 continue; // points use same parameters, no need additional constraints
776 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam1 =
777 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*anIt1);
778 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam2 =
779 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*anIt2);
780 aConstrList.push_back(
781 GCSConstraintPtr(new GCS::ConstraintEqual(aParam1->parameter(), aParam2->parameter())));
784 ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
785 theConstraint, aConstrList, CONSTRAINT_PT_PT_COINCIDENT));
786 aResult->setGroup(theGroupID);
787 std::list<EntityWrapperPtr> aSubs(1, thePoint1);
788 aSubs.push_back(thePoint2);
789 aResult->setEntities(aSubs);
793 ConstraintWrapperPtr createConstraintPointOnEntity(
794 ConstraintPtr theConstraint,
795 const GroupID& theGroupID,
796 const SketchSolver_ConstraintType& theType,
797 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
798 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
800 GCSConstraintPtr aNewConstr;
802 switch (theEntity->type()) {
804 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
805 aNewConstr = GCSConstraintPtr(new GCS::ConstraintPointOnLine(*(thePoint->point()), *aLine));
809 case ENTITY_CIRCLE: {
810 std::shared_ptr<GCS::Circle> aCirc = std::dynamic_pointer_cast<GCS::Circle>(theEntity->entity());
811 aNewConstr = GCSConstraintPtr(
812 new GCS::ConstraintP2PDistance(*(thePoint->point()), aCirc->center, aCirc->rad));
816 return ConstraintWrapperPtr();
819 ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
820 theConstraint, aNewConstr, theType));
821 aResult->setGroup(theGroupID);
822 std::list<EntityWrapperPtr> aSubs(1, thePoint);
823 aSubs.push_back(theEntity);
824 aResult->setEntities(aSubs);
828 // calculate length of the line
829 static inline double lineLength(const GCS::Line& theLine)
831 double aDir[2] = {*(theLine.p2.x) - *(theLine.p1.x), *(theLine.p2.y) - *(theLine.p1.y)};
832 return sqrt(aDir[0] * aDir[0] + aDir[1] * aDir[1]);
835 // check the point is on the line
836 static inline bool isPointOnLine(const GCS::Point& thePoint, const GCS::Line& theLine)
838 double aDir[2] = {*(theLine.p2.x) - *(theLine.p1.x), *(theLine.p2.y) - *(theLine.p1.y)};
839 double aVec[2] = {*(thePoint.x) - *(theLine.p1.x), *(thePoint.y) - *(theLine.p1.y)};
840 double aCross = aVec[0] * aDir[1] - aVec[1] * aDir[0];
841 return fabs(aCross) < tolerance;
844 ConstraintWrapperPtr createConstraintMiddlePoint(
845 ConstraintPtr theConstraint,
846 const GroupID& theGroupID,
847 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
848 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
850 GCSPointPtr aPoint = thePoint->point();
851 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
853 return ConstraintWrapperPtr();
855 std::list<GCSConstraintPtr> aConstrList;
856 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintPointOnLine(*aPoint, *aLine)));
857 double aDist = lineLength(*aLine);
858 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aDistance =
859 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(createParameter(theGroupID, aDist));
860 aConstrList.push_back(GCSConstraintPtr(
861 new GCS::ConstraintP2PDistance(*aPoint, aLine->p1, aDistance->parameter())));
862 aConstrList.push_back(GCSConstraintPtr(
863 new GCS::ConstraintP2PDistance(*aPoint, aLine->p2, aDistance->parameter())));
865 // Workaround to avoid conflicting constraints when the point is already placed on line
866 if (thePoint->group() != GID_UNKNOWN && isPointOnLine(*aPoint, *aLine)) {
867 std::shared_ptr<GeomDataAPI_Point2D> aCoord =
868 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint->baseAttribute());
870 *(aPoint->x) = (*(aLine->p1.x) + *(aLine->p2.x)) * 0.5;
871 *(aPoint->y) = (*(aLine->p1.y) + *(aLine->p2.y)) * 0.5;
872 aCoord->setValue(*(aPoint->x), *(aPoint->y));
876 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(new PlaneGCSSolver_ConstraintWrapper(
877 theConstraint, aConstrList, CONSTRAINT_MIDDLE_POINT));
878 aResult->setGroup(theGroupID);
879 std::list<EntityWrapperPtr> aSubs(1, thePoint);
880 aSubs.push_back(theEntity);
881 aResult->setEntities(aSubs);
882 aResult->setValueParameter(aDistance);
887 ConstraintWrapperPtr createConstraintDistancePointPoint(
888 ConstraintPtr theConstraint,
889 const GroupID& theGroupID,
890 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
891 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
892 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2)
894 GCSConstraintPtr aNewConstr(new GCS::ConstraintP2PDistance(
895 *(thePoint1->point()), *(thePoint2->point()), theValue->parameter()));
897 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
898 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PT_PT_DISTANCE));
899 aResult->setGroup(theGroupID);
900 std::list<EntityWrapperPtr> aSubs(1, thePoint1);
901 aSubs.push_back(thePoint2);
902 aResult->setEntities(aSubs);
903 aResult->setValueParameter(theValue);
907 ConstraintWrapperPtr createConstraintDistancePointLine(
908 ConstraintPtr theConstraint,
909 const GroupID& theGroupID,
910 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
911 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
912 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
914 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
915 GCSConstraintPtr aNewConstr(new GCS::ConstraintP2LDistance(
916 *(thePoint->point()), *(aLine), theValue->parameter()));
918 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
919 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PT_LINE_DISTANCE));
920 aResult->setGroup(theGroupID);
921 std::list<EntityWrapperPtr> aSubs(1, thePoint);
922 aSubs.push_back(theEntity);
923 aResult->setEntities(aSubs);
924 aResult->setValueParameter(theValue);
928 ConstraintWrapperPtr createConstraintRadius(
929 ConstraintPtr theConstraint,
930 const GroupID& theGroupID,
931 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
932 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
934 std::shared_ptr<GCS::Circle> aCircle = std::dynamic_pointer_cast<GCS::Circle>(theEntity->entity());
935 GCSConstraintPtr aNewConstr(new GCS::ConstraintEqual(aCircle->rad, theValue->parameter()));
937 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
938 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_RADIUS));
939 aResult->setGroup(theGroupID);
940 std::list<EntityWrapperPtr> aSubs(1, theEntity);
941 aResult->setEntities(aSubs);
942 aResult->setValueParameter(theValue);
946 ConstraintWrapperPtr createConstraintAngle(
947 ConstraintPtr theConstraint,
948 const GroupID& theGroupID,
949 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
950 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
951 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
953 std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
954 std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
955 GCSConstraintPtr aNewConstr(new GCS::ConstraintL2LAngle(
956 *(aLine1), *(aLine2), theValue->parameter()));
958 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
959 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_ANGLE));
960 aResult->setGroup(theGroupID);
961 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
962 aSubs.push_back(theEntity2);
963 aResult->setEntities(aSubs);
964 aResult->setValueParameter(theValue);
968 ConstraintWrapperPtr createConstraintHorizVert(
969 ConstraintPtr theConstraint,
970 const GroupID& theGroupID,
971 const SketchSolver_ConstraintType& theType,
972 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
974 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
975 GCSConstraintPtr aNewConstr;
976 if (theType == CONSTRAINT_HORIZONTAL)
977 aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.y, aLine->p2.y));
979 aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.x, aLine->p2.x));
981 ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
982 theConstraint, aNewConstr, theType));
983 aResult->setGroup(theGroupID);
984 std::list<EntityWrapperPtr> aSubs(1, theEntity);
985 aResult->setEntities(aSubs);
989 ConstraintWrapperPtr createConstraintCollinear(
990 ConstraintPtr theConstraint,
991 const GroupID& theGroupID,
992 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
993 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
995 std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
996 std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
998 // create two point-on-line constraints
999 std::list<GCSConstraintPtr> aConstrList;
1000 aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p1, *aLine1)) );
1001 aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p2, *aLine1)) );
1003 ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
1004 theConstraint, aConstrList, CONSTRAINT_COLLINEAR));
1005 aResult->setGroup(theGroupID);
1006 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1007 aSubs.push_back(theEntity2);
1008 aResult->setEntities(aSubs);
1012 ConstraintWrapperPtr createConstraintParallel(
1013 ConstraintPtr theConstraint,
1014 const GroupID& theGroupID,
1015 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1016 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1018 std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1019 std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1020 GCSConstraintPtr aNewConstr(new GCS::ConstraintParallel(*(aLine1), *(aLine2)));
1022 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1023 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PARALLEL));
1024 aResult->setGroup(theGroupID);
1025 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1026 aSubs.push_back(theEntity2);
1027 aResult->setEntities(aSubs);
1031 ConstraintWrapperPtr createConstraintPerpendicular(
1032 ConstraintPtr theConstraint,
1033 const GroupID& theGroupID,
1034 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1035 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1037 std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1038 std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1039 GCSConstraintPtr aNewConstr(new GCS::ConstraintPerpendicular(*(aLine1), *(aLine2)));
1041 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1042 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PERPENDICULAR));
1043 aResult->setGroup(theGroupID);
1044 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1045 aSubs.push_back(theEntity2);
1046 aResult->setEntities(aSubs);
1050 ConstraintWrapperPtr createConstraintEqual(
1051 ConstraintPtr theConstraint,
1052 const GroupID& theGroupID,
1053 const SketchSolver_ConstraintType& theType,
1054 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1055 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
1056 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed)
1058 if (theType == CONSTRAINT_EQUAL_LINE_ARC)
1059 return ConstraintWrapperPtr(); // line-arc equivalence is not supported yet
1061 std::list<GCSConstraintPtr> aConstrList;
1062 if (theType == CONSTRAINT_EQUAL_LINES) {
1063 std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1064 std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1066 aConstrList.push_back(GCSConstraintPtr(
1067 new GCS::ConstraintP2PDistance(aLine1->p1, aLine1->p2, theIntermed->parameter())));
1068 aConstrList.push_back(GCSConstraintPtr(
1069 new GCS::ConstraintP2PDistance(aLine2->p1, aLine2->p2, theIntermed->parameter())));
1071 std::shared_ptr<GCS::Circle> aCirc1 =
1072 std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1073 std::shared_ptr<GCS::Circle> aCirc2 =
1074 std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1076 aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintEqual(aCirc1->rad, aCirc2->rad)));
1079 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1080 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, theType));
1081 aResult->setGroup(theGroupID);
1082 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1083 aSubs.push_back(theEntity2);
1084 aResult->setEntities(aSubs);
1086 aResult->setValueParameter(theIntermed);
1090 ConstraintWrapperPtr createConstraintTangent(
1091 ConstraintPtr theConstraint,
1092 const GroupID& theGroupID,
1093 const SketchSolver_ConstraintType& theType,
1094 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1095 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1097 GCSConstraintPtr aNewConstr;
1098 if (theType == CONSTRAINT_TANGENT_ARC_LINE || theType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
1099 std::shared_ptr<GCS::Circle> aCirc = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1100 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1102 aNewConstr = GCSConstraintPtr(new GCS::ConstraintP2LDistance(aCirc->center, *aLine, aCirc->rad));
1104 std::shared_ptr<GCS::Circle> aCirc1 = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1105 std::shared_ptr<GCS::Circle> aCirc2 = std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1107 double aDX = *(aCirc1->center.x) - *(aCirc2->center.x);
1108 double aDY = *(aCirc1->center.y) - *(aCirc2->center.y);
1109 double aDist = sqrt(aDX * aDX + aDY * aDY);
1110 aNewConstr = GCSConstraintPtr(new GCS::ConstraintTangentCircumf(aCirc1->center, aCirc2->center,
1111 aCirc1->rad, aCirc2->rad, (aDist < *(aCirc1->rad) || aDist < *(aCirc2->rad))));
1114 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1115 new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, theType));
1116 aResult->setGroup(theGroupID);
1117 std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1118 aSubs.push_back(theEntity2);
1119 aResult->setEntities(aSubs);
1127 void adjustAngle(ConstraintWrapperPtr theConstraint)
1129 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1131 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
1132 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
1134 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
1135 const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
1136 std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
1137 for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
1138 const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
1139 std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
1140 for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt)
1141 aPoints[i][j] = aBuilder->point(*aLPIt);
1144 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
1145 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
1146 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
1148 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
1149 if (!anIntersection)
1152 for (int i = 0; i < 2; i++) {
1153 for (int j = 0; j < 2; j++) {
1154 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
1155 if (fabs(aDist[i][j]) <= tolerance)
1158 if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
1159 aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
1160 // the intersection point is an inner point of the line,
1161 // we change the sign of distance till start point to calculate correct coordinates
1163 aDist[i][0] *= -1.0;
1166 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
1167 for (int i = 0; i < 2; i++) {
1168 if (aDist[i][1] > fabs(aDist[i][0]))
1169 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
1170 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
1172 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
1173 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
1174 // main direction is opposite => change signs
1175 if (aDist[i][0] < 0.0) {
1176 aDist[i][0] *= -1.0;
1177 aDist[i][1] *= -1.0;
1182 double anAngle = aLine[0]->direction()->angle(aLine[1]->direction()) / PI * 180;
1183 if (anAngle * aConstraint->value() < 0.0)
1184 aConstraint->setValue(-aConstraint->value());
1185 if ((90.0 - fabs(anAngle)) * (fabs(aConstraint->value()) - 90.0) > 0.0) {
1186 if (aConstraint->value() < 0.0)
1187 aConstraint->setValue(-180.0 - aConstraint->value());
1189 aConstraint->setValue(180.0 - aConstraint->value());
1193 void makeMirrorPoints(EntityWrapperPtr theOriginal,
1194 EntityWrapperPtr theMirrored,
1195 EntityWrapperPtr theMirrorLine)
1197 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1199 std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
1200 std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
1201 // orthogonal direction
1202 aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
1204 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
1205 std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
1206 double aDist = aVec->dot(aMLDir->xy());
1207 aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
1208 double aCoord[2] = {aVec->x(), aVec->y()};
1209 std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
1210 for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
1211 (*aMIt)->setValue(aCoord[i]);
1213 // update corresponding attribute
1214 AttributePtr anAttr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMirrored)->baseAttribute();
1216 std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
1217 aMirroredPnt->setValue(aCoord[0], aCoord[1]);
1221 void adjustPtLineDistance(ConstraintWrapperPtr theConstraint)
1223 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1225 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1226 std::shared_ptr<GeomAPI_Lin2d> aLine;
1227 std::list<EntityWrapperPtr> aSubs = theConstraint->entities();
1228 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
1229 for (; aSIt != aSubs.end(); ++aSIt) {
1230 if ((*aSIt)->type() == ENTITY_POINT)
1231 aPoint = aBuilder->point(*aSIt);
1232 else if ((*aSIt)->type() == ENTITY_LINE)
1233 aLine = aBuilder->line(*aSIt);
1236 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
1237 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
1238 if (aPtLineVec->cross(aLineVec) * theConstraint->value() < 0.0)
1239 theConstraint->setValue(theConstraint->value() * (-1.0));