1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_MultiRotation.cpp
4 // Created: 21 Apr 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_MultiRotation.h"
8 #include "SketchPlugin_Tools.h"
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_AttributeInteger.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_AttributeRefList.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Tools.h>
23 #include <SketchPlugin_SketchEntity.h>
25 #include <GeomAPI_Pnt2d.h>
26 #include <GeomAPI_XY.h>
28 #include <SketcherPrs_Factory.h>
32 #define PI 3.1415926535897932
34 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
38 void SketchPlugin_MultiRotation::initAttributes()
40 data()->addAttribute(CENTER_ID(), ModelAPI_AttributeRefAttr::typeId());
42 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
43 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
44 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
45 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
46 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
47 data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
52 void SketchPlugin_MultiRotation::execute()
55 // it is possible, that this method is called before this feature has back reference to sketch
56 // in this case, the execute is performed after this is done
60 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
61 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
65 // Obtain center and angle of rotation
66 AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
67 if (!aCenter || !aCenter->isInitialized())
70 //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
71 // attribute(ANGLE_ID()))->value();
73 // Convert angle to radians
74 //anAngle *= PI / 180.0;
76 // Wait all objects being created, then send update events
77 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
78 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
80 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
82 std::shared_ptr<ModelAPI_Data> aData = data();
83 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
84 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
85 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
86 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
87 int aCurrentNbCopies = aRefListOfShapes->size() ?
88 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
89 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
90 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
91 std::list<ObjectPtr> anAddition;
92 std::vector<bool> isUsed(anInitialList.size(), false);
93 // collect new items and check the items to remove
94 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
95 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
96 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
97 std::vector<bool>::iterator aUsedIt = isUsed.begin();
98 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
99 if (*anIt == anObject) {
103 if (anIt == anInitialList.end())
104 anAddition.push_back(anObject);
106 // remove unused items
107 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
108 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
109 std::vector<bool>::iterator aUsedIter = isUsed.begin();
110 std::set<FeaturePtr> aFeaturesToBeRemoved;
111 for (; aUsedIter != isUsed.end(); aUsedIter++) {
113 aRefListOfShapes->remove(*anInitIter);
114 aRefListOfRotated->remove(*aTargetIter++);
115 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
116 aRefListOfRotated->remove(*aTargetIter);
117 // remove the corresponding feature from the sketch
118 ResultConstructionPtr aRC =
119 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
120 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
121 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
123 aFeaturesToBeRemoved.insert(aFeature);
126 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
129 if (anInitIter != anInitialList.end())
132 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
133 // change number of copies
134 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
135 bool isAdd = aNbCopies > aCurrentNbCopies;
136 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
137 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
140 aTargetList = aRefListOfRotated->list();
141 aTargetIter = aTargetList.begin();
142 ObjectPtr anObjToCopy = *aTargetIter;
143 std::set<FeaturePtr> aFeaturesToBeRemoved;
144 while (aTargetIter != aTargetList.end()) {
145 aRefListOfRotated->remove(*aTargetIter);
148 if (ind > aMinCopies && ind <=aMaxCopies) {
149 while (ind <= aMaxCopies) {
151 // Add new shifted item
152 ObjectPtr anObject = copyFeature(anObjToCopy);
153 aTargetList.insert(aTargetIter, anObject);
156 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
157 ObjectPtr anObject = *aRemoveIt;
158 aTargetList.erase(aRemoveIt);
159 aRefListOfRotated->remove(anObject);
160 // remove the corresponding feature from the sketch
161 ResultConstructionPtr aRC =
162 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
163 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
164 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
166 aFeaturesToBeRemoved.insert(aFeature);
171 if (aTargetIter != aTargetList.end())
172 anObjToCopy = *aTargetIter;
175 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
177 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
178 aRefListOfRotated->append(*aTargetIter);
181 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
182 for (; anAddIter != anAddition.end(); anAddIter++) {
183 aRefListOfShapes->append(*anAddIter);
184 aRefListOfRotated->append(*anAddIter);
185 for (int i = 0; i < aNbCopies; i++) {
186 ObjectPtr anObject = copyFeature(*anAddIter);
187 aRefListOfRotated->append(anObject);
191 //// if (fabs(anAngle) > 1.e-12) {
192 //// // Recalculate positions of features
193 //// aTargetList = aRefListOfRotated->list();
194 //// aTargetIter = aTargetList.begin();
195 //// while (aTargetIter != aTargetList.end()) {
196 //// ObjectPtr anInitialObject = *aTargetIter++;
197 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
198 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
202 // send events to update the sub-features by the solver
204 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
207 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
212 AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
217 void SketchPlugin_MultiRotation::erase()
219 static Events_Loop* aLoop = Events_Loop::loop();
220 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
222 // Set copy attribute to false on all copied features.
223 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
224 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
225 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
228 if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
229 for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
230 ObjectPtr anObject = aRefListOfRotated->object(anIndex);
231 if(aRefListOfShapes->isInList(anObject)) {
232 // Don't modify attribute of original features, just skip.
236 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
238 FeaturePtr aFeature = aRes->document()->feature(aRes);
240 AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
241 if(aBooleanAttr.get()) {
242 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
243 aBooleanAttr->setValue(false);
244 // Redisplay object as it is not copy anymore.
245 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
253 SketchPlugin_ConstraintBase::erase();
256 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
258 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
259 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
260 if (!aFeature || !aResult)
263 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
264 aNewFeature->execute();
266 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
267 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
269 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
270 const std::list<ResultPtr>& aResults = aNewFeature->results();
271 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
272 for (; anIt != aResults.end(); anIt++) {
273 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
275 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
276 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
277 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
278 (aShapeIn->isFace() && aShapeOut->isFace()))
284 /*void SketchPlugin_MultiRotation::rotateFeature(
285 ObjectPtr theInitial, ObjectPtr theTarget,
286 double theCenterX, double theCenterY, double theAngle)
288 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
289 double cosA = std::cos(theAngle);
290 double sinA = std::sin(theAngle);
292 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
293 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
295 // block feature update
296 aTargetFeature->data()->blockSendAttributeUpdated(true);
298 std::list<AttributePtr> anInitAttrList =
299 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
300 std::list<AttributePtr> aTargetAttrList =
301 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
302 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
303 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
304 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
305 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
306 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
307 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
308 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
309 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
310 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
311 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
312 double dx = cosA * aDir->x() - sinA * aDir->y();
313 double dy = sinA * aDir->x() + cosA * aDir->y();
314 aPnt->setX(aCenter->x() + dx);
315 aPnt->setY(aCenter->y() + dy);
317 aPointTo->setValue(aPnt->x(), aPnt->y());
320 // unblock feature update
321 aTargetFeature->data()->blockSendAttributeUpdated(false);
325 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
327 if (theID == ROTATION_LIST_ID()) {
328 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
329 if (aRotationObjectRefs->size() == 0) {
330 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
334 // Clear list of objects
335 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
336 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
337 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
338 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
339 std::set<FeaturePtr> aFeaturesToBeRemoved;
340 while (aTargetIter != aTargetList.end()) {
342 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
343 aRefListOfRotated->remove(*aTargetIter);
344 // remove the corresponding feature from the sketch
345 ResultConstructionPtr aRC =
346 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
347 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
348 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
350 aFeaturesToBeRemoved.insert(aFeature);
353 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
355 aRefListOfRotated->clear();
356 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
357 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
358 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
359 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();