Salome HOME
Issue #17347: B-Splines in Sketcher
[modules/shaper.git] / src / SketchAPI / SketchAPI_Projection.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 "SketchAPI_Projection.h"
21
22 #include <SketchPlugin_Line.h>
23 #include <SketchPlugin_BSpline.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Ellipse.h>
26 #include <SketchPlugin_EllipticArc.h>
27 #include <SketchPlugin_Point.h>
28
29 #include <SketchAPI_Arc.h>
30 #include <SketchAPI_BSpline.h>
31 #include <SketchAPI_Circle.h>
32 #include <SketchAPI_Ellipse.h>
33 #include <SketchAPI_EllipticArc.h>
34 #include <SketchAPI_Line.h>
35 #include <SketchAPI_Point.h>
36
37 #include <ModelHighAPI_Dumper.h>
38 #include <ModelHighAPI_Selection.h>
39 #include <ModelHighAPI_Tools.h>
40 //--------------------------------------------------------------------------------------
41 SketchAPI_Projection::SketchAPI_Projection(
42     const std::shared_ptr<ModelAPI_Feature> & theFeature)
43 : SketchAPI_SketchEntity(theFeature)
44 {
45   initialize();
46 }
47
48 SketchAPI_Projection::SketchAPI_Projection(
49     const std::shared_ptr<ModelAPI_Feature> & theFeature,
50     const ModelHighAPI_Selection & theExternalFeature )
51 : SketchAPI_SketchEntity(theFeature)
52 {
53   if (initialize()) {
54     setExternalFeature(theExternalFeature);
55   }
56 }
57
58 SketchAPI_Projection::SketchAPI_Projection(
59     const std::shared_ptr<ModelAPI_Feature> & theFeature,
60     const std::string & theExternalName)
61 : SketchAPI_SketchEntity(theFeature)
62 {
63   if (initialize()) {
64     setByExternalName(theExternalName);
65   }
66 }
67
68 SketchAPI_Projection::~SketchAPI_Projection()
69 {
70
71 }
72
73 //--------------------------------------------------------------------------------------
74 void SketchAPI_Projection::setExternalFeature(const ModelHighAPI_Selection & theExternalFeature)
75 {
76   fillAttribute(theExternalFeature, externalFeature());
77
78   execute(true);
79 }
80
81 void SketchAPI_Projection::setByExternalName(const std::string& theExternalName)
82 {
83   setExternalFeature(ModelHighAPI_Selection("EDGE", theExternalName));
84 }
85
86 void SketchAPI_Projection::setIncludeToResult(bool theKeepResult)
87 {
88   fillAttribute(theKeepResult, includeToResult());
89   execute(true);
90 }
91
92 //--------------------------------------------------------------------------------------
93 std::shared_ptr<SketchAPI_SketchEntity> SketchAPI_Projection::createdFeature() const
94 {
95   AttributeRefAttrPtr aProjectedRefAttr = projectedFeature();
96   FeaturePtr aProjectedFeature = ModelAPI_Feature::feature(aProjectedRefAttr->object());
97   std::shared_ptr<SketchAPI_SketchEntity> anEntity;
98
99   if(!aProjectedFeature.get()) {
100     return anEntity;
101   }
102
103   if (aProjectedFeature->getKind() == SketchPlugin_Line::ID())
104     anEntity.reset(new SketchAPI_Line(aProjectedFeature));
105   else if (aProjectedFeature->getKind() == SketchPlugin_Circle::ID())
106     anEntity.reset(new SketchAPI_Circle(aProjectedFeature));
107   else if (aProjectedFeature->getKind() == SketchPlugin_Arc::ID())
108     anEntity.reset(new SketchAPI_Arc(aProjectedFeature));
109   else if (aProjectedFeature->getKind() == SketchPlugin_Ellipse::ID())
110     anEntity.reset(new SketchAPI_Ellipse(aProjectedFeature));
111   else if (aProjectedFeature->getKind() == SketchPlugin_EllipticArc::ID())
112     anEntity.reset(new SketchAPI_EllipticArc(aProjectedFeature));
113   else if (aProjectedFeature->getKind() == SketchPlugin_BSpline::ID())
114     anEntity.reset(new SketchAPI_BSpline(aProjectedFeature));
115   else if (aProjectedFeature->getKind() == SketchPlugin_Point::ID())
116     anEntity.reset(new SketchAPI_Point(aProjectedFeature));
117
118   return anEntity;
119 }
120
121 //--------------------------------------------------------------------------------------
122
123 void SketchAPI_Projection::dump(ModelHighAPI_Dumper& theDumper) const
124 {
125   FeaturePtr aBase = feature();
126   const std::string& aSketchName = theDumper.parentName(aBase);
127
128   AttributeSelectionPtr anExternal = externalFeature();
129   AttributeBooleanPtr isIncludeToRes = includeToResult();
130   theDumper << aBase << " = " << aSketchName << ".addProjection("
131             << anExternal << ", " << isIncludeToRes << ")" << std::endl;
132   // dump "auxiliary" flag if necessary
133   SketchAPI_SketchEntity::dump(theDumper);
134
135   // Dump created line feature
136   AttributeRefAttrPtr aProjectedRefAttr = projectedFeature();
137   FeaturePtr aProjectedFeature = ModelAPI_Feature::feature(aProjectedRefAttr->object());
138   theDumper << aProjectedFeature << " = "
139     << theDumper.name(aBase) << ".createdFeature()" << std::endl;
140 }