Salome HOME
First part of the test correction for improvement: 2D points selection in multi-trans...
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MultiRotation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_MultiRotation.cpp
4 // Created: 21 Apr 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_MultiRotation.h"
8 #include "SketchPlugin_Tools.h"
9
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_Validator.h>
20
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_XY.h>
23
24 #include <SketcherPrs_Factory.h>
25
26 #include <cmath>
27
28 #define PI 3.1415926535897932
29
30 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
31 : myBlockAngle(false)
32 {
33 }
34
35 void SketchPlugin_MultiRotation::initAttributes()
36 {
37   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
38
39   data()->addAttribute(ANGLE_TYPE(),   ModelAPI_AttributeString::typeId());
40   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
41   data()->addAttribute(ANGLE_FULL_ID(), ModelAPI_AttributeDouble::typeId());
42   data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
43   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
44   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
45   data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
46   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
47   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
48 }
49
50 void SketchPlugin_MultiRotation::execute()
51 {
52   if (!sketch()) {
53     // it is possible, that this method is called before this feature has back reference to sketch
54     // in this case, the execute is performed after this is done
55     return;
56   }
57
58   AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
59   int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
60   if (aNbCopies <= 0)
61     return;
62
63   // Obtain center and angle of rotation
64   std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65       attribute(CENTER_ID()));
66   if (!aCenter || !aCenter->isInitialized())
67     return;
68
69   if (attribute(ANGLE_ID())->isInitialized() && !attribute(ANGLE_FULL_ID())->isInitialized()) {
70     myBlockAngle = true;
71     SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
72                                              aNbCopies, true);
73     myBlockAngle = false;
74   }
75
76   // make a visible points
77   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
78
79   double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80                                                              attribute(ANGLE_ID()))->value();
81
82   // Convert angle to radians
83   anAngle *= PI / 180.0;
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 aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
95       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
96   int aCurrentNbCopies = aRefListOfShapes->size() ?
97       aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
98   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
99   std::list<ObjectPtr> aTargetList = aRefListOfRotated->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 < aRotationObjectRefs->size(); anInd++) {
104     ObjectPtr anObject = aRotationObjectRefs->object(anInd);
105     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
106     std::vector<bool>::iterator aUsedIt = isUsed.begin();
107     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
108       if (*anIt == anObject) {
109         *aUsedIt = true;
110         break;
111       }
112     if (anIt == anInitialList.end())
113       anAddition.push_back(anObject);
114   }
115   // remove unused items
116   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
117   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
118   std::vector<bool>::iterator aUsedIter = isUsed.begin();
119   for (; aUsedIter != isUsed.end(); aUsedIter++) {
120     if (!(*aUsedIter)) {
121       aRefListOfShapes->remove(*anInitIter);
122       aRefListOfRotated->remove(*aTargetIter++);
123       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
124         aRefListOfRotated->remove(*aTargetIter);
125         // remove the corresponding feature from the sketch
126         ResultConstructionPtr aRC =
127             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
128         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
129         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
130         if (aFeature)
131           aDoc->removeFeature(aFeature);
132       }
133     } else {
134       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
135         aTargetIter++;
136     }
137     if (anInitIter != anInitialList.end())
138       anInitIter++;
139   }
140   // change number of copies
141   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
142     bool isAdd = aNbCopies > aCurrentNbCopies;
143     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
144     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
145     int ind = 0;
146
147     aTargetList = aRefListOfRotated->list();
148     aTargetIter = aTargetList.begin();
149     ObjectPtr anObjToCopy = *aTargetIter;
150     while (aTargetIter != aTargetList.end()) {
151       aRefListOfRotated->remove(*aTargetIter);
152       aTargetIter++;
153       ind++;
154       if (ind > aMinCopies && ind <=aMaxCopies) {
155         while (ind <= aMaxCopies) {
156           if (isAdd) {
157             // Add new shifted item
158             ObjectPtr anObject = copyFeature(anObjToCopy);
159             aTargetList.insert(aTargetIter, anObject);
160           } else {
161             // remove object
162             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
163             ObjectPtr anObject = *aRemoveIt;
164             aTargetList.erase(aRemoveIt);
165             aRefListOfRotated->remove(anObject);
166             // remove the corresponding feature from the sketch
167             ResultConstructionPtr aRC =
168                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
169             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
170             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
171             if (aFeature)
172               aDoc->removeFeature(aFeature);
173           }
174           ind++;
175         }
176         ind = 0;
177         if (aTargetIter != aTargetList.end())
178           anObjToCopy = *aTargetIter;
179       }
180     }
181
182     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
183       aRefListOfRotated->append(*aTargetIter);
184   }
185   // add new items
186   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
187   for (; anAddIter != anAddition.end(); anAddIter++) {
188     aRefListOfShapes->append(*anAddIter);
189     aRefListOfRotated->append(*anAddIter);
190     for (int i = 0; i < aNbCopies; i++) {
191       ObjectPtr anObject = copyFeature(*anAddIter);
192       aRefListOfRotated->append(anObject);
193     }
194   }
195
196 ////  if (fabs(anAngle) > 1.e-12) {
197 ////    // Recalculate positions of features
198 ////    aTargetList = aRefListOfRotated->list();
199 ////    aTargetIter = aTargetList.begin();
200 ////    while (aTargetIter != aTargetList.end()) {
201 ////      ObjectPtr anInitialObject = *aTargetIter++;
202 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
203 ////        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
204 ////    }
205 ////  }
206
207   // send events to update the sub-features by the solver
208   if (isUpdateFlushed)
209     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
210 }
211
212 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
213 {
214   if (!sketch())
215     return thePrevious;
216
217   AISObjectPtr anAIS = thePrevious;
218   if (!anAIS) {
219     anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
220   }
221   return anAIS;
222 }
223
224
225 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
226 {
227   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
228   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
229   if (!aFeature || !aResult)
230     return ObjectPtr();
231
232   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
233   aNewFeature->execute();
234
235   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
236   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
237
238   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
239   const std::list<ResultPtr>& aResults = aNewFeature->results();
240   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
241   for (; anIt != aResults.end(); anIt++) {
242     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
243     if (!aRC) continue;
244     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
245     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
246         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
247         (aShapeIn->isFace() && aShapeOut->isFace()))
248       return aRC;
249   }
250   return ObjectPtr();
251 }
252
253 void SketchPlugin_MultiRotation::rotateFeature(
254     ObjectPtr theInitial, ObjectPtr theTarget,
255     double theCenterX, double theCenterY, double theAngle)
256 {
257   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
258   double cosA = std::cos(theAngle);
259   double sinA = std::sin(theAngle);
260
261   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
262   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
263
264   // block feature update
265   aTargetFeature->data()->blockSendAttributeUpdated(true);
266
267   std::list<AttributePtr> anInitAttrList =
268       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
269   std::list<AttributePtr> aTargetAttrList =
270       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
271   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
272   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
273   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
274     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
275         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
276     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
277         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
278     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
279     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
280       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
281       double dx = cosA * aDir->x() - sinA * aDir->y();
282       double dy = sinA * aDir->x() + cosA * aDir->y();
283       aPnt->setX(aCenter->x() + dx);
284       aPnt->setY(aCenter->y() + dy);
285     }
286     aPointTo->setValue(aPnt->x(), aPnt->y());
287   }
288
289   // unblock feature update
290   aTargetFeature->data()->blockSendAttributeUpdated(false);
291 }
292
293
294 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
295 {
296   if (theID == ROTATION_LIST_ID()) {
297     AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
298     if (aRotationObjectRefs->size() == 0) {
299       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
300       if (aNbCopies <= 0)
301         return;
302
303       // Clear list of objects
304       AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
305           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
306       std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
307       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
308       while (aTargetIter != aTargetList.end()) {
309         aTargetIter++;
310         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
311           aRefListOfRotated->remove(*aTargetIter);
312           // remove the corresponding feature from the sketch
313           ResultConstructionPtr aRC =
314             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
315           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
316           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
317           if (aFeature)
318             aDoc->removeFeature(aFeature);
319         }
320       }
321       aRefListOfRotated->clear();
322       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
323         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
324       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
325         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
326     }
327   }
328   else if (theID == ANGLE_ID() && !myBlockAngle) {
329     int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
330     if (aNbCopies > 0) {
331       myBlockAngle = true;
332       SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
333                                                aNbCopies, true);
334       myBlockAngle = false;
335       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
336     }
337   }
338   else if (theID == ANGLE_FULL_ID() && !myBlockAngle) {
339     int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
340     if (aNbCopies > 0) {
341       myBlockAngle = true;
342       SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_FULL_ID()), attribute(ANGLE_ID()),
343                                                aNbCopies, false);
344       myBlockAngle = false;
345       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
346     }
347   }
348   else if (theID == NUMBER_OF_OBJECTS_ID()) {
349     if (attribute(NUMBER_OF_OBJECTS_ID())->isInitialized() &&
350         attribute(ANGLE_ID())->isInitialized() &&
351         attribute(ANGLE_TYPE())->isInitialized()) {
352       AttributeStringPtr aMethodTypeAttr = string(ANGLE_TYPE());
353       std::string aMethodType = aMethodTypeAttr->value();
354       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
355       if (aNbCopies > 0) {
356         myBlockAngle = true;
357         if (aMethodType == "SingleAngle")
358           SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
359                                                    aNbCopies, true);
360         else {
361           SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_FULL_ID()), attribute(ANGLE_ID()),
362                                                    aNbCopies, false);
363         }
364         myBlockAngle = false;
365         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
366       }
367     }
368   }
369 }