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