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 std::shared_ptr<GeomDataAPI_Point2D> aStart = GeomDataAPI_Point2D::getPoint2D(aData,
67 std::shared_ptr<GeomDataAPI_Point2D> aEnd = GeomDataAPI_Point2D::getPoint2D(aData,
72 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
74 // Wait all objects being created, then send update events
75 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
76 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
78 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
80 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
81 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
82 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
83 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
84 int aCurrentNbCopies = aRefListOfShapes->size() ?
85 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
86 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
87 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
88 std::list<ObjectPtr> anAddition;
89 std::vector<bool> isUsed(anInitialList.size(), false);
90 // collect new items and check the items to remove
91 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
92 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
93 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
94 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
95 std::vector<bool>::iterator aUsedIt = isUsed.begin();
96 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
97 if (*anIt == anObject) {
101 if (anIt == anInitialList.end())
102 anAddition.push_back(anObject);
104 // remove unused items
105 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
106 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
107 std::vector<bool>::iterator aUsedIter = isUsed.begin();
108 for (; aUsedIter != isUsed.end(); aUsedIter++) {
110 aRefListOfShapes->remove(*anInitIter);
111 aRefListOfTranslated->remove(*aTargetIter++);
112 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
113 aRefListOfTranslated->remove(*aTargetIter);
114 // remove the corresponding feature from the sketch
115 ResultConstructionPtr aRC =
116 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
117 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
118 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
120 aDoc->removeFeature(aFeature);
123 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
126 if (anInitIter != anInitialList.end())
129 // change number of copies
130 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
131 bool isAdd = aNbCopies > aCurrentNbCopies;
132 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
133 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
136 aTargetList = aRefListOfTranslated->list();
137 aTargetIter = aTargetList.begin();
138 ObjectPtr anObjToCopy = *aTargetIter;
139 while (aTargetIter != aTargetList.end()) {
140 aRefListOfTranslated->remove(*aTargetIter);
143 if (ind > aMinCopies && ind <=aMaxCopies) {
144 while (ind <= aMaxCopies) {
146 // Add new shifted item
147 ObjectPtr anObject = copyFeature(anObjToCopy);
148 aTargetList.insert(aTargetIter, anObject);
151 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
152 ObjectPtr anObject = *aRemoveIt;
153 aTargetList.erase(aRemoveIt);
154 aRefListOfTranslated->remove(anObject);
155 // remove the corresponding feature from the sketch
156 ResultConstructionPtr aRC =
157 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
158 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
159 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
161 aDoc->removeFeature(aFeature);
166 if (aTargetIter != aTargetList.end())
167 anObjToCopy = *aTargetIter;
171 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
172 aRefListOfTranslated->append(*aTargetIter);
175 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
176 for (; anAddIter != anAddition.end(); anAddIter++) {
177 aRefListOfShapes->append(*anAddIter);
178 aRefListOfTranslated->append(*anAddIter);
179 for (int i = 0; i < aNbCopies; i++) {
180 ObjectPtr anObject = copyFeature(*anAddIter);
181 aRefListOfTranslated->append(anObject);
185 // send events to update the sub-features by the solver
187 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
190 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
195 AISObjectPtr anAIS = thePrevious;
197 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
202 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
204 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
205 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
206 if (!aFeature || !aResult)
209 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
211 aNewFeature->execute();
212 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
213 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
215 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
216 const std::list<ResultPtr>& aResults = aNewFeature->results();
217 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
218 for (; anIt != aResults.end(); anIt++) {
219 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
221 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
222 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
223 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
224 (aShapeIn->isFace() && aShapeOut->isFace()))
230 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
232 if (theID == TRANSLATION_LIST_ID()) {
233 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
234 if (aTranslationObjectRefs->size() == 0) {
235 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
238 // Clear list of objects
239 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
240 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
241 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
242 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
243 while (aTargetIter != aTargetList.end()) {
245 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
246 aRefListOfTranslated->remove(*aTargetIter);
247 // remove the corresponding feature from the sketch
248 ResultConstructionPtr aRC =
249 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
250 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
251 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
253 aDoc->removeFeature(aFeature);
256 aRefListOfTranslated->clear();
257 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
258 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
259 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
260 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();