1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_MultiTranslation.h"
21 #include "SketchPlugin_Tools.h"
23 #include <GeomAPI_XY.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Tools.h>
37 #include <SketchPlugin_SketchEntity.h>
38 #include <SketcherPrs_Factory.h>
40 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
44 void SketchPlugin_MultiTranslation::initAttributes()
46 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
48 data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
49 data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
51 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
52 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
53 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
54 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
55 ModelAPI_Session::get()->validators()->
56 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
57 ModelAPI_Session::get()->validators()->
58 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
61 void SketchPlugin_MultiTranslation::execute()
64 // it is possible, that this method is called before this feature has back reference to sketch
65 // in this case, the execute is performed after this is done
69 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
70 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
74 // Calculate shift vector
75 AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
76 AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
78 if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
81 DataPtr aData = data();
82 AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
83 AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
87 std::shared_ptr<GeomAPI_XY>
88 aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
90 // Wait all objects being created, then send update events
91 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
92 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
94 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
96 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
97 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
98 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
99 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
100 int aCurrentNbCopies = aRefListOfShapes->size() ?
101 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
102 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
103 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
104 std::list<ObjectPtr> anAddition;
105 std::vector<bool> isUsed(anInitialList.size(), false);
106 // collect new items and check the items to remove
107 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
108 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
109 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
110 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
111 std::vector<bool>::iterator aUsedIt = isUsed.begin();
112 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
113 if (*anIt == anObject) {
117 if (anIt == anInitialList.end())
118 anAddition.push_back(anObject);
120 // remove unused items
121 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
122 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
123 std::vector<bool>::iterator aUsedIter = isUsed.begin();
124 std::set<FeaturePtr> aFeaturesToBeRemoved;
125 for (; aUsedIter != isUsed.end(); aUsedIter++) {
127 aRefListOfShapes->remove(*anInitIter);
128 aRefListOfTranslated->remove(*aTargetIter++);
129 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
130 i++, aTargetIter++) {
131 aRefListOfTranslated->remove(*aTargetIter);
132 // remove the corresponding feature from the sketch
133 ResultConstructionPtr aRC =
134 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
135 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
136 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
138 aFeaturesToBeRemoved.insert(aFeature);
141 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
144 if (anInitIter != anInitialList.end())
147 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
148 // change number of copies
149 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
150 bool isAdd = aNbCopies > aCurrentNbCopies;
151 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
152 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
155 aTargetList = aRefListOfTranslated->list();
156 aTargetIter = aTargetList.begin();
157 ObjectPtr anObjToCopy = *aTargetIter;
158 while (aTargetIter != aTargetList.end()) {
159 aRefListOfTranslated->remove(*aTargetIter);
162 if (ind > aMinCopies && ind <=aMaxCopies) {
163 while (ind <= aMaxCopies) {
165 // Add new shifted item
166 ObjectPtr anObject = copyFeature(anObjToCopy);
167 aTargetList.insert(aTargetIter, anObject);
170 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
171 ObjectPtr anObject = *aRemoveIt;
172 aTargetList.erase(aRemoveIt);
173 aRefListOfTranslated->remove(anObject);
174 // remove the corresponding feature from the sketch
175 ResultConstructionPtr aRC =
176 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
177 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
178 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
180 aDoc->removeFeature(aFeature);
185 if (aTargetIter != aTargetList.end())
186 anObjToCopy = *aTargetIter;
190 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
191 aRefListOfTranslated->append(*aTargetIter);
194 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
195 for (; anAddIter != anAddition.end(); anAddIter++) {
196 aRefListOfShapes->append(*anAddIter);
197 aRefListOfTranslated->append(*anAddIter);
198 for (int i = 0; i < aNbCopies; i++) {
199 ObjectPtr anObject = copyFeature(*anAddIter);
200 aRefListOfTranslated->append(anObject);
204 // send events to update the sub-features by the solver
206 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
209 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
214 AISObjectPtr anAIS = SketcherPrs_Factory::translateConstraint(this, sketch(),
219 void SketchPlugin_MultiTranslation::erase()
221 static Events_Loop* aLoop = Events_Loop::loop();
222 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
224 // Set copy attribute to false on all copied features.
225 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
227 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
228 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
230 if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
231 for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
232 ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
233 if(aRefListOfShapes->isInList(anObject)) {
234 // Don't modify attribute of original features, just skip.
238 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
240 FeaturePtr aFeature = aRes->document()->feature(aRes);
242 AttributeBooleanPtr aBooleanAttr =
243 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
244 if(aBooleanAttr.get()) {
245 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
246 aBooleanAttr->setValue(false);
247 // Redisplay object as it is not copy anymore.
248 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
256 SketchPlugin_ConstraintBase::erase();
259 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
261 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
262 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
263 if (!aFeature || !aResult)
266 FeaturePtr aNewFeature =
267 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
269 aNewFeature->execute();
270 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
271 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
273 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
274 const std::list<ResultPtr>& aResults = aNewFeature->results();
275 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
276 for (; anIt != aResults.end(); anIt++) {
277 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
279 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
280 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
281 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
282 (aShapeIn->isFace() && aShapeOut->isFace()))
288 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
290 if (theID == TRANSLATION_LIST_ID()) {
291 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
292 if (aTranslationObjectRefs->size() == 0) {
293 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
297 DocumentPtr aDoc = document();
298 // Clear list of objects
299 AttributeRefListPtr aRefListOfTranslated = reflist(SketchPlugin_Constraint::ENTITY_B());
300 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
301 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
302 while (aTargetIter != aTargetList.end()) {
304 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
305 aRefListOfTranslated->remove(*aTargetIter);
306 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
308 aDoc->removeFeature(aFeature);
311 aRefListOfTranslated->clear();
312 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();