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