Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MultiRotation.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_MultiRotation.h"
22 #include "SketchPlugin_Tools.h"
23
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeInteger.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Tools.h>
36
37 #include <SketchPlugin_SketchEntity.h>
38
39 #include <GeomAPI_Pnt2d.h>
40 #include <GeomAPI_XY.h>
41
42 #include <SketcherPrs_Factory.h>
43
44 #include <cmath>
45
46 #define PI 3.1415926535897932
47
48 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
49 {
50 }
51
52 void SketchPlugin_MultiRotation::initAttributes()
53 {
54   data()->addAttribute(CENTER_ID(), ModelAPI_AttributeRefAttr::typeId());
55
56   data()->addAttribute(ANGLE_TYPE(),   ModelAPI_AttributeString::typeId());
57   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
60   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
61   data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
62   ModelAPI_Session::get()->validators()->
63     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
64   ModelAPI_Session::get()->validators()->
65     registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
66 }
67
68 void SketchPlugin_MultiRotation::execute()
69 {
70   if (!sketch()) {
71     // it is possible, that this method is called before this feature has back reference to sketch
72     // in this case, the execute is performed after this is done
73     return;
74   }
75
76   AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
77   int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
78   if (aNbCopies <= 0)
79     return;
80
81   // Obtain center and angle of rotation
82   AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
83   if (!aCenter || !aCenter->isInitialized())
84     return;
85
86   //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
87   //                                                           attribute(ANGLE_ID()))->value();
88
89   // Convert angle to radians
90   //anAngle *= PI / 180.0;
91
92   // Wait all objects being created, then send update events
93   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
94   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
95   if (isUpdateFlushed)
96     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
97
98   std::shared_ptr<ModelAPI_Data> aData = data();
99   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
100       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
101   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
102       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
103   int aCurrentNbCopies = aRefListOfShapes->size() ?
104       aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
105   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
106   std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
107   std::list<ObjectPtr> anAddition;
108   std::vector<bool> isUsed(anInitialList.size(), false);
109   // collect new items and check the items to remove
110   for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
111     ObjectPtr anObject = aRotationObjectRefs->object(anInd);
112     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
113     std::vector<bool>::iterator aUsedIt = isUsed.begin();
114     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
115       if (*anIt == anObject) {
116         *aUsedIt = true;
117         break;
118       }
119     if (anIt == anInitialList.end())
120       anAddition.push_back(anObject);
121   }
122   // remove unused items
123   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
124   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
125   std::vector<bool>::iterator aUsedIter = isUsed.begin();
126   std::set<FeaturePtr> aFeaturesToBeRemoved;
127   for (; aUsedIter != isUsed.end(); aUsedIter++) {
128     if (!(*aUsedIter)) {
129       aRefListOfShapes->remove(*anInitIter);
130       aRefListOfRotated->remove(*aTargetIter++);
131       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
132            i++, aTargetIter++) {
133         aRefListOfRotated->remove(*aTargetIter);
134         // remove the corresponding feature from the sketch
135         ResultConstructionPtr aRC =
136             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
137         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
138         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
139         if (aFeature)
140           aFeaturesToBeRemoved.insert(aFeature);
141       }
142     } else {
143       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
144         aTargetIter++;
145     }
146     if (anInitIter != anInitialList.end())
147       anInitIter++;
148   }
149   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
150   // change number of copies
151   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
152     bool isAdd = aNbCopies > aCurrentNbCopies;
153     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
154     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
155     int ind = 0;
156
157     aTargetList = aRefListOfRotated->list();
158     aTargetIter = aTargetList.begin();
159     ObjectPtr anObjToCopy = *aTargetIter;
160     std::set<FeaturePtr> aFeaturesToBeRemoved;
161     while (aTargetIter != aTargetList.end()) {
162       aRefListOfRotated->remove(*aTargetIter);
163       aTargetIter++;
164       ind++;
165       if (ind > aMinCopies && ind <=aMaxCopies) {
166         while (ind <= aMaxCopies) {
167           if (isAdd) {
168             // Add new shifted item
169             ObjectPtr anObject = copyFeature(anObjToCopy);
170             aTargetList.insert(aTargetIter, anObject);
171           } else {
172             // remove object
173             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
174             ObjectPtr anObject = *aRemoveIt;
175             aTargetList.erase(aRemoveIt);
176             aRefListOfRotated->remove(anObject);
177             // remove the corresponding feature from the sketch
178             ResultConstructionPtr aRC =
179                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
180             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
181             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
182             if (aFeature)
183               aFeaturesToBeRemoved.insert(aFeature);
184           }
185           ind++;
186         }
187         ind = 0;
188         if (aTargetIter != aTargetList.end())
189           anObjToCopy = *aTargetIter;
190       }
191     }
192     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
193
194     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
195       aRefListOfRotated->append(*aTargetIter);
196   }
197   // add new items
198   std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
199   for (; anAddIter != anAddition.end(); anAddIter++) {
200     aRefListOfShapes->append(*anAddIter);
201     aRefListOfRotated->append(*anAddIter);
202     for (int i = 0; i < aNbCopies; i++) {
203       ObjectPtr anObject = copyFeature(*anAddIter);
204       aRefListOfRotated->append(anObject);
205     }
206   }
207
208 ////  if (fabs(anAngle) > 1.e-12) {
209 ////    // Recalculate positions of features
210 ////    aTargetList = aRefListOfRotated->list();
211 ////    aTargetIter = aTargetList.begin();
212 ////    while (aTargetIter != aTargetList.end()) {
213 ////      ObjectPtr anInitialObject = *aTargetIter++;
214 ////      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
215 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
216 ////    }
217 ////  }
218
219   // send events to update the sub-features by the solver
220   if (isUpdateFlushed)
221     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
222 }
223
224 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
225 {
226   if (!sketch())
227     return thePrevious;
228
229   AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
230                                                              thePrevious);
231   return anAIS;
232 }
233
234 void SketchPlugin_MultiRotation::erase()
235 {
236   static Events_Loop* aLoop = Events_Loop::loop();
237   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
238
239   // Set copy attribute to false on all copied features.
240   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
241       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
242   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
243       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
244
245   if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
246     for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
247       ObjectPtr anObject = aRefListOfRotated->object(anIndex);
248       if(aRefListOfShapes->isInList(anObject)) {
249         // Don't modify attribute of original features, just skip.
250         continue;
251       }
252       if(anObject.get()) {
253         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
254         if(aRes.get()) {
255           FeaturePtr aFeature = aRes->document()->feature(aRes);
256           if(aFeature.get()) {
257             AttributeBooleanPtr aBooleanAttr =
258               aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
259             if(aBooleanAttr.get()) {
260               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
261                 aBooleanAttr->setValue(false);
262               // Redisplay object as it is not copy anymore.
263               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
264             }
265           }
266         }
267       }
268     }
269   }
270
271   SketchPlugin_ConstraintBase::erase();
272 }
273
274 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
275 {
276   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
277   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
278   if (!aFeature || !aResult)
279     return ObjectPtr();
280
281   FeaturePtr aNewFeature =
282     SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
283   aNewFeature->execute();
284
285   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
286   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
287
288   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
289   const std::list<ResultPtr>& aResults = aNewFeature->results();
290   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
291   for (; anIt != aResults.end(); anIt++) {
292     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
293     if (!aRC) continue;
294     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
295     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
296         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
297         (aShapeIn->isFace() && aShapeOut->isFace()))
298       return aRC;
299   }
300   return ObjectPtr();
301 }
302
303 /*void SketchPlugin_MultiRotation::rotateFeature(
304     ObjectPtr theInitial, ObjectPtr theTarget,
305     double theCenterX, double theCenterY, double theAngle)
306 {
307   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
308   double cosA = std::cos(theAngle);
309   double sinA = std::sin(theAngle);
310
311   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
312   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
313
314   // block feature update
315   aTargetFeature->data()->blockSendAttributeUpdated(true);
316
317   std::list<AttributePtr> anInitAttrList =
318       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
319   std::list<AttributePtr> aTargetAttrList =
320       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
321   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
322   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
323   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
324     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
325         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
326     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
327         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
328     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
329     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
330       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
331       double dx = cosA * aDir->x() - sinA * aDir->y();
332       double dy = sinA * aDir->x() + cosA * aDir->y();
333       aPnt->setX(aCenter->x() + dx);
334       aPnt->setY(aCenter->y() + dy);
335     }
336     aPointTo->setValue(aPnt->x(), aPnt->y());
337   }
338
339   // unblock feature update
340   aTargetFeature->data()->blockSendAttributeUpdated(false);
341 }*/
342
343
344 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
345 {
346   if (theID == ROTATION_LIST_ID()) {
347     AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
348     if (aRotationObjectRefs->size() == 0) {
349       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
350       if (aNbCopies <= 0)
351         return;
352
353       // Clear list of objects
354       AttributeRefListPtr aRefListOfRotated = reflist(SketchPlugin_Constraint::ENTITY_B());
355       std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
356       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
357       std::set<FeaturePtr> aFeaturesToBeRemoved;
358       while (aTargetIter != aTargetList.end()) {
359         aTargetIter++;
360         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
361           aRefListOfRotated->remove(*aTargetIter);
362           // remove the corresponding feature from the sketch
363           FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
364           if (aFeature)
365             aFeaturesToBeRemoved.insert(aFeature);
366         }
367       }
368       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
369
370       aRefListOfRotated->clear();
371       reflist(SketchPlugin_Constraint::ENTITY_A())->clear();
372     }
373   }
374 }