]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Rectangle.cpp
Salome HOME
move rectangle plugin from python addons to C++.
[modules/shaper.git] / src / SketchAPI / SketchAPI_Rectangle.cpp
1 // Copyright (C) 2014-2020  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_Rectangle.h"
21 //--------------------------------------------------------------------------------------
22 #include <GeomAPI_Pnt2d.h>
23 //--------------------------------------------------------------------------------------
24 #include <ModelHighAPI_Selection.h>
25 #include <ModelHighAPI_Tools.h>
26 #include <ModelHighAPI_Dumper.h>
27
28 //--------------------------------------------------------------------------------------
29 SketchAPI_Rectangle::SketchAPI_Rectangle(
30     const std::shared_ptr<ModelAPI_Feature> & theFeature)
31   : SketchAPI_SketchEntity(theFeature)
32 {
33   initialize();
34 }
35
36 SketchAPI_Rectangle::SketchAPI_Rectangle(
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_Rectangle::SketchAPI_Rectangle(
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_Rectangle::~SketchAPI_Rectangle()
58 {
59 }
60
61 //--------------------------------------------------------------------------------------
62 void SketchAPI_Rectangle::setByCoordinates(
63     double theX1, double theY1, double theX2, double theY2)
64 {
65   fillAttribute(startPoint(), theX1, theY1);
66   fillAttribute(endPoint(), theX2, theY2);
67
68   execute();
69 }
70
71 void SketchAPI_Rectangle::setByPoints(
72     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
73     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
74 {
75   fillAttribute(theStartPoint, startPoint());
76   fillAttribute(theEndPoint, endPoint());
77
78   execute();
79 }
80
81 //--------------------------------------------------------------------------------------
82
83 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Rectangle::lines() const
84 {
85   std::list<FeaturePtr> aFeatures;
86   std::list<ObjectPtr> aList = linesList()->list();
87   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
88   for (; anIt != aList.end(); ++anIt)
89     aFeatures.push_back(ModelAPI_Feature::feature(*anIt));
90   return SketchAPI_SketchEntity::wrap(aFeatures);
91 }
92
93 //==================================================================================================
94 void SketchAPI_Rectangle::dump(ModelHighAPI_Dumper& theDumper) const
95 {
96
97  FeaturePtr aBase = feature();
98
99   /// do not dump sub-features eg: lines and lines constraints
100   AttributeRefListPtr noToDumpList =  aBase->reflist(SketchPlugin_Rectangle::NOT_TO_DUMP_LIST_ID());
101   for( int i = 0; i < noToDumpList->size(); i++){
102     theDumper.doNotDumpFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(noToDumpList->object(i)));
103   }
104
105   if (isCopy())
106     return; // no need to dump copied feature
107
108   const std::string& aSketchName = theDumper.parentName(aBase);
109
110   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
111   if (anExternal->context()) {
112     // rectangle is external
113     theDumper << aBase << " = " << aSketchName << ".addRectangle(" << anExternal << ")" << std::endl;
114   } else {
115     // rectangle given by start and end points
116     theDumper << aBase << " = " << aSketchName << ".addRectangle("
117               << startPoint() << ", " << endPoint() << ")" << std::endl;
118   }
119   // dump "auxiliary" flag if necessary
120   SketchAPI_SketchEntity::dump(theDumper);
121
122 }