0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / samples / mfc / occtdemo / Transform / Transform_Presentation.cpp
CommitLineData
7fd59977 1// Transform_Presentation.cpp: implementation of the Transform_Presentation class.
2// Transform shapes: apply gp_Trsf or gp_GTrsf transformation
3////////////////////////////////////////////////////////////////////////////
4
5#include "stdafx.h"
6#include "Transform_Presentation.h"
7#include <BRep_Builder.hxx>
8#include <BRepTools.hxx>
9#include <BRepBuilderAPI_Transform.hxx>
10#include <AIS_Axis.hxx>
11#include <Geom_Axis1Placement.hxx>
12#include <Geom_Line.hxx>
13#include <Geom_Circle.hxx>
14#include <Geom_TrimmedCurve.hxx>
6262338c 15#include <Prs3d_Drawer.hxx>
7fd59977 16#include <Prs3d_ArrowAspect.hxx>
17#include <Geom_Plane.hxx>
18#include <Geom_RectangularTrimmedSurface.hxx>
19#include <gp_GTrsf.hxx>
20#include <BRepBuilderAPI_GTransform.hxx>
21
22
23#ifdef WNT
24 #define EOL "\r\n"
25 #define DELIM "\\"
26#else
27 #define EOL "\n"
28 #define DELIM "/"
29#endif
30
31// Initialization of global variable with an instance of this class
32OCCDemo_Presentation* OCCDemo_Presentation::Current = new Transform_Presentation;
33
34// Initialization of array of samples
35const Transform_Presentation::PSampleFuncType Transform_Presentation::SampleFuncs[] =
36{
37 &Transform_Presentation::sample1,
38 &Transform_Presentation::sample2
39};
40
41//////////////////////////////////////////////////////////////////////
42// Construction/Destruction
43//////////////////////////////////////////////////////////////////////
44
45Transform_Presentation::Transform_Presentation()
46{
47 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
48 setName ("Transform shapes");
49}
50
51//////////////////////////////////////////////////////////////////////
52// Sample execution
53//////////////////////////////////////////////////////////////////////
54
55void Transform_Presentation::DoSample()
56{
57 getAISContext()->EraseAll();
58 if (myIndex >=0 && myIndex < myNbSamples)
59 (this->*SampleFuncs[myIndex])();
60}
61
62//////////////////////////////////////////////////////////////////////
63// Auxiliary functions
64//////////////////////////////////////////////////////////////////////
65
66Standard_Boolean Transform_Presentation::readShape(const Standard_CString theFileName,
67 TopoDS_Shape& theShape)
68{
69 theShape.Nullify();
70 TCollection_AsciiString aPath;
71 aPath = aPath + GetDataDir() + DELIM + theFileName;
72 BRep_Builder aBld;
73 Standard_Boolean isRead = BRepTools::Read (theShape, aPath.ToCString(), aBld);
74 if (!isRead)
75 {
76 aPath = aPath + " is not found";
77 setResultText(aPath.ToCString());
78 }
79 return isRead;
80}
81
82// apply standard transformation and get transformed shape
83static TopoDS_Shape Transform(const TopoDS_Shape& aShape, const gp_Trsf& aTrsf)
84{
85 BRepBuilderAPI_Transform aTransform(aTrsf);
86 Standard_Boolean toCopy = Standard_False; // share entities if possible
87 aTransform.Perform(aShape, toCopy);
88 if (aTransform.IsDone())
89 return aTransform.Shape();
90 return TopoDS_Shape();
91}
92
93// apply general transformation and get transformed shape
94static TopoDS_Shape GTransform(const TopoDS_Shape& aShape, const gp_GTrsf& aTrsf)
95{
96 BRepBuilderAPI_GTransform aTransform(aTrsf);
97 Standard_Boolean toCopy = Standard_False; // share entities if possible
98 aTransform.Perform(aShape, toCopy);
99 if (aTransform.IsDone())
100 return aTransform.Shape();
101 return TopoDS_Shape();
102}
103
104//////////////////////////////////////////////////////////////////////
105// Sample functions
106//////////////////////////////////////////////////////////////////////
107
108void Transform_Presentation::sample1()
109{
110 SetViewScale(4);
111 setResultTitle ("Standard transformations");
112
113 // define initial shape
114 static TopoDS_Shape aShape;
115 TopoDS_Shape aNewShape;
116 if (aShape.IsNull() && !readShape("shape1.brep",aShape))
117 return;
118
119 TCollection_AsciiString aText;
120 aText +=
121 "// Apply standard transformation and get transformed shape" EOL
122 "TopoDS_Shape Transform(const TopoDS_Shape& aShape, const gp_Trsf& aTrsf)" EOL
123 "{" EOL
124 " BRepBuilderAPI_Transform aTransform(aTrsf);" EOL
125 " Standard_Boolean toCopy = Standard_False; // share entities if possible" EOL
126 " aTransform.Perform(aShape, toCopy);" EOL
127 " if (aTransform.IsDone())" EOL
128 " return aTransform.Shape();" EOL
129 " return TopoDS_Shape();" EOL
130 "}" EOL EOL
131
132 "// ..." EOL EOL
133
134 " // define initial shape and shape used for result" EOL
135 " TopoDS_Shape aShape, aNewShape;" EOL
136 " // aShape = ... ;" EOL EOL
137
138 " // create standard transformation" EOL
139 " gp_Trsf aTrsf;" EOL EOL
140
141 " // define translation" EOL
142 " Standard_Real dx=-80, dy=-60, dz=-50;" EOL
143 " gp_Vec aVec(dx,dy,dz);" EOL
144 " aTrsf.SetTranslation(aVec);" EOL EOL
145
146 " aNewShape = Transform(aShape, aTrsf);" EOL EOL
147
148 " // add rotation" EOL
149 " gp_Trsf aRotTrsf;" EOL
150 " gp_Pnt ax1Pnt(dx,dy,dz);" EOL
151 " gp_Dir ax1Dir(0,0,1);" EOL
152 " gp_Ax1 ax1 (ax1Pnt,ax1Dir);" EOL
153 " Standard_Real angle = 60. * PI / 180;" EOL
154 " aRotTrsf.SetRotation(ax1, angle);" EOL
155 " aTrsf = aRotTrsf * aTrsf;" EOL EOL
156
157 " aNewShape = Transform(aShape, aTrsf);" EOL EOL
158
159 " // add scaling" EOL
160 " gp_Trsf aScaleTrsf;" EOL
161 " gp_Pnt scalePnt = ax1Pnt;" EOL
162 " Standard_Real scaleFactor = 0.6;" EOL
163 " aScaleTrsf.SetScale(scalePnt, scaleFactor);" EOL
164 " aTrsf = aScaleTrsf * aTrsf;" EOL EOL
165
166 " aNewShape = Transform(aShape, aTrsf);" EOL EOL
167
168 " // add point mirror" EOL
169 " gp_Trsf aMir1Trsf;" EOL
170 " gp_Pnt mirPnt(dx/4,dy/4,dz/4);" EOL
171 " aMir1Trsf.SetMirror(mirPnt);" EOL
172 " aTrsf = aMir1Trsf * aTrsf;" EOL EOL
173
174 " aNewShape = Transform(aShape, aTrsf);" EOL EOL
175
176 " // add axial mirror" EOL
177 " gp_Trsf aMir2Trsf;" EOL
178 " gp_Ax1 mirAx1(gp_Pnt(0,0,0),gp_Dir(0,0,1));" EOL
179 " aMir2Trsf.SetMirror(mirAx1);" EOL
180 " aTrsf = aMir2Trsf * aTrsf;" EOL EOL
181
182 " aNewShape = Transform(aShape, aTrsf);" EOL EOL
183
184 " // add planar mirror" EOL
185 " gp_Trsf aMir3Trsf;" EOL
186 " gp_Ax2 mirAx2(gp_Pnt(-dx/4,0,0),gp_Dir(1,0,0));" EOL
187 " aMir3Trsf.SetMirror(mirAx2);" EOL
188 " aTrsf = aMir3Trsf * aTrsf;" EOL EOL
189
190 " aNewShape = Transform(aShape, aTrsf);" EOL EOL;
191 setResultText(aText.ToCString());
192
193 Handle(AIS_InteractiveObject) aIShape = drawShape (aShape);
194 if (WAIT_A_SECOND) return;
195
196 // create standard transformation
197 gp_Trsf aTrsf;
198
199 // define translation
200 Standard_Real dx=-80, dy=-60, dz=-50;
201 gp_Vec aVec(dx,dy,dz);
202 aTrsf.SetTranslation(aVec);
203
204 Handle(AIS_InteractiveObject) aIVec = drawVector(gp::Origin(),aVec,Quantity_NOC_WHITE);
205 if (WAIT_A_LITTLE) return;
206
207 aNewShape = Transform(aShape, aTrsf);
208
209 if (aNewShape.IsNull())
210 {
211 aText += EOL "Error: transformation failure" EOL;
212 setResultText(aText.ToCString());
213 return;
214 }
215 Handle(AIS_InteractiveObject) oldObj = aIShape;
216 aIShape = drawShape (aNewShape);
217 getAISContext()->Erase(oldObj);
218 if (WAIT_A_SECOND) return;
219 getAISContext()->Erase(aIVec);
220
221 // add rotation
222 gp_Trsf aRotTrsf;
223 gp_Pnt ax1Pnt(dx,dy,dz);
224 gp_Dir ax1Dir(0,0,1);
225 gp_Ax1 ax1 (ax1Pnt,ax1Dir);
226 Standard_Real angle = 60. * PI / 180;
227 aRotTrsf.SetRotation(ax1, angle);
228 aTrsf = aRotTrsf * aTrsf;
229
230 Handle(AIS_InteractiveObject) aIAx1 = drawCurve(new Geom_Line(ax1),Quantity_NOC_WHITE);
231 gp_Ax2 ax2(ax1Pnt.Translated(gp_Vec(0,0,70)),ax1Dir);
232 Handle(Geom_Curve) aCirc = new Geom_TrimmedCurve(new Geom_Circle(ax2,20),0,angle);
233 Handle(AIS_InteractiveObject) aICirc = drawCurve(aCirc,Quantity_NOC_WHITE,Standard_False);
6262338c 234 Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer;
7fd59977 235 aDrawer->Link()->SetLineArrowDraw(Standard_True);
236 aDrawer->Link()->ArrowAspect()->SetLength(2);
237 getAISContext()->SetLocalAttributes (aICirc, aDrawer);
238 getAISContext()->Display (aICirc);
239 if (WAIT_A_LITTLE) return;
240
241 aNewShape = Transform(aShape, aTrsf);
242
243 if (aNewShape.IsNull())
244 {
245 aText += EOL "Error: transformation failure" EOL;
246 setResultText(aText.ToCString());
247 return;
248 }
249 oldObj = aIShape;
250 aIShape = drawShape (aNewShape);
251 getAISContext()->Erase(oldObj);
252 if (WAIT_A_SECOND) return;
253 getAISContext()->Erase(aIAx1);
254 getAISContext()->Erase(aICirc);
255
256 // add scaling
257 gp_Trsf aScaleTrsf;
258 gp_Pnt scalePnt = ax1Pnt;
259 Standard_Real scaleFactor = 0.6;
260 aScaleTrsf.SetScale(scalePnt, scaleFactor);
261 aTrsf = aScaleTrsf * aTrsf;
262
263 Handle(AIS_InteractiveObject) aIPnt = drawPoint(scalePnt, Quantity_NOC_WHITE);
264 if (WAIT_A_LITTLE) return;
265
266 aNewShape = Transform(aShape, aTrsf);
267
268 if (aNewShape.IsNull())
269 {
270 aText += EOL "Error: transformation failure" EOL;
271 setResultText(aText.ToCString());
272 return;
273 }
274 oldObj = aIShape;
275 aIShape = drawShape (aNewShape);
276 getAISContext()->Erase(oldObj);
277 if (WAIT_A_SECOND) return;
278 getAISContext()->Erase(aIPnt);
279
280 // add point mirror
281 gp_Trsf aMir1Trsf;
282 gp_Pnt mirPnt(dx/4,dy/4,dz/4);
283 aMir1Trsf.SetMirror(mirPnt);
284 aTrsf = aMir1Trsf * aTrsf;
285
286 aIPnt = drawPoint(mirPnt, Quantity_NOC_WHITE);
287 if (WAIT_A_LITTLE) return;
288
289 aNewShape = Transform(aShape, aTrsf);
290
291 if (aNewShape.IsNull())
292 {
293 aText += EOL "Error: transformation failure" EOL;
294 setResultText(aText.ToCString());
295 return;
296 }
297 oldObj = aIShape;
298 aIShape = drawShape (aNewShape);
299 getAISContext()->Erase(oldObj);
300 if (WAIT_A_SECOND) return;
301 getAISContext()->Erase(aIPnt);
302
303 // add axial mirror
304 gp_Trsf aMir2Trsf;
305 gp_Ax1 mirAx1(gp_Pnt(0,0,0),gp_Dir(0,0,1));
306 aMir2Trsf.SetMirror(mirAx1);
307 aTrsf = aMir2Trsf * aTrsf;
308
309 aIAx1 = drawCurve(new Geom_Line(mirAx1),Quantity_NOC_WHITE);
310 if (WAIT_A_LITTLE) return;
311
312 aNewShape = Transform(aShape, aTrsf);
313
314 if (aNewShape.IsNull())
315 {
316 aText += EOL "Error: transformation failure" EOL;
317 setResultText(aText.ToCString());
318 return;
319 }
320 oldObj = aIShape;
321 aIShape = drawShape (aNewShape);
322 getAISContext()->Erase(oldObj);
323 if (WAIT_A_SECOND) return;
324 getAISContext()->Erase(aIAx1);
325
326 // add planar mirror
327 gp_Trsf aMir3Trsf;
328 gp_Ax2 mirAx2(gp_Pnt(-dx/4,0,0),gp_Dir(1,0,0));
329 aMir3Trsf.SetMirror(mirAx2);
330 aTrsf = aMir3Trsf * aTrsf;
331
332 Handle(AIS_InteractiveObject) aIAx2 = drawSurface(
333 new Geom_RectangularTrimmedSurface(new Geom_Plane(mirAx2),-50.,50.,-50.,50.),
334 Quantity_NOC_WHITE);
335 if (WAIT_A_LITTLE) return;
336
337 aNewShape = Transform(aShape, aTrsf);
338
339 if (aNewShape.IsNull())
340 {
341 aText += EOL "Error: transformation failure" EOL;
342 setResultText(aText.ToCString());
343 return;
344 }
345 oldObj = aIShape;
346 aIShape = drawShape (aNewShape);
347 getAISContext()->Erase(oldObj);
348 if (WAIT_A_SECOND) return;
349 getAISContext()->Erase(aIAx2);
350}
351
352void Transform_Presentation::sample2()
353{
354 ResetView();
355 SetViewCenter(219.25218771377, 60.449092616063);
356 SetViewScale(3.4554379699768);
357
358 setResultTitle ("General transformations");
359
360 // define initial shape
361 static TopoDS_Shape aShape;
362 TopoDS_Shape aNewShape;
363 if (aShape.IsNull() && !readShape("Pump_Nut.brep",aShape))
364 return;
365
366 TCollection_AsciiString aText;
367 aText +=
368 "// Apply general transformation and get transformed shape" EOL
369 "TopoDS_Shape GTransform(const TopoDS_Shape& aShape, const gp_GTrsf& aTrsf)" EOL
370 "{" EOL
371 " BRepBuilderAPI_GTransform aTransform(aTrsf);" EOL
372 " Standard_Boolean toCopy = Standard_False; // share entities if possible" EOL
373 " aTransform.Perform(aShape, toCopy);" EOL
374 " if (aTransform.IsDone())" EOL
375 " return aTransform.Shape();" EOL
376 " return TopoDS_Shape();" EOL
377 "}" EOL EOL
378
379 "// ..." EOL EOL
380
381 " // define initial shape and shape used for result" EOL
382 " TopoDS_Shape aShape, aNewShape;" EOL
383 " // aShape = ... ;" EOL EOL
384
385 " // create general transformation" EOL
386 " gp_GTrsf aTrsf;" EOL EOL
387
388 " // define affinity respect to line" EOL
389 " gp_Ax1 ax1(gp_Pnt(81,280,0),gp_Dir(0,1,0));" EOL
390 " Standard_Real ratio = 2;" EOL
391 " aTrsf.SetAffinity(ax1, ratio);" EOL EOL
392
393 " aNewShape = GTransform(aShape, aTrsf);" EOL EOL
394
395 " // add affinity respect to plane" EOL
396 " gp_GTrsf aTrsf1;" EOL
397 " gp_Ax2 ax2(gp_Pnt(81,280,0),gp_Dir(0,1,0));" EOL
398 " ratio = 8;" EOL
399 " aTrsf1.SetAffinity(ax2, ratio);" EOL
400 " aTrsf = aTrsf1 * aTrsf;" EOL EOL
401
402 " aNewShape = GTransform(aShape, aTrsf);" EOL EOL;
403 setResultText(aText.ToCString());
404
405 Handle(AIS_InteractiveObject) aIShape = drawShape (aShape);
406 if (WAIT_A_SECOND) return;
407
408 // create general transformation
409 gp_GTrsf aTrsf;
410
411 // define affinity respect to line
412 gp_Ax1 ax1(gp_Pnt(81,280,0),gp_Dir(0,1,0));
413 Standard_Real ratio = 2;
414 aTrsf.SetAffinity(ax1, ratio);
415
416 Handle(AIS_InteractiveObject) aIAx1 = drawCurve(new Geom_Line(ax1),Quantity_NOC_WHITE);
417 if (WAIT_A_LITTLE) return;
418
419 aNewShape = GTransform(aShape, aTrsf);
420
421 if (aNewShape.IsNull())
422 {
423 aText += EOL "Error: transformation failure" EOL;
424 setResultText(aText.ToCString());
425 return;
426 }
427 Handle(AIS_InteractiveObject) oldObj = aIShape;
428 aIShape = drawShape (aNewShape);
429 getAISContext()->Erase(oldObj);
430 if (WAIT_A_SECOND) return;
431 getAISContext()->Erase(aIAx1);
432
433 // add affinity respect to plane
434 gp_GTrsf aTrsf1;
435 gp_Ax2 ax2(gp_Pnt(81,280,0),gp_Dir(0,1,0));
436 ratio = 8;
437 aTrsf1.SetAffinity(ax2, ratio);
438 aTrsf.PreMultiply(aTrsf1);
439
440 Handle(AIS_InteractiveObject) aIAx2 = drawSurface(
441 new Geom_RectangularTrimmedSurface(new Geom_Plane(ax2),-100.,100.,-100.,100.),
442 Quantity_NOC_WHITE);
443 if (WAIT_A_LITTLE) return;
444
445 aNewShape = GTransform(aShape, aTrsf);
446
447 if (aNewShape.IsNull())
448 {
449 aText += EOL "Error: transformation failure" EOL;
450 setResultText(aText.ToCString());
451 return;
452 }
453 oldObj = aIShape;
454 aIShape = drawShape (aNewShape);
455 getAISContext()->Erase(oldObj);
456 if (WAIT_A_SECOND) return;
457 getAISContext()->Erase(aIAx2);
458}