Salome HOME
Fixes for unit tests
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMirror.cpp
1 #include <SketchSolver_ConstraintMirror.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <ModelAPI_AttributeRefList.h>
6
7 void SketchSolver_ConstraintMirror::getAttributes(
8     EntityWrapperPtr& theMirrorLine,
9     std::vector<EntityWrapperPtr>& theBaseEntities,
10     std::vector<EntityWrapperPtr>& theMirrorEntities)
11 {
12   AttributePtr aMirLineAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
13   AttributeRefAttrPtr aMirLineRefAttr =
14       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aMirLineAttr);
15   if (!aMirLineRefAttr || !aMirLineRefAttr->isInitialized() || !aMirLineRefAttr->isObject()) {
16     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
17     return;
18   }
19
20   myType = TYPE(myBaseConstraint);
21   myStorage->update(aMirLineAttr/*, myGroupID*/);
22   theMirrorLine = myStorage->entity(aMirLineAttr);
23
24   // Create SolveSpace entity for all features
25   AttributeRefListPtr aBaseRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
26       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
27   AttributeRefListPtr aMirroredRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
28       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_C()));
29   myNumberOfObjects = aMirroredRefList->size();
30   if (!aBaseRefList || !aMirroredRefList) {
31     myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
32     return;
33   }
34
35   std::list<ObjectPtr> aBaseList = aBaseRefList->list();
36   std::list<ObjectPtr> aMirroredList = aMirroredRefList->list();
37
38   FeaturePtr aFeature;
39   for (int i = 0; i < 2; i++) {
40     std::list<ObjectPtr>::iterator anIter = i == 0 ? aBaseList.begin() : aMirroredList.begin();
41     std::list<ObjectPtr>::iterator aEndIter = i == 0 ? aBaseList.end() : aMirroredList.end();
42     std::vector<EntityWrapperPtr>* aList = i == 0 ? &theBaseEntities : & theMirrorEntities;
43     for ( ; anIter != aEndIter; anIter++) {
44       aFeature = ModelAPI_Feature::feature(*anIter);
45       if (!aFeature)
46         continue;
47
48       myStorage->update(aFeature, GID_UNKNOWN, true);
49       aList->push_back(myStorage->entity(aFeature));
50     }
51   }
52
53   if (theBaseEntities.size() > theMirrorEntities.size() || aMirroredList.empty())
54     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
55 }
56
57 void SketchSolver_ConstraintMirror::process()
58 {
59   cleanErrorMsg();
60   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
61     // Not enough parameters are assigned
62     return;
63   }
64
65   EntityWrapperPtr aMirrorLine;
66   std::vector<EntityWrapperPtr> aBaseList;
67   std::vector<EntityWrapperPtr> aMirrorList;
68   getAttributes(aMirrorLine, aBaseList, aMirrorList);
69   if (!myErrorMsg.empty())
70     return;
71
72   if (aBaseList.size() != aMirrorList.size()) {
73     myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
74     return;
75   }
76
77   std::list<ConstraintWrapperPtr> aNewConstraints;
78   SketchSolver_ConstraintType aConstrType = getType();
79   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
80   std::list<ConstraintWrapperPtr> aMirConstrList;
81
82   // update mirrored features to be in the current group
83   std::vector<EntityWrapperPtr>::iterator aMIt = aMirrorList.begin();
84   for (; aMIt != aMirrorList.end(); ++aMIt)
85     myStorage->update((*aMIt)->baseFeature(), myGroupID);
86
87   std::vector<EntityWrapperPtr>::iterator aBIt = aBaseList.begin();
88   for (aMIt = aMirrorList.begin(); aBIt != aBaseList.end(); ++aBIt, ++aMIt) {
89     aNewConstraints = aBuilder->createConstraint(
90         myBaseConstraint, myGroupID, mySketchID, aConstrType,
91         0.0, *aBIt, *aMIt, aMirrorLine);
92     aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
93   }
94   myStorage->addConstraint(myBaseConstraint, aMirConstrList);
95
96   adjustConstraint();
97 }
98
99
100 void SketchSolver_ConstraintMirror::update()
101 {
102   cleanErrorMsg();
103   remove();
104   process();
105 }
106
107 void SketchSolver_ConstraintMirror::adjustConstraint()
108 {
109   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
110
111   const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
112   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
113   for (; aCIt != aConstraints.end(); ++aCIt)
114     if ((*aCIt)->type() == CONSTRAINT_SYMMETRIC)
115       aBuilder->adjustConstraint(*aCIt);
116 }