Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchAPI / SketchAPI_Line.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Line.h"
22 //--------------------------------------------------------------------------------------
23 #include <GeomAPI_Pnt2d.h>
24 //--------------------------------------------------------------------------------------
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
28 //--------------------------------------------------------------------------------------
29 SketchAPI_Line::SketchAPI_Line(
30     const std::shared_ptr<ModelAPI_Feature> & theFeature)
31 : SketchAPI_SketchEntity(theFeature)
32 {
33   initialize();
34 }
35
36 SketchAPI_Line::SketchAPI_Line(
37     const std::shared_ptr<ModelAPI_Feature> & theFeature,
38     double theX1, double theY1, double theX2, double theY2)
39 : SketchAPI_SketchEntity(theFeature)
40 {
41   if (initialize()) {
42     setByCoordinates(theX1, theY1, theX2, theY2);
43   }
44 }
45
46 SketchAPI_Line::SketchAPI_Line(
47     const std::shared_ptr<ModelAPI_Feature> & theFeature,
48     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
49     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
50 : SketchAPI_SketchEntity(theFeature)
51 {
52   if (initialize()) {
53     setByPoints(theStartPoint, theEndPoint);
54   }
55 }
56
57 SketchAPI_Line::SketchAPI_Line(
58     const std::shared_ptr<ModelAPI_Feature> & theFeature,
59     const ModelHighAPI_Selection & theExternal )
60 : SketchAPI_SketchEntity(theFeature)
61 {
62   if (initialize()) {
63     setByExternal(theExternal);
64   }
65 }
66
67 SketchAPI_Line::SketchAPI_Line(
68     const std::shared_ptr<ModelAPI_Feature> & theFeature,
69     const std::string & theExternalName )
70 : SketchAPI_SketchEntity(theFeature)
71 {
72   if (initialize()) {
73     setByExternalName(theExternalName);
74   }
75 }
76
77 SketchAPI_Line::~SketchAPI_Line()
78 {
79
80 }
81
82 //--------------------------------------------------------------------------------------
83 void SketchAPI_Line::setByCoordinates(
84     double theX1, double theY1, double theX2, double theY2)
85 {
86   fillAttribute(startPoint(), theX1, theY1);
87   fillAttribute(endPoint(), theX2, theY2);
88
89   execute();
90 }
91
92 void SketchAPI_Line::setByPoints(
93     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
94     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
95 {
96   fillAttribute(theStartPoint, startPoint());
97   fillAttribute(theEndPoint, endPoint());
98
99   execute();
100 }
101
102 void SketchAPI_Line::setByExternal(const ModelHighAPI_Selection & theExternal)
103 {
104   fillAttribute(theExternal, external());
105
106   execute();
107 }
108
109 void SketchAPI_Line::setByExternalName(const std::string & theExternalName)
110 {
111   fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
112
113   execute();
114 }
115
116 //--------------------------------------------------------------------------------------
117 void SketchAPI_Line::setStartPoint(double theX, double theY)
118 {
119   fillAttribute(startPoint(), theX, theY);
120
121   execute();
122 }
123 void SketchAPI_Line::setStartPoint(const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
124 {
125   fillAttribute(thePoint, startPoint());
126
127   execute();
128 }
129 void SketchAPI_Line::setEndPoint(double theX, double theY)
130 {
131   fillAttribute(endPoint(), theX, theY);
132
133   execute();
134 }
135 void SketchAPI_Line::setEndPoint(const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
136 {
137   fillAttribute(thePoint, endPoint());
138
139   execute();
140 }
141
142 //--------------------------------------------------------------------------------------
143
144 void SketchAPI_Line::dump(ModelHighAPI_Dumper& theDumper) const
145 {
146   if (isCopy())
147     return; // no need to dump copied feature
148
149   FeaturePtr aBase = feature();
150   const std::string& aSketchName = theDumper.parentName(aBase);
151
152   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
153   if (anExternal->context()) {
154     // line is external
155     theDumper << aBase << " = " << aSketchName << ".addLine(" << anExternal << ")" << std::endl;
156   } else {
157     // segment given by its points
158     theDumper << aBase << " = " << aSketchName << ".addLine("
159               << startPoint() << ", " << endPoint() << ")" << std::endl;
160   }
161   // dump "auxiliary" flag if necessary
162   SketchAPI_SketchEntity::dump(theDumper);
163 }