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_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
22 #include <SketcherPrs_Factory.h>
24 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
29 void SketchPlugin_MultiTranslation::initAttributes()
31 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
33 data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
34 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
35 data()->addAttribute(END_FULL_POINT_ID(), GeomDataAPI_Point2D::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 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
59 attribute(START_POINT_ID()));
60 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61 attribute(END_POINT_ID()));
62 if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
65 if (attribute(END_POINT_ID())->isInitialized() && !attribute(END_FULL_POINT_ID())->isInitialized()) {
67 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
68 attribute(END_FULL_POINT_ID()), aNbCopies, true);
73 // make a visible points
74 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
76 std::string aSecondPointAttributeID = END_POINT_ID();
77 AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
78 std::string aMethodType = aMethodTypeAttr->value();
79 if (aMethodType != "SingleValue")
80 aSecondPointAttributeID = END_FULL_POINT_ID();
81 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), aSecondPointAttributeID, 1);
83 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
85 // Wait all objects being created, then send update events
86 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
87 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
89 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
91 std::shared_ptr<ModelAPI_Data> aData = data();
92 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
93 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
94 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
95 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
96 int aCurrentNbCopies = aRefListOfShapes->size() ?
97 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
98 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
99 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
100 std::list<ObjectPtr> anAddition;
101 std::vector<bool> isUsed(anInitialList.size(), false);
102 // collect new items and check the items to remove
103 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
104 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
105 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
106 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
107 std::vector<bool>::iterator aUsedIt = isUsed.begin();
108 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
109 if (*anIt == anObject) {
113 if (anIt == anInitialList.end())
114 anAddition.push_back(anObject);
116 // remove unused items
117 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
118 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
119 std::vector<bool>::iterator aUsedIter = isUsed.begin();
120 for (; aUsedIter != isUsed.end(); aUsedIter++) {
122 aRefListOfShapes->remove(*anInitIter);
123 aRefListOfTranslated->remove(*aTargetIter++);
124 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
125 aRefListOfTranslated->remove(*aTargetIter);
126 // remove the corresponding feature from the sketch
127 ResultConstructionPtr aRC =
128 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
129 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
130 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
132 aDoc->removeFeature(aFeature);
135 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
138 if (anInitIter != anInitialList.end())
141 // change number of copies
142 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
143 bool isAdd = aNbCopies > aCurrentNbCopies;
144 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
145 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
148 aTargetList = aRefListOfTranslated->list();
149 aTargetIter = aTargetList.begin();
150 ObjectPtr anObjToCopy = *aTargetIter;
151 while (aTargetIter != aTargetList.end()) {
152 aRefListOfTranslated->remove(*aTargetIter);
155 if (ind > aMinCopies && ind <=aMaxCopies) {
156 while (ind <= aMaxCopies) {
158 // Add new shifted item
159 ObjectPtr anObject = copyFeature(anObjToCopy);
160 aTargetList.insert(aTargetIter, anObject);
163 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
164 ObjectPtr anObject = *aRemoveIt;
165 aTargetList.erase(aRemoveIt);
166 aRefListOfTranslated->remove(anObject);
167 // remove the corresponding feature from the sketch
168 ResultConstructionPtr aRC =
169 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
170 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
171 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
173 aDoc->removeFeature(aFeature);
178 if (aTargetIter != aTargetList.end())
179 anObjToCopy = *aTargetIter;
183 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
184 aRefListOfTranslated->append(*aTargetIter);
187 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
188 for (; anAddIter != anAddition.end(); anAddIter++) {
189 aRefListOfShapes->append(*anAddIter);
190 aRefListOfTranslated->append(*anAddIter);
191 for (int i = 0; i < aNbCopies; i++) {
192 ObjectPtr anObject = copyFeature(*anAddIter);
193 aRefListOfTranslated->append(anObject);
197 // send events to update the sub-features by the solver
199 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
202 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
207 AISObjectPtr anAIS = thePrevious;
209 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
214 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
216 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
217 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
218 if (!aFeature || !aResult)
221 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
223 aNewFeature->execute();
224 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
225 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
227 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
228 const std::list<ResultPtr>& aResults = aNewFeature->results();
229 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
230 for (; anIt != aResults.end(); anIt++) {
231 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
233 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
234 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
235 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
236 (aShapeIn->isFace() && aShapeOut->isFace()))
242 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
244 if (theID == TRANSLATION_LIST_ID()) {
245 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
246 if (aTranslationObjectRefs->size() == 0) {
247 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
250 // Clear list of objects
251 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
252 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
253 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
254 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
255 while (aTargetIter != aTargetList.end()) {
257 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
258 aRefListOfTranslated->remove(*aTargetIter);
259 // remove the corresponding feature from the sketch
260 ResultConstructionPtr aRC =
261 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
262 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
263 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
265 aDoc->removeFeature(aFeature);
268 aRefListOfTranslated->clear();
269 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
270 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
271 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
272 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
275 else if (theID == END_POINT_ID() && !myBlockValue) {
276 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
279 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
280 attribute(END_FULL_POINT_ID()), aNbCopies, true);
281 myBlockValue = false;
282 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
285 else if (theID == END_FULL_POINT_ID() && !myBlockValue) {
286 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
289 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
290 attribute(END_POINT_ID()), aNbCopies, false);
291 myBlockValue = false;
292 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
295 else if (theID == NUMBER_OF_OBJECTS_ID()) {
296 if (attribute(NUMBER_OF_OBJECTS_ID())->isInitialized() &&
297 attribute(END_POINT_ID())->isInitialized() &&
298 attribute(VALUE_TYPE())->isInitialized()) {
299 AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
300 std::string aMethodType = aMethodTypeAttr->value();
301 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
304 if (aMethodType == "SingleValue")
305 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
306 attribute(END_FULL_POINT_ID()), aNbCopies, true);
308 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
309 attribute(END_POINT_ID()), aNbCopies, false);
311 myBlockValue = false;
312 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));