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 <SketchPlugin_SketchEntity.h>
24 #include <SketcherPrs_Factory.h>
26 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
30 void SketchPlugin_MultiTranslation::initAttributes()
32 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
34 data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
35 data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
37 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
38 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
39 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
40 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
45 void SketchPlugin_MultiTranslation::execute()
48 // it is possible, that this method is called before this feature has back reference to sketch
49 // in this case, the execute is performed after this is done
53 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
54 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
58 // Calculate shift vector
59 AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
60 AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
62 if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
65 DataPtr aData = data();
66 AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
67 AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
71 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
73 // Wait all objects being created, then send update events
74 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
75 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
77 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
79 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
80 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
81 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
82 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
83 int aCurrentNbCopies = aRefListOfShapes->size() ?
84 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
85 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
86 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
87 std::list<ObjectPtr> anAddition;
88 std::vector<bool> isUsed(anInitialList.size(), false);
89 // collect new items and check the items to remove
90 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
91 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
92 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
93 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
94 std::vector<bool>::iterator aUsedIt = isUsed.begin();
95 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
96 if (*anIt == anObject) {
100 if (anIt == anInitialList.end())
101 anAddition.push_back(anObject);
103 // remove unused items
104 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
105 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
106 std::vector<bool>::iterator aUsedIter = isUsed.begin();
107 for (; aUsedIter != isUsed.end(); aUsedIter++) {
109 aRefListOfShapes->remove(*anInitIter);
110 aRefListOfTranslated->remove(*aTargetIter++);
111 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
112 aRefListOfTranslated->remove(*aTargetIter);
113 // remove the corresponding feature from the sketch
114 ResultConstructionPtr aRC =
115 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
116 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
117 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
119 aDoc->removeFeature(aFeature);
122 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
125 if (anInitIter != anInitialList.end())
128 // change number of copies
129 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
130 bool isAdd = aNbCopies > aCurrentNbCopies;
131 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
132 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
135 aTargetList = aRefListOfTranslated->list();
136 aTargetIter = aTargetList.begin();
137 ObjectPtr anObjToCopy = *aTargetIter;
138 while (aTargetIter != aTargetList.end()) {
139 aRefListOfTranslated->remove(*aTargetIter);
142 if (ind > aMinCopies && ind <=aMaxCopies) {
143 while (ind <= aMaxCopies) {
145 // Add new shifted item
146 ObjectPtr anObject = copyFeature(anObjToCopy);
147 aTargetList.insert(aTargetIter, anObject);
150 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
151 ObjectPtr anObject = *aRemoveIt;
152 aTargetList.erase(aRemoveIt);
153 aRefListOfTranslated->remove(anObject);
154 // remove the corresponding feature from the sketch
155 ResultConstructionPtr aRC =
156 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
157 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
158 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
160 aDoc->removeFeature(aFeature);
165 if (aTargetIter != aTargetList.end())
166 anObjToCopy = *aTargetIter;
170 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
171 aRefListOfTranslated->append(*aTargetIter);
174 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
175 for (; anAddIter != anAddition.end(); anAddIter++) {
176 aRefListOfShapes->append(*anAddIter);
177 aRefListOfTranslated->append(*anAddIter);
178 for (int i = 0; i < aNbCopies; i++) {
179 ObjectPtr anObject = copyFeature(*anAddIter);
180 aRefListOfTranslated->append(anObject);
184 // send events to update the sub-features by the solver
186 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
189 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
194 AISObjectPtr anAIS = thePrevious;
196 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
201 void SketchPlugin_MultiTranslation::erase()
203 // Set copy attribute to false on all copied features.
204 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
205 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
207 if(aRefListOfTranslated.get()) {
208 static Events_Loop* aLoop = Events_Loop::loop();
209 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
211 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
212 for(std::list<ObjectPtr>::const_iterator aTargetIt = aTargetList.cbegin(); aTargetIt != aTargetList.cend(); aTargetIt++) {
213 if((*aTargetIt).get()) {
214 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aTargetIt);
216 FeaturePtr aFeature = aRes->document()->feature(aRes);
218 AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
219 if(aBooleanAttr.get()) {
220 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
221 aBooleanAttr->setValue(false);
222 // Redisplay object as it is not copy anymore.
223 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
231 SketchPlugin_ConstraintBase::erase();
234 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
236 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
237 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
238 if (!aFeature || !aResult)
241 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
243 aNewFeature->execute();
244 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
245 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
247 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
248 const std::list<ResultPtr>& aResults = aNewFeature->results();
249 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
250 for (; anIt != aResults.end(); anIt++) {
251 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
253 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
254 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
255 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
256 (aShapeIn->isFace() && aShapeOut->isFace()))
262 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
264 if (theID == TRANSLATION_LIST_ID()) {
265 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
266 if (aTranslationObjectRefs->size() == 0) {
267 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
270 // Clear list of objects
271 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
272 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
273 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
274 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
275 while (aTargetIter != aTargetList.end()) {
277 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
278 aRefListOfTranslated->remove(*aTargetIter);
279 // remove the corresponding feature from the sketch
280 ResultConstructionPtr aRC =
281 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
282 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
283 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
285 aDoc->removeFeature(aFeature);
288 aRefListOfTranslated->clear();
289 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
290 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
291 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
292 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();