]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintMirror.cpp
Salome HOME
Avoid the ""Conflicting constraints" message on constraint Mirror
[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/*, myGroupID*/);
49       aList->push_back(myStorage->entity(aFeature));
50     }
51   }
52
53   // Mirrored entities are placed out-of-group by default, due to they are copies.
54   // Place them into current group manually.
55   std::vector<EntityWrapperPtr>::iterator aMirIt = theMirrorEntities.begin();
56   for (; aMirIt != theMirrorEntities.end(); ++aMirIt)
57     (*aMirIt)->setGroup(myGroupID);
58
59   if (theBaseEntities.size() > theMirrorEntities.size())
60     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
61 }
62
63 void SketchSolver_ConstraintMirror::process()
64 {
65   cleanErrorMsg();
66   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
67     // Not enough parameters are assigned
68     return;
69   }
70
71   EntityWrapperPtr aMirrorLine;
72   std::vector<EntityWrapperPtr> aBaseList;
73   std::vector<EntityWrapperPtr> aMirrorList;
74   getAttributes(aMirrorLine, aBaseList, aMirrorList);
75   if (!myErrorMsg.empty())
76     return;
77
78   if (aBaseList.size() != aMirrorList.size()) {
79     myErrorMsg = SketchSolver_Error::INCORRECT_MIRROR_ATTRIBUTE();
80     return;
81   }
82
83   std::list<ConstraintWrapperPtr> aNewConstraints;
84   SketchSolver_ConstraintType aConstrType = getType();
85   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
86   std::list<ConstraintWrapperPtr> aMirConstrList;
87
88   std::vector<EntityWrapperPtr>::iterator aBIt = aBaseList.begin();
89   std::vector<EntityWrapperPtr>::iterator aMIt = aMirrorList.begin();
90   for (; aBIt != aBaseList.end(); ++aBIt, ++aMIt) {
91     aNewConstraints = aBuilder->createConstraint(
92         myBaseConstraint, myGroupID, mySketchID, aConstrType,
93         0.0, *aBIt, *aMIt, aMirrorLine);
94     aMirConstrList.insert(aMirConstrList.end(), aNewConstraints.begin(), aNewConstraints.end());
95   }
96
97   // update mirrored features to be in the current group
98   for (aMIt = aMirrorList.begin(); aMIt != aMirrorList.end(); ++aMIt)
99     myStorage->update((*aMIt)->baseFeature(), myGroupID);
100   myStorage->addConstraint(myBaseConstraint, aMirConstrList);
101
102   adjustConstraint();
103 }
104
105
106 void SketchSolver_ConstraintMirror::update()
107 {
108   cleanErrorMsg();
109   remove();
110   process();
111 }
112
113 void SketchSolver_ConstraintMirror::adjustConstraint()
114 {
115   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
116
117   const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
118   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
119   for (; aCIt != aConstraints.end(); ++aCIt)
120     if ((*aCIt)->type() == CONSTRAINT_SYMMETRIC)
121       aBuilder->adjustConstraint(*aCIt);
122 }