1 // Created on: 2000-05-30
2 // Created by: Sergey MOZOKHIN
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <AIS_InteractiveContext.hxx>
18 #include <Aspect_TypeOfMarker.hxx>
19 #include <Bnd_Box.hxx>
22 #include <Draw_Interpretor.hxx>
23 #include <Draw_PluginMacro.hxx>
24 #include <Draw_ProgressIndicator.hxx>
25 #include <Graphic3d_MaterialAspect.hxx>
26 #include <MeshVS_DataMapOfIntegerAsciiString.hxx>
27 #include <MeshVS_DeformedDataSource.hxx>
28 #include <MeshVS_Drawer.hxx>
29 #include <MeshVS_DrawerAttribute.hxx>
30 #include <MeshVS_ElementalColorPrsBuilder.hxx>
31 #include <MeshVS_Mesh.hxx>
32 #include <MeshVS_MeshEntityOwner.hxx>
33 #include <MeshVS_MeshPrsBuilder.hxx>
34 #include <MeshVS_NodalColorPrsBuilder.hxx>
35 #include <MeshVS_PrsBuilder.hxx>
36 #include <MeshVS_TextPrsBuilder.hxx>
37 #include <MeshVS_VectorPrsBuilder.hxx>
38 #include <OSD_Path.hxx>
39 #include <Quantity_Color.hxx>
40 #include <Quantity_HArray1OfColor.hxx>
41 #include <Quantity_NameOfColor.hxx>
43 #include <SelectMgr_SelectionManager.hxx>
44 #include <Standard_ErrorHandler.hxx>
45 #include <StdSelect_ViewerSelector3d.hxx>
47 #include <StlAPI_Writer.hxx>
48 #include <StlMesh_Mesh.hxx>
49 #include <StlMesh_SequenceOfMeshTriangle.hxx>
50 #include <TColgp_SequenceOfXYZ.hxx>
51 #include <TCollection_AsciiString.hxx>
52 #include <TColStd_Array1OfReal.hxx>
53 #include <TColStd_HPackedMapOfInteger.hxx>
54 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <V3d_View.hxx>
57 #include <ViewerTest.hxx>
58 #include <VrmlAPI.hxx>
59 #include <VrmlAPI_Writer.hxx>
60 #include <VrmlData_DataMapOfShapeAppearance.hxx>
61 #include <VrmlData_Scene.hxx>
62 #include <VrmlData_ShapeConvert.hxx>
64 #include <XSDRAWIGES.hxx>
65 #include <XSDRAWSTEP.hxx>
66 #include <XSDRAWSTLVRML.hxx>
67 #include <XSDRAWSTLVRML_DataSource.hxx>
68 #include <XSDRAWSTLVRML_DataSource3D.hxx>
69 #include <XSDRAWSTLVRML_DrawableMesh.hxx>
71 // avoid warnings on 'extern "C"' functions returning C++ classes
73 #pragma warning(4:4190)
80 extern Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
81 const Handle(AIS_InteractiveObject)& theAISObj,
82 Standard_Boolean theReplaceIfExists = Standard_True);
84 static Standard_Integer writestl
85 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
87 if (argc < 3 || argc > 4) {
88 di << "Use: " << argv[0]
89 << " shape file [ascii/binary (0/1) : 1 by default]\n";
91 TopoDS_Shape aShape = DBRep::Get(argv[1]);
92 Standard_Boolean isASCIIMode = Standard_False;
94 isASCIIMode = (Draw::Atoi(argv[3]) == 0);
96 StlAPI_Writer aWriter;
97 aWriter.ASCIIMode() = isASCIIMode;
98 StlAPI_ErrorStatus aStatus = aWriter.Write (aShape, argv[2]);
102 case StlAPI_MeshIsEmpty: di << "** Error **: Mesh is empty. Please, compute triangulation before."; break;
103 case StlAPI_CannotOpenFile: di << "** Error **: Cannot create/open a file with the passed name."; break;
104 case StlAPI_StatusOK: default: break;
110 static Standard_Integer readstl
111 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
113 if (argc<3) di << "wrong number of parameters" << "\n";
115 TopoDS_Shape aShape ;
116 StlAPI::Read(aShape,argv[2]);
117 DBRep::Set(argv[1],aShape);
122 static Standard_Integer writevrml
123 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
125 if (argc < 3 || argc > 5)
127 di << "wrong number of parameters\n";
131 TopoDS_Shape aShape = DBRep::Get(argv[1]);
133 // Get the optional parameters
134 Standard_Integer aVersion = 2;
135 Standard_Integer aType = 1;
138 aVersion = Draw::Atoi(argv[3]);
140 aType = Draw::Atoi(argv[4]);
144 aVersion = Max(1, aVersion);
145 aVersion = Min(2, aVersion);
146 aType = Max(0, aType);
147 aType = Min(2, aType);
149 VrmlAPI_Writer writer;
153 case 0: writer.SetRepresentation(VrmlAPI_ShadedRepresentation); break;
154 case 1: writer.SetRepresentation(VrmlAPI_WireFrameRepresentation); break;
155 case 2: writer.SetRepresentation(VrmlAPI_BothRepresentation); break;
158 writer.Write(aShape, argv[2], aVersion);
163 //=======================================================================
164 //function : loadvrml
166 //=======================================================================
168 static Standard_Integer loadvrml
169 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
171 if (argc<3) di << "wrong number of parameters" << "\n";
173 TopoDS_Shape aShape ;
174 VrmlData_DataMapOfShapeAppearance aShapeAppMap;
176 //-----------------------------------------------------------
178 istream aStream (&aFic);
180 if (aFic.open(argv[2], ios::in)) {
182 // Get path of the VRML file.
183 OSD_Path aPath(argv[2]);
184 TCollection_AsciiString aVrmlDir(".");
185 TCollection_AsciiString aDisk = aPath.Disk();
186 TCollection_AsciiString aTrek = aPath.Trek();
187 if (!aTrek.IsEmpty())
189 if (!aDisk.IsEmpty())
193 aTrek.ChangeAll('|', '/');
197 VrmlData_Scene aScene;
199 aScene.SetVrmlDir (aVrmlDir);
201 const char * aStr = 0L;
202 switch (aScene.Status()) {
204 case VrmlData_StatusOK:
206 aShape = aScene.GetShape(aShapeAppMap);
209 case VrmlData_EmptyData: aStr = "EmptyData"; break;
210 case VrmlData_UnrecoverableError: aStr = "UnrecoverableError"; break;
211 case VrmlData_GeneralError: aStr = "GeneralError"; break;
212 case VrmlData_EndOfFile: aStr = "EndOfFile"; break;
213 case VrmlData_NotVrmlFile: aStr = "NotVrmlFile"; break;
214 case VrmlData_CannotOpenFile: aStr = "CannotOpenFile"; break;
215 case VrmlData_VrmlFormatError: aStr = "VrmlFormatError"; break;
216 case VrmlData_NumericInputError: aStr = "NumericInputError"; break;
217 case VrmlData_IrrelevantNumber: aStr = "IrrelevantNumber"; break;
218 case VrmlData_BooleanInputError: aStr = "BooleanInputError"; break;
219 case VrmlData_StringInputError: aStr = "StringInputError"; break;
220 case VrmlData_NodeNameUnknown: aStr = "NodeNameUnknown"; break;
221 case VrmlData_NonPositiveSize: aStr = "NonPositiveSize"; break;
222 case VrmlData_ReadUnknownNode: aStr = "ReadUnknownNode"; break;
223 case VrmlData_NonSupportedFeature: aStr = "NonSupportedFeature"; break;
224 case VrmlData_OutputStreamUndefined:aStr = "OutputStreamUndefined"; break;
225 case VrmlData_NotImplemented: aStr = "NotImplemented"; break;
230 di << " ++ VRML Error: " << aStr << " in line "
231 << aScene.GetLineError() << "\n";
234 DBRep::Set(argv[1],aShape);
238 di << "cannot open file\n";
242 //-----------------------------------------------------------
247 //-----------------------------------------------------------------------------
248 static Standard_Integer createmesh
249 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
253 di << "Wrong number of parameters\n";
254 di << "Use: " << argv[0] << " <mesh name> <stl file>\n";
258 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
259 if (aContext.IsNull())
261 di << "No active view. Please call 'vinit' first\n";
265 // Progress indicator
266 OSD_Path aFile( argv[2] );
267 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
268 Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
270 di << "Reading OK...\n";
271 Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );
272 di << "Data source is created successful\n";
273 Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
274 di << "MeshVS_Mesh is created successful\n";
276 aMesh->SetDataSource( aDS );
277 aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh.operator->() ), Standard_True );
279 aMesh->GetDrawer()->SetColor( MeshVS_DA_EdgeColor, Quantity_NOC_YELLOW );
281 // Hide all nodes by default
282 Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
283 Standard_Integer aLen = aSTLMesh->Vertices().Length();
284 for ( Standard_Integer anIndex = 1; anIndex <= aLen; anIndex++ )
285 aNodes->ChangeMap().Add( anIndex );
286 aMesh->SetHiddenNodes( aNodes );
287 aMesh->SetSelectableNodes ( aNodes );
289 VDisplayAISObject(argv[1], aMesh);
290 aContext->Deactivate( aMesh );
292 Draw::Set( argv[1], new XSDRAWSTLVRML_DrawableMesh( aMesh ) );
293 Handle( V3d_View ) aView = ViewerTest::CurrentView();
294 if ( !aView.IsNull() )
299 //-----------------------------------------------------------------------------
301 static Standard_Integer create3d
302 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
306 di << "Wrong number of parameters\n";
307 di << "Use: " << argv[0] << " <mesh name>\n";
311 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
312 if (aContext.IsNull())
314 di << "No active view. Please call 'vinit' first\n";
318 Handle( XSDRAWSTLVRML_DataSource3D ) aDS = new XSDRAWSTLVRML_DataSource3D();
319 di << "Data source is created successful\n";
320 Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
321 di << "MeshVS_Mesh is created successful\n";
323 aMesh->SetDataSource( aDS );
324 aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh.operator->() ), Standard_True );
326 aMesh->GetDrawer()->SetColor( MeshVS_DA_EdgeColor, Quantity_NOC_YELLOW );
328 // Hide all nodes by default
329 Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
330 Standard_Integer aLen = aDS->GetAllNodes().Extent();
331 for ( Standard_Integer anIndex = 1; anIndex <= aLen; anIndex++ )
332 aNodes->ChangeMap().Add( anIndex );
333 aMesh->SetHiddenNodes( aNodes );
334 aMesh->SetSelectableNodes ( aNodes );
336 VDisplayAISObject(argv[1], aMesh);
337 aContext->Deactivate( aMesh );
339 Draw::Set( argv[1], new XSDRAWSTLVRML_DrawableMesh( aMesh ) );
340 Handle( V3d_View ) aView = ViewerTest::CurrentView();
341 if ( !aView.IsNull() )
347 Handle( MeshVS_Mesh ) getMesh( const char* theName, Draw_Interpretor& di)
349 Handle( XSDRAWSTLVRML_DrawableMesh ) aDrawMesh =
350 Handle( XSDRAWSTLVRML_DrawableMesh )::DownCast( Draw::Get( theName ) );
352 if( aDrawMesh.IsNull() )
354 di << "There is no such object\n";
359 Handle( MeshVS_Mesh ) aMesh = aDrawMesh->GetMesh();
362 di << "There is invalid mesh\n";
370 //-----------------------------------------------------------------------------
371 static Standard_Integer setcolor
372 (Draw_Interpretor& di, Standard_Integer argc, const char** argv, Standard_Integer theParam )
375 di << "Wrong number of parameters\n";
378 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
379 if( !aMesh.IsNull() )
381 Standard_Real aRed = Draw::Atof (argv[2]);
382 Standard_Real aGreen = Draw::Atof (argv[3]);
383 Standard_Real aBlue = Draw::Atof (argv[4]);
384 aMesh->GetDrawer()->SetColor( (MeshVS_DrawerAttribute)theParam,
385 Quantity_Color( aRed, aGreen, aBlue, Quantity_TOC_RGB ) );
387 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
389 if( aContext.IsNull() )
390 di << "The context is null\n";
392 aContext->Redisplay( aMesh );
397 //-----------------------------------------------------------------------------
398 static Standard_Integer meshcolor
399 (Draw_Interpretor& theInterp, Standard_Integer argc, const char** argv )
401 return setcolor( theInterp, argc, argv, MeshVS_DA_InteriorColor );
403 //-----------------------------------------------------------------------------
404 static Standard_Integer linecolor
405 (Draw_Interpretor& theInterp, Standard_Integer argc, const char** argv )
407 return setcolor( theInterp, argc, argv, MeshVS_DA_EdgeColor );
409 //-----------------------------------------------------------------------------
410 static Standard_Integer meshmat
411 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
414 di << "Wrong number of parameters\n";
417 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
418 if( !aMesh.IsNull() )
420 Standard_Integer aMaterial = Draw::Atoi (argv[2]);
422 Graphic3d_MaterialAspect aMatAsp =
423 (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aMaterial;
427 Standard_Real aTransparency = Draw::Atof(argv[3]);
428 aMatAsp.SetTransparency(aTransparency);
430 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_FrontMaterial, aMatAsp );
431 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_BackMaterial, aMatAsp );
433 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
435 if( aContext.IsNull() )
436 di << "The context is null\n";
438 aContext->Redisplay( aMesh );
443 //-----------------------------------------------------------------------------
444 static Standard_Integer shrink
445 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
448 di << "Wrong number of parameters\n";
451 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
452 if( !aMesh.IsNull() )
454 Standard_Real aShrinkCoeff = Draw::Atof (argv[2]);
455 aMesh->GetDrawer()->SetDouble( MeshVS_DA_ShrinkCoeff, aShrinkCoeff );
457 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
459 if( aContext.IsNull() )
460 di << "The context is null\n";
462 aContext->Redisplay( aMesh );
468 //-----------------------------------------------------------------------------
469 static Standard_Integer closed (Draw_Interpretor& theDI, Standard_Integer theArgc, const char** theArgv)
473 theDI << "Wrong number of parameters.\n";
477 Handle(MeshVS_Mesh) aMesh = getMesh (theArgv[1], theDI);
480 Standard_Boolean aFlag = Draw::Atoi (theArgv[2]) != 0;
481 aMesh->GetDrawer()->SetBoolean (MeshVS_DA_SupressBackFaces, aFlag);
483 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
484 if (aContext.IsNull())
486 theDI << "The context is null\n";
490 aContext->Redisplay (aMesh);
497 //-----------------------------------------------------------------------------
499 static Standard_Integer mdisplay
500 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
503 di << "Wrong number of parameters\n";
506 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
507 if( !aMesh.IsNull() )
509 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
511 if( aContext.IsNull() )
512 di << "The context is null\n";
515 if( aContext->HasOpenedContext() )
516 aContext->CloseLocalContext();
518 aContext->Display( aMesh );
524 //-----------------------------------------------------------------------------
525 static Standard_Integer merase
526 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
529 di << "Wrong number of parameters\n";
532 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
533 if( !aMesh.IsNull() )
535 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
537 if( aContext.IsNull() )
538 di << "The context is null\n";
541 if( aContext->HasOpenedContext() )
542 aContext->CloseLocalContext();
544 aContext->Erase( aMesh );
548 di << "Mesh is null\n";
552 //-----------------------------------------------------------------------------
553 static Standard_Integer hidesel
554 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
558 di << "Wrong number of parameters\n";
559 di << "Use: " << argv[0] << " <mesh name>\n";
563 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
564 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
567 di << "The mesh is invalid\n";
571 if( aContext.IsNull() )
572 di << "The context is null\n";
575 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes = aMesh->GetHiddenNodes();
576 if (aHiddenNodes.IsNull())
578 aHiddenNodes = new TColStd_HPackedMapOfInteger();
580 Handle(TColStd_HPackedMapOfInteger) aHiddenElements = aMesh->GetHiddenElems();
581 if (aHiddenElements.IsNull())
583 aHiddenElements = new TColStd_HPackedMapOfInteger();
585 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
587 Handle( MeshVS_MeshEntityOwner ) anOwner =
588 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
589 if( !anOwner.IsNull() )
591 if( anOwner->Type()==MeshVS_ET_Node )
593 aHiddenNodes->ChangeMap().Add( anOwner->ID() );
597 aHiddenElements->ChangeMap().Add( anOwner->ID() );
601 aContext->ClearSelected();
602 aMesh->SetHiddenNodes( aHiddenNodes );
603 aMesh->SetHiddenElems( aHiddenElements );
604 aContext->Redisplay( aMesh );
609 //-----------------------------------------------------------------------------
610 static Standard_Integer showonly
611 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
615 di << "Wrong number of parameters\n";
616 di << "Use: " << argv[0] << " <mesh name>\n";
621 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
622 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
625 di << "The mesh is invalid\n";
629 if( aContext.IsNull() )
630 di << "The context is null\n";
633 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes =
634 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllNodes());
635 Handle(TColStd_HPackedMapOfInteger) aHiddenElements =
636 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllElements());
637 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
639 Handle( MeshVS_MeshEntityOwner ) anOwner =
640 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
641 if( !anOwner.IsNull() )
643 if( anOwner->Type() == MeshVS_ET_Node )
645 aHiddenNodes->ChangeMap().Remove( anOwner->ID() );
649 aHiddenElements->ChangeMap().Remove( anOwner->ID() );
653 aMesh->SetHiddenNodes( aHiddenNodes );
654 aMesh->SetHiddenElems( aHiddenElements );
655 aContext->Redisplay( aMesh );
660 //-----------------------------------------------------------------------------
661 static Standard_Integer showall
662 (Draw_Interpretor& di, Standard_Integer argc, const char** argv )
666 di << "Wrong number of parameters\n";
667 di << "Use: " << argv[0] << " <mesh name>\n";
671 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
672 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
675 di << "The mesh is invalid\n";
679 if( aContext.IsNull() )
680 di << "The context is null\n";
683 aMesh->SetHiddenNodes( new TColStd_HPackedMapOfInteger() );
684 aMesh->SetHiddenElems( new TColStd_HPackedMapOfInteger() );
685 aContext->Redisplay( aMesh );
691 //-----------------------------------------------------------------------------
692 static Standard_Integer meshcolors( Draw_Interpretor& di,
693 Standard_Integer argc,
701 di << "Wrong number of parameters\n";
702 di << "Use : meshcolors <mesh name> <mode> <isreflect>\n";
703 di << "mode : {elem1|elem2|nodal|nodaltex|none}\n";
704 di << " elem1 - different color for each element\n";
705 di << " elem2 - one color for one side\n";
706 di << " nodal - different color for each node\n";
707 di << " nodaltex - different color for each node with texture interpolation\n";
708 di << " none - clear\n";
709 di << "isreflect : {0|1} \n";
714 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
716 if ( aMesh.IsNull() )
718 di << "Mesh not found\n";
721 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
724 di << "The context is null\n";
727 if( !aMesh.IsNull() )
729 TCollection_AsciiString aMode = TCollection_AsciiString (argv[2]);
730 Quantity_Color aColor1( (Quantity_NameOfColor)( Quantity_NOC_BLUE1 ) );
731 Quantity_Color aColor2( (Quantity_NameOfColor)( Quantity_NOC_RED1 ) );
732 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") || aMode.IsEqual("nodal") || aMode.IsEqual("nodaltex") || aMode.IsEqual("none") )
734 Handle(MeshVS_PrsBuilder) aTempBuilder;
735 Standard_Integer aReflection = Draw::Atoi(argv[3]);
737 for (Standard_Integer aCount = 0 ; aCount < aMesh->GetBuildersCount(); aCount++ ){
738 aTempBuilder = aMesh->FindBuilder("MeshVS_ElementalColorPrsBuilder");
739 if( !aTempBuilder.IsNull())
740 aMesh->RemoveBuilderById(aTempBuilder->GetId());
742 aTempBuilder = aMesh->FindBuilder("MeshVS_NodalColorPrsBuilder");
743 if( !aTempBuilder.IsNull())
744 aMesh->RemoveBuilderById(aTempBuilder->GetId());
747 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") )
749 Handle(MeshVS_ElementalColorPrsBuilder) aBuilder = new MeshVS_ElementalColorPrsBuilder(
750 aMesh, MeshVS_DMF_ElementalColorDataPrs | MeshVS_DMF_OCCMask );
752 const TColStd_PackedMapOfInteger& anAllElements = aMesh->GetDataSource()->GetAllElements();
753 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllElements );
755 if( aMode.IsEqual("elem1") )
756 for ( ; anIter.More(); anIter.Next() )
758 Quantity_Color aColor( (Quantity_NameOfColor)( anIter.Key() % Quantity_NOC_WHITE ) );
759 aBuilder->SetColor1( anIter.Key(), aColor );
762 for ( ; anIter.More(); anIter.Next() )
763 aBuilder->SetColor2( anIter.Key(), aColor1, aColor2 );
765 aMesh->AddBuilder( aBuilder, Standard_True );
769 if( aMode.IsEqual("nodal") )
771 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
772 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask );
773 aMesh->AddBuilder( aBuilder, Standard_True );
776 const TColStd_PackedMapOfInteger& anAllNodes =
777 aMesh->GetDataSource()->GetAllNodes();
778 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllNodes );
779 for ( ; anIter.More(); anIter.Next() )
781 Quantity_Color aColor( (Quantity_NameOfColor)(
782 anIter.Key() % Quantity_NOC_WHITE ) );
783 aBuilder->SetColor( anIter.Key(), aColor );
785 aMesh->AddBuilder( aBuilder, Standard_True );
788 if(aMode.IsEqual("nodaltex"))
790 // assign nodal builder to the mesh
791 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
792 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask);
793 aMesh->AddBuilder(aBuilder, Standard_True);
794 aBuilder->UseTexture(Standard_True);
796 // prepare color map for texture
797 Aspect_SequenceOfColor aColorMap;
798 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_RED);
799 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_YELLOW);
800 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_BLUE1);
802 // prepare scale map for mesh - it will be assigned to mesh as texture coordinates
803 // make mesh color interpolated from minimum X coord to maximum X coord
804 Handle(MeshVS_DataSource) aDataSource = aMesh->GetDataSource();
805 Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
807 // get bounding box for calculations
808 aDataSource->GetBoundingBox().Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
809 Standard_Real aDelta = aMaxX - aMinX;
811 // assign color scale map values (0..1) to nodes
812 TColStd_DataMapOfIntegerReal aScaleMap;
813 TColStd_Array1OfReal aCoords(1, 3);
814 Standard_Integer aNbNodes;
815 MeshVS_EntityType aType;
818 const TColStd_PackedMapOfInteger& anAllNodes =
819 aMesh->GetDataSource()->GetAllNodes();
820 TColStd_MapIteratorOfPackedMapOfInteger anIter(anAllNodes);
821 for (; anIter.More(); anIter.Next())
823 //get node coordinates to aCoord variable
824 aDataSource->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aType);
826 Standard_Real aScaleValue;
829 aScaleValue = (aCoords.Value(1) - (Standard_Real) aMinX) / aDelta;
830 } catch(Standard_Failure) {
834 aScaleMap.Bind(anIter.Key(), aScaleValue);
837 //set color map for builder and a color for invalid scale value
838 aBuilder->SetColorMap(aColorMap);
839 aBuilder->SetInvalidColor(Quantity_NOC_BLACK);
840 aBuilder->SetTextureCoords(aScaleMap);
841 aMesh->AddBuilder(aBuilder, Standard_True);
844 aMesh->GetDrawer()->SetBoolean (MeshVS_DA_ColorReflection, aReflection != 0);
846 anIC->Redisplay( aMesh );
850 di << "Wrong mode name\n";
855 catch ( Standard_Failure )
862 //-----------------------------------------------------------------------------
863 static Standard_Integer meshvectors( Draw_Interpretor& di,
864 Standard_Integer argc,
869 di << "Wrong number of parameters\n";
870 di << "Use : meshvectors <mesh name> < -mode {elem|nodal|none} > [-maxlen len] [-color name] [-arrowpart ratio] [-issimple {1|0}]\n";
871 di << "Supported mode values:\n";
872 di << " elem - vector per element\n";
873 di << " nodal - vector per node\n";
874 di << " none - clear\n";
879 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
881 if ( aMesh.IsNull() )
883 di << "Mesh not found\n";
886 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
889 di << "The context is null\n";
893 TCollection_AsciiString aParam;
894 TCollection_AsciiString aMode("none");
895 Standard_Real aMaxlen(1.0);
896 Quantity_Color aColor(Quantity_NOC_ORANGE);
897 Standard_Real anArrowPart(0.1);
898 Standard_Boolean isSimplePrs(Standard_False);
900 for (Standard_Integer anIdx = 2; anIdx < argc; anIdx++)
902 if (!aParam.IsEmpty())
904 if (aParam == "-mode")
908 else if (aParam == "-maxlen")
910 aMaxlen = Draw::Atof(argv[anIdx]);
912 else if (aParam == "-color")
914 aColor = ViewerTest::GetColorFromName(argv[anIdx]);
916 else if (aParam == "-arrowpart")
918 anArrowPart = Draw::Atof(argv[anIdx]);
920 else if (aParam == "-issimple")
922 isSimplePrs = Draw::Atoi(argv[anIdx]) != 0;
926 else if (argv[anIdx][0] == '-')
928 aParam = argv[anIdx];
932 if( !aMode.IsEqual("elem") && !aMode.IsEqual("nodal") && !aMode.IsEqual("none") )
934 di << "Wrong mode name\n";
938 Handle(MeshVS_PrsBuilder) aTempBuilder;
940 aTempBuilder = aMesh->FindBuilder("MeshVS_VectorPrsBuilder");
941 if( !aTempBuilder.IsNull())
942 aMesh->RemoveBuilderById(aTempBuilder->GetId());
944 if( !aMode.IsEqual("none") )
946 Handle(MeshVS_VectorPrsBuilder) aBuilder = new MeshVS_VectorPrsBuilder( aMesh.operator->(),
949 MeshVS_DMF_VectorDataPrs,
955 Standard_Boolean anIsElement = aMode.IsEqual("elem");
956 const TColStd_PackedMapOfInteger& anAllIDs = anIsElement ? aMesh->GetDataSource()->GetAllElements() :
957 aMesh->GetDataSource()->GetAllNodes();
959 Standard_Integer aNbNodes;
960 MeshVS_EntityType aEntType;
962 TColStd_Array1OfReal aCoords(1, 3);
964 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllIDs );
965 for ( ; anIter.More(); anIter.Next() )
967 Standard_Boolean IsValidData = Standard_False;
969 aMesh->GetDataSource()->GetGeomType(anIter.Key(), anIsElement, aEntType);
970 if (aEntType == MeshVS_ET_Face)
971 IsValidData = aMesh->GetDataSource()->GetNormal(anIter.Key(), 3, aCoords.ChangeValue(1), aCoords.ChangeValue(2), aCoords.ChangeValue(3));
973 IsValidData = aMesh->GetDataSource()->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aEntType);
978 aNorm = gp_Vec(aCoords.Value(1), aCoords.Value(2), aCoords.Value(3));
979 if(aNorm.Magnitude() < gp::Resolution())
981 aNorm = gp_Vec(0,0,1); //method GetGeom(...) returns coordinates of nodes
986 aNorm = gp_Vec(0,0,1);
988 aBuilder->SetVector(anIsElement, anIter.Key(), aNorm.Normalized());
991 aMesh->AddBuilder( aBuilder, Standard_False );
992 aMesh->GetDrawer()->SetDouble ( MeshVS_DA_VectorArrowPart, anArrowPart );
995 anIC->Redisplay( aMesh );
999 //-----------------------------------------------------------------------------
1001 static Standard_Integer meshtext( Draw_Interpretor& di,
1002 Standard_Integer argc,
1007 di << "Wrong number of parameters\n";
1008 di << "Use : meshtext <mesh name>\n";
1012 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
1014 if ( aMesh.IsNull() )
1016 di << "Mesh not found\n";
1020 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
1021 if ( anIC.IsNull() )
1023 di << "The context is null\n";
1027 // Prepare triangle labels
1028 MeshVS_DataMapOfIntegerAsciiString aLabels;
1029 Standard_Integer aLen = aMesh->GetDataSource()->GetAllElements().Extent();
1030 for ( Standard_Integer anIndex = 1; anIndex <= aLen; anIndex++ ){
1031 aLabels.Bind( anIndex, TCollection_AsciiString( anIndex ) );
1034 Handle(MeshVS_TextPrsBuilder) aTextBuilder = new MeshVS_TextPrsBuilder( aMesh.operator->(), 20., Quantity_NOC_YELLOW );
1035 aTextBuilder->SetTexts( Standard_True, aLabels );
1036 aMesh->AddBuilder( aTextBuilder );
1041 static Standard_Integer meshdeform( Draw_Interpretor& di,
1042 Standard_Integer argc,
1047 di << "Wrong number of parameters\n";
1048 di << "Use : meshdeform <mesh name> < -mode {on|off} > [-scale scalefactor]\n";
1052 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
1054 if ( aMesh.IsNull() )
1056 di << "Mesh not found\n";
1059 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
1060 if ( anIC.IsNull() )
1062 di << "The context is null\n";
1066 TCollection_AsciiString aParam;
1067 TCollection_AsciiString aMode("off");
1068 Standard_Real aScale(1.0);
1070 for (Standard_Integer anIdx = 2; anIdx < argc; anIdx++)
1072 if (!aParam.IsEmpty())
1074 if (aParam == "-mode")
1076 aMode = argv[anIdx];
1078 else if (aParam == "-scale")
1080 aScale = Draw::Atof(argv[anIdx]);
1084 else if (argv[anIdx][0] == '-')
1086 aParam = argv[anIdx];
1090 if(!aMode.IsEqual("on") && !aMode.IsEqual("off"))
1092 di << "Wrong mode name\n";
1096 Handle ( MeshVS_DeformedDataSource ) aDefDS =
1097 new MeshVS_DeformedDataSource( aMesh->GetDataSource() , aScale );
1099 const TColStd_PackedMapOfInteger& anAllIDs = aMesh->GetDataSource()->GetAllNodes();
1101 Standard_Integer aNbNodes;
1102 MeshVS_EntityType aEntType;
1104 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllIDs );
1105 for ( ; anIter.More(); anIter.Next() )
1107 TColStd_Array1OfReal aCoords(1, 3);
1108 aMesh->GetDataSource()->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aEntType);
1110 gp_Vec aNorm = gp_Vec(aCoords.Value(1), aCoords.Value(2), aCoords.Value(3));
1111 if( !aNorm.Magnitude() )
1112 aNorm = gp_Vec(0,0,1);
1113 aDefDS->SetVector(anIter.Key(), aNorm.Normalized());
1116 aMesh->SetDataSource(aDefDS);
1118 anIC->Redisplay( aMesh );
1120 Handle( V3d_View ) aView = ViewerTest::CurrentView();
1121 if ( !aView.IsNull() )
1127 static Standard_Integer mesh_edge_width( Draw_Interpretor& di,
1128 Standard_Integer argc,
1136 di << "Wrong number of parameters\n";
1137 di << "Use : mesh_edge_width <mesh name> <width>\n";
1141 Handle(MeshVS_Mesh) aMesh = getMesh( argv[ 1 ], di );
1142 if ( aMesh.IsNull() )
1144 di << "Mesh not found\n";
1148 const char* aWidthStr = argv[ 2 ];
1149 if ( aWidthStr == 0 || Draw::Atof( aWidthStr ) <= 0 )
1151 di << "Width must be real value more than zero\n";
1155 double aWidth = Draw::Atof( aWidthStr );
1157 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
1158 if ( anIC.IsNull() )
1160 di << "The context is null\n";
1164 Handle(MeshVS_Drawer) aDrawer = aMesh->GetDrawer();
1165 if ( aDrawer.IsNull() )
1167 di << "The drawer is null\n";
1171 aDrawer->SetDouble( MeshVS_DA_EdgeWidth, aWidth );
1172 anIC->Redisplay( aMesh );
1174 catch ( Standard_Failure )
1182 //-----------------------------------------------------------------------------
1184 static Standard_Integer meshinfo(Draw_Interpretor& di,
1185 Standard_Integer argc,
1190 di << "Wrong number of parameters. Use : meshinfo mesh\n";
1194 Handle(MeshVS_Mesh) aMesh = getMesh(argv[ 1 ], di);
1195 if ( aMesh.IsNull() )
1197 di << "Mesh not found\n";
1201 Handle(XSDRAWSTLVRML_DataSource) stlMeshSource = Handle(XSDRAWSTLVRML_DataSource)::DownCast(aMesh->GetDataSource());
1202 if (!stlMeshSource.IsNull())
1204 const TColStd_PackedMapOfInteger& nodes = stlMeshSource->GetAllNodes();
1205 const TColStd_PackedMapOfInteger& tris = stlMeshSource->GetAllElements();
1207 di << "Nb nodes = " << nodes.Extent() << "\n";
1208 di << "Nb triangles = " << tris.Extent() << "\n";
1214 //-----------------------------------------------------------------------------
1216 void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands)
1218 const char* g = "XSTEP-STL/VRML"; // Step transfer file commands
1219 //XSDRAW::LoadDraw(theCommands);
1221 theCommands.Add ("writevrml", "shape file [version VRML#1.0/VRML#2.0 (1/2): 2 by default] [representation shaded/wireframe/both (0/1/2): 1 by default]",__FILE__,writevrml,g);
1222 theCommands.Add ("writestl", "shape file [ascii/binary (0/1) : 1 by default] [InParallel (0/1) : 0 by default]",__FILE__,writestl,g);
1223 theCommands.Add ("readstl", "shape file",__FILE__,readstl,g);
1224 theCommands.Add ("loadvrml" , "shape file",__FILE__,loadvrml,g);
1226 theCommands.Add ("meshfromstl", "creates MeshVS_Mesh from STL file", __FILE__, createmesh, g );
1227 theCommands.Add ("mesh3delem", "creates 3d element mesh to test", __FILE__, create3d, g );
1228 theCommands.Add ("meshshadcolor", "change MeshVS_Mesh shading color", __FILE__, meshcolor, g );
1229 theCommands.Add ("meshlinkcolor", "change MeshVS_Mesh line color", __FILE__, linecolor, g );
1230 theCommands.Add ("meshmat", "change MeshVS_Mesh material and transparency", __FILE__, meshmat, g );
1231 theCommands.Add ("meshshrcoef", "change MeshVS_Mesh shrink coeff", __FILE__, shrink, g );
1232 theCommands.Add ("meshclosed", "meshclosed meshname (0/1) \nChange MeshVS_Mesh drawing mode. 0 - not closed object, 1 - closed object", __FILE__, closed, g);
1233 theCommands.Add ("meshshow", "display MeshVS_Mesh object", __FILE__, mdisplay, g );
1234 theCommands.Add ("meshhide", "erase MeshVS_Mesh object", __FILE__, merase, g );
1235 theCommands.Add ("meshhidesel", "hide selected entities", __FILE__, hidesel, g );
1236 theCommands.Add ("meshshowsel", "show only selected entities", __FILE__, showonly, g );
1237 theCommands.Add ("meshshowall", "show all entities", __FILE__, showall, g );
1238 theCommands.Add ("meshcolors", "display color presentation", __FILE__, meshcolors, g );
1239 theCommands.Add ("meshvectors", "display sample vectors", __FILE__, meshvectors, g );
1240 theCommands.Add ("meshtext", "display text labels", __FILE__, meshtext, g );
1241 theCommands.Add ("meshdeform", "display deformed mesh", __FILE__, meshdeform, g );
1242 theCommands.Add ("mesh_edge_width", "set width of edges", __FILE__, mesh_edge_width, g );
1243 theCommands.Add ("meshinfo", "displays the number of nodes and triangles", __FILE__, meshinfo, g );
1246 //==============================================================================
1247 // XSDRAWSTLVRML::Factory
1248 //==============================================================================
1249 void XSDRAWSTLVRML::Factory(Draw_Interpretor& theDI)
1251 XSDRAWIGES::InitSelect();
1252 XSDRAWIGES::InitToBRep(theDI);
1253 XSDRAWIGES::InitFromBRep(theDI);
1254 XSDRAWSTEP::InitCommands(theDI);
1255 XSDRAWSTLVRML::InitCommands(theDI);
1256 XSDRAW::LoadDraw(theDI);
1258 theDI << "Draw Plugin : All TKXSDRAW commands are loaded\n";
1262 // Declare entry point PLUGINFACTORY
1263 DPLUGIN(XSDRAWSTLVRML)