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 class ArrayAttribute {
106 ArrayAttribute(AttributePtr theAttribute)
108 myDouble = std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttribute);
109 myInteger = std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttribute);
112 bool isInitialized() const
114 return (myDouble && myDouble->isInitialized()) || (myInteger && myInteger->isInitialized());
119 return myDouble.get() ? myDouble->size() : myInteger->size();
122 double value(const int theIndex) const
124 return myDouble.get() ? myDouble->value(theIndex) : myInteger->value(theIndex);
128 AttributeDoubleArrayPtr myDouble;
129 AttributeIntArrayPtr myInteger;
130 } anArray(theAttribute);
132 if (!anArray.isInitialized())
133 return EntityWrapperPtr();
135 PlaneGCSSolver_Storage* aStorage = theStorage;
136 // Weights, knots and multiplicities of B-spline curve are not processed by the solver
137 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
138 if (anOwner->getKind() == SketchPlugin_BSpline::ID() &&
139 (theAttribute->id() == SketchPlugin_BSpline::WEIGHTS_ID() ||
140 theAttribute->id() == SketchPlugin_BSpline::KNOTS_ID() ||
141 theAttribute->id() == SketchPlugin_BSpline::MULTS_ID()))
144 int aSize = anArray.size();
145 GCS::VEC_pD aParameters;
146 aParameters.reserve(aSize);
147 for (int index = 0; index < aSize; ++index) {
148 double* aParam = createParameter(aStorage);
149 *aParam = anArray.value(index);
150 aParameters.push_back(aParam);
153 return EntityWrapperPtr(new PlaneGCSSolver_ScalarArrayWrapper(aParameters));
156 static PointWrapperPtr createPoint(const GeomPnt2dPtr& thePoint, PlaneGCSSolver_Storage* theStorage)
158 GCSPointPtr aNewPoint(new GCS::Point);
160 aNewPoint->x = createParameter(theStorage);
161 aNewPoint->y = createParameter(theStorage);
163 *(aNewPoint->x) = thePoint->x();
164 *(aNewPoint->y) = thePoint->y();
167 return PointWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
170 static EntityWrapperPtr createPoint(const AttributePtr& theAttribute,
171 PlaneGCSSolver_Storage* theStorage)
173 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
174 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
176 return EntityWrapperPtr();
179 if (aPoint2D->isInitialized())
180 aPnt = aPoint2D->pnt();
182 return createPoint(aPnt, theStorage);
185 static EntityWrapperPtr createPointArray(const AttributePtr& theAttribute,
186 PlaneGCSSolver_Storage* theStorage)
188 std::shared_ptr<GeomDataAPI_Point2DArray> aPointArray =
189 std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(theAttribute);
191 return EntityWrapperPtr();
193 int aSize = aPointArray->size();
195 std::vector<PointWrapperPtr> aPointWrappers;
196 aPointWrappers.reserve(aSize);
197 for (int index = 0; index < aSize; ++index)
198 aPointWrappers.push_back(createPoint(aPointArray->pnt(index), theStorage));
200 return EntityWrapperPtr(new PlaneGCSSolver_PointArrayWrapper(aPointWrappers));
203 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
204 AttributePtr theAttribute)
206 EntityWrapperPtr aResult;
208 aResult = myStorage->entity(theAttribute);
210 aResult = createPoint(theAttribute, myStorage);
212 aResult = createScalar(theAttribute, myStorage);
214 aResult = createBoolean(theAttribute);
216 aResult = createPointArray(theAttribute, myStorage);
218 aResult = createScalarArray(theAttribute, myStorage);
219 if (aResult && !myStorage)
220 aResult->setExternal(true);