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