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