7fd59977 |
1 | // ThruSections_Presentation.cpp: implementation of the ThruSections_Presentation class. |
2 | // Building shapes passed through sections. |
3 | ////////////////////////////////////////////////////////////////////// |
4 | |
5 | #include "stdafx.h" |
6 | #include "ThruSections_Presentation.h" |
7 | |
8 | #include <Standard.hxx> |
9 | #include <Precision.hxx> |
10 | |
11 | #include <BRepBuilderAPI_MakePolygon.hxx> |
12 | #include <BRepBuilderAPI_MakeEdge.hxx> |
13 | #include <BRepBuilderAPI_MakeWire.hxx> |
14 | #include <BRepOffsetAPI_ThruSections.hxx> |
15 | |
16 | #include <TopoDS.hxx> |
17 | #include <TopoDS_Wire.hxx> |
18 | |
19 | #include <gce_MakeCirc.hxx> |
20 | #include <gce_MakeElips.hxx> |
21 | #include <gp.hxx> |
22 | #include <gp_Pnt.hxx> |
23 | #include <gp_Dir.hxx> |
24 | |
25 | #include <Geom_BSplineCurve.hxx> |
26 | #include <GeomAPI_PointsToBSpline.hxx> |
27 | #include <Geom_Plane.hxx> |
28 | |
29 | #include <TColgp_Array1OfPnt.hxx> |
30 | |
31 | |
32 | |
33 | // Initialization of global variable with an instance of this class |
34 | OCCDemo_Presentation* OCCDemo_Presentation::Current = new ThruSections_Presentation; |
35 | |
36 | // Initialization of array of samples |
37 | const ThruSections_Presentation::PSampleFuncType ThruSections_Presentation::SampleFuncs[] = |
38 | { |
39 | &ThruSections_Presentation::sample1, |
40 | &ThruSections_Presentation::sample2 |
41 | }; |
42 | |
43 | #ifdef WNT |
44 | #define EOL "\r\n" |
45 | #else |
46 | #define EOL "\n" |
47 | #endif |
48 | |
49 | |
50 | ////////////////////////////////////////////////////////////////////// |
51 | // Construction/Destruction |
52 | ////////////////////////////////////////////////////////////////////// |
53 | |
54 | ThruSections_Presentation::ThruSections_Presentation() |
55 | { |
56 | myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType); |
57 | setName("Lofting through sections"); |
58 | } |
59 | |
60 | ////////////////////////////////////////////////////////////////////// |
61 | // Sample execution |
62 | ////////////////////////////////////////////////////////////////////// |
63 | |
64 | void ThruSections_Presentation::DoSample() |
65 | { |
66 | getAISContext()->EraseAll(); |
67 | if (myIndex >=0 && myIndex < myNbSamples) |
68 | (this->*SampleFuncs[myIndex])(); |
69 | } |
70 | |
71 | ////////////////////////////////////////////////////////////////////// |
72 | // Sample functions |
73 | ////////////////////////////////////////////////////////////////////// |
74 | |
75 | ////////////////////////////////////////////////////////////////////// |
76 | // function: CreateCurve |
77 | // purpose: |
78 | ////////////////////////////////////////////////////////////////////// |
79 | |
92efcf78 |
80 | static Handle(Geom_BSplineCurve) CreateCurve(Standard_Real aCoords[][3],Standard_Integer nPoles) |
7fd59977 |
81 | { |
82 | TColgp_Array1OfPnt aCurvePoint (1, nPoles); |
83 | |
84 | for (Standard_Integer i=0; i < nPoles; i++) |
85 | aCurvePoint(i+1) = gp_Pnt (aCoords[i][0], aCoords[i][1], aCoords[i][2]); |
86 | |
87 | GeomAPI_PointsToBSpline aPTB (aCurvePoint); |
88 | |
89 | Handle (Geom_BSplineCurve) aCurve = aPTB.Curve(); |
90 | return aCurve; |
91 | } |
92 | |
93 | ////////////////////////////////////////////////////////////////////// |
94 | // function: CreateShape |
95 | // purpose: |
96 | ////////////////////////////////////////////////////////////////////// |
97 | |
98 | TopoDS_Shape ThruSections_Presentation::CreateShape(TopTools_SequenceOfShape& aWires, |
99 | Standard_Boolean IsSolid, |
100 | Standard_Boolean IsRuled, |
101 | Standard_Real aPrec) |
102 | { |
103 | BRepOffsetAPI_ThruSections aTSec(IsSolid,IsRuled,aPrec); |
104 | |
105 | for(Standard_Integer i = 1 ; i < aWires.Length() + 1 ; i++) |
106 | aTSec.AddWire(TopoDS::Wire(aWires(i))); |
107 | |
108 | aTSec.Build(); |
109 | |
110 | TopoDS_Shape aShape = aTSec.Shape(); |
111 | return aShape; |
112 | } |
113 | |
114 | ////////////////////////////////////////////////////////////////////// |
115 | // function: sample1 |
116 | // purpose: |
117 | ////////////////////////////////////////////////////////////////////// |
118 | |
119 | void ThruSections_Presentation::sample1() |
120 | { |
121 | ResetView(); |
122 | SetViewCenter(-5.5147159194746e-007, 2.4494893207696); |
123 | SetViewScale(75.788671414734); |
124 | |
125 | setResultTitle("Lofting through closed sections"); |
126 | TCollection_AsciiString aText; |
127 | aText = ( |
128 | |
129 | "// this data used for building a shape through sections:" EOL |
130 | "Standard_Boolean IsSolid,IsRuled;" EOL |
131 | "Standard_Real aPrec;" EOL EOL |
132 | |
133 | "// creates wires:" EOL |
134 | "TopoDS_Wire aWire1,aWire2,aWire3;" EOL EOL |
135 | |
136 | "// initializes these wires:" EOL |
137 | "//aWire1 = ... ; aWire2 = ...; aWire3 = ...;" EOL EOL |
138 | |
139 | "// creates an algorithm for building a shell or " EOL |
140 | "//solid passing through a set of wires:" EOL |
141 | "BRepOffsetAPI_ThruSections aTSec;" EOL EOL |
142 | |
143 | "// adds wires to the set of wires:" EOL |
144 | "aTSec.AddWire(aWire1);" EOL |
145 | "aTSec.AddWire(aWire2);" EOL |
146 | "aTSec.AddWire(aWire3);" EOL EOL |
147 | |
148 | "// Initializes algorithm for building a shell " EOL |
149 | "//passing through a set of wires with the faces are" EOL |
150 | "//smoothed out by approximation:" EOL |
151 | "aTSec.Init();" EOL |
152 | "// builds the shell:" EOL |
153 | "aTSec.Build();" EOL |
154 | "// takes this shape:" EOL |
155 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
156 | |
157 | "// Initializes algorithm for building a shell " EOL |
158 | "//passing through a set of wires with the faces are ruled:" EOL |
159 | "IsSolid = Standard_False;" EOL |
160 | "IsRuled = Standard_True;" EOL |
161 | "aTSec.Init(IsSolid,IsRuled);" EOL |
162 | "// builds the shell:" EOL |
163 | "aTSec.Build();" EOL |
164 | "// takes this shape:" EOL |
165 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
166 | |
167 | "// Initializes algorithm for building a solid" EOL |
168 | "//passing through a set of wires with the faces are" EOL |
169 | "//smoothed out by approximation:" EOL |
170 | "IsSolid = Standard_True;" EOL |
171 | "aTSec.Init(IsSolid);" EOL |
172 | "// builds the solid:" EOL |
173 | "aTSec.Build();" EOL |
174 | "// takes this shape:" EOL |
175 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
176 | |
177 | "// Initializes algorithm for building a solid" EOL |
178 | "//passing through a set of wires with the faces are" EOL |
179 | "//smoothed out by approximation with precision aPrec:" EOL |
180 | "IsSolid = Standard_True;" EOL |
181 | "IsRuled = Standard_False;" EOL |
182 | "aPrec = 0.5;" EOL |
183 | "aTSec.Init(IsSolid,IsRuled,aPrec);" EOL |
184 | "// builds the solid:" EOL |
185 | "aTSec.Build();" EOL |
186 | "// takes this shape:" EOL |
187 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
188 | |
189 | ); |
190 | |
191 | setResultText(aText.ToCString()); |
192 | |
193 | //==================================================================== |
194 | |
92efcf78 |
195 | Handle(AIS_InteractiveObject) aShow1,aShow2,aShowWire; |
7fd59977 |
196 | |
197 | // this data used for building a shape through sections: |
198 | Standard_Boolean IsSolid,IsRuled; |
199 | Standard_Real aPrec; |
200 | |
201 | // the number of points of wires: |
202 | const Standard_Integer aNbOfPnt = 4; |
203 | |
204 | // creates arrays of coordinates of wires: |
205 | Standard_Real aCoords [][aNbOfPnt][3] = |
206 | { |
207 | {{-4,0,0},{0,4,0},{4,0,0},{0,-4,0}}, |
208 | {{-2,-2,4},{-2,2,4},{2,2,4},{2,-2,4}} |
209 | }; |
210 | |
211 | |
212 | // the number of wires: |
213 | Standard_Integer aNbOfWire = (sizeof(aCoords)/(sizeof(Standard_Real)*3))/aNbOfPnt; |
214 | |
215 | TopTools_SequenceOfShape aWires; |
216 | for( Standard_Integer i = 0 ; i < aNbOfWire ; i++) |
217 | { |
218 | BRepBuilderAPI_MakePolygon aPol; |
219 | for( Standard_Integer j = 0 ; j < aNbOfPnt ; j++) |
220 | aPol.Add(gp_Pnt(aCoords[i][j][0],aCoords[i][j][1],aCoords[i][j][2])); |
221 | |
222 | aPol.Close(); |
223 | |
224 | TopoDS_Wire aW; |
225 | aW = aPol.Wire(); |
226 | aWires.Append(aW); |
227 | } |
228 | |
229 | |
230 | // creates shell passing through a set of wires |
231 | //with the faces are smoothed out by approximation: |
232 | TopoDS_Shape aShape = CreateShape(aWires); |
233 | |
234 | //draw this shell: |
235 | getAISContext()->EraseAll(); |
236 | for( i =1 ; i < aWires.Length() + 1 ; i++) |
237 | drawShape(TopoDS::Wire(aWires(i))); |
238 | if(WAIT_A_LITTLE) return; |
239 | aShow1 = drawShape(aShape); |
240 | if(WAIT_A_SECOND) return; |
241 | |
242 | // creates a circle: |
243 | gp_Pnt aCenter = gp_Pnt(0,0,6); |
244 | gp_Dir aNorm = gp::DZ(); |
245 | Standard_Real aRad = 5; |
246 | gce_MakeCirc aMC(aCenter,aNorm,aRad); |
247 | gp_Circ aCirc = aMC.Value(); |
248 | |
249 | // creates shell passing through a set of wires |
250 | //with the faces are ruled: |
251 | aWires.Append(BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aCirc))); |
252 | IsSolid = Standard_False; |
253 | IsRuled = Standard_True; |
254 | aShape = CreateShape(aWires,IsSolid,IsRuled); |
255 | |
256 | // draw this shell: |
257 | aShowWire = drawShape(aWires.Last()); |
258 | if(WAIT_A_SECOND) return; |
259 | aShow2 = drawShape(aShape); |
260 | getAISContext()->Erase(aShow1); |
261 | |
262 | // creates shell passing through a set of wires |
263 | //with the faces are smoothed out by approximation: |
264 | aShape = CreateShape(aWires); |
265 | |
266 | // draw this shell: |
267 | if(WAIT_A_LITTLE) return; |
268 | aShow1 = drawShape(aShape); |
269 | getAISContext()->Erase(aShow2); |
270 | |
271 | // creates solid passing through a set of wires |
272 | //with the faces are smoothed out by approximation: |
273 | IsSolid = Standard_True; |
274 | aShape = CreateShape(aWires,IsSolid); |
275 | |
276 | // draw this solid: |
277 | if(WAIT_A_LITTLE) return; |
278 | aShow2 = drawShape(aShape); |
279 | getAISContext()->Erase(aShow1); |
280 | |
281 | if(WAIT_A_SECOND) return; |
282 | BRepBuilderAPI_MakeEdge aME1(aCirc,0,PI/4); |
283 | BRepBuilderAPI_MakeEdge aME2(aCirc,PI,5*PI/4); |
284 | |
285 | TopoDS_Edge aE1 = aME1.Edge(); |
286 | TopoDS_Edge aE2 = aME2.Edge(); |
287 | TopoDS_Edge aE3 = BRepBuilderAPI_MakeEdge(aME1.Vertex2(),aME2.Vertex1()); |
288 | TopoDS_Edge aE4 = BRepBuilderAPI_MakeEdge(aME2.Vertex2(),aME1.Vertex1()); |
289 | aWires.SetValue(aWires.Length(),BRepBuilderAPI_MakeWire(aE1,aE3,aE2,aE4)); |
290 | |
291 | // creates solid passing through a set of wires |
292 | //with the faces are smoothed out by approximation with precision aPrec: |
293 | IsSolid = Standard_True; |
294 | IsRuled = Standard_False; |
295 | aPrec = 0.5; |
296 | aShape = CreateShape(aWires,IsSolid,IsRuled,aPrec); |
297 | |
298 | // draw this solid: |
299 | drawShape(TopoDS::Wire(aWires.Last())); |
300 | getAISContext()->Erase(aShowWire); |
301 | if(WAIT_A_LITTLE) return; |
302 | drawShape(aShape); |
303 | getAISContext()->Erase(aShow2); |
304 | } |
305 | |
306 | |
307 | ////////////////////////////////////////////////////////////////////// |
308 | // function: sample2 |
309 | // purpose: |
310 | ////////////////////////////////////////////////////////////////////// |
311 | |
312 | void ThruSections_Presentation::sample2() |
313 | { |
314 | ResetView(); |
315 | SetViewCenter(2.4529999187450e-007, 4.0824822167758); |
316 | SetViewScale(63.822115234655); |
317 | |
318 | |
319 | setResultTitle("Lofting through unclosed sections"); |
320 | TCollection_AsciiString aText; |
321 | aText = ( |
322 | |
323 | "// this data used for building a shape through sections:" EOL |
324 | "const Standard_Boolean IsSolid = Standard_False;" EOL |
325 | "Standard_Boolean IsRuled;" EOL |
326 | "Standard_Real aPrec;" EOL EOL |
327 | |
328 | "// creates wires:" EOL |
329 | "TopoDS_Wire aWire1,aWire2;" EOL EOL |
330 | |
331 | "// initializes these wires:" EOL |
332 | "//aWire1 = ... ; aWire2 = ...;" EOL EOL |
333 | |
334 | "// creates an algorithm for building a shell or " EOL |
335 | "//solid passing through a set of wires:" EOL |
336 | "BRepOffsetAPI_ThruSections aTSec;" EOL EOL |
337 | |
338 | "// adds wires to the set of wires:" EOL |
339 | "aTSec.AddWire(aWire1);" EOL |
340 | "aTSec.AddWire(aWire2);" EOL EOL |
341 | |
342 | "// Initializes algorithm for building a shell " EOL |
343 | "//passing through a set of wires with the faces are" EOL |
344 | "//smoothed out by approximation:" EOL |
345 | "aTSec.Init();" EOL |
346 | "// builds the shell:" EOL |
347 | "aTSec.Build();" EOL |
348 | "// takes this shape:" EOL |
349 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
350 | |
351 | "// Initializes algorithm for building a shell" EOL |
352 | "//passing through a set of wires with the faces are" EOL |
353 | "//smoothed out by approximation with precision aPrec:" EOL |
354 | "IsRuled = Standard_False;" EOL |
355 | "aPrec = 0.5;" EOL |
356 | "aTSec.Init(IsSolid,IsRuled,aPrec);" EOL |
357 | "// builds the solid:" EOL |
358 | "aTSec.Build();" EOL |
359 | "// takes this shape:" EOL |
360 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
361 | |
362 | "// Initializes algorithm for building a shell " EOL |
363 | "//passing through a set of wires with the faces are ruled:" EOL |
364 | "IsRuled = Standard_True;" EOL |
365 | "aTSec.Init(IsSolid,IsRuled);" EOL |
366 | "// builds the shell:" EOL |
367 | "aTSec.Build();" EOL |
368 | "// takes this shape:" EOL |
369 | "TopoDS_Shape aShape = aTSec.Shape();" EOL EOL |
370 | |
371 | ); |
372 | |
373 | setResultText(aText.ToCString()); |
374 | |
375 | //==================================================================== |
376 | |
92efcf78 |
377 | Handle(AIS_InteractiveObject) aShow1,aShow2,aShowWire1,aShowWire2,aShowWire3; |
7fd59977 |
378 | |
379 | // this data used for building a shape through sections: |
380 | const Standard_Boolean IsSolid = Standard_False; |
381 | Standard_Boolean IsRuled; |
382 | Standard_Real aPrec; |
383 | |
384 | // creates arrays of coordinates of wires: |
385 | Standard_Real aCoords1[][3] = |
386 | { |
387 | {-5,0,0},{-2,3,0},{3,-2.5,0},{5,0,0} |
388 | }; |
389 | |
390 | Standard_Real aCoords2[][3] = |
391 | { |
392 | {-5,0,7},{-2,3,7},{3,-2.5,7},{5,0,7} |
393 | }; |
394 | |
395 | Standard_Real aCoords3 [][3] = |
396 | { |
397 | {-4,0,10},{0,4,10},{4,0,10},{0,-4,10} |
398 | }; |
399 | |
400 | // numbers of points for wires: |
401 | Standard_Integer aSize1 = sizeof(aCoords1)/(sizeof(Standard_Real)*3); |
402 | Standard_Integer aSize2 = sizeof(aCoords2)/(sizeof(Standard_Real)*3); |
403 | Standard_Integer aSize3 = sizeof(aCoords3)/(sizeof(Standard_Real)*3); |
404 | |
405 | // creates curves: |
92efcf78 |
406 | Handle(Geom_BSplineCurve) aCurve1 = CreateCurve(aCoords1,aSize1); |
407 | Handle(Geom_BSplineCurve) aCurve2 = CreateCurve(aCoords2,aSize2); |
7fd59977 |
408 | |
409 | // creates wires based on the curves: |
410 | TopoDS_Wire aW1 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aCurve1)); |
411 | TopoDS_Wire aW2 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aCurve2)); |
412 | |
413 | // the sequence of wires: |
414 | TopTools_SequenceOfShape aWires; |
415 | |
416 | // adds the wires to the sequence: |
417 | aWires.Append(aW1); |
418 | aWires.Append(aW2); |
419 | |
420 | // creates shell passing through a set of wires |
421 | //with the faces are smoothed out by approximation: |
422 | TopoDS_Shape aShape = CreateShape(aWires); |
423 | |
424 | // draw this shell and wires: |
425 | aShowWire1 = drawShape(aW1); |
426 | aShowWire2 = drawShape(aW2); |
427 | if(WAIT_A_LITTLE) return; |
428 | aShow1 = drawShape(aShape); |
429 | if(WAIT_A_SECOND) return; |
430 | |
431 | // changes coordinates of second array: |
432 | for( Standard_Integer i = 0 ; i < aSize2 ; i++) |
433 | { |
434 | aCoords2[i][0] = 0.5*aCoords2[i][0]; |
435 | aCoords2[i][1] = -aCoords2[i][1]; |
436 | } |
437 | |
438 | // create curve: |
439 | aCurve2 = CreateCurve(aCoords2,aSize2); |
440 | |
441 | // create wire: |
442 | aW2 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(aCurve2)); |
443 | |
444 | // changes value of the last element of sequence: |
445 | aWires.SetValue(aWires.Length(),aW2); |
446 | |
447 | // creates shell with new wire: |
448 | aPrec = 0.5; |
449 | IsRuled = Standard_False; |
450 | aShape = CreateShape(aWires,IsSolid,IsRuled,aPrec); |
451 | |
452 | // draw this shell and new wire with precision aPrec: |
453 | aShowWire3 = drawShape(aW2); |
454 | getAISContext()->Erase(aShowWire2); |
455 | if(WAIT_A_LITTLE) return; |
456 | aShow2 = drawShape(aShape); |
457 | getAISContext()->Erase(aShow1); |
458 | if(WAIT_A_SECOND) return; |
459 | |
460 | |
461 | // creates elipses: |
462 | gce_MakeElips aMEl(gp::XOY(),5,3.5); |
463 | gce_MakeElips aME2(gp_Pnt(-2.5,0,7),gp_Pnt(0,2,7),gp_Pnt(0,0,7)); |
464 | gp_Elips aElips1 = aMEl.Value(); |
465 | gp_Elips aElips2 = aME2.Value(); |
466 | |
467 | // create edges based on pathes of elipses: |
468 | TopoDS_Edge aE1 = BRepBuilderAPI_MakeEdge(aElips1,-PI/4,5*PI/4); |
469 | TopoDS_Edge aE2 = BRepBuilderAPI_MakeEdge(aElips2,-PI/4,5*PI/4); |
470 | |
471 | // creates wires: |
472 | aW1 = BRepBuilderAPI_MakeWire(aE1); |
473 | aW2 = BRepBuilderAPI_MakeWire(aE2); |
474 | |
475 | // sequence clear: |
476 | aWires.Clear(); |
477 | |
478 | // adds wires to sequence: |
479 | aWires.Append(aW1); |
480 | aWires.Append(aW2); |
481 | |
482 | // creates shell based on the new wires: |
483 | aShape = CreateShape(aWires); |
484 | |
485 | // draw this shell and new wires: |
486 | drawShape(aW1); |
487 | drawShape(aW2); |
488 | getAISContext()->Erase(aShowWire1); |
489 | getAISContext()->Erase(aShowWire3); |
490 | if(WAIT_A_LITTLE) return; |
491 | aShow1 = drawShape(aShape); |
492 | getAISContext()->Erase(aShow2); |
493 | if(WAIT_A_SECOND) return; |
494 | |
495 | // creates polygon: |
496 | BRepBuilderAPI_MakePolygon aPol; |
497 | for( i = 0 ; i < aSize3; i++) |
498 | aPol.Add(gp_Pnt(aCoords3[i][0],aCoords3[i][1],aCoords3[i][2])); |
499 | |
500 | // takes the wire: |
501 | aW2 = aPol.Wire(); |
502 | |
503 | // adds the polygon wire to sequence: |
504 | aWires.Append(aW2); |
505 | |
506 | // creates shell passing through a set of wires |
507 | //with the faces are ruled: |
508 | IsRuled = Standard_True; |
509 | aShape = CreateShape(aWires,IsSolid,IsRuled); |
510 | |
511 | drawShape(aW2); |
512 | if(WAIT_A_LITTLE) return; |
513 | aShow2 = drawShape(aShape); |
514 | getAISContext()->Erase(aShow1); |
515 | } |
516 | |