1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
28 //--------------------------------------------------------------------------------------
29 SketchAPI_Rectangle::SketchAPI_Rectangle(
30 const std::shared_ptr<ModelAPI_Feature> & theFeature)
31 : SketchAPI_SketchEntity(theFeature)
36 SketchAPI_Rectangle::SketchAPI_Rectangle(const std::shared_ptr<ModelAPI_Feature> & theFeature,
37 double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter)
38 : SketchAPI_SketchEntity(theFeature)
41 setByCoordinates(theX1, theY1, theX2, theY2, isFirstPointCenter);
45 SketchAPI_Rectangle::SketchAPI_Rectangle(const std::shared_ptr<ModelAPI_Feature> & theFeature,
46 const std::shared_ptr<GeomAPI_Pnt2d> & theFirstPoint,
47 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint, bool isFirstPointCenter)
48 : SketchAPI_SketchEntity(theFeature)
51 setByPoints(theFirstPoint, theEndPoint, isFirstPointCenter);
55 SketchAPI_Rectangle::~SketchAPI_Rectangle()
59 //--------------------------------------------------------------------------------------
60 void SketchAPI_Rectangle::setByCoordinates(
61 double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter)
63 if(isFirstPointCenter){
64 fillAttribute(centerPoint(), theX1, theY1);
65 double xStart = 2.0*theX1 - theX2;
66 double yStart = 2.0*theY1 - theY2;
67 fillAttribute(startPoint(), xStart, yStart);
70 fillAttribute(startPoint(), theX1, theY1);
72 fillAttribute(endPoint(), theX2, theY2);
76 void SketchAPI_Rectangle::setByPoints(const std::shared_ptr<GeomAPI_Pnt2d> & theFirstPoint,
77 const std::shared_ptr<GeomAPI_Pnt2d> & theSecondPoint, bool isFirstPointCenter)
79 if(isFirstPointCenter){
80 fillAttribute(theFirstPoint, centerPoint());
81 double xStart = 2.0*theFirstPoint->x() - theSecondPoint->x();
82 double yStart = 2.0*theFirstPoint->y() - theSecondPoint->y();
84 std::shared_ptr<GeomAPI_Pnt2d> theStartPoint = std::make_shared<GeomAPI_Pnt2d>(xStart, yStart);
85 fillAttribute(theStartPoint, startPoint());
88 fillAttribute(theFirstPoint, startPoint());
90 fillAttribute(theSecondPoint, endPoint());
94 //--------------------------------------------------------------------------------------
96 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Rectangle::lines() const
98 std::list<FeaturePtr> aFeatures;
99 std::list<ObjectPtr> aList = linesList()->list();
100 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
101 for (; anIt != aList.end(); ++anIt)
102 aFeatures.push_back(ModelAPI_Feature::feature(*anIt));
103 return SketchAPI_SketchEntity::wrap(aFeatures);
106 //==================================================================================================
107 void SketchAPI_Rectangle::dump(ModelHighAPI_Dumper& theDumper) const
110 FeaturePtr aBase = feature();
112 /// do not dump sub-features eg: lines and lines constraints
113 AttributeRefListPtr noToDumpList = aBase->reflist(SketchPlugin_Rectangle::NOT_TO_DUMP_LIST_ID());
114 for( int i = 0; i < noToDumpList->size(); i++){
115 theDumper.doNotDumpFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(noToDumpList->object(i)));
119 return; // no need to dump copied feature
121 const std::string& aSketchName = theDumper.parentName(aBase);
123 FeaturePtr aCenterPointFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aBase->refattr(SketchPlugin_Rectangle::CENTER_REF_ID())->object());
124 if(aCenterPointFeature){
125 // rectangle has center
126 theDumper << aBase << " = " << aSketchName << ".addRectangle("
127 << startPoint() << ", " << centerPoint() << ", 1)" << std::endl;
130 // rectangle given by start and end points
131 theDumper << aBase << " = " << aSketchName << ".addRectangle("
132 << startPoint() << ", " << endPoint() << ")" << std::endl;
134 // dump "auxiliary" flag if necessary
135 SketchAPI_SketchEntity::dump(theDumper);