Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintMirror.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintMirror.cpp
4 // Created: 17 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintMirror.h"
8
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
17
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Sketch.h>
20
21 #include <SketcherPrs_Factory.h>
22
23 #include <Config_PropManager.h>
24
25 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
26 {
27 }
28
29 void SketchPlugin_ConstraintMirror::initAttributes()
30 {
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   aSelection->setSelectionType("EDGE");
38   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
39   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
40 }
41
42 void SketchPlugin_ConstraintMirror::execute()
43 {
44   AttributeSelectionListPtr aMirrorObjectRefs =
45       selectionList(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
46
47   std::shared_ptr<ModelAPI_Data> aData = data();
48   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
49       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
50   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
51       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
52   std::list<ObjectPtr> anInitialList =  aRefListOfShapes->list();
53   std::list<ObjectPtr> aMirroredList =  aRefListOfMirrored->list();
54   std::vector<bool> isUsed(anInitialList.size(), false);
55   // add new items to the list
56   for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
57     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aMirrorObjectRefs->value(anInd);
58     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
59     std::vector<bool>::iterator aUsedIt = isUsed.begin();
60     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
61       if (*anIt == aSelect->context()) {
62         *aUsedIt = true;
63         break;
64       }
65     if (anIt == anInitialList.end())
66       aRefListOfShapes->append(aSelect->context());
67   }
68   // remove unused items
69   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
70   std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
71   std::vector<bool>::iterator aUsedIter = isUsed.begin();
72   for (; aUsedIter != isUsed.end(); aUsedIter++) {
73     if (!(*aUsedIter)) {
74       aRefListOfShapes->remove(*anInitIter);
75       if (aMirrorIter != aMirroredList.end()) {
76         aRefListOfMirrored->remove(*aMirrorIter);
77         // remove the corresponding feature from the sketch
78         ResultConstructionPtr aRC =
79             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
80         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
81         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
82         if (aFeature)
83           aDoc->removeFeature(aFeature);
84       }
85     }
86     if (anInitIter != anInitialList.end())
87       anInitIter++;
88     if (aMirrorIter != aMirroredList.end())
89       aMirrorIter++;
90   }
91
92   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
93
94   // Check consistency of initial list and mirrored list
95   anInitialList =  aRefListOfShapes->list();
96   anInitIter = anInitialList.begin();
97   aMirrorIter = aMirroredList.begin();
98   int indFirstWrong = 0; // index of element starts difference in the lists
99   std::set<int> anInvalidInd; // list of indices of removed features
100   std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
101   for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
102     // Add features and store indices of objects to remove
103     aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
104     ResultConstructionPtr aRCIn;
105     if (!aFeatureIn) {
106       aRCIn = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
107       if (aRCIn)
108         aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(
109             aRCIn->document()->feature(aRCIn));
110     }
111     if (aMirrorIter == aMirroredList.end())
112       aFeatureOut = std::shared_ptr<SketchPlugin_Feature>();
113     else {
114       aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter);
115       if (!aFeatureOut) {
116         ResultConstructionPtr aRC =
117             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
118         if (aRC)
119           aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(
120               aRC->document()->feature(aRC));
121       }
122     }
123     if (!aFeatureIn) {
124       if (aFeatureOut)
125         break; // the lists are inconsistent
126       continue;
127     }
128     if (!aFeatureOut) {
129       if (aMirrorIter != aMirroredList.end())
130         break; // the lists are inconsistent
131       // There is no mirrored object yet, create it
132       FeaturePtr aNewFeature = sketch()->addFeature(aFeatureIn->getKind());
133       aFeatureIn->data()->copyTo(aNewFeature->data());
134       aNewFeature->execute();
135       ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
136
137       std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
138       const std::list<ResultPtr>& aResults = aNewFeature->results();
139       std::list<ResultPtr>::const_iterator anIt = aResults.begin();
140       for (; anIt != aResults.end(); anIt++) {
141         ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
142         if (!aRC) continue;
143         std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
144         if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
145             (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
146             (aShapeIn->isFace() && aShapeOut->isFace()))
147           aRefListOfMirrored->append(aRC);
148       }
149       continue;
150     }
151     if (aFeatureIn->getKind() != aFeatureOut->getKind())
152       break; // the lists are inconsistent
153     if (!aFeatureIn->data()->isValid()) {
154       // initial feature was removed, delete it from lists
155       anInvalidInd.insert(indFirstWrong);
156     }
157     aMirrorIter++;
158   }
159   // Remove from the list objects already deleted before
160   std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
161   for ( ; anIt != anInvalidInd.rend(); anIt++) {
162     if (*anIt < indFirstWrong) indFirstWrong--;
163     aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
164     aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
165   }
166   // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
167   if (anInitIter != anInitialList.end()) {
168     while (aRefListOfMirrored->size() > indFirstWrong)
169       aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
170     // Create mirrored features instead of removed
171     anInitialList =  aRefListOfShapes->list();
172     anInitIter = anInitialList.begin();
173     for (int i = 0; i < indFirstWrong; i++) anInitIter++;
174     for ( ; anInitIter != anInitialList.end(); anInitIter++) {
175       aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
176       FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
177       aRefListOfMirrored->append(aNewFeature);
178     }
179   }
180 }
181
182 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
183 {
184   if (!sketch())
185     return thePrevious;
186
187   AISObjectPtr anAIS = thePrevious;
188   if (!anAIS) {
189     anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
190   }
191   return anAIS;
192 }
193
194