Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_AttributeBuilder.cpp
1 // Copyright (C) 2014-2019  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 <PlaneGCSSolver_AngleWrapper.h>
21 #include <PlaneGCSSolver_AttributeBuilder.h>
22 #include <PlaneGCSSolver_PointWrapper.h>
23 #include <PlaneGCSSolver_ScalarWrapper.h>
24
25 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <SketchPlugin_ConstraintAngle.h>
28 #include <SketchPlugin_MultiRotation.h>
29
30 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(
31     PlaneGCSSolver_Storage* theStorage)
32   : PlaneGCSSolver_EntityBuilder(theStorage)
33 {
34 }
35
36 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(const StoragePtr& theStorage)
37   : PlaneGCSSolver_EntityBuilder(
38         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(theStorage).get())
39 {
40 }
41
42 static double* createParameter(PlaneGCSSolver_Storage* theStorage)
43 {
44   return theStorage ? theStorage->createParameter() : (new double(0));
45 }
46
47 static EntityWrapperPtr createScalar(const AttributePtr&     theAttribute,
48                                      PlaneGCSSolver_Storage* theStorage)
49 {
50   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
51   if (!aScalar)
52     return EntityWrapperPtr();
53
54   ScalarWrapperPtr aWrapper;
55   // following attributes should be converted from degrees to radians
56   //  - value of the Angle constraint
57   //  - angle of the Multi-Rotation constraint
58   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
59   if ((theAttribute->id() == SketchPlugin_Constraint::VALUE() &&
60       anOwner->getKind() == SketchPlugin_ConstraintAngle::ID()) ||
61      (theAttribute->id() == SketchPlugin_MultiRotation::ANGLE_ID() &&
62       anOwner->getKind() == SketchPlugin_MultiRotation::ID()))
63     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_AngleWrapper(createParameter(theStorage)));
64   else
65     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
66
67   if (aScalar->isInitialized())
68     aWrapper->setValue(aScalar->value());
69   return aWrapper;
70 }
71
72 static EntityWrapperPtr createPoint(const AttributePtr&     theAttribute,
73                                     PlaneGCSSolver_Storage* theStorage)
74 {
75   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
76       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
77   if (!aPoint2D)
78     return EntityWrapperPtr();
79
80   GCSPointPtr aNewPoint(new GCS::Point);
81
82   aNewPoint->x = createParameter(theStorage);
83   aNewPoint->y = createParameter(theStorage);
84   if (aPoint2D->isInitialized()) {
85     *(aNewPoint->x) = aPoint2D->x();
86     *(aNewPoint->y) = aPoint2D->y();
87   }
88
89   return EntityWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
90 }
91
92 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
93     AttributePtr theAttribute)
94 {
95   EntityWrapperPtr aResult;
96   if (myStorage)
97     aResult = myStorage->entity(theAttribute);
98   if (!aResult)
99     aResult = createPoint(theAttribute, myStorage);
100   if (!aResult)
101     aResult = createScalar(theAttribute, myStorage);
102   if (aResult && !myStorage)
103     aResult->setExternal(true);
104   return aResult;
105 }