1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintMirror.cpp
4 // Created: 17 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintMirror.h"
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Sketch.h>
20 #include <SketcherPrs_Factory.h>
22 #include <Config_PropManager.h>
24 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
28 void SketchPlugin_ConstraintMirror::initAttributes()
30 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
32 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
33 data()->addAttribute(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(), ModelAPI_AttributeRefList::typeId());
34 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
35 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
38 void SketchPlugin_ConstraintMirror::execute()
40 AttributeRefListPtr aMirrorObjectRefs = reflist(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
42 // Wait all objects being created, then send update events
43 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
44 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
46 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
48 std::shared_ptr<ModelAPI_Data> aData = data();
49 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
50 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
51 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
52 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
53 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
54 std::list<ObjectPtr> aMirroredList = aRefListOfMirrored->list();
55 std::vector<bool> isUsed(anInitialList.size(), false);
56 // add new items to the list
57 for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
58 ObjectPtr anObject = aMirrorObjectRefs->object(anInd);
59 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
60 std::vector<bool>::iterator aUsedIt = isUsed.begin();
61 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
62 if (*anIt == anObject) {
66 if (anIt == anInitialList.end())
67 aRefListOfShapes->append(anObject);
69 // remove unused items
70 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
71 std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
72 std::vector<bool>::iterator aUsedIter = isUsed.begin();
73 for (; aUsedIter != isUsed.end(); aUsedIter++) {
75 aRefListOfShapes->remove(*anInitIter);
76 if (aMirrorIter != aMirroredList.end()) {
77 aRefListOfMirrored->remove(*aMirrorIter);
78 // remove the corresponding feature from the sketch
79 ResultConstructionPtr aRC =
80 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
81 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
82 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
84 aDoc->removeFeature(aFeature);
87 if (anInitIter != anInitialList.end())
89 if (aMirrorIter != aMirroredList.end())
93 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
95 // Check consistency of initial list and mirrored list
96 anInitialList = aRefListOfShapes->list();
97 anInitIter = anInitialList.begin();
98 aMirrorIter = aMirroredList.begin();
99 int indFirstWrong = 0; // index of element starts difference in the lists
100 std::set<int> anInvalidInd; // list of indices of removed features
101 std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
102 for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
103 // Add features and store indices of objects to remove
104 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
105 ResultConstructionPtr aRCIn;
107 aRCIn = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
109 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(
110 aRCIn->document()->feature(aRCIn));
112 if (aMirrorIter == aMirroredList.end())
113 aFeatureOut = std::shared_ptr<SketchPlugin_Feature>();
115 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter);
117 ResultConstructionPtr aRC =
118 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
120 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(
121 aRC->document()->feature(aRC));
126 break; // the lists are inconsistent
130 if (aMirrorIter != aMirroredList.end())
131 break; // the lists are inconsistent
132 // There is no mirrored object yet, create it
133 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureIn, sketch(), true);
134 aNewFeature->execute();
135 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
137 std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
138 const std::list<ResultPtr>& aResults = aNewFeature->results();
139 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
140 for (; anIt != aResults.end(); anIt++) {
141 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
143 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
144 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
145 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
146 (aShapeIn->isFace() && aShapeOut->isFace()))
147 aRefListOfMirrored->append(aRC);
151 if (aFeatureIn->getKind() != aFeatureOut->getKind())
152 break; // the lists are inconsistent
153 if (!aFeatureIn->data()->isValid()) {
154 // initial feature was removed, delete it from lists
155 anInvalidInd.insert(indFirstWrong);
159 // Remove from the list objects already deleted before
160 std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
161 for ( ; anIt != anInvalidInd.rend(); anIt++) {
162 if (*anIt < indFirstWrong) indFirstWrong--;
163 aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
164 aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
166 // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
167 if (anInitIter != anInitialList.end()) {
168 while (aRefListOfMirrored->size() > indFirstWrong)
169 aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
170 // Create mirrored features instead of removed
171 anInitialList = aRefListOfShapes->list();
172 anInitIter = anInitialList.begin();
173 for (int i = 0; i < indFirstWrong; i++) anInitIter++;
174 for ( ; anInitIter != anInitialList.end(); anInitIter++) {
175 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
176 FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
177 aRefListOfMirrored->append(aNewFeature);
181 // send events to update the sub-features by the solver
183 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
186 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
191 AISObjectPtr anAIS = thePrevious;
193 anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
198 void SketchPlugin_ConstraintMirror::erase()
200 // Set copy attribute to false on all copied features.
201 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
202 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
204 if(aRefListOfMirrored.get()) {
205 static Events_Loop* aLoop = Events_Loop::loop();
206 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
208 std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
209 for(std::list<ObjectPtr>::const_iterator aTargetIt = aTargetList.cbegin(); aTargetIt != aTargetList.cend(); aTargetIt++) {
210 if((*aTargetIt).get()) {
211 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aTargetIt);
213 FeaturePtr aFeature = aRes->document()->feature(aRes);
215 AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
216 if(aBooleanAttr.get()) {
217 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
218 aBooleanAttr->setValue(false);
219 // Redisplay object as it is not copy anymore.
220 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
228 SketchPlugin_ConstraintBase::erase();
231 void SketchPlugin_ConstraintMirror::attributeChanged(const std::string& theID)
233 if (theID == MIRROR_LIST_ID()) {
234 AttributeRefListPtr aMirrorObjectRefs = reflist(MIRROR_LIST_ID());
235 if (aMirrorObjectRefs->size() == 0) {
236 // Clear list of objects
237 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
238 data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
239 std::list<ObjectPtr> aTargetList = aRefListOfMirrored->list();
240 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
241 for (; aTargetIter != aTargetList.end(); aTargetIter++) {
242 aRefListOfMirrored->remove(*aTargetIter);
243 // remove the corresponding feature from the sketch
244 ResultConstructionPtr aRC =
245 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
246 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
247 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
249 aDoc->removeFeature(aFeature);
251 aRefListOfMirrored->clear();
252 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
253 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();