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 <ModelAPI_AttributeInteger.h>
34 #include <SketchPlugin_BSpline.h>
35 #include <SketchPlugin_ConstraintAngle.h>
36 #include <SketchPlugin_MultiRotation.h>
38 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(
39 PlaneGCSSolver_Storage* theStorage)
40 : PlaneGCSSolver_EntityBuilder(theStorage)
44 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(const StoragePtr& theStorage)
45 : PlaneGCSSolver_EntityBuilder(
46 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(theStorage).get())
50 static double* createParameter(PlaneGCSSolver_Storage* theStorage)
52 return theStorage ? theStorage->createParameter() : (new double(0));
55 static EntityWrapperPtr createBoolean(const AttributePtr& theAttribute)
57 BooleanWrapperPtr aWrapper;
58 AttributeBooleanPtr aBoolean =
59 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttribute);
61 aWrapper = BooleanWrapperPtr(new PlaneGCSSolver_BooleanWrapper(aBoolean->value()));
65 static EntityWrapperPtr createScalar(const AttributePtr& theAttribute,
66 PlaneGCSSolver_Storage* theStorage)
69 AttributeDoublePtr aDouble = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
71 aValue = aDouble->isInitialized() ? aDouble->value() : 0.0;
73 AttributeIntegerPtr anInt = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
75 aValue = anInt->isInitialized() ? anInt->value() : 0.0;
77 return EntityWrapperPtr();
80 ScalarWrapperPtr aWrapper;
81 // following attributes should be converted from degrees to radians
82 // - value of the Angle constraint
83 // - angle of the Multi-Rotation constraint
84 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
85 if ((theAttribute->id() == SketchPlugin_Constraint::VALUE() &&
86 anOwner->getKind() == SketchPlugin_ConstraintAngle::ID()) ||
87 (theAttribute->id() == SketchPlugin_MultiRotation::ANGLE_ID() &&
88 anOwner->getKind() == SketchPlugin_MultiRotation::ID()))
89 aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_AngleWrapper(createParameter(theStorage)));
90 else if (anOwner->getKind() == SketchPlugin_BSpline::ID() &&
91 theAttribute->id() == SketchPlugin_BSpline::DEGREE_ID())
92 // Degree of B-spline is not processed by the solver
93 aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(nullptr)));
95 aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
97 aWrapper->setValue(aValue);
101 static EntityWrapperPtr createScalarArray(const AttributePtr& theAttribute,
102 PlaneGCSSolver_Storage* theStorage)
104 AttributeDoubleArrayPtr anArray =
105 std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttribute);
106 if (!anArray || !anArray->isInitialized())
107 return EntityWrapperPtr();
109 PlaneGCSSolver_Storage* aStorage = theStorage;
110 // Weights of B-spline curve are not processed by the solver
111 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
112 if (anOwner->getKind() == SketchPlugin_BSpline::ID() &&
113 theAttribute->id() == SketchPlugin_BSpline::WEIGHTS_ID())
116 int aSize = anArray->size();
117 GCS::VEC_pD aParameters;
118 aParameters.reserve(aSize);
119 for (int index = 0; index < aSize; ++index) {
120 double* aParam = createParameter(aStorage);
121 *aParam = anArray->value(index);
122 aParameters.push_back(aParam);
125 return EntityWrapperPtr(new PlaneGCSSolver_ScalarArrayWrapper(aParameters));
128 static PointWrapperPtr createPoint(const GeomPnt2dPtr& thePoint, PlaneGCSSolver_Storage* theStorage)
130 GCSPointPtr aNewPoint(new GCS::Point);
132 aNewPoint->x = createParameter(theStorage);
133 aNewPoint->y = createParameter(theStorage);
135 *(aNewPoint->x) = thePoint->x();
136 *(aNewPoint->y) = thePoint->y();
139 return PointWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
142 static EntityWrapperPtr createPoint(const AttributePtr& theAttribute,
143 PlaneGCSSolver_Storage* theStorage)
145 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
146 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
148 return EntityWrapperPtr();
151 if (aPoint2D->isInitialized())
152 aPnt = aPoint2D->pnt();
154 return createPoint(aPnt, theStorage);
157 static EntityWrapperPtr createPointArray(const AttributePtr& theAttribute,
158 PlaneGCSSolver_Storage* theStorage)
160 std::shared_ptr<GeomDataAPI_Point2DArray> aPointArray =
161 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
163 return EntityWrapperPtr();
165 int aSize = aPointArray->size();
167 std::vector<PointWrapperPtr> aPointWrappers;
168 aPointWrappers.reserve(aSize);
169 for (int index = 0; index < aSize; ++index)
170 aPointWrappers.push_back(createPoint(aPointArray->pnt(index), theStorage));
172 return EntityWrapperPtr(new PlaneGCSSolver_PointArrayWrapper(aPointWrappers));
175 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
176 AttributePtr theAttribute)
178 EntityWrapperPtr aResult;
180 aResult = myStorage->entity(theAttribute);
182 aResult = createPoint(theAttribute, myStorage);
184 aResult = createScalar(theAttribute, myStorage);
186 aResult = createBoolean(theAttribute);
188 aResult = createPointArray(theAttribute, myStorage);
190 aResult = createScalarArray(theAttribute, myStorage);
191 if (aResult && !myStorage)
192 aResult->setExternal(true);