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>
25 #define PI 3.1415926535897932
27 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
31 void SketchPlugin_MultiRotation::initAttributes()
33 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
34 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
35 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
36 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
37 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
38 AttributeSelectionListPtr aSelection =
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41 aSelection->setSelectionType("EDGE");
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
43 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
46 void SketchPlugin_MultiRotation::execute()
48 AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
49 int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value();
51 // Obtain center and angle of rotation
52 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53 attribute(CENTER_ID()));
54 if (!aCenter || !aCenter->isInitialized())
56 // make a visible points
57 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
59 double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
60 attribute(ANGLE_ID()))->value();
61 // Convert angle to radians
62 anAngle *= PI / 180.0;
64 // Wait all objects being created, then send update events
65 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
66 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
68 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
70 std::shared_ptr<ModelAPI_Data> aData = data();
71 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
72 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
73 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
74 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
75 int aCurrentNbCopies = aRefListOfShapes->size() ?
76 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
77 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
78 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
79 std::list<ResultPtr> anAddition;
80 std::vector<bool> isUsed(anInitialList.size(), false);
81 // collect new items and check the items to remove
82 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
83 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
84 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
85 std::vector<bool>::iterator aUsedIt = isUsed.begin();
86 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
87 if (*anIt == aSelect->context()) {
91 if (anIt == anInitialList.end())
92 anAddition.push_back(aSelect->context());
94 // remove unused items
95 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
96 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
97 std::vector<bool>::iterator aUsedIter = isUsed.begin();
98 for (; aUsedIter != isUsed.end(); aUsedIter++) {
100 aRefListOfShapes->remove(*anInitIter);
101 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
102 aRefListOfRotated->remove(*aTargetIter);
103 // remove the corresponding feature from the sketch
104 ResultConstructionPtr aRC =
105 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
106 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
107 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
109 aDoc->removeFeature(aFeature);
112 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
115 if (anInitIter != anInitialList.end())
118 // change number of copies
119 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
120 bool isAdd = aNbCopies > aCurrentNbCopies;
121 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
122 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
125 aTargetList = aRefListOfRotated->list();
126 aTargetIter = aTargetList.begin();
127 ObjectPtr anObjToCopy = *aTargetIter;
128 while (aTargetIter != aTargetList.end()) {
129 aRefListOfRotated->remove(*aTargetIter);
132 if (ind > aMinCopies && ind <=aMaxCopies) {
133 while (ind <= aMaxCopies) {
135 // Add new shifted item
136 ObjectPtr anObject = copyFeature(anObjToCopy);
137 aTargetList.insert(aTargetIter, anObject);
140 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
141 ObjectPtr anObject = *aRemoveIt;
142 aTargetList.erase(aRemoveIt);
143 aRefListOfRotated->remove(anObject);
144 // remove the corresponding feature from the sketch
145 ResultConstructionPtr aRC =
146 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
147 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
148 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
150 aDoc->removeFeature(aFeature);
155 if (aTargetIter != aTargetList.end())
156 anObjToCopy = *aTargetIter;
160 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
161 aRefListOfRotated->append(*aTargetIter);
164 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
165 for (; anAddIter != anAddition.end(); anAddIter++) {
166 aRefListOfShapes->append(*anAddIter);
167 aRefListOfRotated->append(*anAddIter);
168 for (int i = 0; i < aNbCopies; i++) {
169 ObjectPtr anObject = copyFeature(*anAddIter);
170 aRefListOfRotated->append(anObject);
174 //// if (fabs(anAngle) > 1.e-12) {
175 //// // Recalculate positions of features
176 //// aTargetList = aRefListOfRotated->list();
177 //// aTargetIter = aTargetList.begin();
178 //// while (aTargetIter != aTargetList.end()) {
179 //// ObjectPtr anInitialObject = *aTargetIter++;
180 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
181 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
185 // send events to update the sub-features by the solver
187 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
190 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
195 AISObjectPtr anAIS = thePrevious;
197 anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
203 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
205 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
206 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
207 if (!aFeature || !aResult)
210 FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
211 aFeature->data()->copyTo(aNewFeature->data());
212 aNewFeature->execute();
214 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
215 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
217 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
218 const std::list<ResultPtr>& aResults = aNewFeature->results();
219 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
220 for (; anIt != aResults.end(); anIt++) {
221 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
223 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
224 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
225 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
226 (aShapeIn->isFace() && aShapeOut->isFace()))
232 void SketchPlugin_MultiRotation::rotateFeature(
233 ObjectPtr theInitial, ObjectPtr theTarget,
234 double theCenterX, double theCenterY, double theAngle)
236 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
237 double cosA = cos(theAngle);
238 double sinA = sin(theAngle);
240 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
241 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
243 // block feature update
244 aTargetFeature->data()->blockSendAttributeUpdated(true);
246 std::list<AttributePtr> anInitAttrList =
247 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
248 std::list<AttributePtr> aTargetAttrList =
249 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
250 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
251 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
252 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
253 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
254 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
255 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
256 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
257 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
258 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
259 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
260 double dx = cosA * aDir->x() - sinA * aDir->y();
261 double dy = sinA * aDir->x() + cosA * aDir->y();
262 aPnt->setX(aCenter->x() + dx);
263 aPnt->setY(aCenter->y() + dy);
265 aPointTo->setValue(aPnt->x(), aPnt->y());
268 // unblock feature update
269 aTargetFeature->data()->blockSendAttributeUpdated(false);