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 #define PI 3.1415926535897932
25 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
29 void SketchPlugin_MultiRotation::initAttributes()
31 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
32 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
33 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
34 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
35 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
36 AttributeSelectionListPtr aSelection =
37 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38 ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39 aSelection->setSelectionType("EDGE");
40 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
44 void SketchPlugin_MultiRotation::execute()
46 AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
47 int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
48 attribute(NUMBER_OF_COPIES_ID()))->value());
50 // Obtain center and angle of rotation
51 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
52 attribute(CENTER_ID()));
53 if (!aCenter || !aCenter->isInitialized())
55 double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
56 attribute(ANGLE_ID()))->value();
57 // Convert angle to radians
58 anAngle *= PI / 180.0;
60 // Wait all objects being created, then send update events
61 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
62 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
64 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
66 std::shared_ptr<ModelAPI_Data> aData = data();
67 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
68 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
69 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
70 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
71 int aCurrentNbCopies = aRefListOfShapes->size() ?
72 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
73 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
74 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
75 std::list<ResultPtr> anAddition;
76 std::vector<bool> isUsed(anInitialList.size(), false);
77 // collect new items and check the items to remove
78 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
79 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
80 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
81 std::vector<bool>::iterator aUsedIt = isUsed.begin();
82 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
83 if (*anIt == aSelect->context()) {
87 if (anIt == anInitialList.end())
88 anAddition.push_back(aSelect->context());
90 // remove unused items
91 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
92 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
93 std::vector<bool>::iterator aUsedIter = isUsed.begin();
94 for (; aUsedIter != isUsed.end(); aUsedIter++) {
96 aRefListOfShapes->remove(*anInitIter);
97 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
98 aRefListOfRotated->remove(*aTargetIter);
99 // remove the corresponding feature from the sketch
100 ResultConstructionPtr aRC =
101 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
102 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
103 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
105 aDoc->removeFeature(aFeature);
108 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
111 if (anInitIter != anInitialList.end())
114 // change number of copies
115 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
116 bool isAdd = aNbCopies > aCurrentNbCopies;
117 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
118 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
121 aTargetList = aRefListOfRotated->list();
122 aTargetIter = aTargetList.begin();
123 ObjectPtr anObjToCopy = *aTargetIter;
124 while (aTargetIter != aTargetList.end()) {
125 aRefListOfRotated->remove(*aTargetIter);
128 if (ind > aMinCopies && ind <=aMaxCopies) {
129 while (ind <= aMaxCopies) {
131 // Add new shifted item
132 ObjectPtr anObject = copyFeature(anObjToCopy);
133 aTargetList.insert(aTargetIter, anObject);
136 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
137 ObjectPtr anObject = *aRemoveIt;
138 aTargetList.erase(aRemoveIt);
139 aRefListOfRotated->remove(anObject);
140 // remove the corresponding feature from the sketch
141 ResultConstructionPtr aRC =
142 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
143 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
144 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
146 aDoc->removeFeature(aFeature);
151 if (aTargetIter != aTargetList.end())
152 anObjToCopy = *aTargetIter;
156 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
157 aRefListOfRotated->append(*aTargetIter);
160 std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
161 for (; anAddIter != anAddition.end(); anAddIter++) {
162 aRefListOfShapes->append(*anAddIter);
163 aRefListOfRotated->append(*anAddIter);
164 for (int i = 0; i < aNbCopies; i++) {
165 ObjectPtr anObject = copyFeature(*anAddIter);
166 aRefListOfRotated->append(anObject);
170 //// if (fabs(anAngle) > 1.e-12) {
171 //// // Recalculate positions of features
172 //// aTargetList = aRefListOfRotated->list();
173 //// aTargetIter = aTargetList.begin();
174 //// while (aTargetIter != aTargetList.end()) {
175 //// ObjectPtr anInitialObject = *aTargetIter++;
176 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
177 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
181 // send events to update the sub-features by the solver
183 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
186 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
191 AISObjectPtr anAIS = thePrevious;
194 // anAIS = SketcherPrs_Factory::mirrorConstraint(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);