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