Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintMirror.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_ConstraintMirror.h"
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Sketch.h>
33
34 #include <SketcherPrs_Factory.h>
35
36 #include <Config_PropManager.h>
37
38 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
39 {
40 }
41
42 void SketchPlugin_ConstraintMirror::initAttributes()
43 {
44   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
45   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
46   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
47   data()->addAttribute(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(),
48                        ModelAPI_AttributeRefList::typeId());
49   ModelAPI_Session::get()->validators()->
50     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
51   ModelAPI_Session::get()->validators()->
52     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
53 }
54
55 void SketchPlugin_ConstraintMirror::execute()
56 {
57   AttributeRefListPtr aMirrorObjectRefs = reflist(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
58
59   // Wait all objects being created, then send update events
60   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
61   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
62   if (isUpdateFlushed)
63     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
64
65   std::shared_ptr<ModelAPI_Data> aData = data();
66   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
67       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
68   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
69       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
70   std::list<ObjectPtr> anInitialList =  aRefListOfShapes->list();
71   std::list<ObjectPtr> aMirroredList =  aRefListOfMirrored->list();
72   std::vector<bool> isUsed(anInitialList.size(), false);
73   // add new items to the list
74   for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
75     ObjectPtr anObject = aMirrorObjectRefs->object(anInd);
76     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
77     std::vector<bool>::iterator aUsedIt = isUsed.begin();
78     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
79       if (*anIt == anObject) {
80         *aUsedIt = true;
81         break;
82       }
83     if (anIt == anInitialList.end())
84       aRefListOfShapes->append(anObject);
85   }
86   // remove unused items
87   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
88   std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
89   std::vector<bool>::iterator aUsedIter = isUsed.begin();
90   std::set<FeaturePtr> aFeaturesToBeRemoved;
91   for (; aUsedIter != isUsed.end(); aUsedIter++) {
92     if (!(*aUsedIter)) {
93       aRefListOfShapes->remove(*anInitIter);
94       if (aMirrorIter != aMirroredList.end()) {
95         aRefListOfMirrored->remove(*aMirrorIter);
96         // remove the corresponding feature from the sketch
97         ResultConstructionPtr aRC =
98             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
99         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
100         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
101         if (aFeature)
102           aFeaturesToBeRemoved.insert(aFeature);
103       }
104     }
105     if (anInitIter != anInitialList.end())
106       anInitIter++;
107     if (aMirrorIter != aMirroredList.end())
108       aMirrorIter++;
109   }
110   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
111
112   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
113
114   // Check consistency of initial list and mirrored list
115   anInitialList =  aRefListOfShapes->list();
116   anInitIter = anInitialList.begin();
117   aMirrorIter = aMirroredList.begin();
118   int indFirstWrong = 0; // index of element starts difference in the lists
119   std::set<int> anInvalidInd; // list of indices of removed features
120   std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
121   for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
122     // Add features and store indices of objects to remove
123     aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
124     ResultConstructionPtr aRCIn;
125     if (!aFeatureIn) {
126       aRCIn = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
127       if (aRCIn)
128         aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(
129             aRCIn->document()->feature(aRCIn));
130     }
131     if (aMirrorIter == aMirroredList.end())
132       aFeatureOut = std::shared_ptr<SketchPlugin_Feature>();
133     else {
134       aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter);
135       if (!aFeatureOut) {
136         ResultConstructionPtr aRC =
137             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
138         if (aRC)
139           aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(
140               aRC->document()->feature(aRC));
141       }
142     }
143     if (!aFeatureIn) {
144       if (aFeatureOut)
145         break; // the lists are inconsistent
146       continue;
147     }
148     if (!aFeatureOut) {
149       if (aMirrorIter != aMirroredList.end())
150         break; // the lists are inconsistent
151       // There is no mirrored object yet, create it
152       FeaturePtr aNewFeature =
153         SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureIn, sketch(), true);
154       aNewFeature->execute();
155       ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
156
157       std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
158       const std::list<ResultPtr>& aResults = aNewFeature->results();
159       std::list<ResultPtr>::const_iterator anIt = aResults.begin();
160       for (; anIt != aResults.end(); anIt++) {
161         ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
162         if (!aRC) continue;
163         std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
164         if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
165             (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
166             (aShapeIn->isFace() && aShapeOut->isFace()))
167           aRefListOfMirrored->append(aRC);
168       }
169       continue;
170     }
171     if (aFeatureIn->getKind() != aFeatureOut->getKind())
172       break; // the lists are inconsistent
173     if (!aFeatureIn->data()->isValid()) {
174       // initial feature was removed, delete it from lists
175       anInvalidInd.insert(indFirstWrong);
176     }
177     aMirrorIter++;
178   }
179   // Remove from the list objects already deleted before
180   std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
181   for ( ; anIt != anInvalidInd.rend(); anIt++) {
182     if (*anIt < indFirstWrong) indFirstWrong--;
183     aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
184     aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
185   }
186   // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
187   if (anInitIter != anInitialList.end()) {
188     while (aRefListOfMirrored->size() > indFirstWrong)
189       aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
190     // Create mirrored features instead of removed
191     anInitialList =  aRefListOfShapes->list();
192     anInitIter = anInitialList.begin();
193     for (int i = 0; i < indFirstWrong; i++) anInitIter++;
194     for ( ; anInitIter != anInitialList.end(); anInitIter++) {
195       aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
196       FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
197       aRefListOfMirrored->append(aNewFeature);
198     }
199   }
200
201   // send events to update the sub-features by the solver
202   if (isUpdateFlushed)
203     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
204 }
205
206 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
207 {
208   if (!sketch())
209     return thePrevious;
210
211   AISObjectPtr anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch(),
212                                                              thePrevious);
213   return anAIS;
214 }
215
216 void SketchPlugin_ConstraintMirror::erase()
217 {
218   // Set copy attribute to false on all copied features.
219   AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
220       data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
221
222   if(aRefListOfMirrored.get()) {
223     static Events_Loop* aLoop = Events_Loop::loop();
224     static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
225
226     std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
227     for(std::list<ObjectPtr>::const_iterator aTargetIt =
228         aTargetList.cbegin(); aTargetIt != aTargetList.cend(); aTargetIt++) {
229       if((*aTargetIt).get()) {
230         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aTargetIt);
231         if(aRes.get()) {
232           FeaturePtr aFeature = aRes->document()->feature(aRes);
233           if(aFeature.get()) {
234             AttributeBooleanPtr aBooleanAttr =
235               aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
236             if(aBooleanAttr.get()) {
237               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
238                 aBooleanAttr->setValue(false);
239               // Redisplay object as it is not copy anymore.
240               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
241             }
242           }
243         }
244       }
245     }
246   }
247
248   SketchPlugin_ConstraintBase::erase();
249 }
250
251 void SketchPlugin_ConstraintMirror::attributeChanged(const std::string& theID)
252 {
253   if (theID == MIRROR_LIST_ID()) {
254     AttributeRefListPtr aMirrorObjectRefs = reflist(MIRROR_LIST_ID());
255     if (aMirrorObjectRefs->size() == 0) {
256       DocumentPtr aDoc = document();
257       // Clear list of objects
258       AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
259           data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
260       std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
261       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
262       for (; aTargetIter != aTargetList.end(); aTargetIter++) {
263         FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
264         if (aFeature)
265           aDoc->removeFeature(aFeature);
266       }
267       aRefListOfMirrored->clear();
268       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
269         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
270     }
271   }
272 }
273