Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
[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 #define PI 3.1415926535897932
26
27 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
28 {
29 }
30
31 void SketchPlugin_MultiRotation::initAttributes()
32 {
33   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
34   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
36   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
37   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
38   AttributeSelectionListPtr aSelection = 
39     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40     ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41   aSelection->setSelectionType("EDGE");
42   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
43   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
44 }
45
46 void SketchPlugin_MultiRotation::execute()
47 {
48   AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
49   int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
50       attribute(NUMBER_OF_COPIES_ID()))->value());
51
52   // Obtain center and angle of rotation
53   std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
54       attribute(CENTER_ID()));
55   if (!aCenter || !aCenter->isInitialized())
56     return;
57   double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
58       attribute(ANGLE_ID()))->value();
59   // Convert angle to radians
60   anAngle *= PI / 180.0;
61
62   // Wait all objects being created, then send update events
63   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
64   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
65   if (isUpdateFlushed)
66     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
67
68   std::shared_ptr<ModelAPI_Data> aData = data();
69   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
70       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
71   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
72       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
73   int aCurrentNbCopies = aRefListOfShapes->size() ?
74       aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
75   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
76   std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
77   std::list<ResultPtr> anAddition;
78   std::vector<bool> isUsed(anInitialList.size(), false);
79   // collect new items and check the items to remove
80   for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
81     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
82     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
83     std::vector<bool>::iterator aUsedIt = isUsed.begin();
84     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
85       if (*anIt == aSelect->context()) {
86         *aUsedIt = true;
87         break;
88       }
89     if (anIt == anInitialList.end())
90       anAddition.push_back(aSelect->context());
91   }
92   // remove unused items
93   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
94   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
95   std::vector<bool>::iterator aUsedIter = isUsed.begin();
96   for (; aUsedIter != isUsed.end(); aUsedIter++) {
97     if (!(*aUsedIter)) {
98       aRefListOfShapes->remove(*anInitIter);
99       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
100         aRefListOfRotated->remove(*aTargetIter);
101         // remove the corresponding feature from the sketch
102         ResultConstructionPtr aRC =
103             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
104         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
105         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
106         if (aFeature)
107           aDoc->removeFeature(aFeature);
108       }
109     } else {
110       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
111         aTargetIter++;
112     }
113     if (anInitIter != anInitialList.end())
114       anInitIter++;
115   }
116   // change number of copies
117   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
118     bool isAdd = aNbCopies > aCurrentNbCopies;
119     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
120     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
121     int ind = 0;
122
123     aTargetList = aRefListOfRotated->list();
124     aTargetIter = aTargetList.begin();
125     ObjectPtr anObjToCopy = *aTargetIter;
126     while (aTargetIter != aTargetList.end()) {
127       aRefListOfRotated->remove(*aTargetIter);
128       aTargetIter++;
129       ind++;
130       if (ind > aMinCopies && ind <=aMaxCopies) {
131         while (ind <= aMaxCopies) {
132           if (isAdd) {
133             // Add new shifted item
134             ObjectPtr anObject = copyFeature(anObjToCopy);
135             aTargetList.insert(aTargetIter, anObject);
136           } else {
137             // remove object
138             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
139             ObjectPtr anObject = *aRemoveIt;
140             aTargetList.erase(aRemoveIt);
141             aRefListOfRotated->remove(anObject);
142             // remove the corresponding feature from the sketch
143             ResultConstructionPtr aRC =
144                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
145             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
146             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
147             if (aFeature)
148               aDoc->removeFeature(aFeature);
149           }
150           ind++;
151         }
152         ind = 0;
153         if (aTargetIter != aTargetList.end())
154           anObjToCopy = *aTargetIter;
155       }
156     }
157
158     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
159       aRefListOfRotated->append(*aTargetIter);
160   }
161   // add new items
162   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
163   for (; anAddIter != anAddition.end(); anAddIter++) {
164     aRefListOfShapes->append(*anAddIter);
165     aRefListOfRotated->append(*anAddIter);
166     for (int i = 0; i < aNbCopies; i++) {
167       ObjectPtr anObject = copyFeature(*anAddIter);
168       aRefListOfRotated->append(anObject);
169     }
170   }
171
172 ////  if (fabs(anAngle) > 1.e-12) {
173 ////    // Recalculate positions of features
174 ////    aTargetList = aRefListOfRotated->list();
175 ////    aTargetIter = aTargetList.begin();
176 ////    while (aTargetIter != aTargetList.end()) {
177 ////      ObjectPtr anInitialObject = *aTargetIter++;
178 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
179 ////        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
180 ////    }
181 ////  }
182
183   // send events to update the sub-features by the solver
184   if (isUpdateFlushed)
185     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
186 }
187
188 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
189 {
190   if (!sketch())
191     return thePrevious;
192
193   AISObjectPtr anAIS = thePrevious;
194   if (!anAIS) {
195     anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
196   }
197   return anAIS;
198 }
199
200
201 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
202 {
203   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
204   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
205   if (!aFeature || !aResult)
206     return ObjectPtr();
207
208   FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
209   aFeature->data()->copyTo(aNewFeature->data());
210   aNewFeature->execute();
211
212   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
213   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
214
215   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
216   const std::list<ResultPtr>& aResults = aNewFeature->results();
217   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
218   for (; anIt != aResults.end(); anIt++) {
219     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
220     if (!aRC) continue;
221     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
222     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
223         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
224         (aShapeIn->isFace() && aShapeOut->isFace()))
225       return aRC;
226   }
227   return ObjectPtr();
228 }
229
230 void SketchPlugin_MultiRotation::rotateFeature(
231     ObjectPtr theInitial, ObjectPtr theTarget,
232     double theCenterX, double theCenterY, double theAngle)
233 {
234   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
235   double cosA = cos(theAngle);
236   double sinA = sin(theAngle);
237
238   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
239   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
240
241   // block feature update
242   aTargetFeature->data()->blockSendAttributeUpdated(true);
243
244   std::list<AttributePtr> anInitAttrList =
245       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
246   std::list<AttributePtr> aTargetAttrList =
247       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
248   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
249   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
250   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
251     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
252         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
253     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
254         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
255     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
256     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
257       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
258       double dx = cosA * aDir->x() - sinA * aDir->y();
259       double dy = sinA * aDir->x() + cosA * aDir->y();
260       aPnt->setX(aCenter->x() + dx);
261       aPnt->setY(aCenter->y() + dy);
262     }
263     aPointTo->setValue(aPnt->x(), aPnt->y());
264   }
265
266   // unblock feature update
267   aTargetFeature->data()->blockSendAttributeUpdated(false);
268 }
269