Salome HOME
2D point selection in multi-rotation/multi-translation.
[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   AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
66   AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
67   if (!aStart || !aEnd)
68     return;
69
70   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
71
72   // Wait all objects being created, then send update events
73   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
74   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
75   if (isUpdateFlushed)
76     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
77
78   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
79       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
80   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
81       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
82   int aCurrentNbCopies = aRefListOfShapes->size() ?
83       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
84   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
85   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
86   std::list<ObjectPtr> anAddition;
87   std::vector<bool> isUsed(anInitialList.size(), false);
88   // collect new items and check the items to remove
89   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
90     //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
91     ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
92     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
93     std::vector<bool>::iterator aUsedIt = isUsed.begin();
94     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
95       if (*anIt == anObject) {
96         *aUsedIt = true;
97         break;
98       }
99     if (anIt == anInitialList.end())
100       anAddition.push_back(anObject);
101   }
102   // remove unused items
103   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
104   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
105   std::vector<bool>::iterator aUsedIter = isUsed.begin();
106   for (; aUsedIter != isUsed.end(); aUsedIter++) {
107     if (!(*aUsedIter)) {
108       aRefListOfShapes->remove(*anInitIter);
109       aRefListOfTranslated->remove(*aTargetIter++);
110       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
111         aRefListOfTranslated->remove(*aTargetIter);
112         // remove the corresponding feature from the sketch
113         ResultConstructionPtr aRC =
114             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
115         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
116         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
117         if (aFeature)
118           aDoc->removeFeature(aFeature);
119       }
120     } else {
121       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
122         aTargetIter++;
123     }
124     if (anInitIter != anInitialList.end())
125       anInitIter++;
126   }
127   // change number of copies
128   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
129     bool isAdd = aNbCopies > aCurrentNbCopies;
130     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
131     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
132     int ind = 0;
133
134     aTargetList = aRefListOfTranslated->list();
135     aTargetIter = aTargetList.begin();
136     ObjectPtr anObjToCopy = *aTargetIter;
137     while (aTargetIter != aTargetList.end()) {
138       aRefListOfTranslated->remove(*aTargetIter);
139       aTargetIter++;
140       ind++;
141       if (ind > aMinCopies && ind <=aMaxCopies) {
142         while (ind <= aMaxCopies) {
143           if (isAdd) {
144             // Add new shifted item
145             ObjectPtr anObject = copyFeature(anObjToCopy);
146             aTargetList.insert(aTargetIter, anObject);
147           } else {
148             // remove object
149             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
150             ObjectPtr anObject = *aRemoveIt;
151             aTargetList.erase(aRemoveIt);
152             aRefListOfTranslated->remove(anObject);
153             // remove the corresponding feature from the sketch
154             ResultConstructionPtr aRC =
155                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
156             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
157             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
158             if (aFeature)
159               aDoc->removeFeature(aFeature);
160           }
161           ind++;
162         }
163         ind = 0;
164         if (aTargetIter != aTargetList.end())
165           anObjToCopy = *aTargetIter;
166       }
167     }
168
169     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
170       aRefListOfTranslated->append(*aTargetIter);
171   }
172   // add new items
173   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
174   for (; anAddIter != anAddition.end(); anAddIter++) {
175     aRefListOfShapes->append(*anAddIter);
176     aRefListOfTranslated->append(*anAddIter);
177     for (int i = 0; i < aNbCopies; i++) {
178       ObjectPtr anObject = copyFeature(*anAddIter);
179       aRefListOfTranslated->append(anObject);
180     }
181   }
182
183   // send events to update the sub-features by the solver
184   if (isUpdateFlushed)
185     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
186 }
187
188 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
189 {
190   if (!sketch())
191     return thePrevious;
192
193   AISObjectPtr anAIS = thePrevious;
194   if (!anAIS) {
195     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
196   }
197   return anAIS;
198 }
199
200 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
201 {
202   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
203   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
204   if (!aFeature || !aResult)
205     return ObjectPtr();
206
207   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
208
209   aNewFeature->execute();
210   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
211   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
212
213   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
214   const std::list<ResultPtr>& aResults = aNewFeature->results();
215   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
216   for (; anIt != aResults.end(); anIt++) {
217     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
218     if (!aRC) continue;
219     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
220     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
221         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
222         (aShapeIn->isFace() && aShapeOut->isFace()))
223       return aRC;
224   }
225   return ObjectPtr();
226 }
227
228 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
229 {
230   if (theID == TRANSLATION_LIST_ID()) {
231     AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
232     if (aTranslationObjectRefs->size() == 0) {
233       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
234       if (aNbCopies <= 0)
235         return;
236       // Clear list of objects
237       AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
238           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
239       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
240       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
241       while (aTargetIter != aTargetList.end()) {
242         aTargetIter++;
243         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
244           aRefListOfTranslated->remove(*aTargetIter);
245           // remove the corresponding feature from the sketch
246           ResultConstructionPtr aRC =
247             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
248           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
249           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
250           if (aFeature)
251             aDoc->removeFeature(aFeature);
252         }
253       }
254       aRefListOfTranslated->clear();
255       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
256         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
257       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
258         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
259     }
260   }
261 }