Salome HOME
Issue #2208: Development in progress
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MultiTranslation.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_MultiTranslation.h"
22 #include "SketchPlugin_Tools.h"
23
24 #include <GeomAPI_XY.h>
25 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Tools.h>
37
38 #include <SketchPlugin_SketchEntity.h>
39 #include <SketcherPrs_Factory.h>
40
41 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
42 {
43 }
44
45 void SketchPlugin_MultiTranslation::initAttributes()
46 {
47   data()->addAttribute(VALUE_TYPE(),   ModelAPI_AttributeString::typeId());
48
49   data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
50   data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
51
52   data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
53   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
54   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
55   data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
56   ModelAPI_Session::get()->validators()->
57     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
58   ModelAPI_Session::get()->validators()->
59     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
60 }
61
62 void SketchPlugin_MultiTranslation::execute()
63 {
64   if (!sketch()) {
65     // it is possible, that this method is called before this feature has back reference to sketch
66     // in this case, the execute is performed after this is done
67     return;
68   }
69
70   AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
71   int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
72   if (aNbCopies <= 0)
73     return;
74
75   // Calculate shift vector
76   AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
77   AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
78
79   if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
80     return;
81
82   DataPtr aData = data();
83   AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
84   AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
85   if (!aStart || !aEnd)
86     return;
87
88   std::shared_ptr<GeomAPI_XY>
89     aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
90
91   // Wait all objects being created, then send update events
92   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
93   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
94   if (isUpdateFlushed)
95     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
96
97   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
98       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
99   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
100       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
101   int aCurrentNbCopies = aRefListOfShapes->size() ?
102       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
103   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
104   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
105   std::list<ObjectPtr> anAddition;
106   std::vector<bool> isUsed(anInitialList.size(), false);
107   // collect new items and check the items to remove
108   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
109     //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
110     ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
111     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
112     std::vector<bool>::iterator aUsedIt = isUsed.begin();
113     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
114       if (*anIt == anObject) {
115         *aUsedIt = true;
116         break;
117       }
118     if (anIt == anInitialList.end())
119       anAddition.push_back(anObject);
120   }
121   // remove unused items
122   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
123   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
124   std::vector<bool>::iterator aUsedIter = isUsed.begin();
125   std::set<FeaturePtr> aFeaturesToBeRemoved;
126   for (; aUsedIter != isUsed.end(); aUsedIter++) {
127     if (!(*aUsedIter)) {
128       aRefListOfShapes->remove(*anInitIter);
129       aRefListOfTranslated->remove(*aTargetIter++);
130       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
131            i++, aTargetIter++) {
132         aRefListOfTranslated->remove(*aTargetIter);
133         // remove the corresponding feature from the sketch
134         ResultConstructionPtr aRC =
135             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
136         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
137         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
138         if (aFeature)
139           aFeaturesToBeRemoved.insert(aFeature);
140       }
141     } else {
142       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
143         aTargetIter++;
144     }
145     if (anInitIter != anInitialList.end())
146       anInitIter++;
147   }
148   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
149   // change number of copies
150   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
151     bool isAdd = aNbCopies > aCurrentNbCopies;
152     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
153     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
154     int ind = 0;
155
156     aTargetList = aRefListOfTranslated->list();
157     aTargetIter = aTargetList.begin();
158     ObjectPtr anObjToCopy = *aTargetIter;
159     while (aTargetIter != aTargetList.end()) {
160       aRefListOfTranslated->remove(*aTargetIter);
161       aTargetIter++;
162       ind++;
163       if (ind > aMinCopies && ind <=aMaxCopies) {
164         while (ind <= aMaxCopies) {
165           if (isAdd) {
166             // Add new shifted item
167             ObjectPtr anObject = copyFeature(anObjToCopy);
168             aTargetList.insert(aTargetIter, anObject);
169           } else {
170             // remove object
171             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
172             ObjectPtr anObject = *aRemoveIt;
173             aTargetList.erase(aRemoveIt);
174             aRefListOfTranslated->remove(anObject);
175             // remove the corresponding feature from the sketch
176             ResultConstructionPtr aRC =
177                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
178             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
179             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
180             if (aFeature)
181               aDoc->removeFeature(aFeature);
182           }
183           ind++;
184         }
185         ind = 0;
186         if (aTargetIter != aTargetList.end())
187           anObjToCopy = *aTargetIter;
188       }
189     }
190
191     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
192       aRefListOfTranslated->append(*aTargetIter);
193   }
194   // add new items
195   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
196   for (; anAddIter != anAddition.end(); anAddIter++) {
197     aRefListOfShapes->append(*anAddIter);
198     aRefListOfTranslated->append(*anAddIter);
199     for (int i = 0; i < aNbCopies; i++) {
200       ObjectPtr anObject = copyFeature(*anAddIter);
201       aRefListOfTranslated->append(anObject);
202     }
203   }
204
205   // send events to update the sub-features by the solver
206   if (isUpdateFlushed)
207     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
208 }
209
210 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
211 {
212   if (!sketch())
213     return thePrevious;
214
215   AISObjectPtr anAIS = SketcherPrs_Factory::translateConstraint(this, sketch(),
216                                                                 sketch()->coordinatePlane(),
217                                                                 thePrevious);
218   return anAIS;
219 }
220
221 void SketchPlugin_MultiTranslation::erase()
222 {
223   static Events_Loop* aLoop = Events_Loop::loop();
224   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
225
226   // Set copy attribute to false on all copied features.
227   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
228       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
229   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
230       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
231
232   if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
233     for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
234       ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
235       if(aRefListOfShapes->isInList(anObject)) {
236         // Don't modify attribute of original features, just skip.
237         continue;
238       }
239       if(anObject.get()) {
240         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
241         if(aRes.get()) {
242           FeaturePtr aFeature = aRes->document()->feature(aRes);
243           if(aFeature.get()) {
244             AttributeBooleanPtr aBooleanAttr =
245               aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
246             if(aBooleanAttr.get()) {
247               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
248                 aBooleanAttr->setValue(false);
249               // Redisplay object as it is not copy anymore.
250               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
251             }
252           }
253         }
254       }
255     }
256   }
257
258   SketchPlugin_ConstraintBase::erase();
259 }
260
261 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
262 {
263   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
264   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
265   if (!aFeature || !aResult)
266     return ObjectPtr();
267
268   FeaturePtr aNewFeature =
269     SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
270
271   aNewFeature->execute();
272   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
273   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
274
275   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
276   const std::list<ResultPtr>& aResults = aNewFeature->results();
277   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
278   for (; anIt != aResults.end(); anIt++) {
279     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
280     if (!aRC) continue;
281     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
282     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
283         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
284         (aShapeIn->isFace() && aShapeOut->isFace()))
285       return aRC;
286   }
287   return ObjectPtr();
288 }
289
290 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
291 {
292   if (theID == TRANSLATION_LIST_ID()) {
293     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
294     if (aTranslationObjectRefs->size() == 0) {
295       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
296       if (aNbCopies <= 0)
297         return;
298
299       DocumentPtr aDoc = document();
300       // Clear list of objects
301       AttributeRefListPtr aRefListOfTranslated = reflist(SketchPlugin_Constraint::ENTITY_B());
302       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
303       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
304       while (aTargetIter != aTargetList.end()) {
305         aTargetIter++;
306         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
307           aRefListOfTranslated->remove(*aTargetIter);
308           FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
309           if (aFeature)
310             aDoc->removeFeature(aFeature);
311         }
312       }
313       aRefListOfTranslated->clear();
314       reflist(SketchPlugin_Constraint::ENTITY_A())->clear();
315     }
316   }
317 }