]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MultiTranslation.cpp
Salome HOME
a59283eb5bc0bfdc53b9c25a13bf31087775cc09
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MultiTranslation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_MultiTranslation.cpp
4 // Created: 21 Apr 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_MultiTranslation.h"
8
9 #include <GeomAPI_XY.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_Validator.h>
20
21 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
22 {
23 }
24
25 void SketchPlugin_MultiTranslation::initAttributes()
26 {
27   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
28   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
29   data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
30   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
32   AttributeSelectionListPtr aSelection = 
33     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34     TRANSLATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35   aSelection->setSelectionType("EDGE");
36   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
37   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
38 }
39
40 void SketchPlugin_MultiTranslation::execute()
41 {
42   AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
43   int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
44       attribute(NUMBER_OF_COPIES_ID()))->value());
45
46   // Calculate shift vector
47   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
48       attribute(START_POINT_ID()));
49   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
50       attribute(END_POINT_ID()));
51   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
52     return;
53   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
54
55   // Wait all objects being created, then send update events
56   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
57   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
58   if (isUpdateFlushed)
59     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
60
61   std::shared_ptr<ModelAPI_Data> aData = data();
62   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
63       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
64   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
65       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
66   int aCurrentNbCopies = aRefListOfShapes->size() ?
67       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
68   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
69   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
70   std::list<ResultPtr> anAddition;
71   std::vector<bool> isUsed(anInitialList.size(), false);
72   // collect new items and check the items to remove
73   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
74     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
75     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
76     std::vector<bool>::iterator aUsedIt = isUsed.begin();
77     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
78       if (*anIt == aSelect->context()) {
79         *aUsedIt = true;
80         break;
81       }
82     if (anIt == anInitialList.end())
83       anAddition.push_back(aSelect->context());
84   }
85   // remove unused items
86   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
87   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
88   std::vector<bool>::iterator aUsedIter = isUsed.begin();
89   for (; aUsedIter != isUsed.end(); aUsedIter++) {
90     if (!(*aUsedIter)) {
91       aRefListOfShapes->remove(*anInitIter);
92       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
93         aRefListOfTranslated->remove(*aTargetIter);
94         // remove the corresponding feature from the sketch
95         ResultConstructionPtr aRC =
96             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
97         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
98         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
99         if (aFeature)
100           aDoc->removeFeature(aFeature);
101       }
102     } else {
103       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
104         aTargetIter++;
105     }
106     if (anInitIter != anInitialList.end())
107       anInitIter++;
108   }
109   // change number of copies
110   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
111     bool isAdd = aNbCopies > aCurrentNbCopies;
112     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
113     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
114     int ind = 0;
115
116     aTargetList = aRefListOfTranslated->list();
117     aTargetIter = aTargetList.begin();
118     ObjectPtr anObjToCopy = *aTargetIter;
119     while (aTargetIter != aTargetList.end()) {
120       aRefListOfTranslated->remove(*aTargetIter);
121       aTargetIter++;
122       ind++;
123       if (ind > aMinCopies && ind <=aMaxCopies) {
124         while (ind <= aMaxCopies) {
125           if (isAdd) {
126             // Add new shifted item
127             ObjectPtr anObject = copyFeature(anObjToCopy);
128             aTargetList.insert(aTargetIter, anObject);
129           } else {
130             // remove object
131             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter;
132             ObjectPtr anObject = *(--aRemoveIt);
133             aTargetList.erase(aRemoveIt);
134             // remove the corresponding feature from the sketch
135             ResultConstructionPtr aRC =
136                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
137             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
138             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
139             if (aFeature)
140               aDoc->removeFeature(aFeature);
141           }
142           ind++;
143         }
144         ind = 0;
145         if (aTargetIter != aTargetList.end())
146           anObjToCopy = *aTargetIter;
147       }
148     }
149
150     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
151       aRefListOfTranslated->append(*aTargetIter);
152   }
153   // add new items
154   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
155   for (; anAddIter != anAddition.end(); anAddIter++) {
156     aRefListOfShapes->append(*anAddIter);
157     aRefListOfTranslated->append(*anAddIter);
158     for (int i = 0; i < aNbCopies; i++) {
159       ObjectPtr anObject = copyFeature(*anAddIter);
160       aRefListOfTranslated->append(anObject);
161     }
162   }
163
164   // Recalculate positions of features
165   aTargetList = aRefListOfTranslated->list();
166   aTargetIter = aTargetList.begin();
167   while (aTargetIter != aTargetList.end()) {
168     ObjectPtr anInitialObject = *aTargetIter++;
169     for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
170       shiftFeature(anInitialObject, *aTargetIter,
171           aShiftVec->x() * (i + 1), aShiftVec->y() * (i + 1));
172   }
173
174   // send events to update the sub-features by the solver
175   if (isUpdateFlushed)
176     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
177 }
178
179 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
180 {
181   if (!sketch())
182     return thePrevious;
183
184   AISObjectPtr anAIS = thePrevious;
185   if (!anAIS) {
186 // TODO:
187 //    anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
188   }
189   return anAIS;
190 }
191
192 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
193 {
194   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
195   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
196   if (!aFeature || !aResult)
197     return ObjectPtr();
198
199   FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
200   aFeature->data()->copyTo(aNewFeature->data());
201   aNewFeature->execute();
202
203   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
204   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
205
206   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
207   const std::list<ResultPtr>& aResults = aNewFeature->results();
208   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
209   for (; anIt != aResults.end(); anIt++) {
210     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
211     if (!aRC) continue;
212     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
213     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
214         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
215         (aShapeIn->isFace() && aShapeOut->isFace()))
216       return aRC;
217   }
218   return ObjectPtr();
219 }
220
221 void SketchPlugin_MultiTranslation::shiftFeature(
222     ObjectPtr theInitial, ObjectPtr theTarget, double theDeltaX, double theDeltaY)
223 {
224   FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
225   FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
226
227   // block feature update
228   aTargetFeature->data()->blockSendAttributeUpdated(true);
229
230   std::list<AttributePtr> anInitAttrList =
231       anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
232   std::list<AttributePtr> aTargetAttrList =
233       aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
234   std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
235   std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
236   for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
237     std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
238         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
239     std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
240         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
241     aPointTo->setValue(aPointFrom->x() + theDeltaX, aPointFrom->y() + theDeltaY);
242   }
243
244   // unblock feature update
245   aTargetFeature->data()->blockSendAttributeUpdated(false);
246 }