Salome HOME
c94ae2dca77d3b18156a408b641293167339aa0b
[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_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <SketcherPrs_Factory.h>
23
24 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
25 : myBlockValue(false)
26 {
27 }
28
29 void SketchPlugin_MultiTranslation::initAttributes()
30 {
31   data()->addAttribute(VALUE_TYPE(),   ModelAPI_AttributeString::typeId());
32
33   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
34   data()->addAttribute(START_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
35   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
36   data()->addAttribute(END_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
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   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
60       attribute(START_POINT_ID()));
61   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
62       attribute(END_POINT_ID()));
63   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
64     return;
65
66   if (attribute(END_POINT_ID())->isInitialized() && !attribute(END_FULL_POINT_ID())->isInitialized()) {
67     myBlockValue = true;
68     SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
69                                              attribute(END_FULL_POINT_ID()), aNbCopies, true);
70     myBlockValue = false;
71   }
72
73
74   // make a visible points
75   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
76
77   std::string aSecondPointAttributeID = END_POINT_ID();
78   AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
79   std::string aMethodType = aMethodTypeAttr->value();
80   if (aMethodType != "SingleValue")
81     aSecondPointAttributeID = END_FULL_POINT_ID();
82   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), aSecondPointAttributeID, 1);
83
84   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
85
86   // Wait all objects being created, then send update events
87   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
88   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
89   if (isUpdateFlushed)
90     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
91
92   std::shared_ptr<ModelAPI_Data> aData = data();
93   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
94       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
95   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
96       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
97   int aCurrentNbCopies = aRefListOfShapes->size() ?
98       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
99   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
100   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
101   std::list<ObjectPtr> anAddition;
102   std::vector<bool> isUsed(anInitialList.size(), false);
103   // collect new items and check the items to remove
104   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
105     //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
106     ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
107     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
108     std::vector<bool>::iterator aUsedIt = isUsed.begin();
109     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
110       if (*anIt == anObject) {
111         *aUsedIt = true;
112         break;
113       }
114     if (anIt == anInitialList.end())
115       anAddition.push_back(anObject);
116   }
117   // remove unused items
118   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
119   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
120   std::vector<bool>::iterator aUsedIter = isUsed.begin();
121   for (; aUsedIter != isUsed.end(); aUsedIter++) {
122     if (!(*aUsedIter)) {
123       aRefListOfShapes->remove(*anInitIter);
124       aRefListOfTranslated->remove(*aTargetIter++);
125       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
126         aRefListOfTranslated->remove(*aTargetIter);
127         // remove the corresponding feature from the sketch
128         ResultConstructionPtr aRC =
129             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
130         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
131         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
132         if (aFeature)
133           aDoc->removeFeature(aFeature);
134       }
135     } else {
136       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
137         aTargetIter++;
138     }
139     if (anInitIter != anInitialList.end())
140       anInitIter++;
141   }
142   // change number of copies
143   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
144     bool isAdd = aNbCopies > aCurrentNbCopies;
145     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
146     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
147     int ind = 0;
148
149     aTargetList = aRefListOfTranslated->list();
150     aTargetIter = aTargetList.begin();
151     ObjectPtr anObjToCopy = *aTargetIter;
152     while (aTargetIter != aTargetList.end()) {
153       aRefListOfTranslated->remove(*aTargetIter);
154       aTargetIter++;
155       ind++;
156       if (ind > aMinCopies && ind <=aMaxCopies) {
157         while (ind <= aMaxCopies) {
158           if (isAdd) {
159             // Add new shifted item
160             ObjectPtr anObject = copyFeature(anObjToCopy);
161             aTargetList.insert(aTargetIter, anObject);
162           } else {
163             // remove object
164             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
165             ObjectPtr anObject = *aRemoveIt;
166             aTargetList.erase(aRemoveIt);
167             aRefListOfTranslated->remove(anObject);
168             // remove the corresponding feature from the sketch
169             ResultConstructionPtr aRC =
170                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
171             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
172             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
173             if (aFeature)
174               aDoc->removeFeature(aFeature);
175           }
176           ind++;
177         }
178         ind = 0;
179         if (aTargetIter != aTargetList.end())
180           anObjToCopy = *aTargetIter;
181       }
182     }
183
184     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
185       aRefListOfTranslated->append(*aTargetIter);
186   }
187   // add new items
188   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
189   for (; anAddIter != anAddition.end(); anAddIter++) {
190     aRefListOfShapes->append(*anAddIter);
191     aRefListOfTranslated->append(*anAddIter);
192     for (int i = 0; i < aNbCopies; i++) {
193       ObjectPtr anObject = copyFeature(*anAddIter);
194       aRefListOfTranslated->append(anObject);
195     }
196   }
197
198   // send events to update the sub-features by the solver
199   if (isUpdateFlushed)
200     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
201 }
202
203 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
204 {
205   if (!sketch())
206     return thePrevious;
207
208   AISObjectPtr anAIS = thePrevious;
209   if (!anAIS) {
210     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
211   }
212   return anAIS;
213 }
214
215 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
216 {
217   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
218   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
219   if (!aFeature || !aResult)
220     return ObjectPtr();
221
222   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
223
224   aNewFeature->execute();
225   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
226   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
227
228   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
229   const std::list<ResultPtr>& aResults = aNewFeature->results();
230   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
231   for (; anIt != aResults.end(); anIt++) {
232     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
233     if (!aRC) continue;
234     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
235     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
236         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
237         (aShapeIn->isFace() && aShapeOut->isFace()))
238       return aRC;
239   }
240   return ObjectPtr();
241 }
242
243 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
244 {
245   if (theID == TRANSLATION_LIST_ID()) {
246     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
247     if (aTranslationObjectRefs->size() == 0) {
248       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
249       if (aNbCopies <= 0)
250         return;
251       // Clear list of objects
252       AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
253           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
254       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
255       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
256       while (aTargetIter != aTargetList.end()) {
257         aTargetIter++;
258         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
259           aRefListOfTranslated->remove(*aTargetIter);
260           // remove the corresponding feature from the sketch
261           ResultConstructionPtr aRC =
262             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
263           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
264           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
265           if (aFeature)
266             aDoc->removeFeature(aFeature);
267         }
268       }
269       aRefListOfTranslated->clear();
270       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
271         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
272       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
273         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
274     }
275   }
276   else if (theID == START_POINT_ID() && !myBlockValue) {
277     myBlockValue = true;
278     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
279         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
280     std::shared_ptr<GeomDataAPI_Point2D> aStartFullPoint = 
281         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_FULL_POINT_ID()));
282     aStartFullPoint->setValue(aStartPoint->pnt());
283     myBlockValue = false;
284   }
285   else if (theID == START_FULL_POINT_ID() && !myBlockValue) {
286     myBlockValue = true;
287     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = 
288         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
289     std::shared_ptr<GeomDataAPI_Point2D> aStartFullPoint = 
290         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_FULL_POINT_ID()));
291     aStartPoint->setValue(aStartFullPoint->pnt());
292     myBlockValue = false;
293   }
294   else if (theID == END_POINT_ID() && !myBlockValue) {
295     int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
296     if (aNbCopies > 0) {
297       myBlockValue = true;
298       SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
299                                                attribute(END_FULL_POINT_ID()), aNbCopies, true);
300       myBlockValue = false;
301       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
302     }
303   }
304   else if (theID == END_FULL_POINT_ID() && !myBlockValue) {
305     int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
306     if (aNbCopies > 0) {
307       myBlockValue = true;
308       SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
309                                                attribute(END_POINT_ID()), aNbCopies, false);
310       myBlockValue = false;
311       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
312     }
313   }
314   else if (theID == NUMBER_OF_OBJECTS_ID()) {
315     if (attribute(NUMBER_OF_OBJECTS_ID())->isInitialized() &&
316         attribute(END_POINT_ID())->isInitialized() &&
317         attribute(VALUE_TYPE())->isInitialized()) {
318       AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
319       std::string aMethodType = aMethodTypeAttr->value();
320       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
321       if (aNbCopies > 0) {
322         myBlockValue = true;
323         if (aMethodType == "SingleValue")
324           SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
325                                                    attribute(END_FULL_POINT_ID()), aNbCopies, true);
326         else {
327           SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
328                                                    attribute(END_POINT_ID()), aNbCopies, false);
329         }
330         myBlockValue = false;
331         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
332       }
333     }
334   }
335 }