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