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