Salome HOME
Fix for the issue #593: do not remove naming attribute, but use TNaming_Builder for...
[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_AttributeSelectionList.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_Validator.h>
20
21 #include <SketcherPrs_Factory.h>
22
23 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
24 {
25 }
26
27 void SketchPlugin_MultiTranslation::initAttributes()
28 {
29   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
30   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
31   data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     TRANSLATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   aSelection->setSelectionType("EDGE");
38   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
39   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
40 }
41
42 void SketchPlugin_MultiTranslation::execute()
43 {
44   if (!sketch()) {
45     // it is possible, that 
46     return;
47   }
48
49   AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
50   int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
51
52   // Calculate shift vector
53   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
54       attribute(START_POINT_ID()));
55   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
56       attribute(END_POINT_ID()));
57   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
58     return;
59
60   // make a visible points
61   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
62   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), END_POINT_ID(), 1);
63
64   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
65
66   // Wait all objects being created, then send update events
67   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
68   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
69   if (isUpdateFlushed)
70     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
71
72   std::shared_ptr<ModelAPI_Data> aData = data();
73   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
74       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
75   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
76       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
77   int aCurrentNbCopies = aRefListOfShapes->size() ?
78       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
79   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
80   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
81   std::list<ResultPtr> anAddition;
82   std::vector<bool> isUsed(anInitialList.size(), false);
83   // collect new items and check the items to remove
84   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
85     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(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 == aSelect->context()) {
90         *aUsedIt = true;
91         break;
92       }
93     if (anIt == anInitialList.end())
94       anAddition.push_back(aSelect->context());
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       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
104         aRefListOfTranslated->remove(*aTargetIter);
105         // remove the corresponding feature from the sketch
106         ResultConstructionPtr aRC =
107             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
108         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
109         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
110         if (aFeature)
111           aDoc->removeFeature(aFeature);
112       }
113     } else {
114       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
115         aTargetIter++;
116     }
117     if (anInitIter != anInitialList.end())
118       anInitIter++;
119   }
120   // change number of copies
121   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
122     bool isAdd = aNbCopies > aCurrentNbCopies;
123     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
124     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
125     int ind = 0;
126
127     aTargetList = aRefListOfTranslated->list();
128     aTargetIter = aTargetList.begin();
129     ObjectPtr anObjToCopy = *aTargetIter;
130     while (aTargetIter != aTargetList.end()) {
131       aRefListOfTranslated->remove(*aTargetIter);
132       aTargetIter++;
133       ind++;
134       if (ind > aMinCopies && ind <=aMaxCopies) {
135         while (ind <= aMaxCopies) {
136           if (isAdd) {
137             // Add new shifted item
138             ObjectPtr anObject = copyFeature(anObjToCopy);
139             aTargetList.insert(aTargetIter, anObject);
140           } else {
141             // remove object
142             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
143             ObjectPtr anObject = *aRemoveIt;
144             aTargetList.erase(aRemoveIt);
145             aRefListOfTranslated->remove(anObject);
146             // remove the corresponding feature from the sketch
147             ResultConstructionPtr aRC =
148                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
149             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
150             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
151             if (aFeature)
152               aDoc->removeFeature(aFeature);
153           }
154           ind++;
155         }
156         ind = 0;
157         if (aTargetIter != aTargetList.end())
158           anObjToCopy = *aTargetIter;
159       }
160     }
161
162     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
163       aRefListOfTranslated->append(*aTargetIter);
164   }
165   // add new items
166   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
167   for (; anAddIter != anAddition.end(); anAddIter++) {
168     aRefListOfShapes->append(*anAddIter);
169     aRefListOfTranslated->append(*anAddIter);
170     for (int i = 0; i < aNbCopies; i++) {
171       ObjectPtr anObject = copyFeature(*anAddIter);
172       aRefListOfTranslated->append(anObject);
173     }
174   }
175
176   // send events to update the sub-features by the solver
177   if (isUpdateFlushed)
178     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
179 }
180
181 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
182 {
183   if (!sketch())
184     return thePrevious;
185
186   AISObjectPtr anAIS = thePrevious;
187   if (!anAIS) {
188     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
189   }
190   return anAIS;
191 }
192
193 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
194 {
195   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
196   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
197   if (!aFeature || !aResult)
198     return ObjectPtr();
199
200   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
201
202   aNewFeature->execute();
203   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
204   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
205
206   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
207   const std::list<ResultPtr>& aResults = aNewFeature->results();
208   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
209   for (; anIt != aResults.end(); anIt++) {
210     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
211     if (!aRC) continue;
212     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
213     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
214         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
215         (aShapeIn->isFace() && aShapeOut->isFace()))
216       return aRC;
217   }
218   return ObjectPtr();
219 }
220