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