1 // Copyright (C) 2014-2020 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_ConstraintMirror.h"
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Tools.h>
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Sketch.h>
34 #include <SketcherPrs_Factory.h>
36 #include <Config_PropManager.h>
38 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
42 void SketchPlugin_ConstraintMirror::initAttributes()
44 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
45 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
46 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
47 data()->addAttribute(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(),
48 ModelAPI_AttributeRefList::typeId());
49 ModelAPI_Session::get()->validators()->
50 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
51 ModelAPI_Session::get()->validators()->
52 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
55 void SketchPlugin_ConstraintMirror::execute()
57 AttributeRefListPtr aMirrorObjectRefs = reflist(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
59 // Wait all objects being created, then send update events
60 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
61 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
63 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
65 std::shared_ptr<ModelAPI_Data> aData = data();
66 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
67 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
68 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
69 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
70 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
71 std::list<ObjectPtr> aMirroredList = aRefListOfMirrored->list();
72 std::vector<bool> isUsed(anInitialList.size(), false);
73 // add new items to the list
74 for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
75 ObjectPtr anObject = aMirrorObjectRefs->object(anInd);
76 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
77 std::vector<bool>::iterator aUsedIt = isUsed.begin();
78 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
79 if (*anIt == anObject) {
83 if (anIt == anInitialList.end())
84 aRefListOfShapes->append(anObject);
86 // remove unused items
87 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
88 std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
89 std::vector<bool>::iterator aUsedIter = isUsed.begin();
90 std::set<FeaturePtr> aFeaturesToBeRemoved;
91 for (; aUsedIter != isUsed.end(); aUsedIter++) {
93 aRefListOfShapes->remove(*anInitIter);
94 if (aMirrorIter != aMirroredList.end()) {
95 aRefListOfMirrored->remove(*aMirrorIter);
96 // remove the corresponding feature from the sketch
97 ResultConstructionPtr aRC =
98 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
99 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
100 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
102 aFeaturesToBeRemoved.insert(aFeature);
105 if (anInitIter != anInitialList.end())
107 if (aMirrorIter != aMirroredList.end())
110 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
112 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
114 // Check consistency of initial list and mirrored list
115 anInitialList = aRefListOfShapes->list();
116 anInitIter = anInitialList.begin();
117 aMirrorIter = aMirroredList.begin();
118 int indFirstWrong = 0; // index of element starts difference in the lists
119 std::set<int> anInvalidInd; // list of indices of removed features
120 std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
121 for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
122 // Add features and store indices of objects to remove
123 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
124 ResultConstructionPtr aRCIn;
126 aRCIn = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
128 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(
129 aRCIn->document()->feature(aRCIn));
131 if (aMirrorIter == aMirroredList.end())
132 aFeatureOut = std::shared_ptr<SketchPlugin_Feature>();
134 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter);
136 ResultConstructionPtr aRC =
137 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
139 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(
140 aRC->document()->feature(aRC));
145 break; // the lists are inconsistent
149 if (aMirrorIter != aMirroredList.end())
150 break; // the lists are inconsistent
151 // There is no mirrored object yet, create it
152 FeaturePtr aNewFeature =
153 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureIn, sketch(), true);
154 aNewFeature->execute();
155 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
157 std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
158 const std::list<ResultPtr>& aResults = aNewFeature->results();
159 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
160 for (; anIt != aResults.end(); anIt++) {
161 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
163 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
164 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
165 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
166 (aShapeIn->isFace() && aShapeOut->isFace()))
167 aRefListOfMirrored->append(aRC);
171 if (aFeatureIn->getKind() != aFeatureOut->getKind())
172 break; // the lists are inconsistent
173 if (!aFeatureIn->data()->isValid()) {
174 // initial feature was removed, delete it from lists
175 anInvalidInd.insert(indFirstWrong);
179 // Remove from the list objects already deleted before
180 std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
181 for ( ; anIt != anInvalidInd.rend(); anIt++) {
182 if (*anIt < indFirstWrong) indFirstWrong--;
183 aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
184 aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
186 // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
187 if (anInitIter != anInitialList.end()) {
188 while (aRefListOfMirrored->size() > indFirstWrong)
189 aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
190 // Create mirrored features instead of removed
191 anInitialList = aRefListOfShapes->list();
192 anInitIter = anInitialList.begin();
193 for (int i = 0; i < indFirstWrong; i++) anInitIter++;
194 for ( ; anInitIter != anInitialList.end(); anInitIter++) {
195 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
196 FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
197 aRefListOfMirrored->append(aNewFeature);
201 // send events to update the sub-features by the solver
203 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
206 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
211 AISObjectPtr anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch(),
216 void SketchPlugin_ConstraintMirror::erase()
218 // Set copy attribute to false on all copied features.
219 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
220 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
222 if(aRefListOfMirrored.get()) {
223 static Events_Loop* aLoop = Events_Loop::loop();
224 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
226 std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
227 for(std::list<ObjectPtr>::const_iterator aTargetIt =
228 aTargetList.cbegin(); aTargetIt != aTargetList.cend(); aTargetIt++) {
229 if((*aTargetIt).get()) {
230 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aTargetIt);
232 FeaturePtr aFeature = aRes->document()->feature(aRes);
234 AttributeBooleanPtr aBooleanAttr =
235 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
236 if(aBooleanAttr.get()) {
237 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
238 aBooleanAttr->setValue(false);
239 // Redisplay object as it is not copy anymore.
240 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
248 SketchPlugin_ConstraintBase::erase();
251 void SketchPlugin_ConstraintMirror::attributeChanged(const std::string& theID)
253 if (theID == MIRROR_LIST_ID()) {
254 AttributeRefListPtr aMirrorObjectRefs = reflist(MIRROR_LIST_ID());
255 if (aMirrorObjectRefs->size() == 0) {
256 DocumentPtr aDoc = document();
257 // Clear list of objects
258 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
259 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
260 std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
261 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
262 for (; aTargetIter != aTargetList.end(); aTargetIter++) {
263 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
265 aDoc->removeFeature(aFeature);
267 aRefListOfMirrored->clear();
268 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
269 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();