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