Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / CSharp / OCC / OCCViewer.cpp
1 #include "StdAfx.h"
2 #include ".\occviewer.h"
3
4 #pragma warning( disable : 4800 )
5 OCCViewer::OCCViewer(void)
6 {
7         myGraphicDevice=NULL;
8         myViewer=NULL;
9         myView=NULL;
10         myAISContext=NULL;
11 }
12
13 OCCViewer::~OCCViewer(void)
14 {
15         myView->Remove();
16 }
17
18 bool OCCViewer::InitViewer(void* wnd)
19 {
20         try {
21                         myGraphicDevice = new  Graphic3d_WNTGraphicDevice();
22                 } catch (Standard_Failure) {
23                         return false;
24                 }
25         TCollection_ExtendedString a3DName("Visu3D");
26         myViewer = new V3d_Viewer( myGraphicDevice, a3DName.ToExtString(),"", 1000.0, 
27                                                                         V3d_XposYnegZpos, Quantity_NOC_GRAY30,
28                                                                         V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT, 
29                                                                         Standard_True, Standard_False);
30
31         myViewer->Init();
32         myViewer->SetDefaultLights();
33         myViewer->SetLightOn();
34         myView = myViewer->CreateView();
35         Handle(WNT_Window) aWNTWindow = new WNT_Window(myGraphicDevice, reinterpret_cast<HWND> (wnd));
36         myView->SetWindow(aWNTWindow);
37         if (!aWNTWindow->IsMapped()) 
38                  aWNTWindow->Map();
39         myAISContext = new AIS_InteractiveContext(myViewer);
40         myAISContext->UpdateCurrentViewer();
41         myView->Redraw();
42         myView->MustBeResized();
43         return true;
44
45 }
46
47 bool OCCViewer::ImportBRep(char* filename)
48 {
49         Standard_CString aFileName = (Standard_CString) filename;
50         TopoDS_Shape aShape;
51     BRep_Builder aBuilder;
52     Standard_Boolean result = BRepTools::Read(aShape,aFileName,aBuilder);
53         if (!result)
54                 return false;
55         if(myAISContext->HasOpenedContext())
56                 myAISContext->CloseLocalContext();
57         myAISContext->Display(new AIS_Shape(aShape));
58         return true;
59 }
60
61 void OCCViewer::UpdateView(void)
62 {
63         if (!myView.IsNull())
64                 myView->MustBeResized();
65 }
66
67 void OCCViewer::RedrawView(void)
68 {
69         if (!myView.IsNull())
70                 myView->Redraw();
71 }
72
73 void OCCViewer::SetDegenerateModeOn(void)
74 {
75         if (!myView.IsNull())
76                 myView->SetDegenerateModeOn();
77 }
78
79 void OCCViewer::SetDegenerateModeOff(void)
80 {
81         if (!myView.IsNull())
82                 myView->SetDegenerateModeOff();
83 }
84
85 void OCCViewer::WindowFitAll(int Xmin, int Ymin, int Xmax, int Ymax)
86 {
87         if (!myView.IsNull())
88                 myView->WindowFitAll(Xmin, Ymin, Xmax, Ymax);
89 }
90
91 void OCCViewer::Place(int x, int y, float zoomFactor)
92 {
93         Quantity_Factor aZoomFactor = zoomFactor;
94         if (!myView.IsNull())
95                 myView->Place(x, y, aZoomFactor);
96 }
97
98 void OCCViewer::Zoom(int x1, int y1, int x2, int y2)
99 {
100         if (!myView.IsNull())
101                 myView->Zoom(x1, y1, x2, y2);
102 }
103
104 void OCCViewer::Pan(int x, int y)
105 {
106         if (!myView.IsNull())
107                 myView->Pan(x, y);
108 }
109
110 void OCCViewer::Rotation(int x, int y)
111 {
112         if (!myView.IsNull())
113                 myView->Rotation(x, y);
114 }
115
116 void OCCViewer::StartRotation(int x, int y)
117 {
118         if (!myView.IsNull())
119                 myView->StartRotation(x, y);
120 }
121
122 void OCCViewer::Select(int x1, int y1, int x2, int y2)
123 {
124         if (!myAISContext.IsNull())
125                 myAISContext->Select(x1, y1, x2, y2, myView);
126 }
127
128 void OCCViewer::Select(void)
129 {
130         if (!myAISContext.IsNull())
131                 myAISContext->Select();
132 }
133
134 void OCCViewer::MoveTo(int x, int y)
135 {
136         if ((!myAISContext.IsNull()) && (!myView.IsNull()))
137                 myAISContext->MoveTo(x, y, myView);
138 }
139
140 void OCCViewer::ShiftSelect(int x1, int y1, int x2, int y2)
141 {
142         if ((!myAISContext.IsNull()) && (!myView.IsNull()))
143                 myAISContext->ShiftSelect(x1, y1, x2, y2, myView);
144 }
145
146 void OCCViewer::ShiftSelect(void)
147 {
148         if (!myAISContext.IsNull())
149                 myAISContext->ShiftSelect();
150 }
151
152 void OCCViewer::BackgroundColor(int& r, int& g, int& b)
153 {
154         Standard_Real R1;
155         Standard_Real G1;
156         Standard_Real B1;
157         if (!myView.IsNull())
158                 myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
159         r = (int)R1*255;
160         g = (int)G1*255;
161         b = (int)B1*255;
162 }
163
164
165
166 void OCCViewer::UpdateCurrentViewer(void)
167 {
168         if (!myAISContext.IsNull())
169                 myAISContext->UpdateCurrentViewer();
170 }
171
172 void OCCViewer::FrontView(void)
173 {
174         if (!myView.IsNull())
175                 myView->SetProj(V3d_Xpos); 
176 }
177
178 void OCCViewer::TopView(void)
179 {
180         if (!myView.IsNull())
181                 myView->SetProj(V3d_Zpos); 
182 }
183
184 void OCCViewer::LeftView(void)
185 {
186         if (!myView.IsNull())
187                 myView->SetProj(V3d_Ypos);
188 }
189
190 void OCCViewer::BackView(void)
191 {
192         if (!myView.IsNull())
193                 myView->SetProj(V3d_Xneg);
194 }
195
196 void OCCViewer::RightView(void)
197 {
198         if (!myView.IsNull())
199                 myView->SetProj(V3d_Yneg);
200 }
201
202 void OCCViewer::BottomView(void)
203 {
204         if (!myView.IsNull())
205                 myView->SetProj(V3d_Zneg); 
206 }
207
208 void OCCViewer::AxoView(void)
209 {
210         if (!myView.IsNull())
211                 myView->SetProj(V3d_XposYnegZpos);
212 }
213
214 void OCCViewer::ZoomAllView(void)
215 {
216         if (!myView.IsNull())
217         {
218                 myView->FitAll();
219                 myView->ZFitAll();
220         }
221 }
222
223 float OCCViewer::Scale(void)
224 {
225         if (myView.IsNull())
226                 return -1;
227         else
228                 return (float)myView->Scale();
229 }
230
231 void OCCViewer::ResetView(void)
232 {
233         if (!myView.IsNull())
234                 myView->Reset();
235 }
236
237 void OCCViewer::SetDisplayMode(int aMode)
238 {
239         if (myAISContext.IsNull())
240                 return;
241         AIS_DisplayMode CurrentMode;
242         if (aMode == 0) 
243                 CurrentMode=AIS_WireFrame;
244         else
245                 CurrentMode=AIS_Shaded;
246     if(myAISContext->NbCurrents()==0 || myAISContext->NbSelected()==0)
247        myAISContext->SetDisplayMode(CurrentMode);
248     else 
249         {
250        for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
251                    myAISContext->SetDisplayMode(myAISContext->Current(),aMode,Standard_False);
252                  
253         }
254          myAISContext->UpdateCurrentViewer();
255 }
256
257 void OCCViewer::SetColor(int r, int g, int b)
258 {
259         if (myAISContext.IsNull())
260                 return;
261         Quantity_Color col =  Quantity_Color(r/255.,g/255.,b/255.,Quantity_TOC_RGB);
262         for (;myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
263                   myAISContext->SetColor (myAISContext->Current(),col.Name());
264 }
265
266 void OCCViewer::ObjectColor(int& r, int& g, int& b)
267 {
268         if (myAISContext.IsNull())
269                 return;
270         r=255;
271         g=255;
272         b=255;
273         Handle_AIS_InteractiveObject Current ;
274         Quantity_Color ObjCol;
275         myAISContext->InitCurrent();
276         if (!myAISContext->MoreCurrent())
277                 return;
278     Current = myAISContext->Current();
279         if ( Current->HasColor () ) {
280       ObjCol = myAISContext->Color(myAISContext->Current());
281           Quantity_Parameter r1, r2, r3;
282           ObjCol.Values(r1, r2, r3, Quantity_TOC_RGB);
283                 r=(int)r1*255;
284                 g=(int)r2*255;
285                 b=(int)r3*255;
286         }
287 }
288
289 void OCCViewer::SetBackgroundColor(int r, int g, int b)
290 {
291         if (!myView.IsNull())
292                 myView->SetBackgroundColor(Quantity_TOC_RGB,r/255.,g/255.,b/255.);
293 }
294
295 void OCCViewer::EraseObjects(void)
296 {
297         if (myAISContext.IsNull())
298                 return;
299         for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
300         myAISContext->Erase(myAISContext->Current(),Standard_True,Standard_False);
301         myAISContext->ClearCurrents();
302 }
303
304 float OCCViewer::GetVersion(void)
305 {
306         return (float)OCC_VERSION;
307 }
308
309 void OCCViewer::SetMaterial(int theMaterial)
310 {
311         if (myAISContext.IsNull())
312                 return;
313     for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent (); myAISContext->NextCurrent () )
314         myAISContext->SetMaterial( myAISContext->Current(), (Graphic3d_NameOfMaterial)theMaterial );
315         myAISContext->UpdateCurrentViewer();
316 }
317
318 void OCCViewer::SetTransparency(int theTrans)
319 {
320         if (myAISContext.IsNull())
321                 return;
322         for( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextSelected() )
323                 myAISContext->SetTransparency( myAISContext->Current(), ((Standard_Real)theTrans) / 10.0 );
324 }
325
326 bool OCCViewer::ImportCsfdb(char* filename)
327 {
328         Standard_CString aFileName = (Standard_CString) filename;
329     if ( FSD_File::IsGoodFileType(aFileName) != Storage_VSOk )
330             return false;
331
332     static FSD_File fileDriver;
333     TCollection_AsciiString aName( aFileName );
334     if ( fileDriver.Open( aName, Storage_VSRead ) != Storage_VSOk )
335         return false;
336
337     Handle(ShapeSchema) schema = new ShapeSchema();
338     Handle(Storage_Data) data  = schema->Read( fileDriver );
339     if ( data->ErrorStatus() != Storage_VSOk )
340         return false;
341     fileDriver.Close();
342
343     Handle(Storage_HSeqOfRoot) roots = data->Roots();
344     for ( int i = 1; i <= roots->Length() ; i++ )
345     {
346         Handle(Storage_Root) r = roots->Value( i );
347         Handle(Standard_Persistent) p = r->Object();
348         Handle(PTopoDS_HShape) aPShape = Handle(PTopoDS_HShape)::DownCast(p);
349         if ( !aPShape.IsNull() )
350         {
351                 PTColStd_PersistentTransientMap aMap;
352                 TopoDS_Shape aTShape;
353             MgtBRep::Translate( aPShape, aMap, aTShape, MgtBRep_WithTriangle );
354                         myAISContext->Display(new AIS_Shape(aTShape));
355         }
356     }
357
358         return true;
359 }
360
361 bool OCCViewer::ImportIges(char* filename)
362 {
363         Standard_CString aFileName = (Standard_CString) filename;
364     IGESControl_Reader Reader;
365     int status = Reader.ReadFile( aFileName );
366
367     if ( status == IFSelect_RetDone )
368     {
369         Reader.TransferRoots();
370         TopoDS_Shape aShape = Reader.OneShape();
371         myAISContext->Display(new AIS_Shape(aShape));
372     } else
373                 return false;
374         myAISContext->UpdateCurrentViewer();
375         return true;
376 }
377
378 bool OCCViewer::ImportStep(char* filename)
379 {
380         Standard_CString aFileName = (Standard_CString) filename;
381         STEPControl_Reader aReader;
382         IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
383         if ( status == IFSelect_RetDone )
384     {
385             bool failsonly = false;
386             aReader.PrintCheckLoad( failsonly, IFSelect_ItemsByEntity );
387
388             int nbr = aReader.NbRootsForTransfer();
389             aReader.PrintCheckTransfer( failsonly, IFSelect_ItemsByEntity );
390             for ( Standard_Integer n = 1; n <= nbr; n++ )
391             {
392                 Standard_Boolean ok = aReader.TransferRoot( n );
393                 int nbs = aReader.NbShapes();
394                 if ( nbs > 0 )
395             {
396                     for ( int i = 1; i <= nbs; i++ )
397                 {
398                             TopoDS_Shape shape = aReader.Shape( i );
399                             myAISContext->Display(new AIS_Shape(shape));
400                     }
401             }
402         }
403         } else
404                 return false;
405         return true;
406 }
407
408 bool OCCViewer::ExportBRep(char* filename)
409 {
410         myAISContext->InitCurrent();
411         if (!myAISContext->MoreCurrent())
412                 return false;
413         Handle_AIS_InteractiveObject anIO = myAISContext->Current();
414         Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
415         return (bool)BRepTools::Write( anIS->Shape(), (Standard_CString)filename ); ;
416 }
417
418
419 bool OCCViewer::ExportIges(char* filename)
420 {
421         IGESControl_Controller::Init();
422         IGESControl_Writer writer( Interface_Static::CVal( "XSTEP.iges.unit" ),
423                                Interface_Static::IVal( "XSTEP.iges.writebrep.mode" ) );
424  
425         
426         for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent() )
427         {
428                 Handle_AIS_InteractiveObject anIO = myAISContext->Current();
429                 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
430                 TopoDS_Shape shape = anIS->Shape();
431                 writer.AddShape ( shape );
432         }
433         writer.ComputeModel();
434         return (bool)writer.Write( (Standard_CString)filename );
435 }
436
437 bool OCCViewer::ExpotStep(char* filename)
438 {
439     STEPControl_StepModelType type = STEPControl_AsIs;
440     IFSelect_ReturnStatus status;
441     STEPControl_Writer writer;
442         for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent() )
443     {
444                 Handle_AIS_InteractiveObject anIO = myAISContext->Current();
445                 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
446                 TopoDS_Shape shape = anIS->Shape();
447                 status = writer.Transfer( shape , type );
448         if ( status != IFSelect_RetDone )
449             return false;
450     }
451
452     status = writer.Write( (Standard_CString)filename );
453     if ( status != IFSelect_RetDone )
454             return false;
455         return true;
456 }
457
458 bool OCCViewer::ExportStl(char* filename)
459 {
460         TopoDS_Compound comp;
461         BRep_Builder builder;
462         builder.MakeCompound( comp );
463
464         for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent() )
465         {
466                 Handle_AIS_InteractiveObject anIO = myAISContext->Current();
467                 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
468                 TopoDS_Shape shape = anIS->Shape();
469                 if ( shape.IsNull() ) 
470                         return false;    
471                 builder.Add( comp, shape );
472         }
473
474         StlAPI_Writer writer;
475         writer.Write( comp, (Standard_CString)filename );
476         return true;
477 }
478
479 bool OCCViewer::ExportVrml(char* filename)
480 {
481         TopoDS_Compound res;
482         BRep_Builder builder;
483         builder.MakeCompound( res );
484
485         for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent() )
486         {
487                 Handle_AIS_InteractiveObject anIO = myAISContext->Current();
488                 Handle_AIS_Shape anIS=Handle_AIS_Shape::DownCast(anIO);
489                 TopoDS_Shape shape = anIS->Shape();
490                 if ( shape.IsNull() )
491                         return false;
492         
493                 builder.Add( res, shape );
494         }
495
496         VrmlAPI_Writer writer;
497         writer.Write( res, (Standard_CString)filename );
498
499         return true;
500 }
501
502 bool OCCViewer::Dump(char *filename)
503 {
504         if (myView.IsNull())
505                 return false;
506         myView->Redraw();
507     return (bool)myView->Dump(filename);
508 }
509
510 bool OCCViewer::IsObjectSelected(void)
511 {
512         if (myAISContext.IsNull())
513                 return false;
514         myAISContext->InitCurrent();
515         return (bool)myAISContext->MoreCurrent();
516 }
517
518 int OCCViewer::DisplayMode(void)
519 {
520         if (myAISContext.IsNull())
521                 return -1;
522         int mode = -1;
523         bool OneOrMoreInShading=false;
524         bool OneOrMoreInWireframe=false;
525         for (myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent())
526         {
527                 if ( myAISContext->IsDisplayed( myAISContext->Current(), 1 ) )
528              OneOrMoreInShading = true;
529         if ( myAISContext->IsDisplayed( myAISContext->Current(), 0 ) )
530              OneOrMoreInWireframe = true;
531         }
532         if (OneOrMoreInShading&&OneOrMoreInWireframe)
533                 mode=10;
534         else if(OneOrMoreInShading)
535                 mode=1;
536         else if (OneOrMoreInWireframe)
537                 mode=0;
538         return mode;
539 }
540
541 void OCCViewer::CreateNewView(void* wnd)
542 {
543         if (myAISContext.IsNull())
544                 return;
545         myView = myAISContext->CurrentViewer()->CreateView();
546         if (myGraphicDevice.IsNull())
547                         myGraphicDevice = new  Graphic3d_WNTGraphicDevice();
548         Handle(WNT_Window) aWNTWindow = new WNT_Window(myGraphicDevice, reinterpret_cast<HWND> (wnd));
549         myView->SetWindow(aWNTWindow);
550         Standard_Integer w=100, h=100;
551         aWNTWindow->Size(w,h);
552         if (!aWNTWindow->IsMapped()) 
553                  aWNTWindow->Map();
554 }
555
556 bool OCCViewer::SetAISContext(OCCViewer* Viewer)
557 {
558         this->myAISContext=Viewer->GetAISContext();
559         if (myAISContext.IsNull())
560                 return false;
561         return true;
562 }
563
564 Handle_AIS_InteractiveContext OCCViewer::GetAISContext(void)
565 {
566         return myAISContext;
567 }
568
569 int OCCViewer::CharToInt(char symbol)
570 {
571         TCollection_AsciiString msg = symbol;
572         return msg.IntegerValue();
573 }