1 // Copyright (C) 2014-2023 CEA, EDF
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 // Save the current feature of the document, because new features may appear while executing.
97 // In this case, they will become current. But if the number of copies is updated from outside
98 // of sketch (e.g. by parameter change), the history line should not hold in sketch.
101 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
102 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
103 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
104 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
105 int aCurrentNbCopies = aRefListOfShapes->size() ?
106 aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
107 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
108 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
109 std::list<ObjectPtr> anAddition;
110 std::vector<bool> isUsed(anInitialList.size(), false);
111 // collect new items and check the items to remove
112 for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
113 //std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
114 ObjectPtr anObject = aTranslationObjectRefs->object(anInd);
115 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
116 std::vector<bool>::iterator aUsedIt = isUsed.begin();
117 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
118 if (*anIt == anObject) {
122 if (anIt == anInitialList.end())
123 anAddition.push_back(anObject);
125 // remove unused items
126 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
127 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
128 std::vector<bool>::iterator aUsedIter = isUsed.begin();
129 std::set<FeaturePtr> aFeaturesToBeRemoved;
130 for (; aUsedIter != isUsed.end(); aUsedIter++) {
132 aRefListOfShapes->remove(*anInitIter);
133 aRefListOfTranslated->remove(*aTargetIter++);
134 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
135 i++, aTargetIter++) {
136 aRefListOfTranslated->remove(*aTargetIter);
137 // remove the corresponding feature from the sketch
138 ResultConstructionPtr aRC =
139 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
140 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
141 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
143 aFeaturesToBeRemoved.insert(aFeature);
146 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
149 if (anInitIter != anInitialList.end())
152 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
153 // change number of copies
154 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
155 bool isAdd = aNbCopies > aCurrentNbCopies;
156 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
157 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
160 aTargetList = aRefListOfTranslated->list();
161 aTargetIter = aTargetList.begin();
162 ObjectPtr anObjToCopy = *aTargetIter;
163 while (aTargetIter != aTargetList.end()) {
164 aRefListOfTranslated->remove(*aTargetIter);
167 if (ind > aMinCopies && ind <=aMaxCopies) {
168 while (ind <= aMaxCopies) {
170 // Add new shifted item
171 ObjectPtr anObject = copyFeature(anObjToCopy);
172 aTargetList.insert(aTargetIter, anObject);
175 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
176 ObjectPtr anObject = *aRemoveIt;
177 aTargetList.erase(aRemoveIt);
178 aRefListOfTranslated->remove(anObject);
179 // remove the corresponding feature from the sketch
180 ResultConstructionPtr aRC =
181 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
182 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
183 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
185 aDoc->removeFeature(aFeature);
190 if (aTargetIter != aTargetList.end())
191 anObjToCopy = *aTargetIter;
195 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
196 aRefListOfTranslated->append(*aTargetIter);
199 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
200 for (; anAddIter != anAddition.end(); anAddIter++) {
201 aRefListOfShapes->append(*anAddIter);
202 aRefListOfTranslated->append(*anAddIter);
203 for (int i = 0; i < aNbCopies; i++) {
204 ObjectPtr anObject = copyFeature(*anAddIter);
205 aRefListOfTranslated->append(anObject);
209 restoreCurrentFeature();
211 // send events to update the sub-features by the solver
213 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
216 AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
221 AISObjectPtr anAIS = SketcherPrs_Factory::translateConstraint(this, sketch(),
226 void SketchPlugin_MultiTranslation::erase()
228 static Events_Loop* aLoop = Events_Loop::loop();
229 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
231 // Set copy attribute to false on all copied features.
232 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
233 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
234 AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
235 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
237 if(aRefListOfShapes.get() && aRefListOfTranslated.get()) {
238 for(int anIndex = 0; anIndex < aRefListOfTranslated->size(); anIndex++) {
239 ObjectPtr anObject = aRefListOfTranslated->object(anIndex);
240 if(aRefListOfShapes->isInList(anObject)) {
241 // Don't modify attribute of original features, just skip.
245 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
247 FeaturePtr aFeature = aRes->document()->feature(aRes);
249 AttributeBooleanPtr aBooleanAttr =
250 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
251 if(aBooleanAttr.get()) {
252 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
253 aBooleanAttr->setValue(false);
254 // Redisplay object as it is not copy anymore.
255 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
263 SketchPlugin_ConstraintBase::erase();
266 ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
268 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
269 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
270 if (!aFeature || !aResult)
273 FeaturePtr aNewFeature =
274 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
276 aNewFeature->execute();
277 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
278 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
280 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
281 const std::list<ResultPtr>& aResults = aNewFeature->results();
282 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
283 for (; anIt != aResults.end(); anIt++) {
284 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
286 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
287 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
288 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
289 (aShapeIn->isFace() && aShapeOut->isFace()))
295 void SketchPlugin_MultiTranslation::attributeChanged(const std::string& theID)
297 if (theID == TRANSLATION_LIST_ID()) {
298 AttributeRefListPtr aTranslationObjectRefs = reflist(TRANSLATION_LIST_ID());
299 if (aTranslationObjectRefs->size() == 0) {
300 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
304 DocumentPtr aDoc = document();
305 // Clear list of objects
306 AttributeRefListPtr aRefListOfTranslated = reflist(SketchPlugin_Constraint::ENTITY_B());
307 std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
308 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
309 while (aTargetIter != aTargetList.end()) {
311 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
312 aRefListOfTranslated->remove(*aTargetIter);
313 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
315 aDoc->removeFeature(aFeature);
318 aRefListOfTranslated->clear();
319 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();