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