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