Salome HOME
Issue #2024: Redesign of circle and arc of circle
[modules/shaper.git] / src / SketchAPI / SketchAPI_Line.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : SketchAPI_Line.cpp
3 // Purpose:
4 //
5 // History:
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "SketchAPI_Line.h"
10 //--------------------------------------------------------------------------------------
11 #include <GeomAPI_Pnt2d.h>
12 //--------------------------------------------------------------------------------------
13 #include <ModelHighAPI_Dumper.h>
14 #include <ModelHighAPI_Selection.h>
15 #include <ModelHighAPI_Tools.h>
16 //--------------------------------------------------------------------------------------
17 SketchAPI_Line::SketchAPI_Line(
18     const std::shared_ptr<ModelAPI_Feature> & theFeature)
19 : SketchAPI_SketchEntity(theFeature)
20 {
21   initialize();
22 }
23
24 SketchAPI_Line::SketchAPI_Line(
25     const std::shared_ptr<ModelAPI_Feature> & theFeature,
26     double theX1, double theY1, double theX2, double theY2)
27 : SketchAPI_SketchEntity(theFeature)
28 {
29   if (initialize()) {
30     setByCoordinates(theX1, theY1, theX2, theY2);
31   }
32 }
33
34 SketchAPI_Line::SketchAPI_Line(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature,
36     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
37     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
38 : SketchAPI_SketchEntity(theFeature)
39 {
40   if (initialize()) {
41     setByPoints(theStartPoint, theEndPoint);
42   }
43 }
44
45 SketchAPI_Line::SketchAPI_Line(
46     const std::shared_ptr<ModelAPI_Feature> & theFeature,
47     const ModelHighAPI_Selection & theExternal )
48 : SketchAPI_SketchEntity(theFeature)
49 {
50   if (initialize()) {
51     setByExternal(theExternal);
52   }
53 }
54
55 SketchAPI_Line::SketchAPI_Line(
56     const std::shared_ptr<ModelAPI_Feature> & theFeature,
57     const std::string & theExternalName )
58 : SketchAPI_SketchEntity(theFeature)
59 {
60   if (initialize()) {
61     setByExternalName(theExternalName);
62   }
63 }
64
65 SketchAPI_Line::~SketchAPI_Line()
66 {
67
68 }
69
70 //--------------------------------------------------------------------------------------
71 void SketchAPI_Line::setByCoordinates(
72     double theX1, double theY1, double theX2, double theY2)
73 {
74   fillAttribute(startPoint(), theX1, theY1);
75   fillAttribute(endPoint(), theX2, theY2);
76
77   execute();
78 }
79
80 void SketchAPI_Line::setByPoints(
81     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
82     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
83 {
84   fillAttribute(theStartPoint, startPoint());
85   fillAttribute(theEndPoint, endPoint());
86
87   execute();
88 }
89
90 void SketchAPI_Line::setByExternal(const ModelHighAPI_Selection & theExternal)
91 {
92   fillAttribute(theExternal, external());
93
94   execute();
95 }
96
97 void SketchAPI_Line::setByExternalName(const std::string & theExternalName)
98 {
99   fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
100
101   execute();
102 }
103
104 //--------------------------------------------------------------------------------------
105 void SketchAPI_Line::setStartPoint(double theX, double theY)
106 {
107   fillAttribute(startPoint(), theX, theY);
108
109   execute();
110 }
111 void SketchAPI_Line::setStartPoint(const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
112 {
113   fillAttribute(thePoint, startPoint());
114
115   execute();
116 }
117 void SketchAPI_Line::setEndPoint(double theX, double theY)
118 {
119   fillAttribute(endPoint(), theX, theY);
120
121   execute();
122 }
123 void SketchAPI_Line::setEndPoint(const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
124 {
125   fillAttribute(thePoint, endPoint());
126
127   execute();
128 }
129
130 //--------------------------------------------------------------------------------------
131
132 void SketchAPI_Line::dump(ModelHighAPI_Dumper& theDumper) const
133 {
134   if (isCopy())
135     return; // no need to dump copied feature
136
137   FeaturePtr aBase = feature();
138   const std::string& aSketchName = theDumper.parentName(aBase);
139
140   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
141   if (anExternal->context()) {
142     // line is external
143     theDumper << aBase << " = " << aSketchName << ".addLine(" << anExternal << ")" << std::endl;
144   } else {
145     // segment given by its points
146     theDumper << aBase << " = " << aSketchName << ".addLine("
147               << startPoint() << ", " << endPoint() << ")" << std::endl;
148   }
149   // dump "auxiliary" flag if necessary
150   SketchAPI_SketchEntity::dump(theDumper);
151 }