Salome HOME
Task 2.2. Sketch Angular copy
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_ConstraintMultiRotation.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
24
25 #include <PlaneGCSSolver_AttributeBuilder.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
27 #include <PlaneGCSSolver_ScalarWrapper.h>
28 #include <PlaneGCSSolver_UpdateFeature.h>
29
30 #include <SketchPlugin_MultiRotation.h>
31
32 #include <ModelAPI_AttributeString.h>
33
34 #include <cmath>
35
36 void SketchSolver_ConstraintMultiRotation::getAttributes(
37     EntityWrapperPtr& theCenter, ScalarWrapperPtr& theAngle,
38     bool& theFullValue, bool& theReversed, std::list<EntityWrapperPtr>& theEntities)
39 {
40   AttributePtr anAngleAttr = myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID());
41   PlaneGCSSolver_AttributeBuilder aValueBuilder;
42   theAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(
43       aValueBuilder.createAttribute(anAngleAttr));
44   myStorage->addEntity(anAngleAttr, theAngle);
45
46   AttributeRefAttrPtr aCenterAttr =
47       myBaseConstraint->refattr(SketchPlugin_MultiRotation::CENTER_ID());
48   if (!aCenterAttr || !aCenterAttr->isInitialized()) {
49     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
50     return;
51   }
52
53   myType = CONSTRAINT_MULTI_ROTATION;
54
55   myStorage->update(AttributePtr(aCenterAttr));
56   theCenter = myStorage->entity(AttributePtr(aCenterAttr));
57
58   AttributeStringPtr aMethodTypeAttr =
59       myBaseConstraint->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
60   theFullValue = aMethodTypeAttr->value() != "SingleAngle";
61
62   theReversed = myBaseConstraint->boolean(SketchPlugin_MultiRotation::REVERSED_ID())->value();
63
64   getEntities(theEntities);
65
66   // add owner of central point of Multi-Rotation to the list of monitored features
67   FeaturePtr anOwner = ModelAPI_Feature::feature(aCenterAttr->attr()->owner());
68   if (anOwner)
69     myOriginalFeatures.insert(anOwner);
70 }
71
72 void SketchSolver_ConstraintMultiRotation::process()
73 {
74   cleanErrorMsg();
75   if (!myBaseConstraint || !myStorage) {
76     // Not enough parameters are assigned
77     return;
78   }
79
80   EntityWrapperPtr aRotationCenter;
81   std::list<EntityWrapperPtr> aBaseEntities;
82   getAttributes(aRotationCenter, myAngle, myIsFullValue, myIsRevered, aBaseEntities);
83   if (!myErrorMsg.empty())
84     return;
85
86   myAdjusted = false;
87   adjustConstraint();
88
89   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
90 }
91
92 void SketchSolver_ConstraintMultiRotation::updateLocal()
93 {
94   double aValue = myBaseConstraint->real(SketchPlugin_MultiRotation::ANGLE_ID())->value();
95   bool isReversed = myBaseConstraint->boolean(SketchPlugin_MultiRotation::REVERSED_ID())->value();
96   if (fabs(myAngle->value() - aValue) > tolerance || isReversed != myIsRevered)
97     myAdjusted = false;
98   // update angle value
99   myAngle->setValue(aValue);
100   myIsRevered = isReversed;
101
102   // update center
103   DataPtr aData = myBaseConstraint->data();
104   AttributePoint2DPtr aCenterPointAttribute = GeomDataAPI_Point2D::getPoint2D(aData,
105                                            SketchPlugin_MultiRotation::CENTER_ID());
106   bool aCenterPointChanged = aCenterPointAttribute != myCenterPointAttribute;
107   if (aCenterPointChanged)
108     myCenterPointAttribute = aCenterPointAttribute;
109
110   AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
111   bool aFullValue = aMethodTypeAttr->value() != "SingleAngle";
112   bool isMethodChanged = aFullValue != myIsFullValue;
113   if (isMethodChanged)
114     myIsFullValue = aFullValue;
115
116   if (aCenterPointChanged || isMethodChanged)
117     myAdjusted = false;
118 }
119
120 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
121 {
122   if (myAdjusted)
123     return;
124
125   double anAngleValue = myAngle->value();
126   if (fabs(anAngleValue) < tolerance) {
127     myStorage->setNeedToResolve(false);
128     return;
129   }
130   if (myIsRevered)
131     anAngleValue *= -1.0;
132
133   // Obtain coordinates of rotation center
134   AttributeRefAttrPtr aCenterAttr =
135       myBaseConstraint->refattr(SketchPlugin_MultiRotation::CENTER_ID());
136   std::shared_ptr<PlaneGCSSolver_PointWrapper> aRotCenter =
137       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
138       myStorage->entity(AttributePtr(aCenterAttr)));
139   if (aRotCenter)
140   {
141     GCSPointPtr aCenterPoint = aRotCenter->point();
142     myCenterCoord[0] = *(aCenterPoint->x);
143     myCenterCoord[1] = *(aCenterPoint->y);
144   }
145   else
146   {
147     AttributePoint2DPtr aCenterPnt =
148         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterAttr->attr());
149     myCenterCoord[0] = aCenterPnt->x();
150     myCenterCoord[1] = aCenterPnt->y();
151   }
152
153   if (myIsFullValue && myNumberOfCopies > 0)
154   {
155     // if the full angle value is equal to 360, then distribute rotated items
156     // to avoid superposition of original feature and last copy
157     if (fabs(anAngleValue - 360.0) < 1.e-7)
158       anAngleValue /= (myNumberOfCopies + 1);
159     else
160       anAngleValue /= myNumberOfCopies;
161   }
162
163   myRotationVal[0] = sin(anAngleValue * PI / 180.0);
164   myRotationVal[1] = cos(anAngleValue * PI / 180.0);
165
166   SketchSolver_ConstraintMulti::adjustConstraint();
167 }
168
169 void SketchSolver_ConstraintMultiRotation::getRelative(
170     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
171 {
172   theRelX = theAbsX - myCenterCoord[0];
173   theRelY = theAbsY - myCenterCoord[1];
174 }
175
176 void SketchSolver_ConstraintMultiRotation::getAbsolute(
177     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
178 {
179   theAbsX = theRelX + myCenterCoord[0];
180   theAbsY = theRelY + myCenterCoord[1];
181 }
182
183 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
184 {
185   // rotate direction
186   // myRotationVal[0] = sinA, myRotationVal[1] = cosA
187   double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
188   theY = theX * myRotationVal[0] + theY * myRotationVal[1];
189   theX = aTemp;
190 }
191
192 const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
193 {
194   return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();
195 }