1 // File: XSDRAWSTLVRML.cxx
2 // Created: Tue May 30 17:29:50 2000
3 // Author: Sergey MOZOKHIN
4 // <smh@russox.nnov.matra-dtv.fr>
7 #include <XSDRAWSTLVRML.ixx>
8 #include <Draw_Interpretor.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <VrmlAPI.hxx>
11 #include <OSD_Path.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>
22 #include <XSDRAWSTEP.hxx>
23 #include <XSDRAWIGES.hxx>
24 #include <AIS_InteractiveContext.hxx>
25 #include <ViewerTest.hxx>
27 #include <Draw_ProgressIndicator.hxx>
29 #include <Quantity_Color.hxx>
30 #include <V3d_View.hxx>
31 #include <TCollection_AsciiString.hxx>
33 #include <SelectMgr_SelectionManager.hxx>
34 #include <StdSelect_ViewerSelector3d.hxx>
36 #include <Aspect_TypeOfMarker.hxx>
37 #include <Graphic3d_MaterialAspect.hxx>
39 #include <StlMesh_Mesh.hxx>
40 #include <StlMesh_SequenceOfMeshTriangle.hxx>
42 #include <MeshVS_Mesh.hxx>
43 #include <MeshVS_MeshPrsBuilder.hxx>
44 #include <MeshVS_TextPrsBuilder.hxx>
45 #include <MeshVS_Drawer.hxx>
46 #include <MeshVS_DrawerAttribute.hxx>
47 #include <MeshVS_MeshEntityOwner.hxx>
48 #include <MeshVS_DataMapOfIntegerAsciiString.hxx>
50 #include <XSDRAWSTLVRML_DataSource.hxx>
51 #include <XSDRAWSTLVRML_DrawableMesh.hxx>
52 #include <MeshVS_NodalColorPrsBuilder.hxx>
53 #include <MeshVS_ElementalColorPrsBuilder.hxx>
54 #include <Quantity_NameOfColor.hxx>
55 #include <TColgp_SequenceOfXYZ.hxx>
56 #include <TColStd_HPackedMapOfInteger.hxx>
57 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
58 #include <Standard_ErrorHandler.hxx>
59 #include <VrmlData_Scene.hxx>
60 #include <VrmlData_ShapeConvert.hxx>
61 #include <VrmlData_DataMapOfShapeAppearance.hxx>
62 #include <TColStd_Array1OfReal.hxx>
63 #include <Bnd_Box.hxx>
65 // avoid warnings on 'extern "C"' functions returning C++ classes
67 #pragma warning(4:4190)
74 static Standard_Integer writestl
75 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
77 if (argc<3) di << "wrong number of parameters" << "\n";
79 TopoDS_Shape shape = DBRep::Get(argv[1]);
80 // StlAPI_Writer writer;
81 // writer.ASCIIMode() = Standard_False;
82 // writer.Write (shape, "binary.stl");
83 StlAPI::Write(shape, argv[2],Standard_False); //now write in binary mode
88 static Standard_Integer readstl
89 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
91 if (argc<3) di << "wrong number of parameters" << "\n";
94 StlAPI::Read(shape,argv[2]);
95 DBRep::Set(argv[1],shape);
100 static Standard_Integer writevrml
101 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
103 if (argc<3) di << "wrong number of parameters" << "\n";
105 TopoDS_Shape shape = DBRep::Get(argv[1]);
106 // VrmlAPI_Writer writer;
107 // writer.SetTransparencyToMaterial(writer.GetFrontMaterial(),0.0);
108 // Quantity_Color color;
109 // color.SetValues(Quantity_NOC_GOLD);
110 // Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
111 // Col->SetValue(1,color);
112 // writer.SetDiffuseColorToMaterial(writer.GetFrontMaterial(),Col);
113 // writer.SetRepresentation(VrmlAPI_ShadedRepresentation);
114 // writer.SetDeflection(0.01);
115 // writer.Write(shape, argv[2]);
116 VrmlAPI::Write(shape, argv[2]);
121 //=======================================================================
122 //function : loadvrml
124 //=======================================================================
126 static Standard_Integer loadvrml
127 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
129 if (argc<3) di << "wrong number of parameters" << "\n";
132 VrmlData_DataMapOfShapeAppearance ShapeAppMap;
134 //-----------------------------------------------------------
136 istream aStream (&fic);
138 if (fic.open(argv[2], ios::in)) {
140 VrmlData_Scene aScene;
142 aScene.SetVrmlDir (".");
144 const char * aStr = 0L;
145 switch (aScene.Status()) {
147 case VrmlData_StatusOK:
149 shape = aScene.GetShape(ShapeAppMap);
152 case VrmlData_EmptyData: aStr = "EmptyData"; break;
153 case VrmlData_UnrecoverableError: aStr = "UnrecoverableError"; break;
154 case VrmlData_GeneralError: aStr = "GeneralError"; break;
155 case VrmlData_EndOfFile: aStr = "EndOfFile"; break;
156 case VrmlData_NotVrmlFile: aStr = "NotVrmlFile"; break;
157 case VrmlData_CannotOpenFile: aStr = "CannotOpenFile"; break;
158 case VrmlData_VrmlFormatError: aStr = "VrmlFormatError"; break;
159 case VrmlData_NumericInputError: aStr = "NumericInputError"; break;
160 case VrmlData_IrrelevantNumber: aStr = "IrrelevantNumber"; break;
161 case VrmlData_BooleanInputError: aStr = "BooleanInputError"; break;
162 case VrmlData_StringInputError: aStr = "StringInputError"; break;
163 case VrmlData_NodeNameUnknown: aStr = "NodeNameUnknown"; break;
164 case VrmlData_NonPositiveSize: aStr = "NonPositiveSize"; break;
165 case VrmlData_ReadUnknownNode: aStr = "ReadUnknownNode"; break;
166 case VrmlData_NonSupportedFeature:aStr = "NonSupportedFeature"; break;
169 di << " ++ VRML Error: " << aStr << " in line "
170 << aScene.GetLineError() << "\n";
173 DBRep::Set(argv[1],shape);
177 di << "cannot open file" << "\n";
181 //-----------------------------------------------------------
186 //=======================================================================
187 //function : storevrml
189 //=======================================================================
191 static Standard_Integer storevrml
192 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
195 di << "wrong number of parameters" << "\n";
196 di << "use: storevrml shape file defl type_of_conversion (0, 1, 2)" << "\n";
199 TopAbs_ShapeEnum Types[3] = {TopAbs_FACE, TopAbs_WIRE, TopAbs_EDGE};
200 TopoDS_Shape shape = DBRep::Get(argv[1]);
201 Standard_Real defl = atof(argv[3]);
202 Standard_Integer type = 1;
203 if(argc > 4) type = atoi(argv[4]);
207 Standard_Boolean ExtFace = Standard_False;
208 if(type == 0 || type == 2) ExtFace = Standard_True;
209 Standard_Boolean ExtEdge = Standard_False;
210 if(type == 1 || type == 2) ExtEdge = Standard_True;
212 VrmlData_Scene aScene;
213 VrmlData_ShapeConvert Conv(aScene);
214 Conv.AddShape(shape);
215 Conv.Convert(ExtFace, ExtEdge, defl);
218 ostream outStream (&foc);
219 if (foc.open (argv[2], ios::out))
226 //-----------------------------------------------------------------------------
227 static Standard_Integer createmesh
228 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
233 di << "Use: " << argv[0] << " <mesh name> <stl file>" << "\n";
237 // Progress indicator
238 OSD_Path aFile( argv[2] );
239 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
240 Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
242 di << "Reading OK..." << "\n";
243 Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );
244 di << "Data source is created successful" << "\n";
245 Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
246 di << "MeshVS_Mesh is created successful" << "\n";
248 aMesh->SetDataSource( aDS );
249 aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh.operator->() ), Standard_True );
250 // Prepare triangle labels
251 MeshVS_DataMapOfIntegerAsciiString aLabels;
252 Standard_Integer anIndex = 1, aLen = aSTLMesh->Triangles().Length();
253 for ( ; anIndex <= aLen; anIndex++ ){
254 aLabels.Bind( anIndex, TCollection_AsciiString( anIndex ) );
257 Handle(MeshVS_TextPrsBuilder) aTextBuilder = new MeshVS_TextPrsBuilder( aMesh.operator->(), 20., Quantity_NOC_YELLOW );
258 aTextBuilder->SetTexts( Standard_True, aLabels );
259 aMesh->AddBuilder( aTextBuilder );
261 // Hide all nodes by default
262 Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
263 aLen = aSTLMesh->Vertices().Length();
264 for ( anIndex = 1; anIndex <= aLen; anIndex++ )
265 aNodes->ChangeMap().Add( anIndex );
266 aMesh->SetHiddenNodes( aNodes );
268 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
270 if ( aContext.IsNull() )
272 ViewerTest::ViewerInit();
273 //To create a 3D view if it doesn't exist
274 aContext = ViewerTest::GetAISContext();
275 if( aContext.IsNull() )
277 di << "Cannot create 3D view" << "\n";
282 aContext->Display( aMesh );
283 aContext->Deactivate( aMesh );
285 Draw::Set( argv[1], new XSDRAWSTLVRML_DrawableMesh( aMesh ) );
286 Handle( V3d_View ) V = ViewerTest::CurrentView();
292 //-----------------------------------------------------------------------------
293 Handle( MeshVS_Mesh ) getMesh( const char* name, Draw_Interpretor& di)
295 Handle( XSDRAWSTLVRML_DrawableMesh ) aDrawMesh =
296 Handle( XSDRAWSTLVRML_DrawableMesh )::DownCast( Draw::Get( name ) );
298 if( aDrawMesh.IsNull() )
300 di << "There is no such object" << "\n";
305 Handle( MeshVS_Mesh ) aMesh = aDrawMesh->GetMesh();
308 di << "There is invalid mesh" << "\n";
315 //-----------------------------------------------------------------------------
316 static Standard_Integer meshdm
317 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
320 di << "wrong number of parameters" << "\n";
323 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
324 if( !aMesh.IsNull() )
326 Standard_Integer DisplayMode = 0;
327 sscanf( argv[2], "%i", &DisplayMode );
329 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
331 if( aContext.IsNull() )
332 di << "The context is null" << "\n";
335 Standard_Boolean HasLocal = aContext->HasOpenedContext();
337 aContext->CloseLocalContext();
339 aContext->SetDisplayMode( aMesh, DisplayMode );
340 di << "Setting display mode: " << DisplayMode << "\n";
343 aContext->OpenLocalContext();
349 //-----------------------------------------------------------------------------
350 static Standard_Integer meshsm
351 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
354 di << "wrong number of parameters" << "\n";
357 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
358 if( !aMesh.IsNull() )
360 Standard_Integer SelMode = 0;
361 sscanf( argv[2], "%i", &SelMode );
363 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
365 if( aContext.IsNull() )
366 di << "The context is null" << "\n";
369 if( !aContext->HasOpenedContext() )
370 aContext->OpenLocalContext();
372 aContext->Load( aMesh, -1 );
375 aContext->CloseAllContexts();
377 else if( SelMode==0 )
378 aContext->Activate( aMesh, 0 );
382 aContext->Deactivate( aMesh, 0 );
385 aContext->Activate( aMesh, 1 );
387 aContext->Deactivate( aMesh, 1 );
390 aContext->Activate( aMesh, 4 );
392 aContext->Deactivate( aMesh, 4 );
395 aContext->Activate( aMesh, 8 );
397 aContext->Deactivate( aMesh, 8 );
400 di << "Setting selection mode: " << SelMode << "\n";
406 //-----------------------------------------------------------------------------
407 static Standard_Integer setcolor
408 (Draw_Interpretor& di, Standard_Integer argc, const char** argv, Standard_Integer Param )
411 di << "wrong number of parameters" << "\n";
414 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
415 if( !aMesh.IsNull() )
417 Standard_Real r, g, b;
418 sscanf( argv[2], "%lf", &r );
419 sscanf( argv[3], "%lf", &g );
420 sscanf( argv[4], "%lf", &b );
421 aMesh->GetDrawer()->SetColor( (MeshVS_DrawerAttribute)Param, Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
423 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
425 if( aContext.IsNull() )
426 di << "The context is null" << "\n";
428 aContext->Redisplay( aMesh );
433 //-----------------------------------------------------------------------------
434 static Standard_Integer meshcolor
435 (Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
437 return setcolor( interp, argc, argv, MeshVS_DA_InteriorColor );
439 //-----------------------------------------------------------------------------
440 static Standard_Integer linecolor
441 (Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
443 return setcolor( interp, argc, argv, MeshVS_DA_EdgeColor );
445 //-----------------------------------------------------------------------------
446 static Standard_Integer meshmat
447 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
450 di << "wrong number of parameters" << "\n";
453 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
454 if( !aMesh.IsNull() )
456 Standard_Integer mat;
457 sscanf( argv[2], "%i", &mat );
459 Graphic3d_MaterialAspect aMatAsp =
460 (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)mat;
462 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_FrontMaterial, aMatAsp );
463 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_BackMaterial, aMatAsp );
465 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
467 if( aContext.IsNull() )
468 di << "The context is null" << "\n";
470 aContext->Redisplay( aMesh );
475 //-----------------------------------------------------------------------------
476 static Standard_Integer shrink
477 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
480 di << "wrong number of parameters" << "\n";
483 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
484 if( !aMesh.IsNull() )
487 sscanf( argv[2], "%lf", &sh );
488 aMesh->GetDrawer()->SetDouble( MeshVS_DA_ShrinkCoeff, sh );
490 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
492 if( aContext.IsNull() )
493 di << "The context is null" << "\n";
495 aContext->Redisplay( aMesh );
500 //-----------------------------------------------------------------------------
502 static Standard_Integer mdisplay
503 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
506 di << "wrong number of parameters" << "\n";
509 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
510 if( !aMesh.IsNull() )
512 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
514 if( aContext.IsNull() )
515 di << "The context is null" << "\n";
518 if( aContext->HasOpenedContext() )
519 aContext->CloseLocalContext();
521 aContext->Display( aMesh );
527 //-----------------------------------------------------------------------------
528 static Standard_Integer merase
529 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
532 di << "wrong number of parameters" << "\n";
535 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
536 if( !aMesh.IsNull() )
538 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
540 if( aContext.IsNull() )
541 di << "The context is null" << "\n";
544 if( aContext->HasOpenedContext() )
545 aContext->CloseLocalContext();
547 aContext->Erase( aMesh );
551 di << "Mesh is null" << "\n";
555 //-----------------------------------------------------------------------------
556 static Standard_Integer hidesel
557 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
561 di << "wrong number of parameters" << "\n";
566 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
567 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
570 di << "The mesh is invalid" << "\n";
574 if( aContext.IsNull() )
575 di << "The context is null" << "\n";
578 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes = aMesh->GetHiddenNodes();
579 Handle(TColStd_HPackedMapOfInteger) aHiddenElements = aMesh->GetHiddenElems();
580 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
582 Handle( MeshVS_MeshEntityOwner ) anOwner =
583 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
584 if( !anOwner.IsNull() )
585 if( anOwner->Type()==MeshVS_ET_Node )
586 aHiddenNodes->ChangeMap().Add( anOwner->ID() );
588 aHiddenElements->ChangeMap().Add( anOwner->ID() );
590 aContext->ClearSelected();
591 aMesh->SetHiddenNodes( aHiddenNodes );
592 aMesh->SetHiddenElems( aHiddenElements );
593 aContext->Redisplay( aMesh );
598 //-----------------------------------------------------------------------------
599 static Standard_Integer showonly
600 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
604 di << "wrong number of parameters" << "\n";
609 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
610 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
613 di << "The mesh is invalid" << "\n";
617 if( aContext.IsNull() )
618 di << "The context is null" << "\n";
621 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes =
622 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllNodes());
623 Handle(TColStd_HPackedMapOfInteger) aHiddenElements =
624 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllElements());
625 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
627 Handle( MeshVS_MeshEntityOwner ) anOwner =
628 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
629 if( !anOwner.IsNull() )
630 if( anOwner->Type()==MeshVS_ET_Node )
631 aHiddenNodes->ChangeMap().Remove( anOwner->ID() );
633 aHiddenElements->ChangeMap().Remove( anOwner->ID() );
635 aMesh->SetHiddenNodes( aHiddenNodes );
636 aMesh->SetHiddenElems( aHiddenElements );
637 aContext->Redisplay( aMesh );
642 //-----------------------------------------------------------------------------
643 static Standard_Integer showall
644 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
648 di << "wrong number of parameters" << "\n";
652 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
653 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
656 di << "The mesh is invalid" << "\n";
660 if( aContext.IsNull() )
661 di << "The context is null" << "\n";
664 aMesh->SetHiddenNodes( 0 );
665 aMesh->SetHiddenElems( 0 );
666 aContext->Redisplay( aMesh );
671 //-----------------------------------------------------------------------------
672 static Standard_Integer delmesh
673 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
677 di << "wrong number of parameters" << "\n";
681 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
685 di << "The mesh is invalid" << "\n";
690 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
692 aContext->ClearSelected();
694 if( aContext->HasOpenedContext() )
695 aContext->CloseAllContexts();
697 aContext->Remove( aMesh );
698 aContext->SelectionManager()->Remove( aMesh );
699 aMesh->ClearSelections();
700 aContext->MainSelector()->Clear();
702 Draw::Set( argv[1], Handle(XSDRAWSTLVRML_DrawableMesh)() );
708 //-----------------------------------------------------------------------------
710 static Standard_Integer meshcolors( Draw_Interpretor& di,
711 Standard_Integer argc,
719 di << "Use : meshcolors meshname mode isreflect" << "\n";
720 di << "mode : {elem1|elem2|nodal|nodaltex|none}"<< "\n";
721 di << " elem1 - different color for each element" << "\n";
722 di << " elem2 - one color for one side"<<"\n";
723 di << " nodal - different color for each node"<< "\n";
724 di << " nodaltex - different color for each node with texture interpolation"<< "\n";
725 di << " none - clear"<< "\n";
726 di << "isreflect : {0|1} "<< "\n";
731 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
733 if ( aMesh.IsNull() )
735 di << "Mesh not found" << "\n";
738 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
741 di << "The context is null" << "\n";
744 if( !aMesh.IsNull() )
746 TCollection_AsciiString aMode = TCollection_AsciiString (argv[2]);
747 Quantity_Color aColor1( (Quantity_NameOfColor)( Quantity_NOC_BLUE1 ) );
748 Quantity_Color aColor2( (Quantity_NameOfColor)( Quantity_NOC_RED1 ) );
749 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") || aMode.IsEqual("nodal") || aMode.IsEqual("nodaltex") || aMode.IsEqual("none") )
751 Handle(MeshVS_PrsBuilder) aTempBuilder;
752 Standard_Integer reflection = atoi(argv[3]);
754 for (int count = 0 ; count < aMesh->GetBuildersCount(); count++ ){
755 aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_ElementalColorPrsBuilder"));
756 if( !aTempBuilder.IsNull())
757 aMesh->RemoveBuilderById(aTempBuilder->GetId());
759 aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_NodalColorPrsBuilder"));
760 if( !aTempBuilder.IsNull())
761 aMesh->RemoveBuilderById(aTempBuilder->GetId());
764 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") )
766 Handle(MeshVS_ElementalColorPrsBuilder) aBuilder = new MeshVS_ElementalColorPrsBuilder(
767 aMesh, MeshVS_DMF_ElementalColorDataPrs | MeshVS_DMF_OCCMask );
769 const TColStd_PackedMapOfInteger& anAllElements = aMesh->GetDataSource()->GetAllElements();
770 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllElements );
772 if( aMode.IsEqual("elem1") )
773 for ( ; anIter.More(); anIter.Next() )
775 Quantity_Color aColor( (Quantity_NameOfColor)( anIter.Key() % Quantity_NOC_WHITE ) );
776 aBuilder->SetColor1( anIter.Key(), aColor );
779 for ( ; anIter.More(); anIter.Next() )
780 aBuilder->SetColor2( anIter.Key(), aColor1, aColor2 );
782 aMesh->AddBuilder( aBuilder, Standard_True );
786 if( aMode.IsEqual("nodal") )
788 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
789 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask );
790 aMesh->AddBuilder( aBuilder, Standard_True );
793 const TColStd_PackedMapOfInteger& anAllNodes =
794 aMesh->GetDataSource()->GetAllNodes();
795 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllNodes );
796 for ( ; anIter.More(); anIter.Next() )
798 Quantity_Color aColor( (Quantity_NameOfColor)(
799 anIter.Key() % Quantity_NOC_WHITE ) );
800 aBuilder->SetColor( anIter.Key(), aColor );
802 aMesh->AddBuilder( aBuilder, Standard_True );
805 if(aMode.IsEqual("nodaltex"))
807 // assign nodal builder to the mesh
808 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
809 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask);
810 aMesh->AddBuilder(aBuilder, Standard_True);
811 aBuilder->UseTexture(Standard_True);
813 // prepare color map for texture
814 Aspect_SequenceOfColor aColorMap;
815 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_RED);
816 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_YELLOW);
817 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_BLUE1);
819 // prepare scale map for mesh - it will be assigned to mesh as texture coordinates
820 // make mesh color interpolated from minimum X coord to maximum X coord
821 Handle(MeshVS_DataSource) aDataSource = aMesh->GetDataSource();
822 Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
824 // get bounding box for calculations
825 aDataSource->GetBoundingBox().Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
826 Standard_Real aDelta = aMaxX - aMinX;
828 // assign color scale map values (0..1) to nodes
829 TColStd_DataMapOfIntegerReal aScaleMap;
830 TColStd_Array1OfReal aCoords(1, 3);
831 Standard_Integer aNbNodes;
832 MeshVS_EntityType aType;
835 const TColStd_PackedMapOfInteger& anAllNodes =
836 aMesh->GetDataSource()->GetAllNodes();
837 TColStd_MapIteratorOfPackedMapOfInteger anIter(anAllNodes);
838 for (; anIter.More(); anIter.Next())
840 //get node coordinates to aCoord variable
841 aDataSource->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aType);
843 Standard_Real aScaleValue;
846 aScaleValue = (aCoords.Value(1) - (Standard_Real) aMinX) / aDelta;
847 } catch(Standard_Failure) {
851 aScaleMap.Bind(anIter.Key(), aScaleValue);
854 //set color map for builder and a color for invalid scale value
855 aBuilder->SetColorMap(aColorMap);
856 aBuilder->SetInvalidColor(Quantity_NOC_BLACK);
857 aBuilder->SetTextureCoords(aScaleMap);
858 aMesh->AddBuilder(aBuilder, Standard_True);
860 //set viewer to display texures
861 const Handle(V3d_Viewer)& aViewer = anIC->CurrentViewer();
862 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
863 aViewer->ActiveView()->SetSurfaceDetail(V3d_TEX_ALL);
866 aMesh->GetDrawer()->SetBoolean ( MeshVS_DA_ColorReflection, Standard_Boolean(reflection) );
868 anIC->Redisplay( aMesh );
872 di << "Wrong mode name" << "\n";
877 catch ( Standard_Failure )
879 di << "Error" << "\n";
884 //-----------------------------------------------------------------------------
886 static Standard_Integer mesh_edge_width( Draw_Interpretor& di,
887 Standard_Integer argc,
895 di << "Wrong number of parameters. Use : mesh_edge_width mesh width" << "\n";
899 Handle(MeshVS_Mesh) aMesh = getMesh( argv[ 1 ], di );
900 if ( aMesh.IsNull() )
902 di << "Mesh not found" << "\n";
906 const char* aWidthStr = argv[ 2 ];
907 if ( aWidthStr == 0 || atof( aWidthStr ) <= 0 )
909 di << "Width must be real value more than zero" << "\n";
913 double aWidth = atof( aWidthStr );
915 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
918 di << "The context is null" << "\n";
922 Handle(MeshVS_Drawer) aDrawer = aMesh->GetDrawer();
923 if ( aDrawer.IsNull() )
925 di << "The drawer is null" << "\n";
929 aDrawer->SetDouble( MeshVS_DA_EdgeWidth, aWidth );
930 anIC->Redisplay( aMesh );
932 catch ( Standard_Failure )
934 di << "Error" << "\n";
940 //-----------------------------------------------------------------------------
942 void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands)
944 const char* g = "XSTEP-STL/VRML"; // Step transfer file commands
945 //XSDRAW::LoadDraw(theCommands);
947 theCommands.Add ("writevrml", "shape file",__FILE__,writevrml,g);
948 theCommands.Add ("writestl", "shape file",__FILE__,writestl,g);
949 theCommands.Add ("readstl", "shape file",__FILE__,readstl,g);
950 theCommands.Add ("loadvrml" , "shape file",__FILE__,loadvrml,g);
951 theCommands.Add ("storevrml" , "shape file defl [type]",__FILE__,storevrml,g);
953 theCommands.Add ("meshfromstl", "creates MeshVS_Mesh from STL file", __FILE__, createmesh, g );
954 theCommands.Add ("meshdispmode", "changes MeshVS_Mesh display mode", __FILE__, meshdm, g );
955 theCommands.Add ("meshselmode", "changes MeshVS_Mesh selection mode", __FILE__, meshsm, g );
956 theCommands.Add ("meshshadcolor", "change MeshVS_Mesh shading color", __FILE__, meshcolor, g );
957 theCommands.Add ("meshlinkcolor", "change MeshVS_Mesh line color", __FILE__, linecolor, g );
958 theCommands.Add ("meshmat", "change MeshVS_Mesh material", __FILE__, meshmat, g );
959 theCommands.Add ("meshshrcoef", "change MeshVS_Mesh shrink coeff", __FILE__, shrink, g );
960 theCommands.Add ("meshshow", "display MeshVS_Mesh object", __FILE__, mdisplay, g );
961 theCommands.Add ("meshhide", "erase MeshVS_Mesh object", __FILE__, merase, g );
962 theCommands.Add ("meshhidesel", "hide selected entities", __FILE__, hidesel, g );
963 theCommands.Add ("meshshowsel", "show only selected entities", __FILE__, showonly, g );
964 theCommands.Add ("meshshowall", "show all entities", __FILE__, showall, g );
965 theCommands.Add ("meshdelete", "delete MeshVS_Mesh object", __FILE__, delmesh, g );
966 theCommands.Add ("meshcolors", "display color presentation", __FILE__, meshcolors, g );
967 theCommands.Add ("mesh_edge_width", "set width of edges", __FILE__, mesh_edge_width, g );
970 //==============================================================================
971 // XSDRAWSTLVRML::Factory
972 //==============================================================================
973 void XSDRAWSTLVRML::Factory(Draw_Interpretor& theDI)
975 XSDRAWIGES::InitSelect();
976 XSDRAWIGES::InitToBRep(theDI);
977 XSDRAWIGES::InitFromBRep(theDI);
978 XSDRAWSTEP::InitCommands(theDI);
979 XSDRAWSTLVRML::InitCommands(theDI);
981 XSDRAW::LoadDraw(theDI);
983 theDI << "Draw Plugin : All TKXSDRAW commands are loaded" << "\n";
987 // Declare entry point PLUGINFACTORY
988 DPLUGIN(XSDRAWSTLVRML)