Salome HOME
SketchSolver Refactoring: Eliminate SolveSpace as a sketch solver.
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_AttributeBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_AttributeBuilder.cpp
4 // Created: 10 Feb 2017
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_AngleWrapper.h>
8 #include <PlaneGCSSolver_AttributeBuilder.h>
9 #include <PlaneGCSSolver_PointWrapper.h>
10 #include <PlaneGCSSolver_ScalarWrapper.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <SketchPlugin_ConstraintAngle.h>
15 #include <SketchPlugin_MultiRotation.h>
16
17 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(
18     PlaneGCSSolver_Storage* theStorage)
19   : PlaneGCSSolver_EntityBuilder(theStorage)
20 {
21 }
22
23 PlaneGCSSolver_AttributeBuilder::PlaneGCSSolver_AttributeBuilder(const StoragePtr& theStorage)
24   : PlaneGCSSolver_EntityBuilder(
25         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(theStorage).get())
26 {
27 }
28
29 static double* createParameter(PlaneGCSSolver_Storage* theStorage)
30 {
31   return theStorage ? theStorage->createParameter() : (new double(0));
32 }
33
34 static EntityWrapperPtr createScalar(const AttributePtr&     theAttribute,
35                                      PlaneGCSSolver_Storage* theStorage)
36 {
37   AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
38   if (!aScalar)
39     return EntityWrapperPtr();
40
41   ScalarWrapperPtr aWrapper;
42   // following attributes should be converted from degrees to radians
43   //  - value of the Angle constraint
44   //  - angle of the Multi-Rotation constraint
45   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
46   if ((theAttribute->id() == SketchPlugin_Constraint::VALUE() &&
47       anOwner->getKind() == SketchPlugin_ConstraintAngle::ID()) ||
48      (theAttribute->id() == SketchPlugin_MultiRotation::ANGLE_ID() &&
49       anOwner->getKind() == SketchPlugin_MultiRotation::ID()))
50     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_AngleWrapper(createParameter(theStorage)));
51   else
52     aWrapper = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(createParameter(theStorage)));
53
54   aWrapper->setValue(aScalar->value());
55   return aWrapper;
56 }
57
58 static EntityWrapperPtr createPoint(const AttributePtr&     theAttribute,
59                                     PlaneGCSSolver_Storage* theStorage)
60 {
61   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
62       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
63   if (!aPoint2D)
64     return EntityWrapperPtr();
65
66   GCSPointPtr aNewPoint(new GCS::Point);
67
68   aNewPoint->x = createParameter(theStorage);
69   *(aNewPoint->x) = aPoint2D->x();
70   aNewPoint->y = createParameter(theStorage);
71   *(aNewPoint->y) = aPoint2D->y();
72
73   return EntityWrapperPtr(new PlaneGCSSolver_PointWrapper(aNewPoint));
74 }
75
76 EntityWrapperPtr PlaneGCSSolver_AttributeBuilder::createAttribute(
77     AttributePtr theAttribute)
78 {
79   EntityWrapperPtr aResult;
80   if (myStorage)
81     aResult = myStorage->entity(theAttribute);
82   if (!aResult)
83     aResult = createPoint(theAttribute, myStorage);
84   if (!aResult)
85     aResult = createScalar(theAttribute, myStorage);
86   if (aResult && !myStorage)
87     aResult->setExternal(true);
88   return aResult;
89 }
90
91 const std::list<GCSConstraintPtr>& PlaneGCSSolver_AttributeBuilder::constraints() const
92 {
93   static std::list<GCSConstraintPtr> aList;
94   return aList;
95 }