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"
9 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_AttributeRefList.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_XY.h>
23 #include <SketcherPrs_Factory.h>
27 #define PI 3.1415926535897932
29 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
33 void SketchPlugin_MultiRotation::initAttributes()
35 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
36 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
37 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
38 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
39 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
40 AttributeSelectionListPtr aSelection =
41 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42 ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
43 aSelection->setSelectionType("EDGE");
44 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
45 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
48 void SketchPlugin_MultiRotation::execute()
51 // it is possible, that this method is called before this feature has back reference to sketch
52 // in this case, the execute is performed after this is done
56 AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
57 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
59 // Obtain center and angle of rotation
60 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61 attribute(CENTER_ID()));
62 if (!aCenter || !aCenter->isInitialized())
64 // make a visible points
65 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
67 double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
68 attribute(ANGLE_ID()))->value();
69 // Convert angle to radians
70 anAngle *= PI / 180.0;
72 // Wait all objects being created, then send update events
73 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
74 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
76 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
78 std::shared_ptr<ModelAPI_Data> aData = data();
79 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
80 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
81 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
82 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
83 int aCurrentNbCopies = aRefListOfShapes->size() ?
84 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
85 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
86 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
87 std::list<ResultPtr> anAddition;
88 std::vector<bool> isUsed(anInitialList.size(), false);
89 // collect new items and check the items to remove
90 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
91 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
92 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
93 std::vector<bool>::iterator aUsedIt = isUsed.begin();
94 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
95 if (*anIt == aSelect->context()) {
99 if (anIt == anInitialList.end())
100 anAddition.push_back(aSelect->context());
102 // remove unused items
103 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
104 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
105 std::vector<bool>::iterator aUsedIter = isUsed.begin();
106 for (; aUsedIter != isUsed.end(); aUsedIter++) {
108 aRefListOfShapes->remove(*anInitIter);
109 aRefListOfRotated->remove(*aTargetIter++);
110 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
111 aRefListOfRotated->remove(*aTargetIter);
112 // remove the corresponding feature from the sketch
113 ResultConstructionPtr aRC =
114 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
115 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
116 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
118 aDoc->removeFeature(aFeature);
121 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
124 if (anInitIter != anInitialList.end())
127 // change number of copies
128 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
129 bool isAdd = aNbCopies > aCurrentNbCopies;
130 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
131 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
134 aTargetList = aRefListOfRotated->list();
135 aTargetIter = aTargetList.begin();
136 ObjectPtr anObjToCopy = *aTargetIter;
137 while (aTargetIter != aTargetList.end()) {
138 aRefListOfRotated->remove(*aTargetIter);
141 if (ind > aMinCopies && ind <=aMaxCopies) {
142 while (ind <= aMaxCopies) {
144 // Add new shifted item
145 ObjectPtr anObject = copyFeature(anObjToCopy);
146 aTargetList.insert(aTargetIter, anObject);
149 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
150 ObjectPtr anObject = *aRemoveIt;
151 aTargetList.erase(aRemoveIt);
152 aRefListOfRotated->remove(anObject);
153 // remove the corresponding feature from the sketch
154 ResultConstructionPtr aRC =
155 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
156 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
157 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
159 aDoc->removeFeature(aFeature);
164 if (aTargetIter != aTargetList.end())
165 anObjToCopy = *aTargetIter;
169 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
170 aRefListOfRotated->append(*aTargetIter);
173 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
174 for (; anAddIter != anAddition.end(); anAddIter++) {
175 aRefListOfShapes->append(*anAddIter);
176 aRefListOfRotated->append(*anAddIter);
177 for (int i = 0; i < aNbCopies; i++) {
178 ObjectPtr anObject = copyFeature(*anAddIter);
179 aRefListOfRotated->append(anObject);
183 //// if (fabs(anAngle) > 1.e-12) {
184 //// // Recalculate positions of features
185 //// aTargetList = aRefListOfRotated->list();
186 //// aTargetIter = aTargetList.begin();
187 //// while (aTargetIter != aTargetList.end()) {
188 //// ObjectPtr anInitialObject = *aTargetIter++;
189 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
190 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
194 // send events to update the sub-features by the solver
196 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
199 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
204 AISObjectPtr anAIS = thePrevious;
206 anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
212 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
214 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
215 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
216 if (!aFeature || !aResult)
219 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
220 aNewFeature->execute();
222 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
223 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
225 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
226 const std::list<ResultPtr>& aResults = aNewFeature->results();
227 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
228 for (; anIt != aResults.end(); anIt++) {
229 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
231 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
232 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
233 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
234 (aShapeIn->isFace() && aShapeOut->isFace()))
240 void SketchPlugin_MultiRotation::rotateFeature(
241 ObjectPtr theInitial, ObjectPtr theTarget,
242 double theCenterX, double theCenterY, double theAngle)
244 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
245 double cosA = std::cos(theAngle);
246 double sinA = std::sin(theAngle);
248 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
249 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
251 // block feature update
252 aTargetFeature->data()->blockSendAttributeUpdated(true);
254 std::list<AttributePtr> anInitAttrList =
255 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
256 std::list<AttributePtr> aTargetAttrList =
257 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
258 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
259 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
260 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
261 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
262 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
263 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
264 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
265 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
266 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
267 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
268 double dx = cosA * aDir->x() - sinA * aDir->y();
269 double dy = sinA * aDir->x() + cosA * aDir->y();
270 aPnt->setX(aCenter->x() + dx);
271 aPnt->setY(aCenter->y() + dy);
273 aPointTo->setValue(aPnt->x(), aPnt->y());
276 // unblock feature update
277 aTargetFeature->data()->blockSendAttributeUpdated(false);
281 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
283 if (theID == ROTATION_LIST_ID()) {
284 AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
285 if (aRotationObjectRefs->size() == 0) {
286 // Wait all objects being created, then send update events
287 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
288 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
290 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
292 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
293 // Clear list of objects
294 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
295 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
296 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
297 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
298 while (aTargetIter != aTargetList.end()) {
300 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
301 aRefListOfRotated->remove(*aTargetIter);
302 // remove the corresponding feature from the sketch
303 ResultConstructionPtr aRC =
304 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
305 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
306 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
308 aDoc->removeFeature(aFeature);
311 integer(NUMBER_OF_COPIES_ID())->setValue(0);
313 // send events to update the sub-features by the solver
315 Events_Loop::loop()->setFlushed(anUpdateEvent, true);