1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Scale.h"
23 #include <BRepBuilderAPI_Transform.hxx>
24 #include <BRepBuilderAPI_GTransform.hxx>
26 //=================================================================================================
27 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
29 double theScaleFactor)
31 myMethodType = BY_FACTOR;
32 mySourceShape = theSourceShape;
33 myCenterPoint = theCenterPoint;
34 myScaleFactor = theScaleFactor;
37 //=================================================================================================
38 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
39 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
40 double theScaleFactorX,
41 double theScaleFactorY,
42 double theScaleFactorZ)
44 myMethodType = BY_DIMENSIONS;
45 mySourceShape = theSourceShape;
46 myCenterPoint = theCenterPoint;
47 myScaleFactorX = theScaleFactorX;
48 myScaleFactorY = theScaleFactorY;
49 myScaleFactorZ = theScaleFactorZ;
52 //=================================================================================================
53 bool GeomAlgoAPI_Scale::check()
56 myError = "Scale builder :: source shape is not valid.";
60 myError = "Scale builder :: center point is not valid.";
63 switch (myMethodType) {
65 if (fabs(myScaleFactor) < Precision::Confusion()) {
66 myError = "Scale builder :: the scale factor is null.";
72 if (fabs(myScaleFactorX) < Precision::Confusion()) {
73 myError = "Scale builder :: the scale factor in X is null.";
76 if (fabs(myScaleFactorY) < Precision::Confusion()) {
77 myError = "Scale builder :: the scale factor in Y is null.";
80 if (fabs(myScaleFactorZ) < Precision::Confusion()) {
81 myError = "Scale builder :: the scale factor in Z is null.";
87 myError = "Scale builder :: method not implemented.";
93 //=================================================================================================
94 void GeomAlgoAPI_Scale::build()
96 switch (myMethodType) {
101 case BY_DIMENSIONS : {
106 myError = "Scale builder :: method not yet implemented";
112 //=================================================================================================
113 void GeomAlgoAPI_Scale::buildByFactor()
115 const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
116 gp_Trsf* aTrsf = new gp_Trsf();
117 aTrsf->SetScale(aCenterPoint, myScaleFactor);
119 const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
121 if(aSourceShape.IsNull()) {
122 myError = "Scale builder :: source shape does not contain any actual shape.";
126 // Transform the shape while copying it.
127 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
129 myError = "Scale builder :: transform initialization failed.";
134 setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
136 if(!aBuilder->IsDone()) {
137 myError = "Scale builder :: algorithm failed.";
141 TopoDS_Shape aResult = aBuilder->Shape();
143 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
144 aShape->setImpl(new TopoDS_Shape(aResult));
149 //=================================================================================================
150 void GeomAlgoAPI_Scale::buildByDimensions()
152 const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
154 // Perform the rotation matrix
155 gp_Mat aMatRot(myScaleFactorX, 0., 0.,
156 0., myScaleFactorY, 0.,
157 0., 0., myScaleFactorZ);
159 // Perform the tranformation
160 gp_Pnt anOriginPnt(0., 0., 0.);
164 aGTrsfP0.SetTranslationPart(anOriginPnt.XYZ() - aCenterPoint.XYZ());
165 aGTrsf0P.SetTranslationPart(aCenterPoint.XYZ());
166 aGTrsf.SetVectorialPart(aMatRot);
167 aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
168 aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
170 const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
172 if(aSourceShape.IsNull()) {
173 myError = "Scale builder :: source shape does not contain any actual shape.";
177 // Transform the shape while copying it.
178 BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
180 myError = "Scale builder :: transform initialization failed.";
185 setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
187 if(!aBuilder->IsDone()) {
188 myError = "Scale builder :: algorithm failed.";
192 TopoDS_Shape aResult = aBuilder->Shape();
194 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
195 aShape->setImpl(new TopoDS_Shape(aResult));