]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MultiRotation.cpp
Salome HOME
Fixed validators for Boolean and Intersection
[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       }
125     } else {
126       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
127         aTargetIter++;
128     }
129     if (anInitIter != anInitialList.end())
130       anInitIter++;
131   }
132   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
133   // change number of copies
134   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
135     bool isAdd = aNbCopies > aCurrentNbCopies;
136     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
137     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
138     int ind = 0;
139
140     aTargetList = aRefListOfRotated->list();
141     aTargetIter = aTargetList.begin();
142     ObjectPtr anObjToCopy = *aTargetIter;
143     std::set<FeaturePtr> aFeaturesToBeRemoved;
144     while (aTargetIter != aTargetList.end()) {
145       aRefListOfRotated->remove(*aTargetIter);
146       aTargetIter++;
147       ind++;
148       if (ind > aMinCopies && ind <=aMaxCopies) {
149         while (ind <= aMaxCopies) {
150           if (isAdd) {
151             // Add new shifted item
152             ObjectPtr anObject = copyFeature(anObjToCopy);
153             aTargetList.insert(aTargetIter, anObject);
154           } else {
155             // remove object
156             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
157             ObjectPtr anObject = *aRemoveIt;
158             aTargetList.erase(aRemoveIt);
159             aRefListOfRotated->remove(anObject);
160             // remove the corresponding feature from the sketch
161             ResultConstructionPtr aRC =
162                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
163             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
164             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
165             if (aFeature)
166               aFeaturesToBeRemoved.insert(aFeature);
167           }
168           ind++;
169         }
170         ind = 0;
171         if (aTargetIter != aTargetList.end())
172           anObjToCopy = *aTargetIter;
173       }
174     }
175     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
176
177     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
178       aRefListOfRotated->append(*aTargetIter);
179   }
180   // add new items
181   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
182   for (; anAddIter != anAddition.end(); anAddIter++) {
183     aRefListOfShapes->append(*anAddIter);
184     aRefListOfRotated->append(*anAddIter);
185     for (int i = 0; i < aNbCopies; i++) {
186       ObjectPtr anObject = copyFeature(*anAddIter);
187       aRefListOfRotated->append(anObject);
188     }
189   }
190
191 ////  if (fabs(anAngle) > 1.e-12) {
192 ////    // Recalculate positions of features
193 ////    aTargetList = aRefListOfRotated->list();
194 ////    aTargetIter = aTargetList.begin();
195 ////    while (aTargetIter != aTargetList.end()) {
196 ////      ObjectPtr anInitialObject = *aTargetIter++;
197 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
198 ////        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
199 ////    }
200 ////  }
201
202   // send events to update the sub-features by the solver
203   if (isUpdateFlushed)
204     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
205 }
206
207 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
208 {
209   if (!sketch())
210     return thePrevious;
211
212   AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
213                                                              thePrevious);
214   return anAIS;
215 }
216
217 void SketchPlugin_MultiRotation::erase()
218 {
219   static Events_Loop* aLoop = Events_Loop::loop();
220   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
221
222   // Set copy attribute to false on all copied features.
223   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
224       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
225   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
227
228   if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
229     for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
230       ObjectPtr anObject = aRefListOfRotated->object(anIndex);
231       if(aRefListOfShapes->isInList(anObject)) {
232         // Don't modify attribute of original features, just skip.
233         continue;
234       }
235       if(anObject.get()) {
236         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
237         if(aRes.get()) {
238           FeaturePtr aFeature = aRes->document()->feature(aRes);
239           if(aFeature.get()) {
240             AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
241             if(aBooleanAttr.get()) {
242               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
243                 aBooleanAttr->setValue(false);
244               // Redisplay object as it is not copy anymore.
245               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
246             }
247           }
248         }
249       }
250     }
251   }
252
253   SketchPlugin_ConstraintBase::erase();
254 }
255
256 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
257 {
258   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
259   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
260   if (!aFeature || !aResult)
261     return ObjectPtr();
262
263   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
264   aNewFeature->execute();
265
266   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
267   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
268
269   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
270   const std::list<ResultPtr>& aResults = aNewFeature->results();
271   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
272   for (; anIt != aResults.end(); anIt++) {
273     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
274     if (!aRC) continue;
275     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
276     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
277         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
278         (aShapeIn->isFace() && aShapeOut->isFace()))
279       return aRC;
280   }
281   return ObjectPtr();
282 }
283
284 /*void SketchPlugin_MultiRotation::rotateFeature(
285     ObjectPtr theInitial, ObjectPtr theTarget,
286     double theCenterX, double theCenterY, double theAngle)
287 {
288   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
289   double cosA = std::cos(theAngle);
290   double sinA = std::sin(theAngle);
291
292   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
293   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
294
295   // block feature update
296   aTargetFeature->data()->blockSendAttributeUpdated(true);
297
298   std::list<AttributePtr> anInitAttrList =
299       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
300   std::list<AttributePtr> aTargetAttrList =
301       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
302   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
303   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
304   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
305     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
306         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
307     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
308         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
309     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
310     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
311       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
312       double dx = cosA * aDir->x() - sinA * aDir->y();
313       double dy = sinA * aDir->x() + cosA * aDir->y();
314       aPnt->setX(aCenter->x() + dx);
315       aPnt->setY(aCenter->y() + dy);
316     }
317     aPointTo->setValue(aPnt->x(), aPnt->y());
318   }
319
320   // unblock feature update
321   aTargetFeature->data()->blockSendAttributeUpdated(false);
322 }*/
323
324
325 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
326 {
327   if (theID == ROTATION_LIST_ID()) {
328     AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
329     if (aRotationObjectRefs->size() == 0) {
330       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
331       if (aNbCopies <= 0)
332         return;
333
334       // Clear list of objects
335       AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
336           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
337       std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
338       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
339       std::set<FeaturePtr> aFeaturesToBeRemoved;
340       while (aTargetIter != aTargetList.end()) {
341         aTargetIter++;
342         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
343           aRefListOfRotated->remove(*aTargetIter);
344           // remove the corresponding feature from the sketch
345           ResultConstructionPtr aRC =
346             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
347           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
348           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
349           if (aFeature)
350             aFeaturesToBeRemoved.insert(aFeature);
351         }
352       }
353       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
354
355       aRefListOfRotated->clear();
356       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
357         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
358       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
359         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
360     }
361   }
362 }