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