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