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