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