1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Scale.cpp
4 // Created: 23 Jan 2017
5 // Author: Clarisse Genrault (CEA)
7 #include "GeomAlgoAPI_Scale.h"
9 #include <BRepBuilderAPI_Transform.hxx>
10 #include <BRepBuilderAPI_GTransform.hxx>
12 //=================================================================================================
13 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
14 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
15 double theScaleFactor)
17 myMethodType = BY_FACTOR;
18 mySourceShape = theSourceShape;
19 myCenterPoint = theCenterPoint;
20 myScaleFactor = theScaleFactor;
23 //=================================================================================================
24 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
25 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
26 double theScaleFactorX,
27 double theScaleFactorY,
28 double theScaleFactorZ)
30 myMethodType = BY_DIMENSIONS;
31 mySourceShape = theSourceShape;
32 myCenterPoint = theCenterPoint;
33 myScaleFactorX = theScaleFactorX;
34 myScaleFactorY = theScaleFactorY;
35 myScaleFactorZ = theScaleFactorZ;
38 //=================================================================================================
39 bool GeomAlgoAPI_Scale::check()
42 myError = "Scale builder :: source shape is not valid.";
46 myError = "Scale builder :: center point is not valid.";
49 switch (myMethodType) {
51 if (fabs(myScaleFactor) < Precision::Confusion()) {
52 myError = "Scale builder :: the scale factor is null.";
58 if (fabs(myScaleFactorX) < Precision::Confusion()) {
59 myError = "Scale builder :: the scale factor in X is null.";
62 if (fabs(myScaleFactorY) < Precision::Confusion()) {
63 myError = "Scale builder :: the scale factor in Y is null.";
66 if (fabs(myScaleFactorZ) < Precision::Confusion()) {
67 myError = "Scale builder :: the scale factor in Z is null.";
73 myError = "Scale builder :: method not implemented.";
79 //=================================================================================================
80 void GeomAlgoAPI_Scale::build()
82 switch (myMethodType) {
87 case BY_DIMENSIONS : {
92 myError = "Scale builder :: method not yet implemented";
98 //=================================================================================================
99 void GeomAlgoAPI_Scale::buildByFactor()
101 const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
102 gp_Trsf* aTrsf = new gp_Trsf();
103 aTrsf->SetScale(aCenterPoint, myScaleFactor);
105 const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
107 if(aSourceShape.IsNull()) {
108 myError = "Scale builder :: source shape does not contain any actual shape.";
112 // Transform the shape while copying it.
113 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
115 myError = "Scale builder :: transform initialization failed.";
120 setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
122 if(!aBuilder->IsDone()) {
123 myError = "Scale builder :: algorithm failed.";
127 TopoDS_Shape aResult = aBuilder->Shape();
129 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
130 aShape->setImpl(new TopoDS_Shape(aResult));
135 //=================================================================================================
136 void GeomAlgoAPI_Scale::buildByDimensions()
138 const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
140 // Perform the rotation matrix
141 gp_Mat aMatRot(myScaleFactorX, 0., 0.,
142 0., myScaleFactorY, 0.,
143 0., 0., myScaleFactorZ);
145 // Perform the tranformation
146 gp_Pnt anOriginPnt(0., 0., 0.);
150 aGTrsfP0.SetTranslationPart(anOriginPnt.XYZ() - aCenterPoint.XYZ());
151 aGTrsf0P.SetTranslationPart(aCenterPoint.XYZ());
152 aGTrsf.SetVectorialPart(aMatRot);
153 aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
154 aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
156 const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
158 if(aSourceShape.IsNull()) {
159 myError = "Scale builder :: source shape does not contain any actual shape.";
163 // Transform the shape while copying it.
164 BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
166 myError = "Scale builder :: transform initialization failed.";
171 setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
173 if(!aBuilder->IsDone()) {
174 myError = "Scale builder :: algorithm failed.";
178 TopoDS_Shape aResult = aBuilder->Shape();
180 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
181 aShape->setImpl(new TopoDS_Shape(aResult));