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"
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>
21 #include <SketcherPrs_Factory.h>
23 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
27 void SketchPlugin_MultiTranslation::initAttributes()
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_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());
42 void SketchPlugin_MultiTranslation::execute()
45 // it is possible, that this method is called before this feature has back reference to sketch
46 // in this case, the execute is performed after this is done
50 AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
51 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
53 // Calculate shift vector
54 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
55 attribute(START_POINT_ID()));
56 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57 attribute(END_POINT_ID()));
58 if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
61 // make a visible points
62 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
63 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), END_POINT_ID(), 1);
65 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
67 // Wait all objects being created, then send update events
68 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
69 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
71 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
73 std::shared_ptr<ModelAPI_Data> aData = data();
74 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
75 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
77 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
78 int aCurrentNbCopies = aRefListOfShapes->size() ?
79 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
80 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
81 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
82 std::list<ResultPtr> anAddition;
83 std::vector<bool> isUsed(anInitialList.size(), false);
84 // collect new items and check the items to remove
85 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
86 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
87 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
88 std::vector<bool>::iterator aUsedIt = isUsed.begin();
89 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
90 if (*anIt == aSelect->context()) {
94 if (anIt == anInitialList.end())
95 anAddition.push_back(aSelect->context());
97 // remove unused items
98 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
99 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
100 std::vector<bool>::iterator aUsedIter = isUsed.begin();
101 for (; aUsedIter != isUsed.end(); aUsedIter++) {
103 aRefListOfShapes->remove(*anInitIter);
104 aRefListOfTranslated->remove(*aTargetIter++);
105 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
106 aRefListOfTranslated->remove(*aTargetIter);
107 // remove the corresponding feature from the sketch
108 ResultConstructionPtr aRC =
109 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
110 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
111 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
113 aDoc->removeFeature(aFeature);
116 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
119 if (anInitIter != anInitialList.end())
122 // change number of copies
123 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
124 bool isAdd = aNbCopies > aCurrentNbCopies;
125 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
126 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
129 aTargetList = aRefListOfTranslated->list();
130 aTargetIter = aTargetList.begin();
131 ObjectPtr anObjToCopy = *aTargetIter;
132 while (aTargetIter != aTargetList.end()) {
133 aRefListOfTranslated->remove(*aTargetIter);
136 if (ind > aMinCopies && ind <=aMaxCopies) {
137 while (ind <= aMaxCopies) {
139 // Add new shifted item
140 ObjectPtr anObject = copyFeature(anObjToCopy);
141 aTargetList.insert(aTargetIter, anObject);
144 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
145 ObjectPtr anObject = *aRemoveIt;
146 aTargetList.erase(aRemoveIt);
147 aRefListOfTranslated->remove(anObject);
148 // remove the corresponding feature from the sketch
149 ResultConstructionPtr aRC =
150 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
151 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
152 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
154 aDoc->removeFeature(aFeature);
159 if (aTargetIter != aTargetList.end())
160 anObjToCopy = *aTargetIter;
164 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
165 aRefListOfTranslated->append(*aTargetIter);
168 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
169 for (; anAddIter != anAddition.end(); anAddIter++) {
170 aRefListOfShapes->append(*anAddIter);
171 aRefListOfTranslated->append(*anAddIter);
172 for (int i = 0; i < aNbCopies; i++) {
173 ObjectPtr anObject = copyFeature(*anAddIter);
174 aRefListOfTranslated->append(anObject);
178 // send events to update the sub-features by the solver
180 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
183 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
188 AISObjectPtr anAIS = thePrevious;
190 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
195 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
197 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
198 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
199 if (!aFeature || !aResult)
202 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
204 aNewFeature->execute();
205 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
206 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
208 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
209 const std::list<ResultPtr>& aResults = aNewFeature->results();
210 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
211 for (; anIt != aResults.end(); anIt++) {
212 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
214 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
215 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
216 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
217 (aShapeIn->isFace() && aShapeOut->isFace()))
223 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
225 if (theID == TRANSLATION_LIST_ID()) {
226 AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
227 if (aTranslationObjectRefs->size() == 0) {
228 // Wait all objects being created, then send update events
229 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
230 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
232 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
234 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
235 // Clear list of objects
236 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
237 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
238 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
239 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
240 while (aTargetIter != aTargetList.end()) {
242 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
243 aRefListOfTranslated->remove(*aTargetIter);
244 // remove the corresponding feature from the sketch
245 ResultConstructionPtr aRC =
246 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
247 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
248 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
250 aDoc->removeFeature(aFeature);
253 integer(NUMBER_OF_COPIES_ID())->setValue(0);
255 // send events to update the sub-features by the solver
257 Events_Loop::loop()->setFlushed(anUpdateEvent, true);