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 = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane(),
199 void SketchPlugin_MultiTranslation::erase()
201 static Events_Loop* aLoop = Events_Loop::loop();
202 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
204 // Set copy attribute to false on all copied features.
205 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
206 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
207 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
208 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
210 if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
211 for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
212 ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
213 if(aRefListOfShapes->isInList(anObject)) {
214 // Don't modify attribute of original features, just skip.
218 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
220 FeaturePtr aFeature = aRes->document()->feature(aRes);
222 AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
223 if(aBooleanAttr.get()) {
224 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
225 aBooleanAttr->setValue(false);
226 // Redisplay object as it is not copy anymore.
227 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
235 SketchPlugin_ConstraintBase::erase();
238 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
240 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
241 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
242 if (!aFeature || !aResult)
245 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
247 aNewFeature->execute();
248 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
249 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
251 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
252 const std::list<ResultPtr>& aResults = aNewFeature->results();
253 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
254 for (; anIt != aResults.end(); anIt++) {
255 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
257 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
258 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
259 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
260 (aShapeIn->isFace() && aShapeOut->isFace()))
266 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
268 if (theID == TRANSLATION_LIST_ID()) {
269 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
270 if (aTranslationObjectRefs->size() == 0) {
271 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
274 // Clear list of objects
275 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
276 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
277 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
278 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
279 while (aTargetIter != aTargetList.end()) {
281 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
282 aRefListOfTranslated->remove(*aTargetIter);
283 // remove the corresponding feature from the sketch
284 ResultConstructionPtr aRC =
285 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
286 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
287 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
289 aDoc->removeFeature(aFeature);
292 aRefListOfTranslated->clear();
293 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
294 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
295 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
296 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();