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