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 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
25 void SketchPlugin_MultiTranslation::initAttributes()
27 data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
28 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
29 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
30 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
32 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 TRANSLATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35 aSelection->setSelectionType("EDGE");
36 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
37 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
40 void SketchPlugin_MultiTranslation::execute()
42 AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
43 int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
44 attribute(NUMBER_OF_COPIES_ID()))->value());
46 // Calculate shift vector
47 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
48 attribute(START_POINT_ID()));
49 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
50 attribute(END_POINT_ID()));
51 if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
53 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
55 // Wait all objects being created, then send update events
56 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
57 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
59 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
61 std::shared_ptr<ModelAPI_Data> aData = data();
62 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
63 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
64 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
65 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
66 int aCurrentNbCopies = aRefListOfShapes->size() ?
67 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
68 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
69 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
70 std::list<ResultPtr> anAddition;
71 std::vector<bool> isUsed(anInitialList.size(), false);
72 // collect new items and check the items to remove
73 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
74 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
75 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
76 std::vector<bool>::iterator aUsedIt = isUsed.begin();
77 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
78 if (*anIt == aSelect->context()) {
82 if (anIt == anInitialList.end())
83 anAddition.push_back(aSelect->context());
85 // remove unused items
86 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
87 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
88 std::vector<bool>::iterator aUsedIter = isUsed.begin();
89 for (; aUsedIter != isUsed.end(); aUsedIter++) {
91 aRefListOfShapes->remove(*anInitIter);
92 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
93 aRefListOfTranslated->remove(*aTargetIter);
94 // remove the corresponding feature from the sketch
95 ResultConstructionPtr aRC =
96 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
97 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
98 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
100 aDoc->removeFeature(aFeature);
103 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
106 if (anInitIter != anInitialList.end())
109 // change number of copies
110 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
111 bool isAdd = aNbCopies > aCurrentNbCopies;
112 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
113 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
116 aTargetList = aRefListOfTranslated->list();
117 aTargetIter = aTargetList.begin();
118 ObjectPtr anObjToCopy = *aTargetIter;
119 while (aTargetIter != aTargetList.end()) {
120 aRefListOfTranslated->remove(*aTargetIter);
123 if (ind > aMinCopies && ind <=aMaxCopies) {
124 while (ind <= aMaxCopies) {
126 // Add new shifted item
127 ObjectPtr anObject = copyFeature(anObjToCopy);
128 aTargetList.insert(aTargetIter, anObject);
131 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter;
132 ObjectPtr anObject = *(--aRemoveIt);
133 aTargetList.erase(aRemoveIt);
134 // remove the corresponding feature from the sketch
135 ResultConstructionPtr aRC =
136 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
137 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
138 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
140 aDoc->removeFeature(aFeature);
145 if (aTargetIter != aTargetList.end())
146 anObjToCopy = *aTargetIter;
150 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
151 aRefListOfTranslated->append(*aTargetIter);
154 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
155 for (; anAddIter != anAddition.end(); anAddIter++) {
156 aRefListOfShapes->append(*anAddIter);
157 aRefListOfTranslated->append(*anAddIter);
158 for (int i = 0; i < aNbCopies; i++) {
159 ObjectPtr anObject = copyFeature(*anAddIter);
160 aRefListOfTranslated->append(anObject);
164 // Recalculate positions of features
165 aTargetList = aRefListOfTranslated->list();
166 aTargetIter = aTargetList.begin();
167 while (aTargetIter != aTargetList.end()) {
168 ObjectPtr anInitialObject = *aTargetIter++;
169 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
170 shiftFeature(anInitialObject, *aTargetIter,
171 aShiftVec->x() * (i + 1), aShiftVec->y() * (i + 1));
174 // send events to update the sub-features by the solver
176 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
179 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
184 AISObjectPtr anAIS = thePrevious;
187 // anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
192 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
194 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
195 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
196 if (!aFeature || !aResult)
199 FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
200 aFeature->data()->copyTo(aNewFeature->data());
201 aNewFeature->execute();
203 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
204 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
206 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
207 const std::list<ResultPtr>& aResults = aNewFeature->results();
208 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
209 for (; anIt != aResults.end(); anIt++) {
210 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
212 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
213 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
214 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
215 (aShapeIn->isFace() && aShapeOut->isFace()))
221 void SketchPlugin_MultiTranslation::shiftFeature(
222 ObjectPtr theInitial, ObjectPtr theTarget, double theDeltaX, double theDeltaY)
224 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
225 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
227 // block feature update
228 aTargetFeature->data()->blockSendAttributeUpdated(true);
230 std::list<AttributePtr> anInitAttrList =
231 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
232 std::list<AttributePtr> aTargetAttrList =
233 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
234 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
235 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
236 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
237 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
238 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
239 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
240 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
241 aPointTo->setValue(aPointFrom->x() + theDeltaX, aPointFrom->y() + theDeltaY);
244 // unblock feature update
245 aTargetFeature->data()->blockSendAttributeUpdated(false);