]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Scale.cpp
Salome HOME
Adding Scale feature.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Scale.cpp
1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Scale.cpp
4 // Created:     23 Jan 2017
5 // Author:      Clarisse Genrault (CEA)
6
7 #include "GeomAlgoAPI_Scale.h"
8
9 #include <BRepBuilderAPI_Transform.hxx>
10 #include <BRepBuilderAPI_GTransform.hxx>
11
12 //=================================================================================================
13 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
14                                      std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
15                                      double theScaleFactor)
16 {
17   myMethodType = BY_FACTOR;
18   mySourceShape = theSourceShape;
19   myCenterPoint = theCenterPoint;
20   myScaleFactor = theScaleFactor;
21 }
22
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)
29 {
30   myMethodType = BY_DIMENSIONS;
31   mySourceShape = theSourceShape;
32   myCenterPoint = theCenterPoint;
33   myScaleFactorX = theScaleFactorX;
34   myScaleFactorY = theScaleFactorY;
35   myScaleFactorZ = theScaleFactorZ;
36 }
37
38 //=================================================================================================
39 bool GeomAlgoAPI_Scale::check()
40 {
41   if (!mySourceShape) {
42     myError = "Scale builder :: source shape is invalid.";
43     return false;
44   }
45   if (!myCenterPoint) {
46     myError = "Scale builder :: center point is invalid.";
47     return false;
48   }
49   switch (myMethodType) {
50     case BY_FACTOR: {
51       if (fabs(myScaleFactor) < Precision::Confusion()) {
52         myError = "Scale builder :: the scale factor is null.";
53         return false;
54       }
55       return true;
56     }
57     case BY_DIMENSIONS: {
58       if (fabs(myScaleFactorX) < Precision::Confusion()) {
59         myError = "Scale builder :: the scale factor in X is null.";
60         return false;
61       }
62       if (fabs(myScaleFactorY) < Precision::Confusion()) {
63         myError = "Scale builder :: the scale factor in Y is null.";
64         return false;
65       }
66       if (fabs(myScaleFactorZ) < Precision::Confusion()) {
67         myError = "Scale builder :: the scale factor in Z is null.";
68         return false;
69       }
70       return true;
71     }
72     default: {
73       myError = "Scale builder :: method not implemented.";
74       return false;
75     }
76   }
77 }
78
79 //=================================================================================================
80 void GeomAlgoAPI_Scale::build()
81 {
82   switch (myMethodType) {
83    case BY_FACTOR : {
84      buildByFactor();
85      break;
86    }
87    case BY_DIMENSIONS : {
88      buildByDimensions();
89      break;
90    }
91    default : {
92      myError = "Scale builder :: method not yet implemented";
93      return;
94    }
95   }
96 }
97
98 //=================================================================================================
99 void GeomAlgoAPI_Scale::buildByFactor()
100 {
101   const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
102   gp_Trsf* aTrsf = new gp_Trsf();
103   aTrsf->SetScale(aCenterPoint, myScaleFactor);
104
105   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
106
107   if(aSourceShape.IsNull()) {
108     myError = "Scale builder :: source shape does not contain any actual shape.";
109     return;
110   }
111
112   // Transform the shape while copying it.
113   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
114   if(!aBuilder) {
115     myError = "Scale builder :: transform initialization failed.";
116     return;
117   }
118
119   setImpl(aBuilder);
120   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
121
122   if(!aBuilder->IsDone()) {
123     myError = "Scale builder :: algorithm failed.";
124     return;
125   }
126
127   TopoDS_Shape aResult = aBuilder->Shape();
128
129   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
130   aShape->setImpl(new TopoDS_Shape(aResult));
131   setShape(aShape);
132   setDone(true);
133 }
134
135 //=================================================================================================
136 void GeomAlgoAPI_Scale::buildByDimensions()
137 {
138   const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
139
140   // Perform the rotation matrix
141   gp_Mat aMatRot(myScaleFactorX, 0., 0.,
142                  0., myScaleFactorY, 0.,
143                  0., 0., myScaleFactorZ);
144
145   // Perform the tranformation
146   gp_Pnt anOriginPnt(0., 0., 0.);
147   gp_GTrsf aGTrsf;
148   gp_GTrsf aGTrsfP0;
149   gp_GTrsf aGTrsf0P;
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);
155
156   const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
157
158   if(aSourceShape.IsNull()) {
159     myError = "Scale builder :: source shape does not contain any actual shape.";
160     return;
161   }
162
163   // Transform the shape while copying it.
164   BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
165   if(!aBuilder) {
166     myError = "Scale builder :: transform initialization failed.";
167     return;
168   }
169
170   setImpl(aBuilder);
171   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
172
173   if(!aBuilder->IsDone()) {
174     myError = "Scale builder :: algorithm failed.";
175     return;
176   }
177
178   TopoDS_Shape aResult = aBuilder->Shape();
179
180   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
181   aShape->setImpl(new TopoDS_Shape(aResult));
182   setShape(aShape);
183   setDone(true);
184 }