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