1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_MultiTranslation.cpp
4 // Created: 21 Apr 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_MultiTranslation.h"
8 #include "SketchPlugin_Tools.h"
10 #include <GeomAPI_XY.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Session.h>
21 #include <ModelAPI_Validator.h>
23 #include <SketcherPrs_Factory.h>
25 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
29 void SketchPlugin_MultiTranslation::initAttributes()
31 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
33 data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
34 data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
36 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
37 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
38 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
39 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
40 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
44 void SketchPlugin_MultiTranslation::execute()
47 // it is possible, that this method is called before this feature has back reference to sketch
48 // in this case, the execute is performed after this is done
52 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
53 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
57 // Calculate shift vector
58 AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
59 AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
61 if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
64 DataPtr aData = data();
65 AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
66 AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
70 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
72 // Wait all objects being created, then send update events
73 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
74 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
76 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
78 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
79 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
80 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
81 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
82 int aCurrentNbCopies = aRefListOfShapes->size() ?
83 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
84 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
85 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
86 std::list<ObjectPtr> anAddition;
87 std::vector<bool> isUsed(anInitialList.size(), false);
88 // collect new items and check the items to remove
89 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
90 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
91 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
92 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
93 std::vector<bool>::iterator aUsedIt = isUsed.begin();
94 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
95 if (*anIt == anObject) {
99 if (anIt == anInitialList.end())
100 anAddition.push_back(anObject);
102 // remove unused items
103 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
104 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
105 std::vector<bool>::iterator aUsedIter = isUsed.begin();
106 for (; aUsedIter != isUsed.end(); aUsedIter++) {
108 aRefListOfShapes->remove(*anInitIter);
109 aRefListOfTranslated->remove(*aTargetIter++);
110 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
111 aRefListOfTranslated->remove(*aTargetIter);
112 // remove the corresponding feature from the sketch
113 ResultConstructionPtr aRC =
114 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
115 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
116 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
118 aDoc->removeFeature(aFeature);
121 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
124 if (anInitIter != anInitialList.end())
127 // change number of copies
128 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
129 bool isAdd = aNbCopies > aCurrentNbCopies;
130 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
131 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
134 aTargetList = aRefListOfTranslated->list();
135 aTargetIter = aTargetList.begin();
136 ObjectPtr anObjToCopy = *aTargetIter;
137 while (aTargetIter != aTargetList.end()) {
138 aRefListOfTranslated->remove(*aTargetIter);
141 if (ind > aMinCopies && ind <=aMaxCopies) {
142 while (ind <= aMaxCopies) {
144 // Add new shifted item
145 ObjectPtr anObject = copyFeature(anObjToCopy);
146 aTargetList.insert(aTargetIter, anObject);
149 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
150 ObjectPtr anObject = *aRemoveIt;
151 aTargetList.erase(aRemoveIt);
152 aRefListOfTranslated->remove(anObject);
153 // remove the corresponding feature from the sketch
154 ResultConstructionPtr aRC =
155 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
156 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
157 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
159 aDoc->removeFeature(aFeature);
164 if (aTargetIter != aTargetList.end())
165 anObjToCopy = *aTargetIter;
169 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
170 aRefListOfTranslated->append(*aTargetIter);
173 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
174 for (; anAddIter != anAddition.end(); anAddIter++) {
175 aRefListOfShapes->append(*anAddIter);
176 aRefListOfTranslated->append(*anAddIter);
177 for (int i = 0; i < aNbCopies; i++) {
178 ObjectPtr anObject = copyFeature(*anAddIter);
179 aRefListOfTranslated->append(anObject);
183 // send events to update the sub-features by the solver
185 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
188 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
193 AISObjectPtr anAIS = thePrevious;
195 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
200 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
202 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
203 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
204 if (!aFeature || !aResult)
207 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
209 aNewFeature->execute();
210 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
211 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
213 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
214 const std::list<ResultPtr>& aResults = aNewFeature->results();
215 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
216 for (; anIt != aResults.end(); anIt++) {
217 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
219 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
220 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
221 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
222 (aShapeIn->isFace() && aShapeOut->isFace()))
228 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
230 if (theID == TRANSLATION_LIST_ID()) {
231 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
232 if (aTranslationObjectRefs->size() == 0) {
233 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
236 // Clear list of objects
237 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
238 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
239 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
240 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
241 while (aTargetIter != aTargetList.end()) {
243 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
244 aRefListOfTranslated->remove(*aTargetIter);
245 // remove the corresponding feature from the sketch
246 ResultConstructionPtr aRC =
247 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
248 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
249 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
251 aDoc->removeFeature(aFeature);
254 aRefListOfTranslated->clear();
255 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
256 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
257 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
258 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();