Salome HOME
Issue #2208: Development in progress
[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(),
230                                                              sketch()->coordinatePlane(),
231                                                              thePrevious);
232   return anAIS;
233 }
234
235 void SketchPlugin_MultiRotation::erase()
236 {
237   static Events_Loop* aLoop = Events_Loop::loop();
238   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
239
240   // Set copy attribute to false on all copied features.
241   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
242       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
243   AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
244       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
245
246   if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
247     for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
248       ObjectPtr anObject = aRefListOfRotated->object(anIndex);
249       if(aRefListOfShapes->isInList(anObject)) {
250         // Don't modify attribute of original features, just skip.
251         continue;
252       }
253       if(anObject.get()) {
254         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
255         if(aRes.get()) {
256           FeaturePtr aFeature = aRes->document()->feature(aRes);
257           if(aFeature.get()) {
258             AttributeBooleanPtr aBooleanAttr =
259               aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
260             if(aBooleanAttr.get()) {
261               if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
262                 aBooleanAttr->setValue(false);
263               // Redisplay object as it is not copy anymore.
264               ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
265             }
266           }
267         }
268       }
269     }
270   }
271
272   SketchPlugin_ConstraintBase::erase();
273 }
274
275 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
276 {
277   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
278   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
279   if (!aFeature || !aResult)
280     return ObjectPtr();
281
282   FeaturePtr aNewFeature =
283     SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
284   aNewFeature->execute();
285
286   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
287   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
288
289   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
290   const std::list<ResultPtr>& aResults = aNewFeature->results();
291   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
292   for (; anIt != aResults.end(); anIt++) {
293     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
294     if (!aRC) continue;
295     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
296     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
297         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
298         (aShapeIn->isFace() && aShapeOut->isFace()))
299       return aRC;
300   }
301   return ObjectPtr();
302 }
303
304 /*void SketchPlugin_MultiRotation::rotateFeature(
305     ObjectPtr theInitial, ObjectPtr theTarget,
306     double theCenterX, double theCenterY, double theAngle)
307 {
308   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
309   double cosA = std::cos(theAngle);
310   double sinA = std::sin(theAngle);
311
312   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
313   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
314
315   // block feature update
316   aTargetFeature->data()->blockSendAttributeUpdated(true);
317
318   std::list<AttributePtr> anInitAttrList =
319       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
320   std::list<AttributePtr> aTargetAttrList =
321       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
322   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
323   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
324   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
325     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
326         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
327     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
328         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
329     std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
330     if (aPnt->distance(aCenter->xy()) > 1.e-7) {
331       std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
332       double dx = cosA * aDir->x() - sinA * aDir->y();
333       double dy = sinA * aDir->x() + cosA * aDir->y();
334       aPnt->setX(aCenter->x() + dx);
335       aPnt->setY(aCenter->y() + dy);
336     }
337     aPointTo->setValue(aPnt->x(), aPnt->y());
338   }
339
340   // unblock feature update
341   aTargetFeature->data()->blockSendAttributeUpdated(false);
342 }*/
343
344
345 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
346 {
347   if (theID == ROTATION_LIST_ID()) {
348     AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
349     if (aRotationObjectRefs->size() == 0) {
350       int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
351       if (aNbCopies <= 0)
352         return;
353
354       // Clear list of objects
355       AttributeRefListPtr aRefListOfRotated = reflist(SketchPlugin_Constraint::ENTITY_B());
356       std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
357       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
358       std::set<FeaturePtr> aFeaturesToBeRemoved;
359       while (aTargetIter != aTargetList.end()) {
360         aTargetIter++;
361         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
362           aRefListOfRotated->remove(*aTargetIter);
363           // remove the corresponding feature from the sketch
364           FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
365           if (aFeature)
366             aFeaturesToBeRemoved.insert(aFeature);
367         }
368       }
369       ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
370
371       aRefListOfRotated->clear();
372       reflist(SketchPlugin_Constraint::ENTITY_A())->clear();
373     }
374   }
375 }