0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / XSDRAWSTLVRML / XSDRAWSTLVRML.cxx
1 // File:  XSDRAWSTLVRML.cxx
2 // Created: Tue May 30 17:29:50 2000
3 // Author:  Sergey MOZOKHIN
4 //    <smh@russox.nnov.matra-dtv.fr>
5
6
7 #include <XSDRAWSTLVRML.ixx>
8 #include <Draw_Interpretor.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <VrmlAPI.hxx>
11 #include <OSD_Path.hxx>
12 #include <StlAPI.hxx>
13 #include <XSDRAW.hxx>
14 #include <DBRep.hxx>
15 #include <VrmlAPI_Writer.hxx>
16 #include <Quantity_Color.hxx>
17 #include <Quantity_HArray1OfColor.hxx>
18 #include <StlAPI_Writer.hxx>
19 #include <Draw_PluginMacro.hxx>
20 #include <SWDRAW.hxx>
21 #include <XSDRAW.hxx>
22 #include <XSDRAWSTEP.hxx>
23 #include <XSDRAWIGES.hxx>
24 #include <AIS_InteractiveContext.hxx>
25 #include <ViewerTest.hxx>
26 #include <Draw.hxx>
27 #include <Draw_ProgressIndicator.hxx>
28 #include <RWStl.hxx>
29 #include <Quantity_Color.hxx>
30 #include <V3d_View.hxx>
31 #include <TCollection_AsciiString.hxx>
32
33 #include <SelectMgr_SelectionManager.hxx>
34 #include <StdSelect_ViewerSelector3d.hxx>
35
36 #include <Aspect_TypeOfMarker.hxx>
37 #include <Graphic3d_MaterialAspect.hxx>
38
39 #include <StlMesh_Mesh.hxx>
40 #include <StlMesh_SequenceOfMeshTriangle.hxx>
41
42 #include <MeshVS_Mesh.hxx>
43 #include <MeshVS_MeshPrsBuilder.hxx>
44 #include <MeshVS_TextPrsBuilder.hxx>
45 #include <MeshVS_Drawer.hxx>
46 #include <MeshVS_DrawerAttribute.hxx>
47 #include <MeshVS_MeshEntityOwner.hxx>
48 #include <MeshVS_DataMapOfIntegerAsciiString.hxx>
49
50 #include <XSDRAWSTLVRML_DataSource.hxx>
51 #include <XSDRAWSTLVRML_DrawableMesh.hxx>
52 #include <MeshVS_NodalColorPrsBuilder.hxx>
53 #include <MeshVS_ElementalColorPrsBuilder.hxx>
54 #include <Quantity_NameOfColor.hxx>
55 #include <TColgp_SequenceOfXYZ.hxx>
56 #include <TColStd_HPackedMapOfInteger.hxx>
57 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
58 #include <Standard_ErrorHandler.hxx>
59 #include <VrmlData_Scene.hxx>
60 #include <VrmlData_ShapeConvert.hxx>
61 #include <VrmlData_DataMapOfShapeAppearance.hxx>
62 #include <TColStd_Array1OfReal.hxx>
63 #include <Bnd_Box.hxx>
64
65 // avoid warnings on 'extern "C"' functions returning C++ classes
66 #ifdef WNT
67 #pragma warning(4:4190)
68 #endif
69
70 #ifndef _STDIO_H
71 #include <stdio.h>
72 #endif
73
74 static Standard_Integer writestl
75 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
76 {
77   if (argc<3 || argc>4) di << "Use: " << argv[0] << "shape file [ascii/binary (0/1) : 1 by default]" << "\n";
78   else {
79     TopoDS_Shape shape = DBRep::Get(argv[1]);
80     Standard_Boolean anASCIIMode = Standard_False;
81     if (argc==4) {
82       Standard_Integer mode = atoi(argv[3]);
83       if (mode==0) anASCIIMode = Standard_True;
84     }
85     StlAPI::Write(shape, argv[2],anASCIIMode);
86   }
87   return 0;
88 }
89
90 static Standard_Integer readstl
91 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
92 {
93   if (argc<3) di << "wrong number of parameters"    << "\n";
94   else {
95     TopoDS_Shape shape ;
96     StlAPI::Read(shape,argv[2]);
97     DBRep::Set(argv[1],shape);
98   }
99   return 0;
100 }
101
102 static Standard_Integer writevrml
103 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
104 {
105   if (argc<3) di << "wrong number of parameters"    << "\n";
106   else {
107     TopoDS_Shape shape = DBRep::Get(argv[1]);
108     //     VrmlAPI_Writer writer;
109     //     writer.SetTransparencyToMaterial(writer.GetFrontMaterial(),0.0);
110     //      Quantity_Color color;
111     //      color.SetValues(Quantity_NOC_GOLD);
112     //      Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
113     //      Col->SetValue(1,color);
114     //      writer.SetDiffuseColorToMaterial(writer.GetFrontMaterial(),Col);
115     //      writer.SetRepresentation(VrmlAPI_ShadedRepresentation);
116     //      writer.SetDeflection(0.01);
117     //      writer.Write(shape, argv[2]);
118     VrmlAPI::Write(shape, argv[2]);
119   }
120   return 0;
121 }
122
123 //=======================================================================
124 //function : loadvrml
125 //purpose  :
126 //=======================================================================
127
128 static Standard_Integer loadvrml
129 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
130 {
131   if (argc<3) di << "wrong number of parameters"    << "\n";
132   else {
133     TopoDS_Shape shape ;
134     VrmlData_DataMapOfShapeAppearance ShapeAppMap;
135
136     //-----------------------------------------------------------
137     filebuf fic;
138     istream aStream (&fic);
139
140     if (fic.open(argv[2], ios::in)) {
141
142       VrmlData_Scene aScene;
143
144       aScene.SetVrmlDir (".");
145       aScene << aStream;
146       const char * aStr = 0L;
147       switch (aScene.Status()) {
148
149       case VrmlData_StatusOK:
150         {
151           shape = aScene.GetShape(ShapeAppMap);
152           break;
153         }
154       case VrmlData_EmptyData:          aStr = "EmptyData"; break;
155       case VrmlData_UnrecoverableError: aStr = "UnrecoverableError"; break;
156       case VrmlData_GeneralError:       aStr = "GeneralError"; break;
157       case VrmlData_EndOfFile:          aStr = "EndOfFile"; break;
158       case VrmlData_NotVrmlFile:        aStr = "NotVrmlFile"; break;
159       case VrmlData_CannotOpenFile:     aStr = "CannotOpenFile"; break;
160       case VrmlData_VrmlFormatError:    aStr = "VrmlFormatError"; break;
161       case VrmlData_NumericInputError:  aStr = "NumericInputError"; break;
162       case VrmlData_IrrelevantNumber:   aStr = "IrrelevantNumber"; break;
163       case VrmlData_BooleanInputError:  aStr = "BooleanInputError"; break;
164       case VrmlData_StringInputError:   aStr = "StringInputError"; break;
165       case VrmlData_NodeNameUnknown:    aStr = "NodeNameUnknown"; break;
166       case VrmlData_NonPositiveSize:    aStr = "NonPositiveSize"; break;
167       case VrmlData_ReadUnknownNode:    aStr = "ReadUnknownNode"; break;
168       case VrmlData_NonSupportedFeature:aStr = "NonSupportedFeature"; break;
169       }
170       if (aStr) {
171         di << " ++ VRML Error: " << aStr << " in line "
172           << aScene.GetLineError() << "\n";
173       }
174       else {
175         DBRep::Set(argv[1],shape);
176       }
177     }
178     else {
179       di << "cannot open file" << "\n";
180     }
181
182
183     //-----------------------------------------------------------
184   }
185   return 0;
186 }
187
188 //=======================================================================
189 //function : storevrml
190 //purpose  :
191 //=======================================================================
192
193 static Standard_Integer storevrml
194 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
195 {
196   if (argc < 4) {
197     di << "wrong number of parameters"    << "\n";
198     di << "use: storevrml shape file defl type_of_conversion (0, 1, 2)"    << "\n";
199   }
200   else {
201     TopoDS_Shape shape = DBRep::Get(argv[1]);
202     Standard_Real defl = atof(argv[3]);
203     Standard_Integer type = 1;
204     if(argc > 4) type = atoi(argv[4]);
205     type = Max(0, type);
206     type = Min(2, type);
207
208     Standard_Boolean ExtFace = Standard_False;
209     if(type == 0 || type == 2) ExtFace = Standard_True;
210     Standard_Boolean ExtEdge = Standard_False;
211     if(type == 1 || type == 2) ExtEdge = Standard_True;
212
213     VrmlData_Scene aScene;
214     VrmlData_ShapeConvert Conv(aScene);
215     Conv.AddShape(shape);
216     Conv.Convert(ExtFace, ExtEdge, defl);
217
218     filebuf foc;
219     ostream outStream (&foc);
220     if (foc.open (argv[2], ios::out))
221       outStream << aScene;
222   }
223   return 0;
224 }
225
226
227 //-----------------------------------------------------------------------------
228 static Standard_Integer createmesh
229 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
230 {
231
232   if (argc<3)
233   {
234     di << "Use: " << argv[0] << " <mesh name> <stl file>" << "\n";
235     return 1;
236   }
237
238   // Progress indicator
239   OSD_Path aFile( argv[2] );
240   Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
241   Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
242
243   di << "Reading OK..." << "\n";
244   Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );
245   di << "Data source is created successful" << "\n";
246   Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
247   di << "MeshVS_Mesh is created successful" << "\n";
248
249   aMesh->SetDataSource( aDS );
250   aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh.operator->() ), Standard_True );
251   // Prepare triangle labels
252   MeshVS_DataMapOfIntegerAsciiString aLabels;
253   Standard_Integer anIndex = 1, aLen = aSTLMesh->Triangles().Length();
254   for ( ; anIndex <= aLen; anIndex++ ){
255     aLabels.Bind( anIndex, TCollection_AsciiString( anIndex ) );
256   }
257
258   Handle(MeshVS_TextPrsBuilder) aTextBuilder = new MeshVS_TextPrsBuilder( aMesh.operator->(), 20., Quantity_NOC_YELLOW );
259   aTextBuilder->SetTexts( Standard_True, aLabels );
260   aMesh->AddBuilder( aTextBuilder );
261
262   // Hide all nodes by default
263   Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
264   aLen = aSTLMesh->Vertices().Length();
265   for ( anIndex = 1; anIndex <= aLen; anIndex++ )
266     aNodes->ChangeMap().Add( anIndex );
267   aMesh->SetHiddenNodes( aNodes );
268
269   Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
270
271   if ( aContext.IsNull() )
272   {
273     ViewerTest::ViewerInit();
274     //To create a 3D view if it doesn't exist
275     aContext = ViewerTest::GetAISContext();
276     if( aContext.IsNull() )
277     {
278       di << "Cannot create 3D view" << "\n";
279       return 0;
280     }
281   }
282
283   aContext->Display( aMesh );
284   aContext->Deactivate( aMesh );
285
286   Draw::Set( argv[1], new XSDRAWSTLVRML_DrawableMesh( aMesh ) );
287   Handle( V3d_View ) V = ViewerTest::CurrentView();
288   if ( !V.IsNull() )
289     V->FitAll();
290
291   return 0;
292 }
293 //-----------------------------------------------------------------------------
294 Handle( MeshVS_Mesh ) getMesh( const char* name, Draw_Interpretor& di)
295 {
296   Handle( XSDRAWSTLVRML_DrawableMesh ) aDrawMesh =
297     Handle( XSDRAWSTLVRML_DrawableMesh )::DownCast( Draw::Get( name ) );
298
299   if( aDrawMesh.IsNull() )
300   {
301     di << "There is no such object" << "\n";
302     return NULL;
303   }
304   else
305   {
306     Handle( MeshVS_Mesh ) aMesh = aDrawMesh->GetMesh();
307     if( aMesh.IsNull() )
308     {
309       di << "There is invalid mesh" << "\n";
310       return NULL;
311     }
312     else
313       return aMesh;
314   }
315 }
316 //-----------------------------------------------------------------------------
317 static Standard_Integer meshdm
318 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
319 {
320   if (argc<3)
321     di << "wrong number of parameters" << "\n";
322   else
323   {
324     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
325     if( !aMesh.IsNull() )
326     {
327       Standard_Integer DisplayMode = 0;
328       sscanf( argv[2], "%i", &DisplayMode );
329
330       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
331
332       if( aContext.IsNull() )
333         di << "The context is null" << "\n";
334       else
335       {
336         Standard_Boolean HasLocal = aContext->HasOpenedContext();
337         if( HasLocal )
338           aContext->CloseLocalContext();
339
340         aContext->SetDisplayMode( aMesh, DisplayMode );
341         di << "Setting display mode: " << DisplayMode << "\n";
342
343         if( HasLocal )
344           aContext->OpenLocalContext();
345       }
346     }
347   }
348   return 0;
349 }
350 //-----------------------------------------------------------------------------
351 static Standard_Integer meshsm
352 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
353 {
354   if (argc<3)
355     di << "wrong number of parameters" << "\n";
356   else
357   {
358     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
359     if( !aMesh.IsNull() )
360     {
361       Standard_Integer SelMode = 0;
362       sscanf( argv[2], "%i", &SelMode );
363
364       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
365
366       if( aContext.IsNull() )
367         di << "The context is null" << "\n";
368       else
369       {
370         if( !aContext->HasOpenedContext() )
371           aContext->OpenLocalContext();
372
373         aContext->Load( aMesh, -1 );
374
375         if( SelMode==-1 )
376           aContext->CloseAllContexts();
377
378         else if( SelMode==0 )
379           aContext->Activate( aMesh, 0 );
380
381         else if( SelMode>0 )
382         {
383           aContext->Deactivate( aMesh, 0 );
384
385           if( SelMode & 1 )
386             aContext->Activate( aMesh, 1 );
387           else
388             aContext->Deactivate( aMesh, 1 );
389
390           if( SelMode & 4 )
391             aContext->Activate( aMesh, 4 );
392           else
393             aContext->Deactivate( aMesh, 4 );
394
395           if( SelMode & 8 )
396             aContext->Activate( aMesh, 8 );
397           else
398             aContext->Deactivate( aMesh, 8 );
399         }
400
401         di << "Setting selection mode: " << SelMode << "\n";
402       }
403     }
404   }
405   return 0;
406 }
407 //-----------------------------------------------------------------------------
408 static Standard_Integer setcolor
409 (Draw_Interpretor& di, Standard_Integer argc, const char** argv, Standard_Integer Param )
410 {
411   if (argc<5)
412     di << "wrong number of parameters" << "\n";
413   else
414   {
415     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
416     if( !aMesh.IsNull() )
417     {
418       Standard_Real r, g, b;
419       sscanf( argv[2], "%lf", &r );
420       sscanf( argv[3], "%lf", &g );
421       sscanf( argv[4], "%lf", &b );
422       aMesh->GetDrawer()->SetColor( (MeshVS_DrawerAttribute)Param, Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
423
424       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
425
426       if( aContext.IsNull() )
427         di << "The context is null" << "\n";
428       else
429         aContext->Redisplay( aMesh );
430     }
431   }
432   return 0;
433 }
434 //-----------------------------------------------------------------------------
435 static Standard_Integer meshcolor
436 (Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
437 {
438   return setcolor( interp, argc, argv, MeshVS_DA_InteriorColor );
439 }
440 //-----------------------------------------------------------------------------
441 static Standard_Integer linecolor
442 (Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
443 {
444   return setcolor( interp, argc, argv, MeshVS_DA_EdgeColor );
445 }
446 //-----------------------------------------------------------------------------
447 static Standard_Integer meshmat
448 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
449 {
450   if (argc<3)
451     di << "wrong number of parameters" << "\n";
452   else
453   {
454     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
455     if( !aMesh.IsNull() )
456     {
457       Standard_Integer mat;
458       sscanf( argv[2], "%i", &mat );
459
460       Graphic3d_MaterialAspect aMatAsp =
461         (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)mat;
462
463       aMesh->GetDrawer()->SetMaterial( MeshVS_DA_FrontMaterial, aMatAsp );
464       aMesh->GetDrawer()->SetMaterial( MeshVS_DA_BackMaterial, aMatAsp );
465
466       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
467
468       if( aContext.IsNull() )
469         di << "The context is null" << "\n";
470       else
471         aContext->Redisplay( aMesh );
472     }
473   }
474   return 0;
475 }
476 //-----------------------------------------------------------------------------
477 static Standard_Integer shrink
478 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
479 {
480   if (argc<3)
481     di << "wrong number of parameters" << "\n";
482   else
483   {
484     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
485     if( !aMesh.IsNull() )
486     {
487       Standard_Real sh;
488       sscanf( argv[2], "%lf", &sh );
489       aMesh->GetDrawer()->SetDouble( MeshVS_DA_ShrinkCoeff, sh );
490
491       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
492
493       if( aContext.IsNull() )
494         di << "The context is null" << "\n";
495       else
496         aContext->Redisplay( aMesh );
497     }
498   }
499   return 0;
500 }
501 //-----------------------------------------------------------------------------
502
503 static Standard_Integer mdisplay
504 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
505 {
506   if (argc<2)
507     di << "wrong number of parameters" << "\n";
508   else
509   {
510     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
511     if( !aMesh.IsNull() )
512     {
513       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
514
515       if( aContext.IsNull() )
516         di << "The context is null" << "\n";
517       else
518       {
519         if( aContext->HasOpenedContext() )
520           aContext->CloseLocalContext();
521
522         aContext->Display( aMesh );
523       }
524     }
525   }
526   return 0;
527 }
528 //-----------------------------------------------------------------------------
529 static Standard_Integer merase
530 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
531 {
532   if (argc<2)
533     di << "wrong number of parameters" << "\n";
534   else
535   {
536     Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
537     if( !aMesh.IsNull() )
538     {
539       Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
540
541       if( aContext.IsNull() )
542         di << "The context is null" << "\n";
543       else
544       {
545         if( aContext->HasOpenedContext() )
546           aContext->CloseLocalContext();
547
548         aContext->Erase( aMesh );
549       }
550     }
551     else
552       di << "Mesh is null" << "\n";
553   }
554   return 0;
555 }
556 //-----------------------------------------------------------------------------
557 static Standard_Integer hidesel
558 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
559 {
560   if (argc<1)
561   {
562     di << "wrong number of parameters" << "\n";
563     return 0;
564   }
565
566
567   Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
568   Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
569   if( aMesh.IsNull() )
570   {
571     di << "The mesh is invalid" << "\n";
572     return 0;
573   }
574
575   if( aContext.IsNull() )
576     di << "The context is null" << "\n";
577   else
578   {
579     Handle(TColStd_HPackedMapOfInteger) aHiddenNodes = aMesh->GetHiddenNodes();
580     Handle(TColStd_HPackedMapOfInteger) aHiddenElements = aMesh->GetHiddenElems();
581     for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
582     {
583       Handle( MeshVS_MeshEntityOwner ) anOwner =
584         Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
585       if( !anOwner.IsNull() )
586         if( anOwner->Type()==MeshVS_ET_Node )
587           aHiddenNodes->ChangeMap().Add( anOwner->ID() );
588         else
589           aHiddenElements->ChangeMap().Add( anOwner->ID() );
590     }
591     aContext->ClearSelected();
592     aMesh->SetHiddenNodes( aHiddenNodes );
593     aMesh->SetHiddenElems( aHiddenElements );
594     aContext->Redisplay( aMesh );
595   }
596
597   return 0;
598 }
599 //-----------------------------------------------------------------------------
600 static Standard_Integer showonly
601 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
602 {
603   if (argc<1)
604   {
605     di << "wrong number of parameters" << "\n";
606     return 0;
607   }
608
609
610   Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
611   Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
612   if( aMesh.IsNull() )
613   {
614     di << "The mesh is invalid" << "\n";
615     return 0;
616   }
617
618   if( aContext.IsNull() )
619     di << "The context is null" << "\n";
620   else
621   {
622     Handle(TColStd_HPackedMapOfInteger) aHiddenNodes =
623       new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllNodes());
624     Handle(TColStd_HPackedMapOfInteger) aHiddenElements =
625       new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllElements());
626     for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
627     {
628       Handle( MeshVS_MeshEntityOwner ) anOwner =
629         Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
630       if( !anOwner.IsNull() )
631         if( anOwner->Type()==MeshVS_ET_Node )
632           aHiddenNodes->ChangeMap().Remove( anOwner->ID() );
633         else
634           aHiddenElements->ChangeMap().Remove( anOwner->ID() );
635     }
636     aMesh->SetHiddenNodes( aHiddenNodes );
637     aMesh->SetHiddenElems( aHiddenElements );
638     aContext->Redisplay( aMesh );
639   }
640
641   return 0;
642 }
643 //-----------------------------------------------------------------------------
644 static Standard_Integer showall
645 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
646 {
647   if (argc<1)
648   {
649     di << "wrong number of parameters" << "\n";
650     return 0;
651   }
652
653   Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
654   Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
655   if( aMesh.IsNull() )
656   {
657     di << "The mesh is invalid" << "\n";
658     return 0;
659   }
660
661   if( aContext.IsNull() )
662     di << "The context is null" << "\n";
663   else
664   {
665     aMesh->SetHiddenNodes( 0 );
666     aMesh->SetHiddenElems( 0 );
667     aContext->Redisplay( aMesh );
668   }
669
670   return 0;
671 }
672 //-----------------------------------------------------------------------------
673 static Standard_Integer delmesh
674 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
675 {
676   if (argc<2)
677   {
678     di << "wrong number of parameters" << "\n";
679     return 0;
680   }
681
682   Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
683
684   if( aMesh.IsNull() )
685   {
686     di << "The mesh is invalid" << "\n";
687     return 0;
688   }
689   else
690   {
691     Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
692
693     aContext->ClearSelected();
694
695     if( aContext->HasOpenedContext() )
696       aContext->CloseAllContexts();
697
698     aContext->Remove( aMesh );
699     aContext->SelectionManager()->Remove( aMesh );
700     aMesh->ClearSelections();
701     aContext->MainSelector()->Clear();
702
703     Draw::Set( argv[1], Handle(XSDRAWSTLVRML_DrawableMesh)() );
704
705     Standard::Purge();
706   }
707   return 0;
708 }
709 //-----------------------------------------------------------------------------
710
711 static Standard_Integer meshcolors( Draw_Interpretor& di,
712                                     Standard_Integer argc,
713                                     const char** argv )
714 {
715   try
716   {
717     OCC_CATCH_SIGNALS
718       if ( argc < 2 )
719       {
720         di << "Use : meshcolors meshname mode isreflect" << "\n";
721         di << "mode : {elem1|elem2|nodal|nodaltex|none}"<< "\n";
722         di << "       elem1 - different color for each element" << "\n";
723         di << "       elem2 - one color for one side"<<"\n";
724         di << "       nodal - different color for each node"<< "\n";
725         di << "       nodaltex - different color for each node with texture interpolation"<< "\n";
726         di << "       none  - clear"<< "\n";
727         di << "isreflect : {0|1} "<< "\n";
728
729         return 0;
730       }
731
732       Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
733
734       if ( aMesh.IsNull() )
735       {
736         di << "Mesh not found" << "\n";
737         return 0;
738       }
739       Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
740       if ( anIC.IsNull() )
741       {
742         di << "The context is null" << "\n";
743         return 0;
744       }
745       if( !aMesh.IsNull() )
746       {
747         TCollection_AsciiString aMode = TCollection_AsciiString (argv[2]);
748         Quantity_Color aColor1( (Quantity_NameOfColor)( Quantity_NOC_BLUE1 ) );
749         Quantity_Color aColor2( (Quantity_NameOfColor)( Quantity_NOC_RED1 ) );
750         if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") || aMode.IsEqual("nodal") || aMode.IsEqual("nodaltex") || aMode.IsEqual("none") )
751         {
752           Handle(MeshVS_PrsBuilder) aTempBuilder;
753           Standard_Integer reflection = atoi(argv[3]);
754
755           for (int count = 0 ; count < aMesh->GetBuildersCount(); count++ ){
756             aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_ElementalColorPrsBuilder"));
757             if( !aTempBuilder.IsNull())
758               aMesh->RemoveBuilderById(aTempBuilder->GetId());
759
760             aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_NodalColorPrsBuilder"));
761             if( !aTempBuilder.IsNull())
762               aMesh->RemoveBuilderById(aTempBuilder->GetId());
763           }
764
765           if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") )
766           {
767             Handle(MeshVS_ElementalColorPrsBuilder) aBuilder = new MeshVS_ElementalColorPrsBuilder(
768                 aMesh, MeshVS_DMF_ElementalColorDataPrs | MeshVS_DMF_OCCMask );
769               // Color
770             const TColStd_PackedMapOfInteger& anAllElements = aMesh->GetDataSource()->GetAllElements();
771             TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllElements );
772
773             if( aMode.IsEqual("elem1") )
774               for ( ; anIter.More(); anIter.Next() )
775               {
776                 Quantity_Color aColor( (Quantity_NameOfColor)( anIter.Key() % Quantity_NOC_WHITE ) );
777                 aBuilder->SetColor1( anIter.Key(), aColor );
778               }
779             else
780               for ( ; anIter.More(); anIter.Next() )
781                 aBuilder->SetColor2( anIter.Key(), aColor1, aColor2 );
782
783             aMesh->AddBuilder( aBuilder, Standard_True );
784           }
785
786
787           if( aMode.IsEqual("nodal") )
788           {
789             Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
790                 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask );
791             aMesh->AddBuilder( aBuilder, Standard_True );
792
793             // Color
794             const TColStd_PackedMapOfInteger& anAllNodes =
795               aMesh->GetDataSource()->GetAllNodes();
796             TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllNodes );
797             for ( ; anIter.More(); anIter.Next() )
798             {
799               Quantity_Color aColor( (Quantity_NameOfColor)(
800                 anIter.Key() % Quantity_NOC_WHITE ) );
801               aBuilder->SetColor( anIter.Key(), aColor );
802             }
803             aMesh->AddBuilder( aBuilder, Standard_True );
804           }
805
806           if(aMode.IsEqual("nodaltex"))
807           {
808             // assign nodal builder to the mesh
809             Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
810                    aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask);
811             aMesh->AddBuilder(aBuilder, Standard_True);
812             aBuilder->UseTexture(Standard_True);
813
814             // prepare color map for texture
815             Aspect_SequenceOfColor aColorMap;
816             aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_RED);
817             aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_YELLOW);
818             aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_BLUE1);
819
820             // prepare scale map for mesh - it will be assigned to mesh as texture coordinates
821             // make mesh color interpolated from minimum X coord to maximum X coord
822             Handle(MeshVS_DataSource) aDataSource = aMesh->GetDataSource();
823             Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
824
825             // get bounding box for calculations
826             aDataSource->GetBoundingBox().Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
827             Standard_Real aDelta = aMaxX - aMinX;
828
829             // assign color scale map values (0..1) to nodes
830             TColStd_DataMapOfIntegerReal aScaleMap;
831             TColStd_Array1OfReal aCoords(1, 3);
832             Standard_Integer     aNbNodes;
833             MeshVS_EntityType    aType;
834
835             // iterate nodes
836             const TColStd_PackedMapOfInteger& anAllNodes =
837                   aMesh->GetDataSource()->GetAllNodes();
838             TColStd_MapIteratorOfPackedMapOfInteger anIter(anAllNodes);
839             for (; anIter.More(); anIter.Next())
840             {
841               //get node coordinates to aCoord variable
842               aDataSource->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aType);
843
844               Standard_Real aScaleValue;
845               try {
846                 OCC_CATCH_SIGNALS
847                 aScaleValue = (aCoords.Value(1) - (Standard_Real) aMinX) / aDelta;
848               } catch(Standard_Failure) {
849                 aScaleValue = 0;
850               }
851
852               aScaleMap.Bind(anIter.Key(), aScaleValue);
853             }
854
855             //set color map for builder and a color for invalid scale value
856             aBuilder->SetColorMap(aColorMap);
857             aBuilder->SetInvalidColor(Quantity_NOC_BLACK);
858             aBuilder->SetTextureCoords(aScaleMap);
859             aMesh->AddBuilder(aBuilder, Standard_True);
860
861             //set viewer to display texures
862             const Handle(V3d_Viewer)& aViewer = anIC->CurrentViewer();
863             for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
864                  aViewer->ActiveView()->SetSurfaceDetail(V3d_TEX_ALL);
865           }
866
867           aMesh->GetDrawer()->SetBoolean ( MeshVS_DA_ColorReflection, Standard_Boolean(reflection) );
868
869           anIC->Redisplay( aMesh );
870         }
871         else
872         {
873           di << "Wrong mode name" << "\n";
874           return 0;
875         }
876       }
877   }
878   catch ( Standard_Failure )
879   {
880     di << "Error" << "\n";
881   }
882
883   return 0;
884 }
885 //-----------------------------------------------------------------------------
886
887 static Standard_Integer mesh_edge_width( Draw_Interpretor& di,
888                                         Standard_Integer argc,
889                                         const char** argv )
890 {
891   try
892   {
893     OCC_CATCH_SIGNALS
894       if ( argc < 3 )
895       {
896         di << "Wrong number of parameters. Use : mesh_edge_width mesh width" << "\n";
897         return 0;
898       }
899
900       Handle(MeshVS_Mesh) aMesh = getMesh( argv[ 1 ], di );
901       if ( aMesh.IsNull() )
902       {
903         di << "Mesh not found" << "\n";
904         return 0;
905       }
906
907       const char* aWidthStr = argv[ 2 ];
908       if ( aWidthStr == 0 || atof( aWidthStr ) <= 0 )
909       {
910         di << "Width must be real value more than zero" << "\n";
911         return 0;
912       }
913
914       double aWidth = atof( aWidthStr );
915
916       Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
917       if ( anIC.IsNull() )
918       {
919         di << "The context is null" << "\n";
920         return 0;
921       }
922
923       Handle(MeshVS_Drawer) aDrawer = aMesh->GetDrawer();
924       if ( aDrawer.IsNull() )
925       {
926         di << "The drawer is null" << "\n";
927         return 0;
928       }
929
930       aDrawer->SetDouble( MeshVS_DA_EdgeWidth, aWidth );
931       anIC->Redisplay( aMesh );
932   }
933   catch ( Standard_Failure )
934   {
935     di << "Error" << "\n";
936   }
937
938   return 0;
939 }
940
941 //-----------------------------------------------------------------------------
942
943 void  XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands)
944 {
945   const char* g = "XSTEP-STL/VRML";  // Step transfer file commands
946   //XSDRAW::LoadDraw(theCommands);
947
948   theCommands.Add ("writevrml", "shape file",__FILE__,writevrml,g);
949   theCommands.Add ("writestl",  "shape file [ascii/binary (0/1) : 1 by default]",__FILE__,writestl,g);
950   theCommands.Add ("readstl",   "shape file",__FILE__,readstl,g);
951   theCommands.Add ("loadvrml" , "shape file",__FILE__,loadvrml,g);
952   theCommands.Add ("storevrml" , "shape file defl [type]",__FILE__,storevrml,g);
953
954   theCommands.Add ("meshfromstl",   "creates MeshVS_Mesh from STL file",  __FILE__, createmesh, g );
955   theCommands.Add ("meshdispmode",  "changes MeshVS_Mesh display mode",   __FILE__, meshdm,     g );
956   theCommands.Add ("meshselmode",   "changes MeshVS_Mesh selection mode", __FILE__, meshsm,     g );
957   theCommands.Add ("meshshadcolor", "change MeshVS_Mesh shading color",   __FILE__, meshcolor,  g );
958   theCommands.Add ("meshlinkcolor", "change MeshVS_Mesh line color",      __FILE__, linecolor,  g );
959   theCommands.Add ("meshmat",       "change MeshVS_Mesh material",        __FILE__, meshmat,    g );
960   theCommands.Add ("meshshrcoef",   "change MeshVS_Mesh shrink coeff",    __FILE__, shrink,     g );
961   theCommands.Add ("meshshow",      "display MeshVS_Mesh object",         __FILE__, mdisplay,   g );
962   theCommands.Add ("meshhide",      "erase MeshVS_Mesh object",           __FILE__, merase,     g );
963   theCommands.Add ("meshhidesel",   "hide selected entities",             __FILE__, hidesel,    g );
964   theCommands.Add ("meshshowsel",   "show only selected entities",        __FILE__, showonly,   g );
965   theCommands.Add ("meshshowall",   "show all entities",                  __FILE__, showall,    g );
966   theCommands.Add ("meshdelete",    "delete MeshVS_Mesh object",          __FILE__, delmesh,    g );
967   theCommands.Add ("meshcolors",    "display color presentation",         __FILE__, meshcolors, g );
968   theCommands.Add ("mesh_edge_width", "set width of edges",               __FILE__, mesh_edge_width, g );
969 }
970
971 //==============================================================================
972 // XSDRAWSTLVRML::Factory
973 //==============================================================================
974 void XSDRAWSTLVRML::Factory(Draw_Interpretor& theDI)
975 {
976   XSDRAWIGES::InitSelect();
977   XSDRAWIGES::InitToBRep(theDI);
978   XSDRAWIGES::InitFromBRep(theDI);
979   XSDRAWSTEP::InitCommands(theDI);
980   XSDRAWSTLVRML::InitCommands(theDI);
981   SWDRAW::Init(theDI);
982   XSDRAW::LoadDraw(theDI);
983 #ifdef DEB
984   theDI << "Draw Plugin : All TKXSDRAW commands are loaded" << "\n";
985 #endif
986 }
987
988 // Declare entry point PLUGINFACTORY
989 DPLUGIN(XSDRAWSTLVRML)
990