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