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