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