Salome HOME
7ccedd950459c17875e251758b919f9c7ca659f3
[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_PointArrayWrapper.h>
23 #include <PlaneGCSSolver_PointWrapper.h>
24 #include <PlaneGCSSolver_ScalarWrapper.h>
25 #include <PlaneGCSSolver_ScalarArrayWrapper.h>
26 #include <PlaneGCSSolver_BooleanWrapper.h>
27
28 #include <GeomAPI_Pnt2d.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomDataAPI_Point2DArray.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_AttributeDoubleArray.h>
33 #include <SketchPlugin_ConstraintAngle.h>
34 #include <SketchPlugin_MultiRotation.h>
35
36 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(
37     PlaneGCSSolver_Storage* theStorage)
38   : PlaneGCSSolver_EntityBuilder(theStorage)
39 {
40 }
41
42 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(const StoragePtr& theStorage)
43   : PlaneGCSSolver_EntityBuilder(
44         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(theStorage).get())
45 {
46 }
47
48 static double* createParameter(PlaneGCSSolver_Storage* theStorage)
49 {
50   return theStorage ? theStorage->createParameter() : (new double(0));
51 }
52
53 static EntityWrapperPtr createBoolean(const AttributePtr& theAttribute)
54 {
55   BooleanWrapperPtr aWrapper;
56   AttributeBooleanPtr aBoolean =
57       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttribute);
58   if (aBoolean)
59     aWrapper = BooleanWrapperPtr(new PlaneGCSSolver_BooleanWrapper(aBoolean->value()));
60   return aWrapper;
61 }
62
63 static EntityWrapperPtr createScalar(const AttributePtr&     theAttribute,
64                                      PlaneGCSSolver_Storage* theStorage)
65 {
66   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
67   if (!aScalar)
68     return EntityWrapperPtr();
69
70   ScalarWrapperPtr aWrapper;
71   // following attributes should be converted from degrees to radians
72   //  - value of the Angle constraint
73   //  - angle of the Multi-Rotation constraint
74   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
75   if ((theAttribute->id() == SketchPlugin_Constraint::VALUE() &&
76       anOwner->getKind() == SketchPlugin_ConstraintAngle::ID()) ||
77      (theAttribute->id() == SketchPlugin_MultiRotation::ANGLE_ID() &&
78       anOwner->getKind() == SketchPlugin_MultiRotation::ID()))
79     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_AngleWrapper(createParameter(theStorage)));
80   else
81     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
82
83   if (aScalar->isInitialized())
84     aWrapper->setValue(aScalar->value());
85   return aWrapper;
86 }
87
88 static EntityWrapperPtr createScalarArray(const AttributePtr&     theAttribute,
89                                           PlaneGCSSolver_Storage* theStorage)
90 {
91   AttributeDoubleArrayPtr anArray =
92       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttribute);
93   if (!anArray || !anArray->isInitialized())
94     return EntityWrapperPtr();
95
96   int aSize = anArray->size();
97   GCS::VEC_pD aParameters;
98   aParameters.reserve(aSize);
99   for (int index = 0; index < aSize; ++index) {
100     double* aParam = createParameter(theStorage);
101     *aParam = anArray->value(index);
102     aParameters.push_back(aParam);
103   }
104
105   return EntityWrapperPtr(new PlaneGCSSolver_ScalarArrayWrapper(aParameters));
106 }
107
108 static PointWrapperPtr createPoint(const GeomPnt2dPtr& thePoint, PlaneGCSSolver_Storage* theStorage)
109 {
110   GCSPointPtr aNewPoint(new GCS::Point);
111
112   aNewPoint->x = createParameter(theStorage);
113   aNewPoint->y = createParameter(theStorage);
114   if (thePoint) {
115     *(aNewPoint->x) = thePoint->x();
116     *(aNewPoint->y) = thePoint->y();
117   }
118
119   return PointWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
120 }
121
122 static EntityWrapperPtr createPoint(const AttributePtr&     theAttribute,
123                                     PlaneGCSSolver_Storage* theStorage)
124 {
125   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
126       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
127   if (!aPoint2D)
128     return EntityWrapperPtr();
129
130   GeomPnt2dPtr aPnt;
131   if (aPoint2D->isInitialized())
132     aPnt = aPoint2D->pnt();
133
134   return createPoint(aPnt, theStorage);
135 }
136
137 static EntityWrapperPtr createPointArray(const AttributePtr& theAttribute,
138                                          PlaneGCSSolver_Storage* theStorage)
139 {
140   std::shared_ptr<GeomDataAPI_Point2DArray> aPointArray =
141       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
142   if (!aPointArray)
143     return EntityWrapperPtr();
144
145   int aSize = aPointArray->size();
146
147   std::vector<PointWrapperPtr> aPointWrappers;
148   aPointWrappers.reserve(aSize);
149   for (int index = 0; index < aSize; ++index)
150     aPointWrappers.push_back(createPoint(aPointArray->pnt(index), theStorage));
151
152   return EntityWrapperPtr(new PlaneGCSSolver_PointArrayWrapper(aPointWrappers));
153 }
154
155 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
156     AttributePtr theAttribute)
157 {
158   EntityWrapperPtr aResult;
159   if (myStorage)
160     aResult = myStorage->entity(theAttribute);
161   if (!aResult)
162     aResult = createPoint(theAttribute, myStorage);
163   if (!aResult)
164     aResult = createScalar(theAttribute, myStorage);
165   if (!aResult)
166     aResult = createBoolean(theAttribute);
167   if (!aResult)
168     aResult = createPointArray(theAttribute, myStorage);
169   if (!aResult)
170     aResult = createScalarArray(theAttribute, myStorage);
171   if (aResult && !myStorage)
172     aResult->setExternal(true);
173   return aResult;
174 }