Salome HOME
96beef3a2abbabcaca6b54e93d1c488510afddb2
[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 email : webmaster.salome@opencascade.com<mailto: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, 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   getEntities(theEntities);
62
63   // add owner of central point of Multi-Rotation to the list of monitored features
64   FeaturePtr anOwner = ModelAPI_Feature::feature(aCenterAttr->attr()->owner());
65   if (anOwner)
66     myOriginalFeatures.insert(anOwner);
67 }
68
69 void SketchSolver_ConstraintMultiRotation::process()
70 {
71   cleanErrorMsg();
72   if (!myBaseConstraint || !myStorage) {
73     // Not enough parameters are assigned
74     return;
75   }
76
77   EntityWrapperPtr aRotationCenter;
78   std::list<EntityWrapperPtr> aBaseEntities;
79   getAttributes(aRotationCenter, myAngle, myIsFullValue, aBaseEntities);
80   if (!myErrorMsg.empty())
81     return;
82
83   myAdjusted = false;
84   adjustConstraint();
85
86   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
87 }
88
89 void SketchSolver_ConstraintMultiRotation::updateLocal()
90 {
91   double aValue = myBaseConstraint->real(SketchPlugin_MultiRotation::ANGLE_ID())->value();
92   if (fabs(myAngle->value() - aValue) > tolerance)
93     myAdjusted = false;
94   // update angle value
95   myAngle->setValue(aValue);
96
97   // update center
98   DataPtr aData = myBaseConstraint->data();
99   AttributePoint2DPtr aCenterPointAttribute = GeomDataAPI_Point2D::getPoint2D(aData,
100                                            SketchPlugin_MultiRotation::CENTER_ID());
101   bool aCenterPointChanged = aCenterPointAttribute != myCenterPointAttribute;
102   if (aCenterPointChanged)
103     myCenterPointAttribute = aCenterPointAttribute;
104
105   AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
106   bool aFullValue = aMethodTypeAttr->value() != "SingleAngle";
107   bool isMethodChanged = aFullValue != myIsFullValue;
108   if (isMethodChanged)
109     myIsFullValue = aFullValue;
110
111   if (aCenterPointChanged || isMethodChanged)
112     myAdjusted = false;
113 }
114
115 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
116 {
117   if (myAdjusted)
118     return;
119
120   double anAngleValue = myAngle->value();
121   if (fabs(anAngleValue) < tolerance) {
122     myStorage->setNeedToResolve(false);
123     return;
124   }
125
126   // Obtain coordinates of rotation center
127   std::shared_ptr<PlaneGCSSolver_PointWrapper> aRotCenter =
128       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(myStorage->entity(
129       myBaseConstraint->attribute(SketchPlugin_MultiRotation::CENTER_ID())));
130   GCSPointPtr aCenterPoint = aRotCenter->point();
131   myCenterCoord[0] = *(aCenterPoint->x);
132   myCenterCoord[1] = *(aCenterPoint->y);
133
134   if (myIsFullValue && myNumberOfCopies > 0)
135     anAngleValue /= myNumberOfCopies;
136
137   myRotationVal[0] = sin(anAngleValue * PI / 180.0);
138   myRotationVal[1] = cos(anAngleValue * PI / 180.0);
139
140   SketchSolver_ConstraintMulti::adjustConstraint();
141 }
142
143 void SketchSolver_ConstraintMultiRotation::getRelative(
144     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
145 {
146   theRelX = theAbsX - myCenterCoord[0];
147   theRelY = theAbsY - myCenterCoord[1];
148 }
149
150 void SketchSolver_ConstraintMultiRotation::getAbsolute(
151     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
152 {
153   theAbsX = theRelX + myCenterCoord[0];
154   theAbsY = theRelY + myCenterCoord[1];
155 }
156
157 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
158 {
159   // rotate direction
160   // myRotationVal[0] = sinA, myRotationVal[1] = cosA
161   double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
162   theY = theX * myRotationVal[0] + theY * myRotationVal[1];
163   theX = aTemp;
164 }
165
166 const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
167 {
168   return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();
169 }