Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MultiTranslation.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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   // Save the current feature of the document, because new features may appear while executing.
97   // In this case, they will become current. But if the number of copies is updated from outside
98   // of sketch (e.g. by parameter change), the history line should not hold in sketch.
99   keepCurrentFeature();
100
101   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
102       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
103   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
104       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
105   int aCurrentNbCopies = aRefListOfShapes->size() ?
106       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
107   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
108   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
109   std::list<ObjectPtr> anAddition;
110   std::vector<bool> isUsed(anInitialList.size(), false);
111   // collect new items and check the items to remove
112   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
113     //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
114     ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
115     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
116     std::vector<bool>::iterator aUsedIt = isUsed.begin();
117     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
118       if (*anIt == anObject) {
119         *aUsedIt = true;
120         break;
121       }
122     if (anIt == anInitialList.end())
123       anAddition.push_back(anObject);
124   }
125   // remove unused items
126   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
127   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
128   std::vector<bool>::iterator aUsedIter = isUsed.begin();
129   std::set<FeaturePtr> aFeaturesToBeRemoved;
130   for (; aUsedIter != isUsed.end(); aUsedIter++) {
131     if (!(*aUsedIter)) {
132       aRefListOfShapes->remove(*anInitIter);
133       aRefListOfTranslated->remove(*aTargetIter++);
134       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
135            i++, aTargetIter++) {
136         aRefListOfTranslated->remove(*aTargetIter);
137         // remove the corresponding feature from the sketch
138         ResultConstructionPtr aRC =
139             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
140         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
141         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
142         if (aFeature)
143           aFeaturesToBeRemoved.insert(aFeature);
144       }
145     } else {
146       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
147         aTargetIter++;
148     }
149     if (anInitIter != anInitialList.end())
150       anInitIter++;
151   }
152   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
153   // change number of copies
154   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
155     bool isAdd = aNbCopies > aCurrentNbCopies;
156     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
157     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
158     int ind = 0;
159
160     aTargetList = aRefListOfTranslated->list();
161     aTargetIter = aTargetList.begin();
162     ObjectPtr anObjToCopy = *aTargetIter;
163     while (aTargetIter != aTargetList.end()) {
164       aRefListOfTranslated->remove(*aTargetIter);
165       aTargetIter++;
166       ind++;
167       if (ind > aMinCopies && ind <=aMaxCopies) {
168         while (ind <= aMaxCopies) {
169           if (isAdd) {
170             // Add new shifted item
171             ObjectPtr anObject = copyFeature(anObjToCopy);
172             aTargetList.insert(aTargetIter, anObject);
173           } else {
174             // remove object
175             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
176             ObjectPtr anObject = *aRemoveIt;
177             aTargetList.erase(aRemoveIt);
178             aRefListOfTranslated->remove(anObject);
179             // remove the corresponding feature from the sketch
180             ResultConstructionPtr aRC =
181                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
182             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
183             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
184             if (aFeature)
185               aDoc->removeFeature(aFeature);
186           }
187           ind++;
188         }
189         ind = 0;
190         if (aTargetIter != aTargetList.end())
191           anObjToCopy = *aTargetIter;
192       }
193     }
194
195     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
196       aRefListOfTranslated->append(*aTargetIter);
197   }
198   // add new items
199   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
200   for (; anAddIter != anAddition.end(); anAddIter++) {
201     aRefListOfShapes->append(*anAddIter);
202     aRefListOfTranslated->append(*anAddIter);
203     for (int i = 0; i < aNbCopies; i++) {
204       ObjectPtr anObject = copyFeature(*anAddIter);
205       aRefListOfTranslated->append(anObject);
206     }
207   }
208
209   restoreCurrentFeature();
210
211   // send events to update the sub-features by the solver
212   if (isUpdateFlushed)
213     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
214 }
215
216 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
217 {
218   if (!sketch())
219     return thePrevious;
220
221   AISObjectPtr anAIS = SketcherPrs_Factory::translateConstraint(this, sketch(),
222                                                                 thePrevious);
223   return anAIS;
224 }
225
226 void SketchPlugin_MultiTranslation::erase()
227 {
228   static Events_Loop* aLoop = Events_Loop::loop();
229   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
230
231   // Set copy attribute to false on all copied features.
232   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
233       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
234   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
235       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
236
237   if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
238     for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
239       ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
240       if(aRefListOfShapes->isInList(anObject)) {
241         // Don't modify attribute of original features, just skip.
242         continue;
243       }
244       if(anObject.get()) {
245         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
246         if(aRes.get()) {
247           FeaturePtr aFeature = aRes->document()->feature(aRes);
248           if(aFeature.get()) {
249             AttributeBooleanPtr aBooleanAttr =
250               aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
251             if(aBooleanAttr.get()) {
252               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
253                 aBooleanAttr->setValue(false);
254               // Redisplay object as it is not copy anymore.
255               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
256             }
257           }
258         }
259       }
260     }
261   }
262
263   SketchPlugin_ConstraintBase::erase();
264 }
265
266 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
267 {
268   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
269   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
270   if (!aFeature || !aResult)
271     return ObjectPtr();
272
273   FeaturePtr aNewFeature =
274     SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
275
276   aNewFeature->execute();
277   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
278   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
279
280   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
281   const std::list<ResultPtr>& aResults = aNewFeature->results();
282   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
283   for (; anIt != aResults.end(); anIt++) {
284     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
285     if (!aRC) continue;
286     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
287     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
288         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
289         (aShapeIn->isFace() && aShapeOut->isFace()))
290       return aRC;
291   }
292   return ObjectPtr();
293 }
294
295 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
296 {
297   if (theID == TRANSLATION_LIST_ID()) {
298     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
299     if (aTranslationObjectRefs->size() == 0) {
300       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
301       if (aNbCopies <= 0)
302         return;
303
304       DocumentPtr aDoc = document();
305       // Clear list of objects
306       AttributeRefListPtr aRefListOfTranslated = reflist(SketchPlugin_Constraint::ENTITY_B());
307       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
308       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
309       while (aTargetIter != aTargetList.end()) {
310         aTargetIter++;
311         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
312           aRefListOfTranslated->remove(*aTargetIter);
313           FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
314           if (aFeature)
315             aDoc->removeFeature(aFeature);
316         }
317       }
318       aRefListOfTranslated->clear();
319       reflist(SketchPlugin_Constraint::ENTITY_A())->clear();
320     }
321   }
322 }