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 double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
57 attribute(ANGLE_ID()))->value();
58 // Convert angle to radians
59 anAngle *= PI / 180.0;
61 // Wait all objects being created, then send update events
62 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
63 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
65 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
67 std::shared_ptr<ModelAPI_Data> aData = data();
68 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
69 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
70 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
71 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
72 int aCurrentNbCopies = aRefListOfShapes->size() ?
73 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
74 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
75 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
76 std::list<ResultPtr> anAddition;
77 std::vector<bool> isUsed(anInitialList.size(), false);
78 // collect new items and check the items to remove
79 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
80 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
81 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
82 std::vector<bool>::iterator aUsedIt = isUsed.begin();
83 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
84 if (*anIt == aSelect->context()) {
88 if (anIt == anInitialList.end())
89 anAddition.push_back(aSelect->context());
91 // remove unused items
92 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
93 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
94 std::vector<bool>::iterator aUsedIter = isUsed.begin();
95 for (; aUsedIter != isUsed.end(); aUsedIter++) {
97 aRefListOfShapes->remove(*anInitIter);
98 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
99 aRefListOfRotated->remove(*aTargetIter);
100 // remove the corresponding feature from the sketch
101 ResultConstructionPtr aRC =
102 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
103 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
104 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
106 aDoc->removeFeature(aFeature);
109 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
112 if (anInitIter != anInitialList.end())
115 // change number of copies
116 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
117 bool isAdd = aNbCopies > aCurrentNbCopies;
118 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
119 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
122 aTargetList = aRefListOfRotated->list();
123 aTargetIter = aTargetList.begin();
124 ObjectPtr anObjToCopy = *aTargetIter;
125 while (aTargetIter != aTargetList.end()) {
126 aRefListOfRotated->remove(*aTargetIter);
129 if (ind > aMinCopies && ind <=aMaxCopies) {
130 while (ind <= aMaxCopies) {
132 // Add new shifted item
133 ObjectPtr anObject = copyFeature(anObjToCopy);
134 aTargetList.insert(aTargetIter, anObject);
137 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
138 ObjectPtr anObject = *aRemoveIt;
139 aTargetList.erase(aRemoveIt);
140 aRefListOfRotated->remove(anObject);
141 // remove the corresponding feature from the sketch
142 ResultConstructionPtr aRC =
143 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
144 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
145 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
147 aDoc->removeFeature(aFeature);
152 if (aTargetIter != aTargetList.end())
153 anObjToCopy = *aTargetIter;
157 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
158 aRefListOfRotated->append(*aTargetIter);
161 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
162 for (; anAddIter != anAddition.end(); anAddIter++) {
163 aRefListOfShapes->append(*anAddIter);
164 aRefListOfRotated->append(*anAddIter);
165 for (int i = 0; i < aNbCopies; i++) {
166 ObjectPtr anObject = copyFeature(*anAddIter);
167 aRefListOfRotated->append(anObject);
171 //// if (fabs(anAngle) > 1.e-12) {
172 //// // Recalculate positions of features
173 //// aTargetList = aRefListOfRotated->list();
174 //// aTargetIter = aTargetList.begin();
175 //// while (aTargetIter != aTargetList.end()) {
176 //// ObjectPtr anInitialObject = *aTargetIter++;
177 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
178 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
182 // send events to update the sub-features by the solver
184 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
187 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
192 AISObjectPtr anAIS = thePrevious;
194 anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
200 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
202 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
203 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
204 if (!aFeature || !aResult)
207 FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
208 aFeature->data()->copyTo(aNewFeature->data());
209 aNewFeature->execute();
211 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
212 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
214 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
215 const std::list<ResultPtr>& aResults = aNewFeature->results();
216 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
217 for (; anIt != aResults.end(); anIt++) {
218 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
220 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
221 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
222 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
223 (aShapeIn->isFace() && aShapeOut->isFace()))
229 void SketchPlugin_MultiRotation::rotateFeature(
230 ObjectPtr theInitial, ObjectPtr theTarget,
231 double theCenterX, double theCenterY, double theAngle)
233 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
234 double cosA = cos(theAngle);
235 double sinA = sin(theAngle);
237 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
238 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
240 // block feature update
241 aTargetFeature->data()->blockSendAttributeUpdated(true);
243 std::list<AttributePtr> anInitAttrList =
244 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
245 std::list<AttributePtr> aTargetAttrList =
246 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
247 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
248 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
249 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
250 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
251 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
252 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
253 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
254 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
255 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
256 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
257 double dx = cosA * aDir->x() - sinA * aDir->y();
258 double dy = sinA * aDir->x() + cosA * aDir->y();
259 aPnt->setX(aCenter->x() + dx);
260 aPnt->setY(aCenter->y() + dy);
262 aPointTo->setValue(aPnt->x(), aPnt->y());
265 // unblock feature update
266 aTargetFeature->data()->blockSendAttributeUpdated(false);