Salome HOME
6a276f2cf7bb7e3c0812b9e0a3d856f3394d6a8e
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
1 // Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchSolver_ConstraintMultiRotation.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
23
24 #include <PlaneGCSSolver_AttributeBuilder.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26 #include <PlaneGCSSolver_ScalarWrapper.h>
27 #include <PlaneGCSSolver_UpdateFeature.h>
28
29 #include <SketchPlugin_MultiRotation.h>
30
31 #include <ModelAPI_AttributeString.h>
32
33 #include <cmath>
34
35 void SketchSolver_ConstraintMultiRotation::getAttributes(
36     EntityWrapperPtr& theCenter, ScalarWrapperPtr& theAngle,
37     bool& theFullValue, bool& theReversed, std::list<EntityWrapperPtr>& theEntities)
38 {
39   AttributePtr anAngleAttr = myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID());
40   PlaneGCSSolver_AttributeBuilder aValueBuilder;
41   theAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(
42       aValueBuilder.createAttribute(anAngleAttr));
43   myStorage->addEntity(anAngleAttr, theAngle);
44
45   AttributeRefAttrPtr aCenterAttr =
46       myBaseConstraint->refattr(SketchPlugin_MultiRotation::CENTER_ID());
47   if (!aCenterAttr || !aCenterAttr->isInitialized()) {
48     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
49     return;
50   }
51
52   myType = CONSTRAINT_MULTI_ROTATION;
53
54   myStorage->update(AttributePtr(aCenterAttr));
55   theCenter = myStorage->entity(AttributePtr(aCenterAttr));
56
57   AttributeStringPtr aMethodTypeAttr =
58       myBaseConstraint->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
59   theFullValue = aMethodTypeAttr->value() != "SingleAngle";
60
61   theReversed = myBaseConstraint->boolean(SketchPlugin_MultiRotation::REVERSED_ID())->value();
62
63   getEntities(theEntities);
64
65   // add owner of central point of Multi-Rotation to the list of monitored features
66   FeaturePtr anOwner = ModelAPI_Feature::feature(aCenterAttr->attr()->owner());
67   if (anOwner)
68     myOriginalFeatures.insert(anOwner);
69 }
70
71 void SketchSolver_ConstraintMultiRotation::process()
72 {
73   cleanErrorMsg();
74   if (!myBaseConstraint || !myStorage) {
75     // Not enough parameters are assigned
76     return;
77   }
78
79   EntityWrapperPtr aRotationCenter;
80   std::list<EntityWrapperPtr> aBaseEntities;
81   getAttributes(aRotationCenter, myAngle, myIsFullValue, myIsRevered, aBaseEntities);
82   if (!myErrorMsg.empty())
83     return;
84
85   myAdjusted = false;
86   adjustConstraint();
87
88   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
89 }
90
91 void SketchSolver_ConstraintMultiRotation::updateLocal()
92 {
93   double aValue = myBaseConstraint->real(SketchPlugin_MultiRotation::ANGLE_ID())->value();
94   bool isReversed = myBaseConstraint->boolean(SketchPlugin_MultiRotation::REVERSED_ID())->value();
95   if (fabs(myAngle->value() - aValue) > tolerance || isReversed != myIsRevered)
96     myAdjusted = false;
97   // update angle value
98   myAngle->setValue(aValue);
99   myIsRevered = isReversed;
100
101   // update center
102   DataPtr aData = myBaseConstraint->data();
103   AttributePoint2DPtr aCenterPointAttribute = GeomDataAPI_Point2D::getPoint2D(aData,
104                                            SketchPlugin_MultiRotation::CENTER_ID());
105   bool aCenterPointChanged = aCenterPointAttribute != myCenterPointAttribute;
106   if (aCenterPointChanged)
107     myCenterPointAttribute = aCenterPointAttribute;
108
109   AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
110   bool aFullValue = aMethodTypeAttr->value() != "SingleAngle";
111   bool isMethodChanged = aFullValue != myIsFullValue;
112   if (isMethodChanged)
113     myIsFullValue = aFullValue;
114
115   if (aCenterPointChanged || isMethodChanged)
116     myAdjusted = false;
117 }
118
119 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
120 {
121   if (myAdjusted)
122     return;
123
124   double anAngleValue = myAngle->value();
125   if (fabs(anAngleValue) < tolerance) {
126     myStorage->setNeedToResolve(false);
127     return;
128   }
129   if (myIsRevered)
130     anAngleValue *= -1.0;
131
132   // Obtain coordinates of rotation center
133   AttributeRefAttrPtr aCenterAttr =
134       myBaseConstraint->refattr(SketchPlugin_MultiRotation::CENTER_ID());
135   std::shared_ptr<PlaneGCSSolver_PointWrapper> aRotCenter =
136       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
137       myStorage->entity(AttributePtr(aCenterAttr)));
138   if (aRotCenter)
139   {
140     GCSPointPtr aCenterPoint = aRotCenter->point();
141     myCenterCoord[0] = *(aCenterPoint->x);
142     myCenterCoord[1] = *(aCenterPoint->y);
143   }
144   else
145   {
146     AttributePoint2DPtr aCenterPnt =
147         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCenterAttr->attr());
148     myCenterCoord[0] = aCenterPnt->x();
149     myCenterCoord[1] = aCenterPnt->y();
150   }
151
152   if (myIsFullValue && myNumberOfCopies > 0)
153   {
154     // if the full angle value is equal to 360, then distribute rotated items
155     // to avoid superposition of original feature and last copy
156     if (fabs(anAngleValue - 360.0) < 1.e-7)
157       anAngleValue /= (myNumberOfCopies + 1);
158     else
159       anAngleValue /= myNumberOfCopies;
160   }
161
162   myRotationVal[0] = sin(anAngleValue * PI / 180.0);
163   myRotationVal[1] = cos(anAngleValue * PI / 180.0);
164
165   SketchSolver_ConstraintMulti::adjustConstraint();
166 }
167
168 void SketchSolver_ConstraintMultiRotation::getRelative(
169     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
170 {
171   theRelX = theAbsX - myCenterCoord[0];
172   theRelY = theAbsY - myCenterCoord[1];
173 }
174
175 void SketchSolver_ConstraintMultiRotation::getAbsolute(
176     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
177 {
178   theAbsX = theRelX + myCenterCoord[0];
179   theAbsY = theRelY + myCenterCoord[1];
180 }
181
182 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
183 {
184   // rotate direction
185   // myRotationVal[0] = sinA, myRotationVal[1] = cosA
186   double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
187   theY = theX * myRotationVal[0] + theY * myRotationVal[1];
188   theX = aTemp;
189 }
190
191 const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
192 {
193   return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();
194 }