1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
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>
36 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(
37 PlaneGCSSolver_Storage* theStorage)
38 : PlaneGCSSolver_EntityBuilder(theStorage)
42 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(const StoragePtr& theStorage)
43 : PlaneGCSSolver_EntityBuilder(
44 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(theStorage).get())
48 static double* createParameter(PlaneGCSSolver_Storage* theStorage)
50 return theStorage ? theStorage->createParameter() : (new double(0));
53 static EntityWrapperPtr createBoolean(const AttributePtr& theAttribute)
55 BooleanWrapperPtr aWrapper;
56 AttributeBooleanPtr aBoolean =
57 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttribute);
59 aWrapper = BooleanWrapperPtr(new PlaneGCSSolver_BooleanWrapper(aBoolean->value()));
63 static EntityWrapperPtr createScalar(const AttributePtr& theAttribute,
64 PlaneGCSSolver_Storage* theStorage)
66 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
68 return EntityWrapperPtr();
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)));
81 aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
83 if (aScalar->isInitialized())
84 aWrapper->setValue(aScalar->value());
88 static EntityWrapperPtr createScalarArray(const AttributePtr& theAttribute,
89 PlaneGCSSolver_Storage* theStorage)
91 AttributeDoubleArrayPtr anArray =
92 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttribute);
93 if (!anArray || !anArray->isInitialized())
94 return EntityWrapperPtr();
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);
105 return EntityWrapperPtr(new PlaneGCSSolver_ScalarArrayWrapper(aParameters));
108 static PointWrapperPtr createPoint(const GeomPnt2dPtr& thePoint, PlaneGCSSolver_Storage* theStorage)
110 GCSPointPtr aNewPoint(new GCS::Point);
112 aNewPoint->x = createParameter(theStorage);
113 aNewPoint->y = createParameter(theStorage);
115 *(aNewPoint->x) = thePoint->x();
116 *(aNewPoint->y) = thePoint->y();
119 return PointWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
122 static EntityWrapperPtr createPoint(const AttributePtr& theAttribute,
123 PlaneGCSSolver_Storage* theStorage)
125 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
128 return EntityWrapperPtr();
131 if (aPoint2D->isInitialized())
132 aPnt = aPoint2D->pnt();
134 return createPoint(aPnt, theStorage);
137 static EntityWrapperPtr createPointArray(const AttributePtr& theAttribute,
138 PlaneGCSSolver_Storage* theStorage)
140 std::shared_ptr<GeomDataAPI_Point2DArray> aPointArray =
141 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
143 return EntityWrapperPtr();
145 int aSize = aPointArray->size();
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));
152 return EntityWrapperPtr(new PlaneGCSSolver_PointArrayWrapper(aPointWrappers));
155 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
156 AttributePtr theAttribute)
158 EntityWrapperPtr aResult;
160 aResult = myStorage->entity(theAttribute);
162 aResult = createPoint(theAttribute, myStorage);
164 aResult = createScalar(theAttribute, myStorage);
166 aResult = createBoolean(theAttribute);
168 aResult = createPointArray(theAttribute, myStorage);
170 aResult = createScalarArray(theAttribute, myStorage);
171 if (aResult && !myStorage)
172 aResult->setExternal(true);