1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_MultiTranslation.h"
22 #include "SketchPlugin_Tools.h"
24 #include <GeomAPI_XY.h>
25 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeRefList.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Tools.h>
38 #include <SketchPlugin_SketchEntity.h>
39 #include <SketcherPrs_Factory.h>
41 SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
45 void SketchPlugin_MultiTranslation::initAttributes()
47 data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
49 data()->addAttribute(START_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
50 data()->addAttribute(END_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
52 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
53 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
54 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
55 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
56 ModelAPI_Session::get()->validators()->
57 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
58 ModelAPI_Session::get()->validators()->
59 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
62 void SketchPlugin_MultiTranslation::execute()
65 // it is possible, that this method is called before this feature has back reference to sketch
66 // in this case, the execute is performed after this is done
70 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
71 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
75 // Calculate shift vector
76 AttributeRefAttrPtr aStartAttr = data()->refattr(START_POINT_ID());
77 AttributeRefAttrPtr anEndAttr = data()->refattr(END_POINT_ID());
79 if (!aStartAttr || !anEndAttr || !aStartAttr->isInitialized() || !anEndAttr->isInitialized())
82 DataPtr aData = data();
83 AttributePoint2DPtr aStart = GeomDataAPI_Point2D::getPoint2D(aData, START_POINT_ID());
84 AttributePoint2DPtr aEnd = GeomDataAPI_Point2D::getPoint2D(aData, END_POINT_ID());
88 std::shared_ptr<GeomAPI_XY>
89 aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
91 // Wait all objects being created, then send update events
92 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
93 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
95 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
97 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
98 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
99 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
100 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
101 int aCurrentNbCopies = aRefListOfShapes->size() ?
102 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
103 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
104 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
105 std::list<ObjectPtr> anAddition;
106 std::vector<bool> isUsed(anInitialList.size(), false);
107 // collect new items and check the items to remove
108 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
109 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
110 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
111 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
112 std::vector<bool>::iterator aUsedIt = isUsed.begin();
113 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
114 if (*anIt == anObject) {
118 if (anIt == anInitialList.end())
119 anAddition.push_back(anObject);
121 // remove unused items
122 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
123 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
124 std::vector<bool>::iterator aUsedIter = isUsed.begin();
125 std::set<FeaturePtr> aFeaturesToBeRemoved;
126 for (; aUsedIter != isUsed.end(); aUsedIter++) {
128 aRefListOfShapes->remove(*anInitIter);
129 aRefListOfTranslated->remove(*aTargetIter++);
130 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
131 i++, aTargetIter++) {
132 aRefListOfTranslated->remove(*aTargetIter);
133 // remove the corresponding feature from the sketch
134 ResultConstructionPtr aRC =
135 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
136 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
137 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
139 aFeaturesToBeRemoved.insert(aFeature);
142 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
145 if (anInitIter != anInitialList.end())
148 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
149 // change number of copies
150 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
151 bool isAdd = aNbCopies > aCurrentNbCopies;
152 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
153 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
156 aTargetList = aRefListOfTranslated->list();
157 aTargetIter = aTargetList.begin();
158 ObjectPtr anObjToCopy = *aTargetIter;
159 while (aTargetIter != aTargetList.end()) {
160 aRefListOfTranslated->remove(*aTargetIter);
163 if (ind > aMinCopies && ind <=aMaxCopies) {
164 while (ind <= aMaxCopies) {
166 // Add new shifted item
167 ObjectPtr anObject = copyFeature(anObjToCopy);
168 aTargetList.insert(aTargetIter, anObject);
171 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
172 ObjectPtr anObject = *aRemoveIt;
173 aTargetList.erase(aRemoveIt);
174 aRefListOfTranslated->remove(anObject);
175 // remove the corresponding feature from the sketch
176 ResultConstructionPtr aRC =
177 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
178 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
179 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
181 aDoc->removeFeature(aFeature);
186 if (aTargetIter != aTargetList.end())
187 anObjToCopy = *aTargetIter;
191 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
192 aRefListOfTranslated->append(*aTargetIter);
195 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
196 for (; anAddIter != anAddition.end(); anAddIter++) {
197 aRefListOfShapes->append(*anAddIter);
198 aRefListOfTranslated->append(*anAddIter);
199 for (int i = 0; i < aNbCopies; i++) {
200 ObjectPtr anObject = copyFeature(*anAddIter);
201 aRefListOfTranslated->append(anObject);
205 // send events to update the sub-features by the solver
207 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
210 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
215 AISObjectPtr anAIS = SketcherPrs_Factory::translateConstraint(this, sketch(),
220 void SketchPlugin_MultiTranslation::erase()
222 static Events_Loop* aLoop = Events_Loop::loop();
223 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
225 // Set copy attribute to false on all copied features.
226 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
227 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
228 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
229 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
231 if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
232 for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
233 ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
234 if(aRefListOfShapes->isInList(anObject)) {
235 // Don't modify attribute of original features, just skip.
239 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
241 FeaturePtr aFeature = aRes->document()->feature(aRes);
243 AttributeBooleanPtr aBooleanAttr =
244 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
245 if(aBooleanAttr.get()) {
246 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
247 aBooleanAttr->setValue(false);
248 // Redisplay object as it is not copy anymore.
249 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
257 SketchPlugin_ConstraintBase::erase();
260 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
262 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
263 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
264 if (!aFeature || !aResult)
267 FeaturePtr aNewFeature =
268 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
270 aNewFeature->execute();
271 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
272 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
274 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
275 const std::list<ResultPtr>& aResults = aNewFeature->results();
276 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
277 for (; anIt != aResults.end(); anIt++) {
278 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
280 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
281 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
282 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
283 (aShapeIn->isFace() && aShapeOut->isFace()))
289 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
291 if (theID == TRANSLATION_LIST_ID()) {
292 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
293 if (aTranslationObjectRefs->size() == 0) {
294 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
298 DocumentPtr aDoc = document();
299 // Clear list of objects
300 AttributeRefListPtr aRefListOfTranslated = reflist(SketchPlugin_Constraint::ENTITY_B());
301 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
302 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
303 while (aTargetIter != aTargetList.end()) {
305 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
306 aRefListOfTranslated->remove(*aTargetIter);
307 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
309 aDoc->removeFeature(aFeature);
312 aRefListOfTranslated->clear();
313 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();