]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MultiTranslation.cpp
Salome HOME
Update Multi-Rotation tool to be used by solver
[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             aRefListOfTranslated->remove(anObject);
135             // remove the corresponding feature from the sketch
136             ResultConstructionPtr aRC =
137                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
138             DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
139             FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
140             if (aFeature)
141               aDoc->removeFeature(aFeature);
142           }
143           ind++;
144         }
145         ind = 0;
146         if (aTargetIter != aTargetList.end())
147           anObjToCopy = *aTargetIter;
148       }
149     }
150
151     for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
152       aRefListOfTranslated->append(*aTargetIter);
153   }
154   // add new items
155   std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
156   for (; anAddIter != anAddition.end(); anAddIter++) {
157     aRefListOfShapes->append(*anAddIter);
158     aRefListOfTranslated->append(*anAddIter);
159     for (int i = 0; i < aNbCopies; i++) {
160       ObjectPtr anObject = copyFeature(*anAddIter);
161       aRefListOfTranslated->append(anObject);
162     }
163   }
164
165   // send events to update the sub-features by the solver
166   if (isUpdateFlushed)
167     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
168 }
169
170 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
171 {
172   if (!sketch())
173     return thePrevious;
174
175   AISObjectPtr anAIS = thePrevious;
176   if (!anAIS) {
177 // TODO:
178 //    anAIS = SketcherPrs_Factory::mirrorConstraint(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 }