Salome HOME
GUI for extrusion/revolution features.
[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   AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
45   int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
46
47   // Calculate shift vector
48   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
49       attribute(START_POINT_ID()));
50   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
51       attribute(END_POINT_ID()));
52   if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
53     return;
54   std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
55
56   // Wait all objects being created, then send update events
57   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
58   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
59   if (isUpdateFlushed)
60     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
61
62   std::shared_ptr<ModelAPI_Data> aData = data();
63   AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
64       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
65   AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
66       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
67   int aCurrentNbCopies = aRefListOfShapes->size() ?
68       aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
69   std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
70   std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
71   std::list<ResultPtr> anAddition;
72   std::vector<bool> isUsed(anInitialList.size(), false);
73   // collect new items and check the items to remove
74   for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
75     std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
76     std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
77     std::vector<bool>::iterator aUsedIt = isUsed.begin();
78     for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
79       if (*anIt == aSelect->context()) {
80         *aUsedIt = true;
81         break;
82       }
83     if (anIt == anInitialList.end())
84       anAddition.push_back(aSelect->context());
85   }
86   // remove unused items
87   std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
88   std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
89   std::vector<bool>::iterator aUsedIter = isUsed.begin();
90   for (; aUsedIter != isUsed.end(); aUsedIter++) {
91     if (!(*aUsedIter)) {
92       aRefListOfShapes->remove(*anInitIter);
93       for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
94         aRefListOfTranslated->remove(*aTargetIter);
95         // remove the corresponding feature from the sketch
96         ResultConstructionPtr aRC =
97             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
98         DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
99         FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
100         if (aFeature)
101           aDoc->removeFeature(aFeature);
102       }
103     } else {
104       for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
105         aTargetIter++;
106     }
107     if (anInitIter != anInitialList.end())
108       anInitIter++;
109   }
110   // change number of copies
111   if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
112     bool isAdd = aNbCopies > aCurrentNbCopies;
113     int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
114     int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
115     int ind = 0;
116
117     aTargetList = aRefListOfTranslated->list();
118     aTargetIter = aTargetList.begin();
119     ObjectPtr anObjToCopy = *aTargetIter;
120     while (aTargetIter != aTargetList.end()) {
121       aRefListOfTranslated->remove(*aTargetIter);
122       aTargetIter++;
123       ind++;
124       if (ind > aMinCopies && ind <=aMaxCopies) {
125         while (ind <= aMaxCopies) {
126           if (isAdd) {
127             // Add new shifted item
128             ObjectPtr anObject = copyFeature(anObjToCopy);
129             aTargetList.insert(aTargetIter, anObject);
130           } else {
131             // remove object
132             std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
133             ObjectPtr anObject = *aRemoveIt;
134             aTargetList.erase(aRemoveIt);
135             aRefListOfTranslated->remove(anObject);
136             // remove the corresponding feature from the sketch
137             ResultConstructionPtr aRC =
138                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
139             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
140             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
141             if (aFeature)
142               aDoc->removeFeature(aFeature);
143           }
144           ind++;
145         }
146         ind = 0;
147         if (aTargetIter != aTargetList.end())
148           anObjToCopy = *aTargetIter;
149       }
150     }
151
152     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
153       aRefListOfTranslated->append(*aTargetIter);
154   }
155   // add new items
156   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
157   for (; anAddIter != anAddition.end(); anAddIter++) {
158     aRefListOfShapes->append(*anAddIter);
159     aRefListOfTranslated->append(*anAddIter);
160     for (int i = 0; i < aNbCopies; i++) {
161       ObjectPtr anObject = copyFeature(*anAddIter);
162       aRefListOfTranslated->append(anObject);
163     }
164   }
165
166   // send events to update the sub-features by the solver
167   if (isUpdateFlushed)
168     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
169 }
170
171 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
172 {
173   if (!sketch())
174     return thePrevious;
175
176   AISObjectPtr anAIS = thePrevious;
177   if (!anAIS) {
178     anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
179   }
180   return anAIS;
181 }
182
183 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
184 {
185   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
186   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
187   if (!aFeature || !aResult)
188     return ObjectPtr();
189
190   FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
191   aFeature->data()->copyTo(aNewFeature->data());
192   aNewFeature->execute();
193
194   static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
195   ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
196
197   std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
198   const std::list<ResultPtr>& aResults = aNewFeature->results();
199   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
200   for (; anIt != aResults.end(); anIt++) {
201     ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
202     if (!aRC) continue;
203     std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
204     if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
205         (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
206         (aShapeIn->isFace() && aShapeOut->isFace()))
207       return aRC;
208   }
209   return ObjectPtr();
210 }