1 #include <SketchSolver_ConstraintMirror.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.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>
15 void SketchSolver_ConstraintMirror::getAttributes(
16 EntityWrapperPtr& theMirrorLine,
17 std::vector<EntityWrapperPtr>& theBaseEntities,
18 std::vector<EntityWrapperPtr>& theMirrorEntities)
20 AttributePtr aMirLineAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
21 AttributeRefAttrPtr aMirLineRefAttr =
22 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aMirLineAttr);
23 if (!aMirLineRefAttr || !aMirLineRefAttr->isInitialized() || !aMirLineRefAttr->isObject()) {
24 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
28 myType = TYPE(myBaseConstraint);
29 myStorage->update(aMirLineAttr, myGroupID);
30 theMirrorLine = myStorage->entity(aMirLineAttr);
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 for (int i = 0; i < 2; i++) {
48 std::list<ObjectPtr>::iterator anIter = i == 0 ? aBaseList.begin() : aMirroredList.begin();
49 std::list<ObjectPtr>::iterator aEndIter = i == 0 ? aBaseList.end() : aMirroredList.end();
50 std::vector<EntityWrapperPtr>* aList = i == 0 ? &theBaseEntities : & theMirrorEntities;
51 for ( ; anIter != aEndIter; anIter++) {
52 aFeature = ModelAPI_Feature::feature(*anIter);
56 //// anEntity = changeEntity(aFeature, aType);
57 //// // Sort entities by their type
58 //// std::vector<Slvs_Entity>::iterator anIt = aList->begin();
59 //// for (; anIt != aList->end(); anIt++)
60 //// if (aType < anIt->type)
62 ////// aList->push_back(myStorage->getEntity(anEntity));
63 //// aList->insert(anIt, myStorage->getEntity(anEntity));
64 myStorage->update(aFeature, myGroupID);
65 aList->push_back(myStorage->entity(aFeature));
69 if (theBaseEntities.size() > theMirrorEntities.size())
70 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
73 void SketchSolver_ConstraintMirror::process()
76 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
77 // Not enough parameters are assigned
80 //// if (!mySlvsConstraints.empty()) // some data is changed, update constraint
81 //// update(myBaseConstraint);
83 EntityWrapperPtr aMirrorLine;
84 std::vector<EntityWrapperPtr> aBaseList;
85 std::vector<EntityWrapperPtr> aMirrorList;
86 getAttributes(aMirrorLine, aBaseList, aMirrorList);
87 if (!myErrorMsg.empty())
90 if (aBaseList.size() != aMirrorList.size()) {
91 myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
95 std::list<ConstraintWrapperPtr> aNewConstraints;
96 SketchSolver_ConstraintType aConstrType = getType();
97 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
98 std::list<ConstraintWrapperPtr> aMirConstrList;
100 std::vector<EntityWrapperPtr>::iterator aBIt = aBaseList.begin();
101 std::vector<EntityWrapperPtr>::iterator aMIt = aMirrorList.begin();
102 for (; aBIt != aBaseList.end(); ++aBIt, ++aMIt) {
103 if ((*aBIt)->type() == ENTITY_ARC) {
104 // add new points on arcs and mirror them
105 EntityWrapperPtr aBasePnt = myStorage->calculateMiddlePoint(*aBIt, 0.5);
106 EntityWrapperPtr aMirrPnt = myStorage->calculateMiddlePoint(*aMIt, 0.5);
108 aNewConstraints = aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID,
109 CONSTRAINT_PT_ON_CIRCLE, 0.0, aBasePnt, EntityWrapperPtr(), *aBIt);
110 aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
111 // point on mirrored arc
112 aNewConstraints = aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID,
113 CONSTRAINT_PT_ON_CIRCLE, 0.0, aMirrPnt, EntityWrapperPtr(), *aMIt);
114 aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
115 // mirror these points
116 aNewConstraints = aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID,
117 aConstrType, 0.0, aBasePnt, aMirrPnt, aMirrorLine);
118 aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
120 aNewConstraints = aBuilder->createConstraint(
121 myBaseConstraint, myGroupID, mySketchID, aConstrType,
122 0.0, *aBIt, *aMIt, aMirrorLine);
123 aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
126 myStorage->addConstraint(myBaseConstraint, aMirConstrList);
130 void SketchSolver_ConstraintMirror::update()
133 AttributeRefListPtr aMirroredRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
134 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_C()));
135 if (aMirroredRefList->size() != myNumberOfObjects) {
140 SketchSolver_Constraint::update();
143 ////bool SketchSolver_ConstraintMirror::remove(ConstraintPtr theConstraint)
145 //// cleanErrorMsg();
146 //// if (theConstraint && theConstraint != myBaseConstraint)
148 //// bool isFullyRemoved = true;
149 //// std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
150 //// for (; aCIter != mySlvsConstraints.end(); aCIter++)
151 //// isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
152 //// mySlvsConstraints.clear();
154 //// std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
155 //// for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
156 //// myStorage->removeEntity(aFeatIt->second);
158 //// if (isFullyRemoved) {
159 //// myFeatureMap.clear();
160 //// myAttributeMap.clear();
161 //// myValueMap.clear();
163 //// cleanRemovedEntities();
167 ////bool SketchSolver_ConstraintMirror::checkAttributesChanged(ConstraintPtr theConstraint)
169 //// // First of all, check the mirror line is changed.
170 //// // It may be changed to one of mirrored lines, which is already in this constraint
171 //// // (this case is not marked as attribute changing)
172 //// ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
173 //// AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
174 //// aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
175 //// if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
177 //// FeaturePtr aMirrorLine = ModelAPI_Feature::feature(aRefAttr->object());
178 //// if (!aMirrorLine)
181 //// std::map<FeaturePtr, Slvs_hEntity>::iterator aMirrorIter = myFeatureMap.find(aMirrorLine);
182 //// if (aMirrorIter == myFeatureMap.end())
185 //// // Check the entity is not used as mirror line
186 //// std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
187 //// for (; aCIter != mySlvsConstraints.end(); aCIter++) {
188 //// Slvs_Constraint aMirrorConstr = myStorage->getConstraint(*aCIter);
189 //// if (aMirrorConstr.type != SLVS_C_SYMMETRIC_LINE)
191 //// if (aMirrorConstr.entityA != aMirrorIter->second)
193 //// else break; // check just one symmetric constraint
196 //// // Base verification
197 //// return SketchSolver_Constraint::checkAttributesChanged(theConstraint);
200 ////void SketchSolver_ConstraintMirror::makeMirrorEntity(
201 //// const Slvs_Entity& theBase,
202 //// const Slvs_Entity& theMirror,
203 //// const double theMirrorLine[]) const
205 //// Slvs_hEntity aBasePoint[4];
206 //// Slvs_hEntity aMirrorPoint[4];
207 //// for (int i = 0; i < 4; i++) {
208 //// aBasePoint[i] = theBase.point[i];
209 //// aMirrorPoint[i] = theMirror.point[i];
211 //// if (theBase.type == SLVS_E_ARC_OF_CIRCLE) {
212 //// Slvs_hEntity aTmp = aMirrorPoint[2];
213 //// aMirrorPoint[2] = aMirrorPoint[1];
214 //// aMirrorPoint[1] = aTmp;
215 //// adjustArcPoints(theBase);
217 //// if (theBase.type == SLVS_E_POINT_IN_2D || theBase.type == SLVS_E_POINT_IN_3D) {
218 //// aBasePoint[0] = theBase.h;
219 //// aMirrorPoint[0] = theMirror.h;
222 //// // Mirror line parameters
223 //// std::shared_ptr<GeomAPI_XY> aLinePoints[2];
224 //// for (int i = 0; i < 2; i++)
225 //// aLinePoints[i] = std::shared_ptr<GeomAPI_XY>(
226 //// new GeomAPI_XY(theMirrorLine[2*i], theMirrorLine[2*i+1]));
227 //// // direction of a mirror line
228 //// std::shared_ptr<GeomAPI_Dir2d> aDir = std::shared_ptr<GeomAPI_Dir2d>(
229 //// new GeomAPI_Dir2d(aLinePoints[1]->added(aLinePoints[0]->multiplied(-1.0))));
230 //// // orthogonal direction
231 //// aDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir->y(), -aDir->x()));
233 //// Slvs_hConstraint aFixed; // transient variable
234 //// for (int i = 0; i < 4; i++) {
235 //// if (aBasePoint[i] == SLVS_E_UNKNOWN || aMirrorPoint[i] == SLVS_E_UNKNOWN)
237 //// // check the mirror point is not fixed
238 //// if (myStorage->isPointFixed(aMirrorPoint[i], aFixed, true))
241 //// Slvs_Entity aPointEnt = myStorage->getEntity(aBasePoint[i]);
242 //// double aBaseX = myStorage->getParameter(aPointEnt.param[0]).val;
243 //// double aBaseY = myStorage->getParameter(aPointEnt.param[1]).val;
244 //// std::shared_ptr<GeomAPI_XY> aPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(aBaseX, aBaseY));
246 //// std::shared_ptr<GeomAPI_XY> aVec = std::shared_ptr<GeomAPI_XY>(
247 //// new GeomAPI_XY(aPoint->x() - aLinePoints[0]->x(), aPoint->y() - aLinePoints[0]->y()));
248 //// double aDist = aVec->dot(aDir->xy());
249 //// aPoint = aPoint->added(aDir->xy()->multiplied(-2.0 * aDist));
251 //// Slvs_Entity aMirrorEnt = myStorage->getEntity(aMirrorPoint[i]);
252 //// Slvs_Param aParam = Slvs_MakeParam(aMirrorEnt.param[0], myGroup->getId(), aPoint->x());
253 //// myStorage->updateParameter(aParam);
254 //// aParam = Slvs_MakeParam(aMirrorEnt.param[1], myGroup->getId(), aPoint->y());
255 //// myStorage->updateParameter(aParam);
259 ////void SketchSolver_ConstraintMirror::adjustArcPoints(const Slvs_Entity& theArc) const
261 //// Slvs_Param aParam;
262 //// Slvs_Entity aPoint;
263 //// double anArcParams[3][2];
264 //// for (int i = 0; i < 3; i++) {
265 //// aPoint = myStorage->getEntity(theArc.point[i]);
266 //// for (int j = 0; j < 2; j++) {
267 //// aParam = myStorage->getParameter(aPoint.param[j]);
268 //// anArcParams[i][j] = aParam.val;
270 //// anArcParams[i][j] -= anArcParams[0][j];
273 //// double aRad2 = anArcParams[1][0] * anArcParams[1][0] + anArcParams[1][1] * anArcParams[1][1];
274 //// double aDist2 = anArcParams[2][0] * anArcParams[2][0] + anArcParams[2][1] * anArcParams[2][1];
275 //// if (std::fabs(aRad2 - aDist2) < tolerance)
276 //// return; // nothing to update (last point already on the arc)
277 //// if (aDist2 < tolerance)
278 //// return; // unable to update
279 //// double aCoeff = std::sqrt(aRad2 / aDist2);
280 //// anArcParams[2][0] *= aCoeff;
281 //// anArcParams[2][1] *= aCoeff;
283 //// // Update last point
284 //// aPoint = myStorage->getEntity(theArc.point[2]);
285 //// for (int i = 0; i < 2; i++) {
286 //// aParam = Slvs_MakeParam(aPoint.param[i], myGroup->getId(),
287 //// anArcParams[0][i] + anArcParams[2][i]);
288 //// myStorage->updateParameter(aParam);
292 void SketchSolver_ConstraintMirror::adjustConstraint()
294 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
296 const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
297 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
298 for (; aCIt != aConstraints.end(); ++aCIt)
299 if ((*aCIt)->type() == CONSTRAINT_SYMMETRIC)
300 aBuilder->adjustConstraint(*aCIt);
302 //// AttributeRefAttrPtr aMirLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
303 //// myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
304 //// if (!aMirLineAttr || !aMirLineAttr->isInitialized() || !aMirLineAttr->isObject()) {
305 //// myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
308 //// ResultConstructionPtr aRC =
309 //// std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aMirLineAttr->object());
310 //// FeaturePtr aFeature = aRC ? aRC->document()->feature(aRC) :
311 //// std::dynamic_pointer_cast<ModelAPI_Feature>(aMirLineAttr->object());
312 //// std::map<FeaturePtr, Slvs_hEntity>::iterator aMirLineIter = myFeatureMap.find(aFeature);
313 //// if (aMirLineIter == myFeatureMap.end())
315 //// Slvs_Entity aMirrorLine = myStorage->getEntity(aMirLineIter->second);
317 //// Slvs_Constraint aMirror;
318 //// double aStartEnd[4];
319 //// for (int i = 0; i < 2; i++) {
320 //// Slvs_Entity aPoint = myStorage->getEntity(aMirrorLine.point[i]);
321 //// for (int j = 0; j < 2; j++)
322 //// aStartEnd[2*i+j] = myStorage->getParameter(aPoint.param[j]).val;
325 //// // Calculate length of the mirror line and create temporary constraint
326 //// double aLength = sqrt((aStartEnd[2] - aStartEnd[0]) * (aStartEnd[2] - aStartEnd[0]) +
327 //// (aStartEnd[3] - aStartEnd[1]) * (aStartEnd[3] - aStartEnd[1]));
328 //// if (aLength < tolerance) {
329 //// if (myMirrorLineLength < 1.0)
330 //// myMirrorLineLength = 1.0;
332 //// myMirrorLineLength = aLength;
333 //// std::list<Slvs_Constraint> aDist = myStorage->getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
334 //// std::list<Slvs_Constraint>::const_iterator aDIt = aDist.begin();
335 //// for (; aDIt != aDist.end(); ++aDIt)
336 //// if ((aDIt->ptA == aMirrorLine.point[0] && aDIt->ptB == aMirrorLine.point[1]) ||
337 //// (aDIt->ptA == aMirrorLine.point[1] && aDIt->ptB == aMirrorLine.point[0]))
338 //// break; // length of mirror line is already set
339 //// if (aDIt == aDist.end()) {
340 //// // check the points of mirror line is not fixed
341 //// Slvs_hConstraint aFixed;
342 //// if (!myStorage->isPointFixed(aMirrorLine.point[0], aFixed, true) ||
343 //// !myStorage->isPointFixed(aMirrorLine.point[1], aFixed, true)) {
344 //// // Add length constraint
345 //// aMirror = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_PT_PT_DISTANCE,
346 //// myGroup->getWorkplaneId(), aLength, aMirrorLine.point[0], aMirrorLine.point[1],
347 //// SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
348 //// aMirror.h = myStorage->addConstraint(aMirror);
349 //// myStorage->addTemporaryConstraint(aMirror.h);
353 //// // Search mirror between middle points on the arcs and recompute their coordinates
354 //// std::map<Slvs_hEntity, Slvs_hEntity> aPointsOnCircles;
355 //// std::list<Slvs_Constraint> aMirrorPonCirc;
356 //// std::list<Slvs_Constraint> aPonCirc = myStorage->getConstraintsByType(SLVS_C_PT_ON_CIRCLE);
357 //// std::vector<Slvs_hConstraint>::iterator aConstrIter = mySlvsConstraints.begin();
358 //// for (; aConstrIter != mySlvsConstraints.end(); aConstrIter++) {
359 //// aMirror = myStorage->getConstraint(*aConstrIter);
360 //// if (aMirror.type != SLVS_C_SYMMETRIC_LINE)
362 //// if (aMirror.entityA != aMirrorLine.h)
363 //// continue; // don't update another Mirror constraints
364 //// Slvs_Constraint aPonCircA, aPonCircB;
365 //// aPonCircA.h = SLVS_E_UNKNOWN;
366 //// aPonCircB.h = SLVS_E_UNKNOWN;
367 //// std::list<Slvs_Constraint>::iterator aPtIter = aPonCirc.begin();
368 //// for (; aPtIter != aPonCirc.end(); aPtIter++) {
369 //// if (aMirror.ptA == aPtIter->ptA)
370 //// aPonCircA = *aPtIter;
371 //// if (aMirror.ptB == aPtIter->ptA)
372 //// aPonCircB = *aPtIter;
374 //// if (aPonCircA.h == SLVS_E_UNKNOWN || aPonCircB.h == SLVS_E_UNKNOWN)
376 //// aMirrorPonCirc.push_back(aMirror);
377 //// // Store point IDs to avoid their recalculation twice
378 //// aPointsOnCircles[aPonCircA.ptA] = aPonCircA.entityA;
379 //// aPointsOnCircles[aPonCircB.ptA] = aPonCircB.entityA;
382 //// // Recalculate positions of mirroring points
383 //// std::list<Slvs_Constraint> aMirrorList = myStorage->getConstraintsByType(SLVS_C_SYMMETRIC_LINE);
384 //// std::list<Slvs_Constraint>::iterator aMirIter = aMirrorList.begin();
385 //// for (; aMirIter != aMirrorList.end(); aMirIter++) {
386 //// if (aMirIter->entityA != aMirrorLine.h)
387 //// continue; // don't update another Mirror constraints
388 //// if (aPointsOnCircles.find(aMirIter->ptA) != aPointsOnCircles.end())
389 //// continue; // Avoid mirroring points on circles
390 //// Slvs_Entity aBase = myStorage->getEntity(aMirIter->ptA);
391 //// Slvs_Entity aMirror = myStorage->getEntity(aMirIter->ptB);
392 //// makeMirrorEntity(aBase, aMirror, aStartEnd);
395 //// bool aNeedToResolve = myStorage->isNeedToResolve();
396 //// for (aMirIter = aMirrorPonCirc.begin(); aMirIter != aMirrorPonCirc.end(); aMirIter++) {
397 //// // Make centers of arcs symmetric
398 //// Slvs_Entity aBaseArc = myStorage->getEntity(aPointsOnCircles[aMirIter->ptA]);
399 //// Slvs_Entity aBasePoint = myStorage->getEntity(aBaseArc.point[0]);
400 //// Slvs_Entity aMirrorArc = myStorage->getEntity(aPointsOnCircles[aMirIter->ptB]);
401 //// Slvs_Entity aMirrorPoint = myStorage->getEntity(aMirrorArc.point[0]);
402 //// makeMirrorEntity(aBasePoint, aMirrorPoint, aStartEnd);
403 //// // Calculate middle point for base arc and mirrored point on mirror arc
404 //// aBasePoint = myStorage->getEntity(aMirIter->ptA);
405 //// Slvs_Param aParamX = myStorage->getParameter(aBasePoint.param[0]);
406 //// Slvs_Param aParamY = myStorage->getParameter(aBasePoint.param[1]);
407 //// calculateMiddlePoint(aBaseArc, 0.5, aParamX.val, aParamY.val);
408 //// myStorage->updateParameter(aParamX);
409 //// myStorage->updateParameter(aParamY);
410 //// aMirrorPoint = myStorage->getEntity(aMirIter->ptB);
411 //// aParamX = myStorage->getParameter(aMirrorPoint.param[0]);
412 //// aParamY = myStorage->getParameter(aMirrorPoint.param[1]);
413 //// calculateMiddlePoint(aMirrorArc, 0.5, aParamX.val, aParamY.val);
414 //// myStorage->updateParameter(aParamX);
415 //// myStorage->updateParameter(aParamY);
417 //// // Restore previous value to avoid looped recalculations of sketch
418 //// myStorage->setNeedToResolve(aNeedToResolve);