Salome HOME
Task 2.11. Ability to impose a midpoint on an arc (refers issue #3002)
[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_PointWrapper.h>
23 #include <PlaneGCSSolver_ScalarWrapper.h>
24 #include <PlaneGCSSolver_BooleanWrapper.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 createBoolean(const AttributePtr& theAttribute)
49 {
50   BooleanWrapperPtr aWrapper;
51   AttributeBooleanPtr aBoolean =
52       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttribute);
53   if (aBoolean)
54     aWrapper = BooleanWrapperPtr(new PlaneGCSSolver_BooleanWrapper(aBoolean->value()));
55   return aWrapper;
56 }
57
58 static EntityWrapperPtr createScalar(const AttributePtr&     theAttribute,
59                                      PlaneGCSSolver_Storage* theStorage)
60 {
61   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
62   if (!aScalar)
63     return EntityWrapperPtr();
64
65   ScalarWrapperPtr aWrapper;
66   // following attributes should be converted from degrees to radians
67   //  - value of the Angle constraint
68   //  - angle of the Multi-Rotation constraint
69   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
70   if ((theAttribute->id() == SketchPlugin_Constraint::VALUE() &&
71       anOwner->getKind() == SketchPlugin_ConstraintAngle::ID()) ||
72      (theAttribute->id() == SketchPlugin_MultiRotation::ANGLE_ID() &&
73       anOwner->getKind() == SketchPlugin_MultiRotation::ID()))
74     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_AngleWrapper(createParameter(theStorage)));
75   else
76     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
77
78   if (aScalar->isInitialized())
79     aWrapper->setValue(aScalar->value());
80   return aWrapper;
81 }
82
83 static EntityWrapperPtr createPoint(const AttributePtr&     theAttribute,
84                                     PlaneGCSSolver_Storage* theStorage)
85 {
86   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
87       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
88   if (!aPoint2D)
89     return EntityWrapperPtr();
90
91   GCSPointPtr aNewPoint(new GCS::Point);
92
93   aNewPoint->x = createParameter(theStorage);
94   aNewPoint->y = createParameter(theStorage);
95   if (aPoint2D->isInitialized()) {
96     *(aNewPoint->x) = aPoint2D->x();
97     *(aNewPoint->y) = aPoint2D->y();
98   }
99
100   return EntityWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
101 }
102
103 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
104     AttributePtr theAttribute)
105 {
106   EntityWrapperPtr aResult;
107   if (myStorage)
108     aResult = myStorage->entity(theAttribute);
109   if (!aResult)
110     aResult = createPoint(theAttribute, myStorage);
111   if (!aResult)
112     aResult = createScalar(theAttribute, myStorage);
113   if (!aResult)
114     aResult = createBoolean(theAttribute);
115   if (aResult && !myStorage)
116     aResult->setExternal(true);
117   return aResult;
118 }