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_Events.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
20 #include <SketcherPrs_Factory.h>
22 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
26 void SketchPlugin_MultiTranslation::initAttributes()
28 data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
29 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
30 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
32 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
33 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
34 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
35 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
38 void SketchPlugin_MultiTranslation::execute()
41 // it is possible, that this method is called before this feature has back reference to sketch
42 // in this case, the execute is performed after this is done
46 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
47 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
49 // Calculate shift vector
50 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
51 attribute(START_POINT_ID()));
52 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53 attribute(END_POINT_ID()));
54 if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
57 // make a visible points
58 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
59 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), END_POINT_ID(), 1);
61 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
63 // Wait all objects being created, then send update events
64 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
65 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
67 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
69 std::shared_ptr<ModelAPI_Data> aData = data();
70 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
71 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
72 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
73 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
74 int aCurrentNbCopies = aRefListOfShapes->size() ?
75 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
76 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
77 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
78 std::list<ObjectPtr> anAddition;
79 std::vector<bool> isUsed(anInitialList.size(), false);
80 // collect new items and check the items to remove
81 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
82 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
83 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
84 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
85 std::vector<bool>::iterator aUsedIt = isUsed.begin();
86 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
87 if (*anIt == anObject) {
91 if (anIt == anInitialList.end())
92 anAddition.push_back(anObject);
94 // remove unused items
95 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
96 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
97 std::vector<bool>::iterator aUsedIter = isUsed.begin();
98 for (; aUsedIter != isUsed.end(); aUsedIter++) {
100 aRefListOfShapes->remove(*anInitIter);
101 aRefListOfTranslated->remove(*aTargetIter++);
102 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
103 aRefListOfTranslated->remove(*aTargetIter);
104 // remove the corresponding feature from the sketch
105 ResultConstructionPtr aRC =
106 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
107 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
108 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
110 aDoc->removeFeature(aFeature);
113 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
116 if (anInitIter != anInitialList.end())
119 // change number of copies
120 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
121 bool isAdd = aNbCopies > aCurrentNbCopies;
122 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
123 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
126 aTargetList = aRefListOfTranslated->list();
127 aTargetIter = aTargetList.begin();
128 ObjectPtr anObjToCopy = *aTargetIter;
129 while (aTargetIter != aTargetList.end()) {
130 aRefListOfTranslated->remove(*aTargetIter);
133 if (ind > aMinCopies && ind <=aMaxCopies) {
134 while (ind <= aMaxCopies) {
136 // Add new shifted item
137 ObjectPtr anObject = copyFeature(anObjToCopy);
138 aTargetList.insert(aTargetIter, anObject);
141 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
142 ObjectPtr anObject = *aRemoveIt;
143 aTargetList.erase(aRemoveIt);
144 aRefListOfTranslated->remove(anObject);
145 // remove the corresponding feature from the sketch
146 ResultConstructionPtr aRC =
147 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
148 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
149 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
151 aDoc->removeFeature(aFeature);
156 if (aTargetIter != aTargetList.end())
157 anObjToCopy = *aTargetIter;
161 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
162 aRefListOfTranslated->append(*aTargetIter);
165 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
166 for (; anAddIter != anAddition.end(); anAddIter++) {
167 aRefListOfShapes->append(*anAddIter);
168 aRefListOfTranslated->append(*anAddIter);
169 for (int i = 0; i < aNbCopies; i++) {
170 ObjectPtr anObject = copyFeature(*anAddIter);
171 aRefListOfTranslated->append(anObject);
175 // send events to update the sub-features by the solver
177 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
180 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
185 AISObjectPtr anAIS = thePrevious;
187 anAIS = SketcherPrs_Factory::translateConstraint(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 = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
201 aNewFeature->execute();
202 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
203 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
205 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
206 const std::list<ResultPtr>& aResults = aNewFeature->results();
207 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
208 for (; anIt != aResults.end(); anIt++) {
209 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
211 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
212 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
213 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
214 (aShapeIn->isFace() && aShapeOut->isFace()))
220 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
222 if (theID == TRANSLATION_LIST_ID()) {
223 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
224 if (aTranslationObjectRefs->size() == 0) {
225 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
226 // Clear list of objects
227 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
228 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
229 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
230 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
231 while (aTargetIter != aTargetList.end()) {
233 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
234 aRefListOfTranslated->remove(*aTargetIter);
235 // remove the corresponding feature from the sketch
236 ResultConstructionPtr aRC =
237 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
238 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
239 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
241 aDoc->removeFeature(aFeature);
244 aRefListOfTranslated->clear();
245 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
246 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
247 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
248 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();