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