Salome HOME
5864a194e901dcd8a44756c4fed3a7fc5a767374
[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
9 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_AttributeRefList.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
19
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_XY.h>
22
23 #include <SketcherPrs_Factory.h>
24
25 #include <cmath>
26
27 #define PI 3.1415926535897932
28
29 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
30 {
31 }
32
33 void SketchPlugin_MultiRotation::initAttributes()
34 {
35   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
36   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
37   data()->addAttribute(NUMBER_OF_COPIES_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   AttributeSelectionListPtr aSelection = 
41     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42     ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
43   aSelection->setSelectionType("EDGE");
44   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
45   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
46 }
47
48 void SketchPlugin_MultiRotation::execute()
49 {
50   if (!sketch()) {
51     // it is possible, that this method is called before this feature has back reference to sketch
52     // in this case, the execute is performed after this is done
53     return;
54   }
55
56   AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
57   int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
58
59   // Obtain center and angle of rotation
60   std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61       attribute(CENTER_ID()));
62   if (!aCenter || !aCenter->isInitialized())
63     return;
64   // make a visible points
65   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
66
67   double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
68       attribute(ANGLE_ID()))->value();
69   // Convert angle to radians
70   anAngle *= PI / 180.0;
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   std::shared_ptr<ModelAPI_Data> aData = data();
79   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
80       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
81   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
82       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
83   int aCurrentNbCopies = aRefListOfShapes->size() ?
84       aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
85   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
86   std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
87   std::list<ResultPtr> anAddition;
88   std::vector<bool> isUsed(anInitialList.size(), false);
89   // collect new items and check the items to remove
90   for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
91     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(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 == aSelect->context()) {
96         *aUsedIt = true;
97         break;
98       }
99     if (anIt == anInitialList.end())
100       anAddition.push_back(aSelect->context());
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       aRefListOfRotated->remove(*aTargetIter++);
110       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
111         aRefListOfRotated->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 = aRefListOfRotated->list();
135     aTargetIter = aTargetList.begin();
136     ObjectPtr anObjToCopy = *aTargetIter;
137     while (aTargetIter != aTargetList.end()) {
138       aRefListOfRotated->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             aRefListOfRotated->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       aRefListOfRotated->append(*aTargetIter);
171   }
172   // add new items
173   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
174   for (; anAddIter != anAddition.end(); anAddIter++) {
175     aRefListOfShapes->append(*anAddIter);
176     aRefListOfRotated->append(*anAddIter);
177     for (int i = 0; i < aNbCopies; i++) {
178       ObjectPtr anObject = copyFeature(*anAddIter);
179       aRefListOfRotated->append(anObject);
180     }
181   }
182
183 ////  if (fabs(anAngle) > 1.e-12) {
184 ////    // Recalculate positions of features
185 ////    aTargetList = aRefListOfRotated->list();
186 ////    aTargetIter = aTargetList.begin();
187 ////    while (aTargetIter != aTargetList.end()) {
188 ////      ObjectPtr anInitialObject = *aTargetIter++;
189 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
190 ////        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
191 ////    }
192 ////  }
193
194   // send events to update the sub-features by the solver
195   if (isUpdateFlushed)
196     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
197 }
198
199 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
200 {
201   if (!sketch())
202     return thePrevious;
203
204   AISObjectPtr anAIS = thePrevious;
205   if (!anAIS) {
206     anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
207   }
208   return anAIS;
209 }
210
211
212 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
213 {
214   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
215   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
216   if (!aFeature || !aResult)
217     return ObjectPtr();
218
219   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
220   aNewFeature->execute();
221
222   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
223   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
224
225   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
226   const std::list<ResultPtr>& aResults = aNewFeature->results();
227   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
228   for (; anIt != aResults.end(); anIt++) {
229     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
230     if (!aRC) continue;
231     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
232     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
233         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
234         (aShapeIn->isFace() && aShapeOut->isFace()))
235       return aRC;
236   }
237   return ObjectPtr();
238 }
239
240 void SketchPlugin_MultiRotation::rotateFeature(
241     ObjectPtr theInitial, ObjectPtr theTarget,
242     double theCenterX, double theCenterY, double theAngle)
243 {
244   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
245   double cosA = std::cos(theAngle);
246   double sinA = std::sin(theAngle);
247
248   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
249   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
250
251   // block feature update
252   aTargetFeature->data()->blockSendAttributeUpdated(true);
253
254   std::list<AttributePtr> anInitAttrList =
255       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
256   std::list<AttributePtr> aTargetAttrList =
257       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
258   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
259   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
260   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
261     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
262         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
263     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
264         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
265     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
266     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
267       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
268       double dx = cosA * aDir->x() - sinA * aDir->y();
269       double dy = sinA * aDir->x() + cosA * aDir->y();
270       aPnt->setX(aCenter->x() + dx);
271       aPnt->setY(aCenter->y() + dy);
272     }
273     aPointTo->setValue(aPnt->x(), aPnt->y());
274   }
275
276   // unblock feature update
277   aTargetFeature->data()->blockSendAttributeUpdated(false);
278 }
279
280
281 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
282 {
283   if (theID == ROTATION_LIST_ID()) {
284     AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
285     if (aRotationObjectRefs->size() == 0) {
286       // the commented code is not necessary here because if an update event is flushed
287       // before the setFlushed with true value happens, it leads to crash
288       // Wait all objects being created, then send update events
289       static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
290       //bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
291       //if (isUpdateFlushed)
292       //  Events_Loop::loop()->setFlushed(anUpdateEvent, false);
293
294       int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
295       // Clear list of objects
296       AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
297           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
298       std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
299       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
300       while (aTargetIter != aTargetList.end()) {
301         aTargetIter++;
302         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
303           aRefListOfRotated->remove(*aTargetIter);
304           // remove the corresponding feature from the sketch
305           ResultConstructionPtr aRC =
306             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
307           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
308           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
309           if (aFeature)
310             aDoc->removeFeature(aFeature);
311         }
312       }
313       aRefListOfRotated->clear();
314       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
315         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
316       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
317         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
318
319       // the commented code is incorrect and obsolete, so it is removed
320       // send events to update the sub-features by the solver
321       //if (isUpdateFlushed)
322       //  Events_Loop::loop()->setFlushed(anUpdateEvent, true);
323     }
324   }
325 }