]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MultiRotation.cpp
Salome HOME
Update Multi-Rotation tool to be used by solver
[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             aRefListOfRotated->remove(anObject);
140             // remove the corresponding feature from the sketch
141             ResultConstructionPtr aRC =
142                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
143             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
144             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
145             if (aFeature)
146               aDoc->removeFeature(aFeature);
147           }
148           ind++;
149         }
150         ind = 0;
151         if (aTargetIter != aTargetList.end())
152           anObjToCopy = *aTargetIter;
153       }
154     }
155
156     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
157       aRefListOfRotated->append(*aTargetIter);
158   }
159   // add new items
160   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
161   for (; anAddIter != anAddition.end(); anAddIter++) {
162     aRefListOfShapes->append(*anAddIter);
163     aRefListOfRotated->append(*anAddIter);
164     for (int i = 0; i < aNbCopies; i++) {
165       ObjectPtr anObject = copyFeature(*anAddIter);
166       aRefListOfRotated->append(anObject);
167     }
168   }
169
170 ////  if (fabs(anAngle) > 1.e-12) {
171 ////    // Recalculate positions of features
172 ////    aTargetList = aRefListOfRotated->list();
173 ////    aTargetIter = aTargetList.begin();
174 ////    while (aTargetIter != aTargetList.end()) {
175 ////      ObjectPtr anInitialObject = *aTargetIter++;
176 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
177 ////        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
178 ////    }
179 ////  }
180
181   // send events to update the sub-features by the solver
182   if (isUpdateFlushed)
183     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
184 }
185
186 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
187 {
188   if (!sketch())
189     return thePrevious;
190
191   AISObjectPtr anAIS = thePrevious;
192   if (!anAIS) {
193 // TODO:
194 //    anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
195   }
196   return anAIS;
197 }
198
199
200 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
201 {
202   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
203   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
204   if (!aFeature || !aResult)
205     return ObjectPtr();
206
207   FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
208   aFeature->data()->copyTo(aNewFeature->data());
209   aNewFeature->execute();
210
211   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
212   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
213
214   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
215   const std::list<ResultPtr>& aResults = aNewFeature->results();
216   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
217   for (; anIt != aResults.end(); anIt++) {
218     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
219     if (!aRC) continue;
220     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
221     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
222         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
223         (aShapeIn->isFace() && aShapeOut->isFace()))
224       return aRC;
225   }
226   return ObjectPtr();
227 }
228
229 void SketchPlugin_MultiRotation::rotateFeature(
230     ObjectPtr theInitial, ObjectPtr theTarget,
231     double theCenterX, double theCenterY, double theAngle)
232 {
233   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
234   double cosA = cos(theAngle);
235   double sinA = sin(theAngle);
236
237   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
238   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
239
240   // block feature update
241   aTargetFeature->data()->blockSendAttributeUpdated(true);
242
243   std::list<AttributePtr> anInitAttrList =
244       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
245   std::list<AttributePtr> aTargetAttrList =
246       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
247   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
248   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
249   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
250     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
251         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
252     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
253         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
254     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
255     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
256       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
257       double dx = cosA * aDir->x() - sinA * aDir->y();
258       double dy = sinA * aDir->x() + cosA * aDir->y();
259       aPnt->setX(aCenter->x() + dx);
260       aPnt->setY(aCenter->y() + dy);
261     }
262     aPointTo->setValue(aPnt->x(), aPnt->y());
263   }
264
265   // unblock feature update
266   aTargetFeature->data()->blockSendAttributeUpdated(false);
267 }
268