Salome HOME
Improvement #1140: Display of replicated Sketch entities with thinner line
[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 <SketcherPrs_Factory.h>
24
25 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
26 {
27 }
28
29 void SketchPlugin_MultiTranslation::initAttributes()
30 {
31   data()->addAttribute(VALUE_TYPE(),   ModelAPI_AttributeString::typeId());
32
33   data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
34   data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
35
36   data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
37   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
38   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
39   data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
40   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
41   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
42 }
43
44 void SketchPlugin_MultiTranslation::execute()
45 {
46   if (!sketch()) {
47     // it is possible, that this method is called before this feature has back reference to sketch
48     // in this case, the execute is performed after this is done
49     return;
50   }
51
52   AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
53   int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
54   if (aNbCopies <= 0)
55     return;
56
57   // Calculate shift vector
58   AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
59   AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
60
61   if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
62     return;
63
64   DataPtr aData = data();
65   std::shared_ptr<GeomDataAPI_Point2D> aStart = GeomDataAPI_Point2D::getPoint2D(aData,
66                                                                                 START_POINT_ID());
67   std::shared_ptr<GeomDataAPI_Point2D> aEnd = GeomDataAPI_Point2D::getPoint2D(aData,
68                                                                               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   for (; aUsedIter != isUsed.end(); aUsedIter++) {
109     if (!(*aUsedIter)) {
110       aRefListOfShapes->remove(*anInitIter);
111       aRefListOfTranslated->remove(*aTargetIter++);
112       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
113         aRefListOfTranslated->remove(*aTargetIter);
114         // remove the corresponding feature from the sketch
115         ResultConstructionPtr aRC =
116             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
117         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
118         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
119         if (aFeature)
120           aDoc->removeFeature(aFeature);
121       }
122     } else {
123       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
124         aTargetIter++;
125     }
126     if (anInitIter != anInitialList.end())
127       anInitIter++;
128   }
129   // change number of copies
130   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
131     bool isAdd = aNbCopies > aCurrentNbCopies;
132     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
133     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
134     int ind = 0;
135
136     aTargetList = aRefListOfTranslated->list();
137     aTargetIter = aTargetList.begin();
138     ObjectPtr anObjToCopy = *aTargetIter;
139     while (aTargetIter != aTargetList.end()) {
140       aRefListOfTranslated->remove(*aTargetIter);
141       aTargetIter++;
142       ind++;
143       if (ind > aMinCopies && ind <=aMaxCopies) {
144         while (ind <= aMaxCopies) {
145           if (isAdd) {
146             // Add new shifted item
147             ObjectPtr anObject = copyFeature(anObjToCopy);
148             aTargetList.insert(aTargetIter, anObject);
149           } else {
150             // remove object
151             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
152             ObjectPtr anObject = *aRemoveIt;
153             aTargetList.erase(aRemoveIt);
154             aRefListOfTranslated->remove(anObject);
155             // remove the corresponding feature from the sketch
156             ResultConstructionPtr aRC =
157                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
158             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
159             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
160             if (aFeature)
161               aDoc->removeFeature(aFeature);
162           }
163           ind++;
164         }
165         ind = 0;
166         if (aTargetIter != aTargetList.end())
167           anObjToCopy = *aTargetIter;
168       }
169     }
170
171     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
172       aRefListOfTranslated->append(*aTargetIter);
173   }
174   // add new items
175   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
176   for (; anAddIter != anAddition.end(); anAddIter++) {
177     aRefListOfShapes->append(*anAddIter);
178     aRefListOfTranslated->append(*anAddIter);
179     for (int i = 0; i < aNbCopies; i++) {
180       ObjectPtr anObject = copyFeature(*anAddIter);
181       aRefListOfTranslated->append(anObject);
182     }
183   }
184
185   // send events to update the sub-features by the solver
186   if (isUpdateFlushed)
187     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
188 }
189
190 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
191 {
192   if (!sketch())
193     return thePrevious;
194
195   AISObjectPtr anAIS = thePrevious;
196   if (!anAIS) {
197     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
198   }
199   return anAIS;
200 }
201
202 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
203 {
204   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
205   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
206   if (!aFeature || !aResult)
207     return ObjectPtr();
208
209   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
210
211   aNewFeature->execute();
212   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
213   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
214
215   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
216   const std::list<ResultPtr>& aResults = aNewFeature->results();
217   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
218   for (; anIt != aResults.end(); anIt++) {
219     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
220     if (!aRC) continue;
221     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
222     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
223         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
224         (aShapeIn->isFace() && aShapeOut->isFace()))
225       return aRC;
226   }
227   return ObjectPtr();
228 }
229
230 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
231 {
232   if (theID == TRANSLATION_LIST_ID()) {
233     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
234     if (aTranslationObjectRefs->size() == 0) {
235       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
236       if (aNbCopies <= 0)
237         return;
238       // Clear list of objects
239       AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
240           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
241       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
242       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
243       while (aTargetIter != aTargetList.end()) {
244         aTargetIter++;
245         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
246           aRefListOfTranslated->remove(*aTargetIter);
247           // remove the corresponding feature from the sketch
248           ResultConstructionPtr aRC =
249             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
250           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
251           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
252           if (aFeature)
253             aDoc->removeFeature(aFeature);
254         }
255       }
256       aRefListOfTranslated->clear();
257       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
258         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
259       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
260         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
261     }
262   }
263 }