Salome HOME
Make concealment of results working on compsolids: if at least one sub-body is concea...
[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 #include <SketcherPrs_Factory.h>
22
23 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
24 {
25 }
26
27 void SketchPlugin_MultiTranslation::initAttributes()
28 {
29   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
30   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
31   data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     TRANSLATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   aSelection->setSelectionType("EDGE");
38   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
39   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
40 }
41
42 void SketchPlugin_MultiTranslation::execute()
43 {
44   if (!sketch()) {
45     // it is possible, that this method is called before this feature has back reference to sketch
46     // in this case, the execute is performed after this is done
47     return;
48   }
49
50   AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
51   int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
52
53   // Calculate shift vector
54   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
55       attribute(START_POINT_ID()));
56   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57       attribute(END_POINT_ID()));
58   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
59     return;
60
61   // make a visible points
62   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
63   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), END_POINT_ID(), 1);
64
65   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
66
67   // Wait all objects being created, then send update events
68   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
69   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
70   if (isUpdateFlushed)
71     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
72
73   std::shared_ptr<ModelAPI_Data> aData = data();
74   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
75       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
77       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
78   int aCurrentNbCopies = aRefListOfShapes->size() ?
79       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
80   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
81   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
82   std::list<ResultPtr> anAddition;
83   std::vector<bool> isUsed(anInitialList.size(), false);
84   // collect new items and check the items to remove
85   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
86     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
87     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
88     std::vector<bool>::iterator aUsedIt = isUsed.begin();
89     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
90       if (*anIt == aSelect->context()) {
91         *aUsedIt = true;
92         break;
93       }
94     if (anIt == anInitialList.end())
95       anAddition.push_back(aSelect->context());
96   }
97   // remove unused items
98   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
99   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
100   std::vector<bool>::iterator aUsedIter = isUsed.begin();
101   for (; aUsedIter != isUsed.end(); aUsedIter++) {
102     if (!(*aUsedIter)) {
103       aRefListOfShapes->remove(*anInitIter);
104       aRefListOfTranslated->remove(*aTargetIter++);
105       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
106         aRefListOfTranslated->remove(*aTargetIter);
107         // remove the corresponding feature from the sketch
108         ResultConstructionPtr aRC =
109             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
110         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
111         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
112         if (aFeature)
113           aDoc->removeFeature(aFeature);
114       }
115     } else {
116       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
117         aTargetIter++;
118     }
119     if (anInitIter != anInitialList.end())
120       anInitIter++;
121   }
122   // change number of copies
123   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
124     bool isAdd = aNbCopies > aCurrentNbCopies;
125     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
126     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
127     int ind = 0;
128
129     aTargetList = aRefListOfTranslated->list();
130     aTargetIter = aTargetList.begin();
131     ObjectPtr anObjToCopy = *aTargetIter;
132     while (aTargetIter != aTargetList.end()) {
133       aRefListOfTranslated->remove(*aTargetIter);
134       aTargetIter++;
135       ind++;
136       if (ind > aMinCopies && ind <=aMaxCopies) {
137         while (ind <= aMaxCopies) {
138           if (isAdd) {
139             // Add new shifted item
140             ObjectPtr anObject = copyFeature(anObjToCopy);
141             aTargetList.insert(aTargetIter, anObject);
142           } else {
143             // remove object
144             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
145             ObjectPtr anObject = *aRemoveIt;
146             aTargetList.erase(aRemoveIt);
147             aRefListOfTranslated->remove(anObject);
148             // remove the corresponding feature from the sketch
149             ResultConstructionPtr aRC =
150                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
151             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
152             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
153             if (aFeature)
154               aDoc->removeFeature(aFeature);
155           }
156           ind++;
157         }
158         ind = 0;
159         if (aTargetIter != aTargetList.end())
160           anObjToCopy = *aTargetIter;
161       }
162     }
163
164     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
165       aRefListOfTranslated->append(*aTargetIter);
166   }
167   // add new items
168   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
169   for (; anAddIter != anAddition.end(); anAddIter++) {
170     aRefListOfShapes->append(*anAddIter);
171     aRefListOfTranslated->append(*anAddIter);
172     for (int i = 0; i < aNbCopies; i++) {
173       ObjectPtr anObject = copyFeature(*anAddIter);
174       aRefListOfTranslated->append(anObject);
175     }
176   }
177
178   // send events to update the sub-features by the solver
179   if (isUpdateFlushed)
180     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
181 }
182
183 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
184 {
185   if (!sketch())
186     return thePrevious;
187
188   AISObjectPtr anAIS = thePrevious;
189   if (!anAIS) {
190     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
191   }
192   return anAIS;
193 }
194
195 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
196 {
197   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
198   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
199   if (!aFeature || !aResult)
200     return ObjectPtr();
201
202   FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
203
204   aNewFeature->execute();
205   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
206   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
207
208   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
209   const std::list<ResultPtr>& aResults = aNewFeature->results();
210   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
211   for (; anIt != aResults.end(); anIt++) {
212     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
213     if (!aRC) continue;
214     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
215     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
216         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
217         (aShapeIn->isFace() && aShapeOut->isFace()))
218       return aRC;
219   }
220   return ObjectPtr();
221 }
222
223 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
224 {
225   if (theID == TRANSLATION_LIST_ID()) {
226     AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
227     if (aTranslationObjectRefs->size() == 0) {
228       int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
229       // Clear list of objects
230       AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
231           data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
232       std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
233       std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
234       while (aTargetIter != aTargetList.end()) {
235         aTargetIter++;
236         for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
237           aRefListOfTranslated->remove(*aTargetIter);
238           // remove the corresponding feature from the sketch
239           ResultConstructionPtr aRC =
240             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
241           DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
242           FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
243           if (aFeature)
244             aDoc->removeFeature(aFeature);
245         }
246       }
247       aRefListOfTranslated->clear();
248       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
249         data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
250       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
251         data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
252     }
253   }
254 }