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