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