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