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