1 #include <SketchSolver_ConstraintMirror.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
5 #include <ModelAPI_AttributeDouble.h>
6 #include <ModelAPI_AttributeRefAttr.h>
7 #include <ModelAPI_AttributeRefList.h>
8 #include <ModelAPI_ResultConstruction.h>
10 #include <GeomAPI_Dir2d.h>
11 #include <GeomAPI_XY.h>
14 void SketchSolver_ConstraintMirror::getAttributes(
15 Slvs_Entity& theMirrorLine,
16 std::vector<Slvs_Entity>& theBaseEntities,
17 std::vector<Slvs_Entity>& theMirrorEntities)
19 DataPtr aData = myBaseConstraint->data();
20 AttributeRefAttrPtr aMirLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
21 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
22 if (!aMirLineAttr || !aMirLineAttr->isInitialized() || !aMirLineAttr->isObject()) {
23 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
26 int aType = SLVS_E_UNKNOWN; // type of created entity
27 Slvs_hEntity anEntity = myGroup->getAttributeId(aMirLineAttr);
28 if (anEntity == SLVS_E_UNKNOWN)
29 anEntity = changeEntity(aMirLineAttr, aType);
30 theMirrorLine = myStorage->getEntity(anEntity);
32 // Create SolveSpace entity for all features
33 AttributeRefListPtr aBaseRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
34 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
35 AttributeRefListPtr aMirroredRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
36 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_C()));
37 myNumberOfObjects = aMirroredRefList->size();
38 if (!aBaseRefList || !aMirroredRefList) {
39 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
43 std::list<ObjectPtr> aBaseList = aBaseRefList->list();
44 std::list<ObjectPtr> aMirroredList = aMirroredRefList->list();
47 ResultConstructionPtr aRC;
48 for (int i = 0; i < 2; i++) {
49 std::list<ObjectPtr>::iterator anIter = i == 0 ? aBaseList.begin() : aMirroredList.begin();
50 std::list<ObjectPtr>::iterator aEndIter = i == 0 ? aBaseList.end() : aMirroredList.end();
51 std::vector<Slvs_Entity>* aList = i == 0 ? &theBaseEntities : & theMirrorEntities;
52 for ( ; anIter != aEndIter; anIter++) {
53 aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIter);
54 aFeature = aRC ? aRC->document()->feature(aRC) :
55 std::dynamic_pointer_cast<ModelAPI_Feature>(*anIter);
59 anEntity = changeEntity(aFeature, aType);
60 aList->push_back(myStorage->getEntity(anEntity));
64 if (theBaseEntities.size() > theMirrorEntities.size())
65 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
68 void SketchSolver_ConstraintMirror::process()
71 if (!myBaseConstraint || !myStorage || myGroup == 0) {
72 /// TODO: Put error message here
75 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
76 update(myBaseConstraint);
78 Slvs_Entity aMirrorLine;
79 std::vector<Slvs_Entity> aBaseList;
80 std::vector<Slvs_Entity> aMirrorList;
81 getAttributes(aMirrorLine, aBaseList, aMirrorList);
82 if (!myErrorMsg.empty())
85 if (aBaseList.size() != aMirrorList.size()) {
86 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
90 Slvs_Constraint aConstraint;
91 // Get coordinates of mirror line points for speed up
93 for (int i = 0; i < 2; i++) {
94 Slvs_Entity aPoint = myStorage->getEntity(aMirrorLine.point[i]);
95 for (int j = 0; j < 2; j++)
96 aStartEnd[2*i+j] = myStorage->getParameter(aPoint.param[j]).val;
99 std::vector<Slvs_Entity>::iterator aBaseIter = aBaseList.begin();
100 std::vector<Slvs_Entity>::iterator aMirrorIter = aMirrorList.begin();
101 for (; aBaseIter != aBaseList.end(); aBaseIter++, aMirrorIter++) {
102 // Make aMirrorEnt parameters to be symmetric with aBaseEnt
103 makeMirrorEntity(*aBaseIter, *aMirrorIter, aStartEnd);
105 if (aBaseIter->type == SLVS_E_POINT_IN_2D) {
106 aConstraint = Slvs_MakeConstraint(
107 SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
108 0.0, aBaseIter->h, aMirrorIter->h, aMirrorLine.h, SLVS_E_UNKNOWN);
109 aConstraint.h = myStorage->addConstraint(aConstraint);
110 mySlvsConstraints.push_back(aConstraint.h);
111 } else if (aBaseIter->type == SLVS_E_LINE_SEGMENT) {
112 for (int i = 0; i < 2; i++) {
113 aConstraint = Slvs_MakeConstraint(
114 SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(), 0.0,
115 aBaseIter->point[i], aMirrorIter->point[i], aMirrorLine.h, SLVS_E_UNKNOWN);
116 aConstraint.h = myStorage->addConstraint(aConstraint);
117 mySlvsConstraints.push_back(aConstraint.h);
119 } else if (aBaseIter->type == SLVS_E_CIRCLE) {
120 aConstraint = Slvs_MakeConstraint(
121 SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(), 0.0,
122 aBaseIter->point[0], aMirrorIter->point[0], aMirrorLine.h, SLVS_E_UNKNOWN);
123 aConstraint.h = myStorage->addConstraint(aConstraint);
124 mySlvsConstraints.push_back(aConstraint.h);
125 // Additional constraint for equal radii
126 Slvs_Constraint anEqRadConstr = Slvs_MakeConstraint(
127 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_EQUAL_RADIUS, myGroup->getWorkplaneId(),
128 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aBaseIter->h, aMirrorIter->h);
129 anEqRadConstr.h = myStorage->addConstraint(anEqRadConstr);
130 mySlvsConstraints.push_back(anEqRadConstr.h);
131 } else if (aBaseIter->type == SLVS_E_ARC_OF_CIRCLE) {
132 // Workaround to avoid problems in SolveSpace.
133 // The symmetry of two arcs will be done using symmetry of three points on these arcs:
134 // start point, end point, and any other point on the arc
135 Slvs_hEntity aBaseArcPoints[3] = {
139 Slvs_hEntity aMirrorArcPoints[3] = { // indices of points of arc, center corresponds center, first point corresponds last point
140 aMirrorIter->point[2],
141 aMirrorIter->point[1],
144 Slvs_Entity aBothArcs[2] = {*aBaseIter, *aMirrorIter};
145 Slvs_hEntity aBothMiddlePoints[2];
146 for (int i = 0; i < 2; i++) {
148 calculateMiddlePoint(aBothArcs[i], 0.5, x, y);
149 Slvs_Param aParamX = Slvs_MakeParam(SLVS_E_UNKNOWN, myGroup->getId(), x);
150 Slvs_Param aParamY = Slvs_MakeParam(SLVS_E_UNKNOWN, myGroup->getId(), y);
151 aParamX.h = myStorage->addParameter(aParamX);
152 aParamY.h = myStorage->addParameter(aParamY);
153 Slvs_Entity aPoint = Slvs_MakePoint2d(SLVS_E_UNKNOWN, myGroup->getId(),
154 myGroup->getWorkplaneId(), aParamX.h, aParamY.h);
155 aBothMiddlePoints[i] = myStorage->addEntity(aPoint);
156 // additional constraint point-on-curve
157 Slvs_Constraint aPonCircConstr = Slvs_MakeConstraint(
158 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_PT_ON_CIRCLE, myGroup->getWorkplaneId(),
159 0.0, aBothMiddlePoints[i], SLVS_E_UNKNOWN, aBothArcs[i].h, SLVS_E_UNKNOWN);
160 aPonCircConstr.h = myStorage->addConstraint(aPonCircConstr);
161 mySlvsConstraints.push_back(aPonCircConstr.h);
163 // additional constraint for the point to be in the middle of a base arc
164 Slvs_Entity aLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
165 myGroup->getWorkplaneId(), aBothArcs[i].point[1], aBothMiddlePoints[i]);
166 aLine1.h = myStorage->addEntity(aLine1);
167 Slvs_Entity aLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
168 myGroup->getWorkplaneId(), aBothArcs[i].point[2], aBothMiddlePoints[i]);
169 aLine2.h = myStorage->addEntity(aLine2);
170 Slvs_Constraint aMiddleConstr = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
171 SLVS_C_EQUAL_LENGTH_LINES, myGroup->getWorkplaneId(),
172 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aLine1.h, aLine2.h);
173 aMiddleConstr.h = myStorage->addConstraint(aMiddleConstr);
174 mySlvsConstraints.push_back(aMiddleConstr.h);
178 aBaseArcPoints[2] = aBothMiddlePoints[0];
179 aMirrorArcPoints[2] = aBothMiddlePoints[1];
180 for (int ind = 0; ind < 3; ind++) {
181 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
182 SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(), 0.0,
183 aBaseArcPoints[ind], aMirrorArcPoints[ind], aMirrorLine.h, SLVS_E_UNKNOWN);
184 aConstraint.h = myStorage->addConstraint(aConstraint);
185 mySlvsConstraints.push_back(aConstraint.h);
190 // Set the mirror line unchanged during constraint recalculation
191 for (int i = 0; i < 2; i++) {
192 if (myStorage->isPointFixed(aMirrorLine.point[i], aConstraint.h, true))
194 aConstraint = Slvs_MakeConstraint(
195 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
196 aMirrorLine.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
197 aConstraint.h = myStorage->addConstraint(aConstraint);
198 mySlvsConstraints.push_back(aConstraint.h);
203 void SketchSolver_ConstraintMirror::update(ConstraintPtr theConstraint)
206 if (!theConstraint || theConstraint == myBaseConstraint) {
207 AttributeRefListPtr aMirroredRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
208 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_C()));
209 if (aMirroredRefList->size() != myNumberOfObjects) {
210 remove(myBaseConstraint);
215 SketchSolver_Constraint::update();
218 bool SketchSolver_ConstraintMirror::remove(ConstraintPtr theConstraint)
221 if (theConstraint && theConstraint != myBaseConstraint)
223 bool isFullyRemoved = true;
224 std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
225 for (; aCIter != mySlvsConstraints.end(); aCIter++)
226 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
227 mySlvsConstraints.clear();
229 if (isFullyRemoved) {
230 myFeatureMap.clear();
231 myAttributeMap.clear();
234 cleanRemovedEntities();
238 bool SketchSolver_ConstraintMirror::checkAttributesChanged(ConstraintPtr theConstraint)
240 // First of all, check the mirror line is changed.
241 // It may be changed to one of mirrored lines, which is already in this constraint
242 // (this case is not marked as attribute changing)
243 ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
244 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
245 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
246 if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
248 FeaturePtr aMirrorLine = ModelAPI_Feature::feature(aRefAttr->object());
252 std::map<FeaturePtr, Slvs_hEntity>::iterator aMirrorIter = myFeatureMap.find(aMirrorLine);
253 if (aMirrorIter == myFeatureMap.end())
256 // Check the entity is not used as mirror line
257 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
258 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
259 Slvs_Constraint aMirrorConstr = myStorage->getConstraint(*aCIter);
260 if (aMirrorConstr.type != SLVS_C_SYMMETRIC_LINE)
262 if (aMirrorConstr.entityA != aMirrorIter->second)
264 else break; // check just one symmetric constraint
268 return SketchSolver_Constraint::checkAttributesChanged(theConstraint);
271 void SketchSolver_ConstraintMirror::makeMirrorEntity(
272 const Slvs_Entity& theBase,
273 const Slvs_Entity& theMirror,
274 const double theMirrorLine[]) const
276 Slvs_hEntity aBasePoint[4];
277 Slvs_hEntity aMirrorPoint[4];
278 for (int i = 0; i < 4; i++) {
279 aBasePoint[i] = theBase.point[i];
280 aMirrorPoint[i] = theMirror.point[i];
282 if (theBase.type == SLVS_E_ARC_OF_CIRCLE) {
283 Slvs_hEntity aTmp = aMirrorPoint[2];
284 aMirrorPoint[2] = aMirrorPoint[1];
285 aMirrorPoint[1] = aTmp;
287 if (theBase.type == SLVS_E_POINT_IN_2D || theBase.type == SLVS_E_POINT_IN_3D) {
288 aBasePoint[0] = theBase.h;
289 aMirrorPoint[0] = theMirror.h;
292 // Mirror line parameters
293 std::shared_ptr<GeomAPI_XY> aLinePoints[2];
294 for (int i = 0; i < 2; i++)
295 aLinePoints[i] = std::shared_ptr<GeomAPI_XY>(
296 new GeomAPI_XY(theMirrorLine[2*i], theMirrorLine[2*i+1]));
297 // direction of a mirror line
298 std::shared_ptr<GeomAPI_Dir2d> aDir = std::shared_ptr<GeomAPI_Dir2d>(
299 new GeomAPI_Dir2d(aLinePoints[1]->added(aLinePoints[0]->multiplied(-1.0))));
300 // orthogonal direction
301 aDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir->y(), -aDir->x()));
303 for (int i = 0; i < 4; i++) {
304 if (aBasePoint[i] == SLVS_E_UNKNOWN || aMirrorPoint[i] == SLVS_E_UNKNOWN)
306 Slvs_Entity aPointEnt = myStorage->getEntity(aBasePoint[i]);
307 double aBaseX = myStorage->getParameter(aPointEnt.param[0]).val;
308 double aBaseY = myStorage->getParameter(aPointEnt.param[1]).val;
309 std::shared_ptr<GeomAPI_XY> aPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(aBaseX, aBaseY));
311 std::shared_ptr<GeomAPI_XY> aVec = std::shared_ptr<GeomAPI_XY>(
312 new GeomAPI_XY(aPoint->x() - aLinePoints[0]->x(), aPoint->y() - aLinePoints[0]->y()));
313 double aDist = aVec->dot(aDir->xy());
314 aPoint = aPoint->added(aDir->xy()->multiplied(-2.0 * aDist));
316 Slvs_Entity aMirrorEnt = myStorage->getEntity(aMirrorPoint[i]);
317 Slvs_Param aParam = Slvs_MakeParam(aMirrorEnt.param[0], myGroup->getId(), aPoint->x());
318 myStorage->updateParameter(aParam);
319 aParam = Slvs_MakeParam(aMirrorEnt.param[1], myGroup->getId(), aPoint->y());
320 myStorage->updateParameter(aParam);
324 void SketchSolver_ConstraintMirror::adjustConstraint()
326 // Search mirror between middle points on the arcs and recompute their coordinates
327 std::list<Slvs_Constraint> aPonCirc = myStorage->getConstraintsByType(SLVS_C_PT_ON_CIRCLE);
328 if (aPonCirc.empty())
331 AttributeRefAttrPtr aMirLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
332 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
333 if (!aMirLineAttr || !aMirLineAttr->isInitialized() || !aMirLineAttr->isObject()) {
334 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
337 ResultConstructionPtr aRC =
338 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aMirLineAttr->object());
339 FeaturePtr aFeature = aRC ? aRC->document()->feature(aRC) :
340 std::dynamic_pointer_cast<ModelAPI_Feature>(aMirLineAttr->object());
341 std::map<FeaturePtr, Slvs_hEntity>::iterator aMirLineIter = myFeatureMap.find(aFeature);
342 if (aMirLineIter == myFeatureMap.end())
344 Slvs_Entity aMirrorLine = myStorage->getEntity(aMirLineIter->second);
347 for (int i = 0; i < 2; i++) {
348 Slvs_Entity aPoint = myStorage->getEntity(aMirrorLine.point[i]);
349 for (int j = 0; j < 2; j++)
350 aStartEnd[2*i+j] = myStorage->getParameter(aPoint.param[j]).val;
353 Slvs_Constraint aMirror;
354 std::vector<Slvs_hConstraint>::iterator aConstrIter = mySlvsConstraints.begin();
355 for (; aConstrIter != mySlvsConstraints.end(); aConstrIter++) {
356 aMirror = myStorage->getConstraint(*aConstrIter);
357 if (aMirror.type != SLVS_C_SYMMETRIC_LINE)
359 Slvs_Constraint aPonCircA, aPonCircB;
360 aPonCircA.h = SLVS_E_UNKNOWN;
361 aPonCircB.h = SLVS_E_UNKNOWN;
362 std::list<Slvs_Constraint>::iterator aPtIter = aPonCirc.begin();
363 for (; aPtIter != aPonCirc.end(); aPtIter++) {
364 if (aMirror.ptA == aPtIter->ptA)
365 aPonCircA = *aPtIter;
366 if (aMirror.ptB == aPtIter->ptA)
367 aPonCircB = *aPtIter;
369 if (aPonCircA.h == SLVS_E_UNKNOWN || aPonCircB.h == SLVS_E_UNKNOWN)
372 bool aNeedToResolve = myStorage->isNeedToResolve();
373 // Calculate middle point for base arc and mirrored point on mirror arc
374 Slvs_Entity aBaseArc = myStorage->getEntity(aPonCircA.entityA);
375 Slvs_Entity aBasePoint = myStorage->getEntity(aPonCircA.ptA);
376 Slvs_Param aParamX = myStorage->getParameter(aBasePoint.param[0]);
377 Slvs_Param aParamY = myStorage->getParameter(aBasePoint.param[1]);
378 calculateMiddlePoint(aBaseArc, 0.5, aParamX.val, aParamY.val);
379 myStorage->updateParameter(aParamX);
380 myStorage->updateParameter(aParamY);
381 Slvs_Entity aMirrorArc = myStorage->getEntity(aPonCircB.entityA);
382 Slvs_Entity aMirrorPoint = myStorage->getEntity(aPonCircB.ptA);
383 aParamX = myStorage->getParameter(aMirrorPoint.param[0]);
384 aParamY = myStorage->getParameter(aMirrorPoint.param[1]);
385 calculateMiddlePoint(aMirrorArc, 0.5, aParamX.val, aParamY.val);
386 myStorage->updateParameter(aParamX);
387 myStorage->updateParameter(aParamY);
388 // To avoid looped recalculations of sketch
389 myStorage->setNeedToResolve(aNeedToResolve);