]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MultiTranslation.cpp
Salome HOME
Issue #995: Checkbox 'Make a copy' during translation and rotation operations
[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
9 #include <GeomAPI_XY.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
19
20 #include <SketcherPrs_Factory.h>
21
22 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
23 {
24 }
25
26 void SketchPlugin_MultiTranslation::initAttributes()
27 {
28   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
29   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
30   data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
33   data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
34   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
35   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
36 }
37
38 void SketchPlugin_MultiTranslation::execute()
39 {
40   if (!sketch()) {
41     // it is possible, that this method is called before this feature has back reference to sketch
42     // in this case, the execute is performed after this is done
43     return;
44   }
45
46   AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
47   int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
48   if (aNbCopies <= 0)
49     return;
50
51   // Calculate shift vector
52   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53       attribute(START_POINT_ID()));
54   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
55       attribute(END_POINT_ID()));
56   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
57     return;
58
59   // make a visible points
60   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
61   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), END_POINT_ID(), 1);
62
63   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
64
65   // Wait all objects being created, then send update events
66   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
67   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
68   if (isUpdateFlushed)
69     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
70
71   std::shared_ptr<ModelAPI_Data> aData = data();
72   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
73       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
74   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
75       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
76   int aCurrentNbCopies = aRefListOfShapes->size() ?
77       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
78   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
79   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
80   std::list<ObjectPtr> anAddition;
81   std::vector<bool> isUsed(anInitialList.size(), false);
82   // collect new items and check the items to remove
83   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
84     //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
85     ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
86     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
87     std::vector<bool>::iterator aUsedIt = isUsed.begin();
88     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
89       if (*anIt == anObject) {
90         *aUsedIt = true;
91         break;
92       }
93     if (anIt == anInitialList.end())
94       anAddition.push_back(anObject);
95   }
96   // remove unused items
97   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
98   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
99   std::vector<bool>::iterator aUsedIter = isUsed.begin();
100   for (; aUsedIter != isUsed.end(); aUsedIter++) {
101     if (!(*aUsedIter)) {
102       aRefListOfShapes->remove(*anInitIter);
103       aRefListOfTranslated->remove(*aTargetIter++);
104       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
105         aRefListOfTranslated->remove(*aTargetIter);
106         // remove the corresponding feature from the sketch
107         ResultConstructionPtr aRC =
108             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
109         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
110         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
111         if (aFeature)
112           aDoc->removeFeature(aFeature);
113       }
114     } else {
115       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
116         aTargetIter++;
117     }
118     if (anInitIter != anInitialList.end())
119       anInitIter++;
120   }
121   // change number of copies
122   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
123     bool isAdd = aNbCopies > aCurrentNbCopies;
124     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
125     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
126     int ind = 0;
127
128     aTargetList = aRefListOfTranslated->list();
129     aTargetIter = aTargetList.begin();
130     ObjectPtr anObjToCopy = *aTargetIter;
131     while (aTargetIter != aTargetList.end()) {
132       aRefListOfTranslated->remove(*aTargetIter);
133       aTargetIter++;
134       ind++;
135       if (ind > aMinCopies && ind <=aMaxCopies) {
136         while (ind <= aMaxCopies) {
137           if (isAdd) {
138             // Add new shifted item
139             ObjectPtr anObject = copyFeature(anObjToCopy);
140             aTargetList.insert(aTargetIter, anObject);
141           } else {
142             // remove object
143             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
144             ObjectPtr anObject = *aRemoveIt;
145             aTargetList.erase(aRemoveIt);
146             aRefListOfTranslated->remove(anObject);
147             // remove the corresponding feature from the sketch
148             ResultConstructionPtr aRC =
149                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
150             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
151             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
152             if (aFeature)
153               aDoc->removeFeature(aFeature);
154           }
155           ind++;
156         }
157         ind = 0;
158         if (aTargetIter != aTargetList.end())
159           anObjToCopy = *aTargetIter;
160       }
161     }
162
163     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
164       aRefListOfTranslated->append(*aTargetIter);
165   }
166   // add new items
167   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
168   for (; anAddIter != anAddition.end(); anAddIter++) {
169     aRefListOfShapes->append(*anAddIter);
170     aRefListOfTranslated->append(*anAddIter);
171     for (int i = 0; i < aNbCopies; i++) {
172       ObjectPtr anObject = copyFeature(*anAddIter);
173       aRefListOfTranslated->append(anObject);
174     }
175   }
176
177   // send events to update the sub-features by the solver
178   if (isUpdateFlushed)
179     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
180 }
181
182 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
183 {
184   if (!sketch())
185     return thePrevious;
186
187   AISObjectPtr anAIS = thePrevious;
188   if (!anAIS) {
189     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
190   }
191   return anAIS;
192 }
193
194 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
195 {
196   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
197   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
198   if (!aFeature || !aResult)
199     return ObjectPtr();
200
201   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
202
203   aNewFeature->execute();
204   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
205   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
206
207   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
208   const std::list<ResultPtr>& aResults = aNewFeature->results();
209   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
210   for (; anIt != aResults.end(); anIt++) {
211     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
212     if (!aRC) continue;
213     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
214     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
215         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
216         (aShapeIn->isFace() && aShapeOut->isFace()))
217       return aRC;
218   }
219   return ObjectPtr();
220 }
221
222 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
223 {
224   if (theID == TRANSLATION_LIST_ID()) {
225     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
226     if (aTranslationObjectRefs->size() == 0) {
227       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
228       if (aNbCopies <= 0)
229         return;
230       // Clear list of objects
231       AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
232           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
233       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
234       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
235       while (aTargetIter != aTargetList.end()) {
236         aTargetIter++;
237         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
238           aRefListOfTranslated->remove(*aTargetIter);
239           // remove the corresponding feature from the sketch
240           ResultConstructionPtr aRC =
241             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
242           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
243           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
244           if (aFeature)
245             aDoc->removeFeature(aFeature);
246         }
247       }
248       aRefListOfTranslated->clear();
249       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
250         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
251       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
252         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
253     }
254   }
255 }