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(START_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
35 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
36 data()->addAttribute(END_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
37 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
38 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
39 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
40 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
45 void SketchPlugin_MultiTranslation::execute()
48 // it is possible, that this method is called before this feature has back reference to sketch
49 // in this case, the execute is performed after this is done
53 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
54 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
58 // Calculate shift vector
59 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
60 attribute(START_POINT_ID()));
61 std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
62 attribute(END_POINT_ID()));
63 if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
66 if (attribute(END_POINT_ID())->isInitialized() && !attribute(END_FULL_POINT_ID())->isInitialized()) {
68 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
69 attribute(END_FULL_POINT_ID()), aNbCopies, true);
74 // make a visible points
75 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), START_POINT_ID(), 0);
77 std::string aSecondPointAttributeID = END_POINT_ID();
78 AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
79 std::string aMethodType = aMethodTypeAttr->value();
80 if (aMethodType != "SingleValue")
81 aSecondPointAttributeID = END_FULL_POINT_ID();
82 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), aSecondPointAttributeID, 1);
84 std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
86 // Wait all objects being created, then send update events
87 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
88 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
90 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
92 std::shared_ptr<ModelAPI_Data> aData = data();
93 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
94 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
95 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
96 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
97 int aCurrentNbCopies = aRefListOfShapes->size() ?
98 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
99 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
100 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
101 std::list<ObjectPtr> anAddition;
102 std::vector<bool> isUsed(anInitialList.size(), false);
103 // collect new items and check the items to remove
104 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
105 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
106 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
107 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
108 std::vector<bool>::iterator aUsedIt = isUsed.begin();
109 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
110 if (*anIt == anObject) {
114 if (anIt == anInitialList.end())
115 anAddition.push_back(anObject);
117 // remove unused items
118 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
119 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
120 std::vector<bool>::iterator aUsedIter = isUsed.begin();
121 for (; aUsedIter != isUsed.end(); aUsedIter++) {
123 aRefListOfShapes->remove(*anInitIter);
124 aRefListOfTranslated->remove(*aTargetIter++);
125 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
126 aRefListOfTranslated->remove(*aTargetIter);
127 // remove the corresponding feature from the sketch
128 ResultConstructionPtr aRC =
129 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
130 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
131 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
133 aDoc->removeFeature(aFeature);
136 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
139 if (anInitIter != anInitialList.end())
142 // change number of copies
143 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
144 bool isAdd = aNbCopies > aCurrentNbCopies;
145 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
146 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
149 aTargetList = aRefListOfTranslated->list();
150 aTargetIter = aTargetList.begin();
151 ObjectPtr anObjToCopy = *aTargetIter;
152 while (aTargetIter != aTargetList.end()) {
153 aRefListOfTranslated->remove(*aTargetIter);
156 if (ind > aMinCopies && ind <=aMaxCopies) {
157 while (ind <= aMaxCopies) {
159 // Add new shifted item
160 ObjectPtr anObject = copyFeature(anObjToCopy);
161 aTargetList.insert(aTargetIter, anObject);
164 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
165 ObjectPtr anObject = *aRemoveIt;
166 aTargetList.erase(aRemoveIt);
167 aRefListOfTranslated->remove(anObject);
168 // remove the corresponding feature from the sketch
169 ResultConstructionPtr aRC =
170 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
171 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
172 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
174 aDoc->removeFeature(aFeature);
179 if (aTargetIter != aTargetList.end())
180 anObjToCopy = *aTargetIter;
184 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
185 aRefListOfTranslated->append(*aTargetIter);
188 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
189 for (; anAddIter != anAddition.end(); anAddIter++) {
190 aRefListOfShapes->append(*anAddIter);
191 aRefListOfTranslated->append(*anAddIter);
192 for (int i = 0; i < aNbCopies; i++) {
193 ObjectPtr anObject = copyFeature(*anAddIter);
194 aRefListOfTranslated->append(anObject);
198 // send events to update the sub-features by the solver
200 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
203 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
208 AISObjectPtr anAIS = thePrevious;
210 anAIS = SketcherPrs_Factory::translateConstraint(this, sketch()->coordinatePlane());
215 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
217 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
218 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
219 if (!aFeature || !aResult)
222 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
224 aNewFeature->execute();
225 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
226 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
228 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
229 const std::list<ResultPtr>& aResults = aNewFeature->results();
230 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
231 for (; anIt != aResults.end(); anIt++) {
232 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
234 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
235 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
236 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
237 (aShapeIn->isFace() && aShapeOut->isFace()))
243 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
245 if (theID == TRANSLATION_LIST_ID()) {
246 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
247 if (aTranslationObjectRefs->size() == 0) {
248 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
251 // Clear list of objects
252 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
253 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
254 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
255 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
256 while (aTargetIter != aTargetList.end()) {
258 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
259 aRefListOfTranslated->remove(*aTargetIter);
260 // remove the corresponding feature from the sketch
261 ResultConstructionPtr aRC =
262 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
263 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
264 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
266 aDoc->removeFeature(aFeature);
269 aRefListOfTranslated->clear();
270 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
271 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
272 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
273 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
276 else if (theID == START_POINT_ID() && !myBlockValue) {
278 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
279 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
280 std::shared_ptr<GeomDataAPI_Point2D> aStartFullPoint =
281 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_FULL_POINT_ID()));
282 aStartFullPoint->setValue(aStartPoint->pnt());
283 myBlockValue = false;
285 else if (theID == START_FULL_POINT_ID() && !myBlockValue) {
287 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
288 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
289 std::shared_ptr<GeomDataAPI_Point2D> aStartFullPoint =
290 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_FULL_POINT_ID()));
291 aStartPoint->setValue(aStartFullPoint->pnt());
292 myBlockValue = false;
294 else if (theID == END_POINT_ID() && !myBlockValue) {
295 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
298 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
299 attribute(END_FULL_POINT_ID()), aNbCopies, true);
300 myBlockValue = false;
301 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
304 else if (theID == END_FULL_POINT_ID() && !myBlockValue) {
305 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
308 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
309 attribute(END_POINT_ID()), aNbCopies, false);
310 myBlockValue = false;
311 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
314 else if (theID == NUMBER_OF_OBJECTS_ID()) {
315 if (attribute(NUMBER_OF_OBJECTS_ID())->isInitialized() &&
316 attribute(END_POINT_ID())->isInitialized() &&
317 attribute(VALUE_TYPE())->isInitialized()) {
318 AttributeStringPtr aMethodTypeAttr = string(VALUE_TYPE());
319 std::string aMethodType = aMethodTypeAttr->value();
320 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
323 if (aMethodType == "SingleValue")
324 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_POINT_ID()),
325 attribute(END_FULL_POINT_ID()), aNbCopies, true);
327 SketchPlugin_Tools::updateMultiAttribute(attribute(START_POINT_ID()), attribute(END_FULL_POINT_ID()),
328 attribute(END_POINT_ID()), aNbCopies, false);
330 myBlockValue = false;
331 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));