Salome HOME
fde907d815e9e9616819398e4e1263cd4b610fa1
[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_Session.h>
15 #include <ModelAPI_Validator.h>
16
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Sketch.h>
19
20 #include <SketcherPrs_Factory.h>
21
22 #include <Config_PropManager.h>
23
24 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
25 {
26 }
27
28 void SketchPlugin_ConstraintMirror::initAttributes()
29 {
30   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
33   AttributeSelectionListPtr aSelection = 
34     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35     SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36   aSelection->setSelectionType("EDGE");
37   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
38   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
39 }
40
41 void SketchPlugin_ConstraintMirror::execute()
42 {
43   AttributeSelectionListPtr aMirrorObjectRefs =
44       selectionList(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
45   if (!aMirrorObjectRefs->isInitialized())
46     return;
47
48   std::shared_ptr<ModelAPI_Data> aData = data();
49   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
50       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
51   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
52       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
53   std::list<ObjectPtr> anInitialList =  aRefListOfShapes->list();
54   std::list<ObjectPtr> aMirroredList =  aRefListOfMirrored->list();
55   std::vector<bool> isUsed(anInitialList.size(), false);
56   // add new items to the list
57   for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
58     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aMirrorObjectRefs->value(anInd);
59     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
60     std::vector<bool>::iterator aUsedIt = isUsed.begin();
61     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
62       if (*anIt == aSelect->context()) {
63         *aUsedIt = true;
64         break;
65       }
66     if (anIt == anInitialList.end())
67       aRefListOfShapes->append(aSelect->context());
68   }
69   // remove unused items
70   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
71   std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
72   std::vector<bool>::iterator aUsedIter = isUsed.begin();
73   for (; aUsedIter != isUsed.end(); aUsedIter++) {
74     if (!(*aUsedIter)) {
75       aRefListOfShapes->remove(*anInitIter);
76       if (aMirrorIter != aMirroredList.end()) {
77         aRefListOfMirrored->remove(*aMirrorIter);
78         // remove the corresponding feature from the sketch
79         ResultConstructionPtr aRC =
80             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
81         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
82         FeaturePtr aFeature =  aDoc ? std::dynamic_pointer_cast<SketchPlugin_Feature>(
83               aDoc->feature(aRC)) : FeaturePtr();
84         if (aFeature)
85           aDoc->removeFeature(aFeature);
86       }
87     }
88     if (anInitIter != anInitialList.end())
89       anInitIter++;
90     if (aMirrorIter != aMirroredList.end())
91       aMirrorIter++;
92   }
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
136       std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
137       const std::list<ResultPtr>& aResults = aNewFeature->results();
138       std::list<ResultPtr>::const_iterator anIt = aResults.begin();
139       for (; anIt != aResults.end(); anIt++) {
140         ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
141         if (!aRC) continue;
142         std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
143         if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
144             (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
145             (aShapeIn->isFace() && aShapeOut->isFace()))
146           aRefListOfMirrored->append(aRC);
147       }
148       continue;
149     }
150     if (aFeatureIn->getKind() != aFeatureOut->getKind())
151       break; // the lists are inconsistent
152     if (!aFeatureIn->data()->isValid()) {
153       // initial feature was removed, delete it from lists
154       anInvalidInd.insert(indFirstWrong);
155     }
156     aMirrorIter++;
157   }
158   // Remove from the list objects already deleted before
159   std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
160   for ( ; anIt != anInvalidInd.rend(); anIt++) {
161     if (*anIt < indFirstWrong) indFirstWrong--;
162     aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
163     aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
164   }
165   // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
166   if (anInitIter != anInitialList.end()) {
167     while (aRefListOfMirrored->size() > indFirstWrong)
168       aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
169     // Create mirrored features instead of removed
170     anInitialList =  aRefListOfShapes->list();
171     anInitIter = anInitialList.begin();
172     for (int i = 0; i < indFirstWrong; i++) anInitIter++;
173     for ( ; anInitIter != anInitialList.end(); anInitIter++) {
174       aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
175       FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
176       aRefListOfMirrored->append(aNewFeature);
177     }
178   }
179 }
180
181 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
182 {
183   if (!sketch())
184     return thePrevious;
185
186   AISObjectPtr anAIS = thePrevious;
187   /// TODO: Equal constraint presentation should be put here
188   return anAIS;
189 }
190
191