62e1beed |
1 | #include <windows.h> |
15534713 |
2 | |
3 | // include required OCCT headers |
4 | #include <Standard_Version.hxx> |
5 | #include <Message_ProgressIndicator.hxx> |
6 | //for OCC graphic |
15534713 |
7 | #include <WNT_Window.hxx> |
62e1beed |
8 | #include <WNT_WClass.hxx> |
15534713 |
9 | #include <Graphic3d_CView.hxx> |
10 | #include <Graphic3d_Camera.hxx> |
11 | #include <Graphic3d_TextureParams.hxx> |
62e1beed |
12 | #include <D3DHost_GraphicDriver.hxx> |
c357e426 |
13 | #include <D3DHost_View.hxx> |
15534713 |
14 | //for object display |
15 | #include <V3d_Viewer.hxx> |
16 | #include <V3d_View.hxx> |
15534713 |
17 | #include <AIS_InteractiveContext.hxx> |
18 | #include <AIS_Shape.hxx> |
19 | //topology |
20 | #include <TopoDS_Shape.hxx> |
21 | #include <TopoDS_Compound.hxx> |
22 | //brep tools |
23 | #include <BRep_Builder.hxx> |
24 | #include <BRepTools.hxx> |
15534713 |
25 | // iges I/E |
26 | #include <IGESControl_Reader.hxx> |
27 | #include <IGESControl_Controller.hxx> |
28 | #include <IGESControl_Writer.hxx> |
29 | #include <IFSelect_ReturnStatus.hxx> |
30 | #include <Interface_Static.hxx> |
31 | //step I/E |
32 | #include <STEPControl_Reader.hxx> |
33 | #include <STEPControl_Writer.hxx> |
34 | //for stl export |
35 | #include <StlAPI_Writer.hxx> |
36 | //for vrml export |
37 | #include <VrmlAPI_Writer.hxx> |
38 | //wrapper of pure C++ classes to ref classes |
39 | #include <NCollection_Haft.h> |
40 | |
41 | // list of required OCCT libraries |
42 | #pragma comment(lib, "TKernel.lib") |
43 | #pragma comment(lib, "TKMath.lib") |
44 | #pragma comment(lib, "TKBRep.lib") |
15534713 |
45 | #pragma comment(lib, "TKXSBase.lib") |
46 | #pragma comment(lib, "TKService.lib") |
47 | #pragma comment(lib, "TKV3d.lib") |
48 | #pragma comment(lib, "TKOpenGl.lib") |
62e1beed |
49 | #pragma comment(lib, "TKD3dHost.lib") |
15534713 |
50 | #pragma comment(lib, "TKIGES.lib") |
51 | #pragma comment(lib, "TKSTEP.lib") |
52 | #pragma comment(lib, "TKStl.lib") |
53 | #pragma comment(lib, "TKVrml.lib") |
54 | |
62e1beed |
55 | #pragma comment(lib, "D3D9.lib") |
56 | |
15534713 |
57 | /// <summary> |
58 | /// Proxy class encapsulating calls to OCCT C++ classes within |
59 | /// C++/CLI class visible from .Net (CSharp) |
60 | /// </summary> |
61 | public ref class OCCTProxyD3D |
62 | { |
63 | public: |
64 | |
62e1beed |
65 | OCCTProxyD3D() {} |
15534713 |
66 | |
67 | // ============================================ |
68 | // Viewer functionality |
69 | // ============================================ |
70 | |
71 | /// <summary> |
72 | ///Initialize a viewer |
73 | /// </summary> |
74 | /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param> |
62e1beed |
75 | bool InitViewer() |
15534713 |
76 | { |
62e1beed |
77 | myGraphicDriver() = new D3DHost_GraphicDriver(); |
78 | myGraphicDriver()->ChangeOptions().buffersNoSwap = true; |
15534713 |
79 | //myGraphicDriver()->ChangeOptions().contextDebug = true; |
80 | |
6a24c6de |
81 | myViewer() = new V3d_Viewer (myGraphicDriver()); |
15534713 |
82 | myViewer()->SetDefaultLights(); |
83 | myViewer()->SetLightOn(); |
84 | myView() = myViewer()->CreateView(); |
62e1beed |
85 | |
86 | static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC); |
87 | Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64); |
88 | aWNTWindow->SetVirtual (Standard_True); |
15534713 |
89 | myView()->SetWindow(aWNTWindow); |
15534713 |
90 | myAISContext() = new AIS_InteractiveContext (myViewer()); |
91 | myAISContext()->UpdateCurrentViewer(); |
92 | myView()->MustBeResized(); |
93 | return true; |
94 | } |
95 | |
15534713 |
96 | /// <summary> Resizes custom FBO for Direct3D output. </summary> |
62e1beed |
97 | System::IntPtr ResizeBridgeFBO (int theWinSizeX, |
98 | int theWinSizeY) |
15534713 |
99 | { |
62e1beed |
100 | Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window()); |
101 | aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY); |
102 | myView()->MustBeResized(); |
103 | myView()->Invalidate(); |
c357e426 |
104 | return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface()); |
15534713 |
105 | } |
106 | |
107 | /// <summary> |
108 | /// Make dump of current view to file |
109 | /// </summary> |
110 | /// <param name="theFileName">Name of dump file</param> |
111 | bool Dump (const char* theFileName) |
112 | { |
113 | if (myView().IsNull()) |
114 | { |
115 | return false; |
116 | } |
117 | myView()->Redraw(); |
118 | return myView()->Dump (theFileName) != Standard_False; |
119 | } |
120 | |
121 | /// <summary> |
122 | ///Redraw view |
123 | /// </summary> |
124 | void RedrawView() |
125 | { |
126 | if (!myView().IsNull()) |
127 | { |
128 | myView()->Redraw(); |
129 | } |
130 | } |
131 | |
132 | /// <summary> |
133 | ///Update view |
134 | /// </summary> |
135 | void UpdateView(void) |
136 | { |
137 | if (!myView().IsNull()) |
138 | { |
139 | myView()->MustBeResized(); |
140 | } |
141 | } |
142 | |
143 | /// <summary> |
144 | ///Set computed mode in false |
145 | /// </summary> |
146 | void SetDegenerateModeOn() |
147 | { |
148 | if (!myView().IsNull()) |
149 | { |
150 | myView()->SetComputedMode (Standard_False); |
1eeef710 |
151 | myView()->Redraw(); |
15534713 |
152 | } |
153 | } |
154 | |
155 | /// <summary> |
156 | ///Set computed mode in true |
157 | /// </summary> |
158 | void SetDegenerateModeOff() |
159 | { |
160 | if (!myView().IsNull()) |
161 | { |
162 | myView()->SetComputedMode (Standard_True); |
1eeef710 |
163 | myView()->Redraw(); |
15534713 |
164 | } |
165 | } |
166 | |
167 | /// <summary> |
168 | ///Fit all |
169 | /// </summary> |
170 | void WindowFitAll (int theXmin, int theYmin, |
171 | int theXmax, int theYmax) |
172 | { |
173 | if (!myView().IsNull()) |
174 | { |
175 | myView()->WindowFitAll (theXmin, theYmin, theXmax, theYmax); |
176 | } |
177 | } |
178 | |
179 | /// <summary> |
180 | ///Current place of window |
181 | /// </summary> |
182 | /// <param name="theZoomFactor">Current zoom</param> |
183 | void Place (int theX, int theY, float theZoomFactor) |
184 | { |
ee2be2a8 |
185 | Standard_Real aZoomFactor = theZoomFactor; |
15534713 |
186 | if (!myView().IsNull()) |
187 | { |
188 | myView()->Place (theX, theY, aZoomFactor); |
189 | } |
190 | } |
191 | |
192 | /// <summary> |
193 | ///Set Zoom |
194 | /// </summary> |
195 | void Zoom (int theX1, int theY1, int theX2, int theY2) |
196 | { |
197 | if (!myView().IsNull()) |
198 | { |
199 | myView()->Zoom (theX1, theY1, theX2, theY2); |
200 | } |
201 | } |
202 | |
203 | /// <summary> |
204 | ///Set Pan |
205 | /// </summary> |
206 | void Pan (int theX, int theY) |
207 | { |
208 | if (!myView().IsNull()) |
209 | { |
210 | myView()->Pan (theX, theY); |
211 | } |
212 | } |
213 | |
214 | /// <summary> |
215 | ///Rotation |
216 | /// </summary> |
217 | void Rotation (int theX, int theY) |
218 | { |
219 | if (!myView().IsNull()) |
220 | { |
221 | myView()->Rotation (theX, theY); |
222 | } |
223 | } |
224 | |
225 | /// <summary> |
226 | ///Start rotation |
227 | /// </summary> |
228 | void StartRotation (int theX, int theY) |
229 | { |
230 | if (!myView().IsNull()) |
231 | { |
232 | myView()->StartRotation (theX, theY); |
233 | } |
234 | } |
235 | |
236 | /// <summary> |
237 | ///Select by rectangle |
238 | /// </summary> |
239 | void Select (int theX1, int theY1, int theX2, int theY2) |
240 | { |
241 | if (!myAISContext().IsNull()) |
242 | { |
0577ae8c |
243 | myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True); |
15534713 |
244 | } |
245 | } |
246 | |
247 | /// <summary> |
248 | ///Select by click |
249 | /// </summary> |
250 | void Select() |
251 | { |
252 | if (!myAISContext().IsNull()) |
253 | { |
0577ae8c |
254 | myAISContext()->Select (Standard_True); |
15534713 |
255 | } |
256 | } |
257 | |
258 | /// <summary> |
259 | ///Move view |
260 | /// </summary> |
261 | void MoveTo (int theX, int theY) |
262 | { |
263 | if (!myAISContext().IsNull() && !myView().IsNull()) |
264 | { |
0577ae8c |
265 | myAISContext()->MoveTo (theX, theY, myView(), Standard_True); |
15534713 |
266 | } |
267 | } |
268 | |
269 | /// <summary> |
270 | ///Select by rectangle with pressed "Shift" key |
271 | /// </summary> |
272 | void ShiftSelect (int theX1, int theY1, int theX2, int theY2) |
273 | { |
274 | if (!myAISContext().IsNull() && !myView().IsNull()) |
275 | { |
0577ae8c |
276 | myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True); |
15534713 |
277 | } |
278 | } |
279 | |
280 | /// <summary> |
281 | ///Select by "Shift" key |
282 | /// </summary> |
283 | void ShiftSelect() |
284 | { |
285 | if (!myAISContext().IsNull()) |
286 | { |
0577ae8c |
287 | myAISContext()->ShiftSelect (Standard_True); |
15534713 |
288 | } |
289 | } |
290 | |
291 | /// <summary> |
292 | ///Set background color |
293 | /// </summary> |
294 | void BackgroundColor (int& theRed, int& theGreen, int& theBlue) |
295 | { |
296 | if (!myView().IsNull()) |
297 | { |
298 | Quantity_Color aColor = myView()->BackgroundColor(); |
299 | theRed = (int )aColor.Red() * 255; |
300 | theGreen = (int )aColor.Green() * 255; |
301 | theBlue = (int )aColor.Blue() * 255; |
302 | } |
303 | } |
304 | |
305 | /// <summary> |
306 | ///Get background color Red |
307 | /// </summary> |
308 | int GetBGColR() |
309 | { |
310 | int anRgb[3]; |
311 | BackgroundColor (anRgb[0], anRgb[1], anRgb[2]); |
312 | return anRgb[0]; |
313 | } |
314 | |
315 | /// <summary> |
316 | ///Get background color Green |
317 | /// </summary> |
318 | int GetBGColG() |
319 | { |
320 | int anRgb[3]; |
321 | BackgroundColor (anRgb[0], anRgb[1], anRgb[2]); |
322 | return anRgb[1]; |
323 | } |
324 | |
325 | /// <summary> |
326 | ///Get background color Blue |
327 | /// </summary> |
328 | int GetBGColB() |
329 | { |
330 | int anRgb[3]; |
331 | BackgroundColor (anRgb[0], anRgb[1], anRgb[2]); |
332 | return anRgb[2]; |
333 | } |
334 | |
335 | /// <summary> |
336 | ///Update current viewer |
337 | /// </summary> |
338 | void UpdateCurrentViewer() |
339 | { |
340 | if (!myAISContext().IsNull()) |
341 | { |
342 | myAISContext()->UpdateCurrentViewer(); |
343 | } |
344 | } |
345 | |
346 | /// <summary> |
347 | ///Front side |
348 | /// </summary> |
349 | void FrontView() |
350 | { |
351 | if (!myView().IsNull()) |
352 | { |
32757c6e |
353 | myView()->SetProj (V3d_Yneg); |
15534713 |
354 | } |
355 | } |
356 | |
357 | /// <summary> |
358 | ///Top side |
359 | /// </summary> |
360 | void TopView() |
361 | { |
362 | if (!myView().IsNull()) |
363 | { |
364 | myView()->SetProj (V3d_Zpos); |
365 | } |
366 | } |
367 | |
368 | /// <summary> |
369 | ///Left side |
370 | /// </summary> |
371 | void LeftView() |
372 | { |
373 | if (!myView().IsNull()) |
374 | { |
32757c6e |
375 | myView()->SetProj (V3d_Xneg); |
15534713 |
376 | } |
377 | } |
378 | |
379 | /// <summary> |
380 | ///Back side |
381 | /// </summary> |
382 | void BackView() |
383 | { |
384 | if (!myView().IsNull()) |
385 | { |
32757c6e |
386 | myView()->SetProj (V3d_Ypos); |
15534713 |
387 | } |
388 | } |
389 | |
390 | /// <summary> |
391 | ///Right side |
392 | /// </summary> |
393 | void RightView() |
394 | { |
395 | if (!myView().IsNull()) |
396 | { |
32757c6e |
397 | myView()->SetProj (V3d_Xpos); |
15534713 |
398 | } |
399 | } |
400 | |
401 | /// <summary> |
402 | ///Bottom side |
403 | /// </summary> |
404 | void BottomView() |
405 | { |
406 | if (!myView().IsNull()) |
407 | { |
408 | myView()->SetProj (V3d_Zneg); |
409 | } |
410 | } |
411 | |
412 | /// <summary> |
413 | ///Axo side |
414 | /// </summary> |
415 | void AxoView() |
416 | { |
417 | if (!myView().IsNull()) |
418 | { |
419 | myView()->SetProj (V3d_XposYnegZpos); |
420 | } |
421 | } |
422 | |
423 | /// <summary> |
424 | ///Scale |
425 | /// </summary> |
426 | float Scale() |
427 | { |
428 | return myView().IsNull() |
429 | ? -1.0f |
430 | : float(myView()->Scale()); |
431 | } |
432 | |
433 | /// <summary> |
434 | ///Zoom in all view |
435 | /// </summary> |
436 | void ZoomAllView() |
437 | { |
438 | if (!myView().IsNull()) |
439 | { |
440 | myView()->FitAll(); |
441 | myView()->ZFitAll(); |
442 | } |
443 | } |
444 | |
445 | /// <summary> |
446 | ///Reset view |
447 | /// </summary> |
448 | void Reset() |
449 | { |
450 | if (!myView().IsNull()) |
451 | { |
452 | myView()->Reset(); |
453 | } |
454 | } |
455 | |
456 | /// <summary> |
457 | ///Set display mode of objects |
458 | /// </summary> |
459 | /// <param name="theMode">Set current mode</param> |
460 | void SetDisplayMode (int theMode) |
461 | { |
462 | if (myAISContext().IsNull()) |
463 | { |
464 | return; |
465 | } |
466 | |
467 | AIS_DisplayMode aCurrentMode = theMode == 0 |
468 | ? AIS_WireFrame |
469 | : AIS_Shaded; |
0577ae8c |
470 | if (myAISContext()->NbSelected() == 0) |
15534713 |
471 | { |
0577ae8c |
472 | myAISContext()->SetDisplayMode (aCurrentMode, Standard_False); |
15534713 |
473 | } |
474 | else |
475 | { |
0577ae8c |
476 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
477 | { |
0577ae8c |
478 | myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False); |
15534713 |
479 | } |
480 | } |
481 | myAISContext()->UpdateCurrentViewer(); |
482 | } |
483 | |
484 | /// <summary> |
485 | ///Set color |
486 | /// </summary> |
487 | void SetColor (int theR, int theG, int theB) |
488 | { |
489 | if (myAISContext().IsNull()) |
490 | { |
491 | return; |
492 | } |
493 | |
494 | Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB); |
0577ae8c |
495 | for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
496 | { |
87432b82 |
497 | myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false); |
15534713 |
498 | } |
0577ae8c |
499 | myAISContext()->UpdateCurrentViewer(); |
15534713 |
500 | } |
501 | |
502 | /// <summary> |
503 | ///Get object color red |
504 | /// </summary> |
505 | int GetObjColR() |
506 | { |
507 | int anRgb[3]; |
508 | ObjectColor (anRgb[0], anRgb[1], anRgb[2]); |
509 | return anRgb[0]; |
510 | } |
511 | |
512 | /// <summary> |
513 | ///Get object color green |
514 | /// </summary> |
515 | int GetObjColG() |
516 | { |
517 | int anRgb[3]; |
518 | ObjectColor (anRgb[0], anRgb[1], anRgb[2]); |
519 | return anRgb[1]; |
520 | } |
521 | |
522 | /// <summary> |
523 | ///Get object color blue |
524 | /// </summary> |
525 | int GetObjColB() |
526 | { |
527 | int anRgb[3]; |
528 | ObjectColor (anRgb[0], anRgb[1], anRgb[2]); |
529 | return anRgb[2]; |
530 | } |
531 | |
532 | /// <summary> |
533 | ///Get object color R/G/B |
534 | /// </summary> |
535 | void ObjectColor (int& theRed, int& theGreen, int& theBlue) |
536 | { |
537 | if (myAISContext().IsNull()) |
538 | { |
539 | return; |
540 | } |
541 | |
542 | theRed = 255; |
543 | theGreen = 255; |
544 | theBlue = 255; |
0577ae8c |
545 | myAISContext()->InitSelected(); |
546 | if (!myAISContext()->MoreSelected()) |
15534713 |
547 | { |
548 | return; |
549 | } |
550 | |
0577ae8c |
551 | Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive(); |
15534713 |
552 | if (aCurrent->HasColor()) |
553 | { |
87432b82 |
554 | Quantity_Color anObjCol; |
555 | myAISContext()->Color (aCurrent, anObjCol); |
15534713 |
556 | theRed = int(anObjCol.Red() * 255.0); |
557 | theGreen = int(anObjCol.Green() * 255.0); |
558 | theBlue = int(anObjCol.Blue() * 255.0); |
559 | } |
560 | } |
561 | |
562 | /// <summary> |
563 | ///Set background color R/G/B |
564 | /// </summary> |
565 | void SetBackgroundColor (int theRed, int theGreen, int theBlue) |
566 | { |
567 | if (!myView().IsNull()) |
568 | { |
569 | myView()->SetBackgroundColor (Quantity_TOC_RGB, theRed / 255.0, theGreen / 255.0, theBlue / 255.0); |
570 | } |
571 | } |
572 | |
573 | /// <summary> |
574 | ///Erase objects |
575 | /// </summary> |
576 | void EraseObjects() |
577 | { |
578 | if (myAISContext().IsNull()) |
579 | { |
580 | return; |
581 | } |
7140edaf |
582 | |
0577ae8c |
583 | myAISContext()->EraseSelected (Standard_False); |
584 | myAISContext()->ClearSelected (Standard_True); |
15534713 |
585 | } |
586 | |
587 | /// <summary> |
588 | ///Get version |
589 | /// </summary> |
590 | float GetOCCVersion() |
591 | { |
592 | return (float )OCC_VERSION; |
593 | } |
594 | |
595 | /// <summary> |
596 | ///set material |
597 | /// </summary> |
598 | void SetMaterial (int theMaterial) |
599 | { |
600 | if (myAISContext().IsNull()) |
601 | { |
602 | return; |
603 | } |
0577ae8c |
604 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
605 | { |
0577ae8c |
606 | myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False); |
15534713 |
607 | } |
608 | myAISContext()->UpdateCurrentViewer(); |
609 | } |
610 | |
611 | /// <summary> |
612 | ///set transparency |
613 | /// </summary> |
614 | void SetTransparency (int theTrans) |
615 | { |
616 | if (myAISContext().IsNull()) |
617 | { |
618 | return; |
619 | } |
0577ae8c |
620 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
621 | { |
0577ae8c |
622 | myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0, Standard_False); |
15534713 |
623 | } |
0577ae8c |
624 | myAISContext()->UpdateCurrentViewer(); |
15534713 |
625 | } |
626 | |
627 | /// <summary> |
628 | ///Return true if object is selected |
629 | /// </summary> |
630 | bool IsObjectSelected() |
631 | { |
632 | if (myAISContext().IsNull()) |
633 | { |
634 | return false; |
635 | } |
0577ae8c |
636 | myAISContext()->InitSelected(); |
637 | return myAISContext()->MoreSelected() != Standard_False; |
15534713 |
638 | } |
639 | |
640 | /// <summary> |
641 | ///Return display mode |
642 | /// </summary> |
643 | int DisplayMode() |
644 | { |
645 | if (myAISContext().IsNull()) |
646 | { |
647 | return -1; |
648 | } |
649 | |
650 | bool isOneOrMoreInShading = false; |
651 | bool isOneOrMoreInWireframe = false; |
0577ae8c |
652 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
653 | { |
0577ae8c |
654 | if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded)) |
15534713 |
655 | { |
656 | isOneOrMoreInShading = true; |
657 | } |
0577ae8c |
658 | if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame)) |
15534713 |
659 | { |
660 | isOneOrMoreInWireframe = true; |
661 | } |
662 | } |
663 | if (isOneOrMoreInShading |
664 | && isOneOrMoreInWireframe) |
665 | { |
666 | return 10; |
667 | } |
668 | else if (isOneOrMoreInShading) |
669 | { |
670 | return 1; |
671 | } |
672 | else if (isOneOrMoreInWireframe) |
673 | { |
674 | return 0; |
675 | } |
676 | return -1; |
677 | } |
678 | |
679 | /// <summary> |
15534713 |
680 | ///Set AISContext |
681 | /// </summary> |
682 | bool SetAISContext (OCCTProxyD3D^ theViewer) |
683 | { |
684 | this->myAISContext() = theViewer->GetContext(); |
685 | if (myAISContext().IsNull()) |
686 | { |
687 | return false; |
688 | } |
689 | return true; |
690 | } |
691 | |
692 | /// <summary> |
693 | ///Get AISContext |
694 | /// </summary> |
92efcf78 |
695 | Handle(AIS_InteractiveContext) GetContext() |
15534713 |
696 | { |
697 | return myAISContext(); |
698 | } |
699 | |
700 | public: |
701 | // ============================================ |
702 | // Import / export functionality |
703 | // ============================================ |
704 | |
705 | /// <summary> |
706 | ///Import BRep file |
707 | /// </summary> |
708 | /// <param name="theFileName">Name of import file</param> |
709 | bool ImportBrep (System::String^ theFileName) |
710 | { |
711 | bool isResult = false; |
712 | int aLength = theFileName->Length; |
713 | char* aFilename = new char[aLength + 1]; |
714 | for(int i = 0; i < aLength; ++i) |
715 | { |
716 | aFilename[i] = (char )theFileName->ToCharArray()[i]; |
717 | } |
718 | aFilename[aLength] = '\0'; |
719 | isResult = ImportBrep (aFilename); |
720 | delete[] aFilename; |
721 | return isResult; |
722 | } |
723 | |
724 | /// <summary> |
725 | ///Import BRep file |
726 | /// </summary> |
727 | /// <param name="theFileName">Name of import file</param> |
728 | bool ImportBrep (char* theFileName) |
729 | { |
730 | TopoDS_Shape aShape; |
731 | BRep_Builder aBuilder; |
732 | if (!BRepTools::Read (aShape, theFileName, aBuilder)) |
733 | { |
734 | return false; |
735 | } |
0577ae8c |
736 | |
15534713 |
737 | Handle(AIS_Shape) aPrs = new AIS_Shape (aShape); |
0577ae8c |
738 | myAISContext()->SetMaterial (aPrs, Graphic3d_NOM_GOLD, Standard_False); |
15534713 |
739 | myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False); |
0577ae8c |
740 | myAISContext()->Display (aPrs, Standard_True); |
15534713 |
741 | return true; |
742 | } |
743 | |
744 | /// <summary> |
15534713 |
745 | ///Import Step file |
746 | /// </summary> |
747 | /// <param name="theFileName">Name of import file</param> |
748 | bool ImportStep (char* theFileName) |
749 | { |
750 | STEPControl_Reader aReader; |
751 | if (aReader.ReadFile (theFileName) != IFSelect_RetDone) |
752 | { |
753 | return false; |
754 | } |
755 | |
756 | bool isFailsonly = false; |
757 | aReader.PrintCheckLoad( isFailsonly, IFSelect_ItemsByEntity ); |
758 | |
759 | int aNbRoot = aReader.NbRootsForTransfer(); |
760 | aReader.PrintCheckTransfer (isFailsonly, IFSelect_ItemsByEntity); |
761 | for (Standard_Integer aRootIter = 1; aRootIter <= aNbRoot; ++aRootIter) |
762 | { |
763 | aReader.TransferRoot (aRootIter); |
764 | int aNbShap = aReader.NbShapes(); |
765 | if (aNbShap > 0) |
766 | { |
767 | for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter) |
768 | { |
0577ae8c |
769 | myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False); |
15534713 |
770 | } |
0577ae8c |
771 | myAISContext()->UpdateCurrentViewer(); |
15534713 |
772 | } |
773 | } |
774 | return true; |
775 | } |
776 | |
777 | /// <summary> |
778 | ///Import Iges file |
779 | /// </summary> |
780 | /// <param name="theFileName">Name of import file</param> |
781 | bool ImportIges (char* theFileName) |
782 | { |
783 | IGESControl_Reader aReader; |
784 | if (aReader.ReadFile (theFileName) != IFSelect_RetDone) |
785 | { |
786 | return false; |
787 | } |
788 | |
789 | aReader.TransferRoots(); |
790 | TopoDS_Shape aShape = aReader.OneShape(); |
791 | myAISContext()->Display (new AIS_Shape (aShape), Standard_False); |
792 | myAISContext()->UpdateCurrentViewer(); |
793 | return true; |
794 | } |
795 | |
796 | /// <summary> |
797 | ///Export BRep file |
798 | /// </summary> |
799 | /// <param name="theFileName">Name of export file</param> |
800 | bool ExportBRep (char* theFileName) |
801 | { |
0577ae8c |
802 | myAISContext()->InitSelected(); |
803 | if (!myAISContext()->MoreSelected()) |
15534713 |
804 | { |
805 | return false; |
806 | } |
807 | |
0577ae8c |
808 | Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive()); |
15534713 |
809 | return !anIS.IsNull() |
810 | && BRepTools::Write (anIS->Shape(), theFileName); |
811 | } |
812 | |
813 | /// <summary> |
814 | ///Export Step file |
815 | /// </summary> |
816 | /// <param name="theFileName">Name of export file</param> |
817 | bool ExportStep (char* theFileName) |
818 | { |
819 | STEPControl_StepModelType aType = STEPControl_AsIs; |
820 | STEPControl_Writer aWriter; |
0577ae8c |
821 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
822 | { |
0577ae8c |
823 | Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive()); |
15534713 |
824 | if (anIS.IsNull()) |
825 | { |
826 | return false; |
827 | } |
828 | |
829 | TopoDS_Shape aShape = anIS->Shape(); |
830 | if (aWriter.Transfer (aShape, aType) != IFSelect_RetDone) |
831 | { |
832 | return false; |
833 | } |
834 | } |
835 | return aWriter.Write (theFileName) == IFSelect_RetDone; |
836 | } |
837 | |
838 | /// <summary> |
839 | ///Export Iges file |
840 | /// </summary> |
841 | /// <param name="theFileName">Name of export file</param> |
842 | bool ExportIges (char* theFileName) |
843 | { |
844 | IGESControl_Controller::Init(); |
845 | IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"), |
846 | Interface_Static::IVal ("XSTEP.iges.writebrep.mode")); |
0577ae8c |
847 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
848 | { |
0577ae8c |
849 | Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive()); |
15534713 |
850 | if (anIS.IsNull()) |
851 | { |
852 | return false; |
853 | } |
854 | |
855 | aWriter.AddShape (anIS->Shape()); |
856 | } |
857 | |
858 | aWriter.ComputeModel(); |
859 | return aWriter.Write (theFileName) != Standard_False; |
860 | } |
861 | |
862 | /// <summary> |
863 | ///Export Vrml file |
864 | /// </summary> |
865 | /// <param name="theFileName">Name of export file</param> |
866 | bool ExportVrml (char* theFileName) |
867 | { |
868 | TopoDS_Compound aRes; |
869 | BRep_Builder aBuilder; |
870 | aBuilder.MakeCompound (aRes); |
0577ae8c |
871 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
872 | { |
0577ae8c |
873 | Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive()); |
15534713 |
874 | if (anIS.IsNull()) |
875 | { |
876 | return false; |
877 | } |
878 | aBuilder.Add (aRes, anIS->Shape()); |
879 | } |
880 | |
881 | VrmlAPI_Writer aWriter; |
882 | aWriter.Write (aRes, theFileName); |
883 | return true; |
884 | } |
885 | |
886 | /// <summary> |
887 | ///Export Stl file |
888 | /// </summary> |
889 | /// <param name="theFileName">Name of export file</param> |
890 | bool ExportStl (char* theFileName) |
891 | { |
892 | TopoDS_Compound aComp; |
893 | BRep_Builder aBuilder; |
894 | aBuilder.MakeCompound (aComp); |
0577ae8c |
895 | for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected()) |
15534713 |
896 | { |
0577ae8c |
897 | Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive()); |
15534713 |
898 | if (anIS.IsNull()) |
899 | { |
900 | return false; |
901 | } |
902 | aBuilder.Add (aComp, anIS->Shape()); |
903 | } |
904 | |
905 | StlAPI_Writer aWriter; |
906 | aWriter.Write (aComp, theFileName); |
907 | return true; |
908 | } |
909 | |
910 | /// <summary> |
911 | ///Define which Import/Export function must be called |
912 | /// </summary> |
913 | /// <param name="theFileName">Name of Import/Export file</param> |
914 | /// <param name="theFormat">Determines format of Import/Export file</param> |
915 | /// <param name="theIsImport">Determines is Import or not</param> |
916 | bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport) |
917 | { |
918 | bool isResult = false; |
919 | int aLength = theFileName->Length; |
920 | char* aFilename = new char[aLength + 1]; |
921 | for (int aCharIter = 0; aCharIter < aLength; ++aCharIter) |
922 | { |
923 | aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter]; |
924 | } |
925 | aFilename[aLength] = '\0'; |
926 | |
927 | if (theIsImport) |
928 | { |
929 | switch (theFormat) |
930 | { |
931 | case 0: isResult = ImportBrep (aFilename); break; |
41f03605 |
932 | case 1: isResult = ImportStep (aFilename); break; |
933 | case 2: isResult = ImportIges (aFilename); break; |
15534713 |
934 | } |
935 | } |
936 | else |
937 | { |
938 | switch (theFormat) |
939 | { |
940 | case 0: isResult = ExportBRep (aFilename); break; |
41f03605 |
941 | case 1: isResult = ExportStep (aFilename); break; |
942 | case 2: isResult = ExportIges (aFilename); break; |
943 | case 3: isResult = ExportVrml (aFilename); break; |
944 | case 4: isResult = ExportStl (aFilename); break; |
945 | case 5: isResult = Dump (aFilename); break; |
15534713 |
946 | } |
947 | } |
948 | delete[] aFilename; |
949 | return isResult; |
950 | } |
951 | |
952 | /// <summary> |
953 | ///Initialize OCCTProxyD3D |
954 | /// </summary> |
955 | void InitOCCTProxy() |
956 | { |
957 | myGraphicDriver().Nullify(); |
958 | myViewer().Nullify(); |
959 | myView().Nullify(); |
960 | myAISContext().Nullify(); |
961 | } |
962 | |
963 | private: |
964 | |
92efcf78 |
965 | NCollection_Haft<Handle(V3d_Viewer)> myViewer; |
966 | NCollection_Haft<Handle(V3d_View)> myView; |
967 | NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext; |
968 | NCollection_Haft<Handle(D3DHost_GraphicDriver)> myGraphicDriver; |
15534713 |
969 | |
970 | }; |