1 // Created on: 2012-02-03
3 // Copyright (c) 2012-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.
18 #undef NOMINMAX /* avoid #define min() and max() */
21 #undef NOMSG /* avoid #define SendMessage etc. */
24 #undef NODRAWTEXT /* avoid #define DrawText etc. */
27 #undef NONLS /* avoid #define CompareString etc. */
30 #undef NOGDI /* avoid #define SetPrinter (winspool.h) etc. */
55 #include <WNT_WClass.hxx>
56 #include <WNT_Window.hxx>
60 #include <Draw_Interpretor.hxx>
61 #include <Draw_PluginMacro.hxx>
62 #include <TCollection_AsciiString.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <BRep_Builder.hxx>
65 #include <BRepTools.hxx>
67 #include <NCollection_DoubleMap.hxx>
68 #include <NCollection_List.hxx>
69 #include <NCollection_DataMap.hxx>
70 #include <TopTools_DataMapOfIntegerShape.hxx>
71 #include <OpenGl_GraphicDriver.hxx>
72 #include <Aspect_DisplayConnection.hxx>
74 #include <IVtk_Types.hxx>
75 #include <IVtkVTK_ShapeData.hxx>
76 #include <IVtkOCC_Shape.hxx>
77 #include <IVtkOCC_ShapeMesher.hxx>
78 #include <IVtkTools_ShapeDataSource.hxx>
79 #include <IVtkTools_ShapeObject.hxx>
80 #include <IVtkTools_SubPolyDataFilter.hxx>
81 #include <IVtkTools_DisplayModeFilter.hxx>
82 #include <IVtkTools_ShapePicker.hxx>
84 #include <IVtkDraw.hxx>
85 #include <IVtkDraw_HighlightAndSelectionPipeline.hxx>
86 #include <IVtkDraw_Interactor.hxx>
88 // prevent disabling some MSVC warning messages by VTK headers
91 #pragma warning(disable: 4244)
93 #include <vtkAlgorithmOutput.h>
94 #include <vtkAppendPolyData.h>
95 #include <vtkBMPWriter.h>
96 #include <vtkCamera.h>
97 #include <vtkCellData.h>
98 #include <vtkCommand.h>
99 #include <vtkGeometryFilter.h>
100 #include <vtkIdTypeArray.h>
101 #include <vtkImageResize.h>
102 #include <vtkImageWriter.h>
103 #include <vtkInteractorStyleTrackballCamera.h>
104 #include <vtkJPEGWriter.h>
105 #include <vtkPNGWriter.h>
106 #include <vtkPNMWriter.h>
107 #include <vtkPolyDataMapper.h>
108 #include <vtkProperty.h>
109 #include <vtkRenderWindow.h>
110 #include <vtkRenderer.h>
111 #include <vtkRenderWindowInteractor.h>
112 #include <vtkSmartPointer.h>
113 #include <vtkTIFFWriter.h>
114 #include <vtkWindowToImageFilter.h>
117 #include <X11/Shell.h>
118 #include <X11/Xlib.h>
120 #include <Xw_Window.hxx>
121 #include <vtkXRenderWindowInteractor.h>
122 #include <vtkXOpenGLRenderWindow.h>
123 #include <X11/Xutil.h>
130 // workaround name conflicts with OCCT methods (in class TopoDS_Shape for example)
138 //================================================================
140 //================================================================
142 typedef NCollection_DoubleMap<TopoDS_Shape, TCollection_AsciiString> DoubleMapOfShapesAndNames;
143 typedef NCollection_DoubleMap<vtkSmartPointer<vtkActor>, TCollection_AsciiString> DoubleMapOfActorsAndNames;
145 typedef IVtkDraw_HighlightAndSelectionPipeline PipelinePtr;
147 //================================================================
149 //================================================================
151 Standard_IMPORT Standard_Boolean Draw_VirtualWindows;
153 static Handle(Aspect_DisplayConnection)& GetDisplayConnection()
155 static Handle(Aspect_DisplayConnection) aDisplayConnection;
156 return aDisplayConnection;
159 static void SetDisplayConnection (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
161 GetDisplayConnection() = theDisplayConnection;
164 static DoubleMapOfShapesAndNames& GetMapOfShapes()
166 static DoubleMapOfShapesAndNames aMap;
170 static DoubleMapOfActorsAndNames& GetMapOfActors()
172 static DoubleMapOfActorsAndNames aMap;
176 static vtkSmartPointer<vtkRenderer>& GetRenderer()
178 static vtkSmartPointer<vtkRenderer> aRenderer;
182 static Handle(ShapePipelineMap)& GetPipelines()
184 static Handle(ShapePipelineMap) aPLMap;
187 aPLMap = new ShapePipelineMap();
193 static Handle(PipelinePtr) GetPipeline (const IVtk_IdType& theShapeID)
195 Handle(PipelinePtr) aPtr;
196 GetPipelines()->Find (theShapeID, aPtr);
200 //! Get VTK render pipeline with shape ID got from actor.
201 static Handle(PipelinePtr) PipelineByActor (const vtkSmartPointer<vtkActor>& theActor)
203 IVtk_IdType aShapeID = IVtkTools_ShapeObject::GetShapeSource (theActor)->GetShape()->GetId();
204 return GetPipeline (aShapeID);
207 //! Get VTK render pipeline with actor that has the input name.
208 static Handle(PipelinePtr) PipelineByActorName (const TCollection_AsciiString& theName)
210 const vtkSmartPointer<vtkActor>& anActor = GetMapOfActors().Find2 (theName);
211 return PipelineByActor (anActor);
216 static Handle(WNT_Window)& GetWindow()
218 static Handle(WNT_Window) aWindow;
224 static Handle(Xw_Window)& GetWindow()
226 static Handle(Xw_Window) aXWWin;
232 static vtkSmartPointer<IVtkDraw_Interactor>& GetInteractor()
234 static vtkSmartPointer<IVtkDraw_Interactor> anInteractor;
238 static vtkSmartPointer<IVtkTools_ShapePicker>& GetPicker()
240 static vtkSmartPointer<IVtkTools_ShapePicker> aPicker;
244 //! Generate identical number for shape
245 Standard_Integer GenerateId()
247 static unsigned int aShapesCounter = (unsigned int )-1;
248 return (Standard_Integer )++aShapesCounter;
251 //=========================================================
254 //=========================================================
255 const Handle(WNT_WClass)& IVtkDraw::WClass()
257 static Handle(WNT_WClass) aWindowClass;
259 if (aWindowClass.IsNull())
261 aWindowClass = new WNT_WClass ("GWVTK_Class", NULL,
262 CS_VREDRAW | CS_HREDRAW, 0, 0,
263 ::LoadCursorW (NULL, IDC_ARROW));
269 //==============================================================
270 // Function : ViewerInit
272 //==============================================================
273 void IVtkDraw::ViewerInit (Standard_Integer thePxLeft,
274 Standard_Integer thePxTop,
275 Standard_Integer thePxWidth,
276 Standard_Integer thePxHeight)
278 static Standard_Boolean isFirst = Standard_True;
280 Standard_Integer aPxLeft = 0;
281 Standard_Integer aPxTop = 460;
282 Standard_Integer aPxWidth = 409;
283 Standard_Integer aPxHeight = 409;
295 aPxWidth = thePxWidth;
297 if (thePxHeight != 0)
299 aPxHeight = thePxHeight;
304 SetDisplayConnection (new Aspect_DisplayConnection ());
306 if (GetWindow().IsNull())
308 GetWindow() = new WNT_Window ("IVtkTest", WClass(),
313 GetWindow()->SetVirtual (Draw_VirtualWindows);
317 if (GetWindow().IsNull())
319 GetWindow() = new Xw_Window (GetDisplayConnection(),
322 aPxWidth, aPxHeight);
323 GetWindow()->SetVirtual (Draw_VirtualWindows);
327 GetRenderer() = vtkSmartPointer<vtkRenderer>::New();
329 vtkSmartPointer<vtkRenderWindow> aRenWin = vtkSmartPointer<vtkRenderWindow>::New();
330 aRenWin->AddRenderer (GetRenderer());
331 GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
332 aRenWin->SetSize (aPxWidth, aPxHeight);
335 aRenWin->SetWindowId((void*)GetWindow()->HWindow());
337 Window aWindowId = GetWindow()->XWindow();
338 aRenWin->SetWindowId ((void*)aWindowId);
339 Display *aDisplayId = GetDisplayConnection()->GetDisplay();
340 aRenWin->SetDisplayId (aDisplayId);
343 XSynchronize (aDisplayId, 1);
346 // X11 : For keyboard on SUN
348 wmhints.flags = InputHint;
351 XSetWMHints (aDisplayId, aWindowId, &wmhints);
353 XSelectInput (aDisplayId, aWindowId, ExposureMask | KeyPressMask |
354 ButtonPressMask | ButtonReleaseMask |
355 StructureNotifyMask |
357 Button1MotionMask | Button2MotionMask |
361 XSynchronize (aDisplayId, 0);
366 GetInteractor() = vtkSmartPointer<IVtkDraw_Interactor>::New();
367 GetInteractor()->SetRenderWindow (aRenWin);
368 GetInteractor()->SetOCCWindow (GetWindow());
370 vtkSmartPointer<vtkInteractorStyleTrackballCamera>
371 aStyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
372 GetInteractor()->SetInteractorStyle (aStyle);
375 GetPicker() = vtkSmartPointer<IVtkTools_ShapePicker>::New();
376 GetPicker()->SetTolerance (0.025f);
377 GetPicker()->SetRenderer (GetRenderer());
379 GetInteractor()->SetShapePicker (GetPicker());
380 GetInteractor()->SetPipelines (GetPipelines());
381 GetInteractor()->Initialize();
383 aRenWin->SetOffScreenRendering(Draw_VirtualWindows);
386 isFirst = Standard_False;
392 //================================================================
393 // Function : VtkInit
395 //================================================================
396 static Standard_Integer VtkInit (Draw_Interpretor& ,
397 Standard_Integer theArgNum,
398 const char** theArgs)
400 Standard_Integer aPxLeft = (theArgNum > 1) ? atoi(theArgs[1]) : 0;
401 Standard_Integer aPxTop = (theArgNum > 2) ? atoi(theArgs[2]) : 0;
402 Standard_Integer aPxWidth = (theArgNum > 3) ? atoi(theArgs[3]) : 0;
403 Standard_Integer aPxHeight = (theArgNum > 4) ? atoi(theArgs[4]) : 0;
405 IVtkDraw::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight);
410 //================================================================
411 // Function : CreateActor
413 //================================================================
414 vtkActor* CreateActor (const Standard_Integer theId,
415 const TopoDS_Shape& theShape)
417 if ( theShape.IsNull() )
422 Handle(PipelinePtr) aPL = new PipelinePtr (theShape, theId);
423 GetPipelines()->Bind (theId, aPL);
428 //================================================================
429 // Function : VtkDisplay
431 //================================================================
433 static Standard_Integer VtkDisplay (Draw_Interpretor& theDI,
434 Standard_Integer theArgNum,
435 const char** theArgs)
438 if (!GetInteractor()->IsEnabled())
440 theDI << theArgs[0] << " error : call ivtkinit before\n";
441 return 1; // TCL_ERROR
447 theDI << theArgs[0] << " error : expects at least 1 argument\n";
448 return 1; // TCL_ERROR
451 TCollection_AsciiString aName;
452 TopoDS_Shape anOldShape, aNewShape;
453 vtkSmartPointer<vtkRenderer>& aRenderer = GetRenderer();
454 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
457 aName = theArgs[anIndex];
458 // Get shape from DRAW
459 aNewShape = DBRep::Get (theArgs[anIndex]);
461 // The shape is already in the map
462 if (GetMapOfShapes().IsBound2 (aName))
464 // Get shape from map
465 anOldShape = GetMapOfShapes().Find2 (aName);
467 if (anOldShape.IsEqual (aNewShape))
469 // Get actor from map and display it
470 PipelineByActorName (aName)->AddToRenderer (aRenderer);
475 if (aNewShape.IsNull()) continue;
476 // Create actor from DRAW shape
477 vtkActor* anActor = CreateActor (GenerateId(), aNewShape);
478 // Remove old actor from render
479 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
481 GetMapOfShapes().UnBind2 (aName);
482 GetMapOfShapes().Bind (aNewShape, aName);
483 GetMapOfActors().UnBind2 (aName);
484 GetMapOfActors().Bind (anActor, aName);
486 PipelineByActorName (aName)->AddToRenderer (aRenderer);
487 // Compute selection for displayed actors
488 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
491 // There is no shape with given name in map
494 if (aNewShape.IsNull()) continue;
495 // Create actor from DRAW shape
496 Standard_Integer anId = GenerateId();
497 vtkSmartPointer<vtkActor> anActor = CreateActor (anId, aNewShape);
499 GetMapOfShapes().Bind (aNewShape, aName);
500 GetMapOfActors().Bind (anActor, aName);
502 GetPipeline(anId)->AddToRenderer(aRenderer);
504 // Compute selection for displayed actors
505 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
510 aRenderer->ResetCamera();
511 GetInteractor()->GetRenderWindow()->Render();
516 //================================================================
517 // Function : VtkErase
519 //================================================================
520 static Standard_Integer VtkErase (Draw_Interpretor& theDI,
521 Standard_Integer theArgNum,
522 const char** theArgs)
525 if (!GetInteractor()->IsEnabled())
527 theDI << theArgs[0] << " error : call ivtkinit before\n";
528 return 1; // TCL_ERROR
531 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
535 DoubleMapOfActorsAndNames::Iterator anIterator (GetMapOfActors());
536 while (anIterator.More())
538 PipelineByActor (anIterator.Key1())->RemoveFromRenderer (aRenderer);
542 // Erase named objects
545 TCollection_AsciiString aName;
546 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
548 aName = theArgs[anIndex];
549 if (GetMapOfActors().IsBound2 (aName))
551 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
557 aRenderer->ResetCamera();
558 GetInteractor()->GetRenderWindow()->Render();
562 //================================================================
563 // Function : VtkRemove
564 // Purpose : Remove the actor from memory.
565 //================================================================
566 static Standard_Integer VtkRemove(Draw_Interpretor& theDI,
567 Standard_Integer theArgNum,
568 const char** theArgs)
571 if (!GetInteractor()->IsEnabled())
573 theDI << theArgs[0] << " error : call ivtkinit before\n";
574 return 1; // TCL_ERROR
577 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
579 // Remove all objects
582 // Remove all actors from the renderer
583 DoubleMapOfActorsAndNames::Iterator anIterator(GetMapOfActors());
584 while (anIterator.More())
586 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
587 IVtkTools_ShapeObject::GetShapeSource(anIterator.Key1());
588 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
590 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
594 aRenderer->RemoveActor(anIterator.Key1());
598 // Remove all pipelines from the renderer
599 for (ShapePipelineMap::Iterator anIt(*GetPipelines()); anIt.More(); anIt.Next())
601 anIt.Value()->RemoveFromRenderer(aRenderer);
603 // Clear maps and remove all TopoDS_Shapes, actors and pipelines
604 GetMapOfShapes().Clear();
605 GetMapOfActors().Clear();
606 GetPipelines()->Clear();
608 // Remove named objects
611 TCollection_AsciiString aName;
612 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
614 aName = theArgs[anIndex];
615 if (GetMapOfActors().IsBound2(aName))
617 // Remove the actor and its pipeline (if found) from the renderer
618 vtkSmartPointer<vtkActor> anActor = GetMapOfActors().Find2(aName);
619 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
620 IVtkTools_ShapeObject::GetShapeSource(anActor);
621 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
623 IVtk_IdType aShapeID = aSrc->GetShape()->GetId();
624 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
625 GetPipeline(aSrc->GetShape()->GetId())->RemoveFromRenderer(aRenderer);
626 GetPipelines()->UnBind(aShapeID); // Remove a pipepline
630 aRenderer->RemoveActor(anActor);
632 // Remove the TopoDS_Shape and the actor
633 GetMapOfShapes().UnBind2(aName); // Remove a TopoDS shape
634 GetMapOfActors().UnBind2(aName); // Remove an actor
640 aRenderer->ResetCamera();
641 GetInteractor()->GetRenderWindow()->Render();
645 //================================================================
646 // Function : VtkSetDisplayMode
648 // Draw args : ivtksetdispmode [name] mode(0,1)
649 //================================================================
650 static Standard_Integer VtkSetDisplayMode (Draw_Interpretor& theDI,
651 Standard_Integer theArgNum,
652 const char** theArgs)
655 if (!GetInteractor()->IsEnabled())
657 theDI << theArgs[0] << " error: call ivtkinit before\n";
658 return 1; // TCL_ERROR
662 if (theArgNum != 2 && theArgNum != 3)
664 theDI << theArgs[0] << " error: expects 1 or 2 arguments\n";
665 return 1; // TCL_ERROR
668 vtkSmartPointer<vtkActor> anActor;
669 // Set disp mode for all objects
673 Standard_Integer aMode = Draw::Atoi (theArgs[1]);
674 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
675 while (anIter.More())
677 anActor = anIter.Key1();
678 IVtkTools_ShapeDataSource* aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
681 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
682 if (!anOccShape.IsNull())
684 IVtkTools_DisplayModeFilter* aFilter = GetPipeline ( anOccShape->GetId() )->GetDisplayModeFilter();
685 aFilter->SetDisplayMode((IVtk_DisplayMode)aMode);
693 // Set disp mode for named object
696 Standard_Integer aMode = atoi(theArgs[2]);
697 TCollection_AsciiString aName = theArgs[1];
698 if (GetMapOfActors().IsBound2 (aName))
700 anActor = GetMapOfActors().Find2 (aName);
701 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
704 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
705 if (!anOccShape.IsNull())
707 IVtkTools_DisplayModeFilter* aFilter = GetPipeline (anOccShape->GetId())->GetDisplayModeFilter();
708 aFilter->SetDisplayMode ((IVtk_DisplayMode)aMode);
717 GetInteractor()->Render();
721 //================================================================
722 // Function : VtkSetBoundaryDraw
724 // Draw args : ivtksetboundingdraw [name] draw on/off(0,1)
725 //================================================================
726 static Standard_Integer VtkSetBoundaryDraw(Draw_Interpretor& theDI,
727 Standard_Integer theArgNum,
728 const char** theArgs)
731 if (!GetInteractor()->IsEnabled())
733 theDI << theArgs[0] << " error: call ivtkinit before\n";
734 return 1; // TCL_ERROR
738 if (theArgNum != 2 && theArgNum != 3)
740 theDI << theArgs[0] << " error: expects 1 or 2 arguments\n";
741 return 1; // TCL_ERROR
744 vtkSmartPointer<vtkActor> anActor;
745 // Set disp mode for all objects
749 Standard_Integer toDraw = Draw::Atoi(theArgs[1]);
750 DoubleMapOfActorsAndNames::Iterator anIter(GetMapOfActors());
751 while (anIter.More())
753 anActor = anIter.Key1();
754 // Set Red color for boundary edges
755 vtkLookupTable* aTable = (vtkLookupTable*)anActor->GetMapper()->GetLookupTable();
756 IVtkTools::SetLookupTableColor(aTable, MT_SharedEdge, 1., 0., 0., 1.);
758 IVtkTools_ShapeDataSource* aSrc = IVtkTools_ShapeObject::GetShapeSource(anActor);
761 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
762 if (!anOccShape.IsNull())
764 IVtkTools_DisplayModeFilter* aFilter = GetPipeline(anOccShape->GetId())->GetDisplayModeFilter();
765 aFilter->SetDisplayMode(DM_Shading);
766 aFilter->SetFaceBoundaryDraw(toDraw != 0);
774 // Set disp mode for named object
777 Standard_Integer toDraw = Draw::Atoi(theArgs[2]);
778 TCollection_AsciiString aName = theArgs[1];
779 if (GetMapOfActors().IsBound2(aName))
781 anActor = GetMapOfActors().Find2(aName);
782 // Set Red color for boundary edges
783 vtkLookupTable* aTable = (vtkLookupTable*)anActor->GetMapper()->GetLookupTable();
784 IVtkTools::SetLookupTableColor(aTable, MT_SharedEdge, 1., 0., 0., 1.);
786 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc = IVtkTools_ShapeObject::GetShapeSource(anActor);
789 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
790 if (!anOccShape.IsNull())
792 IVtkTools_DisplayModeFilter* aFilter = GetPipeline(anOccShape->GetId())->GetDisplayModeFilter();
793 aFilter->SetDisplayMode(DM_Shading);
794 aFilter->SetFaceBoundaryDraw(toDraw != 0);
803 GetInteractor()->Render();
808 //================================================================
809 // Function : VtkSetSelectionMode
811 // Draw args : ivtksetselmode [name] mode on/off(0,1)
812 //================================================================
813 static Standard_Integer VtkSetSelectionMode (Draw_Interpretor& theDI,
814 Standard_Integer theArgNum,
815 const char** theArgs)
818 if (!GetInteractor()->IsEnabled())
820 theDI << theArgs[0] << " error: call ivtkinit before\n";
821 return 1; // TCL_ERROR
825 if (theArgNum != 3 && theArgNum != 4)
827 theDI << theArgs[0] << " error: expects 2 or 3 arguments\n";
828 return 1; // TCL_ERROR
831 vtkSmartPointer<vtkActor> anActor;
832 Standard_Integer aMode;
833 Standard_Boolean isTurnOn;
834 // Set sel mode for all objects
837 aMode = atoi (theArgs[1]);
838 isTurnOn = (atoi (theArgs[2]) != 0);
839 if (aMode < 0 || aMode > 8)
841 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
842 return 1; // TCL_ERROR
844 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
845 while (anIter.More())
847 anActor = anIter.Key1();
849 if (aMode == SM_Shape && isTurnOn)
851 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
852 IVtk_SelectionModeList::Iterator anIt (aList);
853 // Turn off all sel modes differed from SM_Shape
856 IVtk_SelectionMode aCurMode = anIt.Value();
857 if (SM_Shape != aCurMode)
859 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
863 GetPicker()->SetSelectionMode (anActor, SM_Shape);
866 if (aMode != SM_Shape)
870 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
871 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
872 if (aMode == SM_Vertex)
874 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
879 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
880 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
883 GetPicker()->SetSelectionMode(anActor, SM_Shape);
885 if (aMode == SM_Vertex)
887 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
895 // Set sel mode for named object
898 aMode = atoi (theArgs[2]);
899 isTurnOn = (atoi (theArgs[3]) != 0);
901 if (aMode < 0 || aMode > 8)
903 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
904 return 1; // TCL_ERROR
907 TCollection_AsciiString aName = theArgs[1];
908 if (GetMapOfActors().IsBound2 (aName))
910 anActor = GetMapOfActors().Find2 (aName);
912 if (aMode == SM_Shape && isTurnOn)
914 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
915 IVtk_SelectionModeList::Iterator anIt (aList);
916 // Turn off all sel modes differed from SM_Shape
919 IVtk_SelectionMode aCurMode = anIt.Value();
920 if (SM_Shape != aCurMode)
922 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
926 GetPicker()->SetSelectionMode (anActor, SM_Shape);
929 if (aMode != SM_Shape)
933 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
934 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
935 if (aMode == SM_Vertex)
937 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
942 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
943 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
946 GetPicker()->SetSelectionMode(anActor, SM_Shape);
948 if (aMode == SM_Vertex)
950 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
958 GetInteractor()->Render();
963 //================================================================
964 // Function : VtkMoveTo
966 // Draw args : ivtkmoveto x y
967 //================================================================
968 static Standard_Integer VtkMoveTo(Draw_Interpretor& theDI,
969 Standard_Integer theArgNum,
970 const char** theArgs)
973 if (!GetInteractor()->IsEnabled())
975 theDI << theArgs[0] << " error: call ivtkinit before\n";
976 return 1; // TCL_ERROR
982 theDI << theArgs[0] << " error: expects 2 arguments\n";
983 return 1; // TCL_ERROR
986 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
987 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
990 GetInteractor()->Selector()->GetPickPosition (aPickPnt.ChangeData());
991 theDI << aPickPnt.X() << " " << aPickPnt.Y() << " " << aPickPnt.Z();
995 //================================================================
996 // Function : VtkSelect
998 // Draw args : ivtkselect x y
999 //================================================================
1000 static Standard_Integer VtkSelect (Draw_Interpretor& theDI,
1001 Standard_Integer theArgNum,
1002 const char** theArgs)
1005 if (!GetInteractor()->IsEnabled())
1007 theDI << theArgs[0] << " error: call ivtkinit before\n";
1008 return 1; // TCL_ERROR
1014 theDI << theArgs[0] << " error: expects 3 arguments\n";
1015 return 1; // TCL_ERROR
1018 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
1019 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
1020 GetInteractor()->OnSelection();
1025 //===================================================================
1026 // Fubction : VtkFit
1028 // Draw args : ivtkfit
1029 //===================================================================
1031 static Standard_Integer VtkFit (Draw_Interpretor& theDI,
1033 const char** theArgs)
1036 if (!GetInteractor()->IsEnabled())
1038 theDI << theArgs[0] << " error : call ivtkinit before \n";
1039 return 1; //TCL_ERROR
1042 GetRenderer()->ResetCamera();
1043 GetInteractor()->Render();
1047 //===================================================================
1048 // Fubction : VtkDump
1050 // Draw args : ivtkdump FullFilename.{png|bmp|jpeg|tiff|pnm}
1051 // [buffer={rgb|rgba|depth}] [width height]
1052 // [stereoproj={L|R}]
1053 //===================================================================
1054 static Standard_Integer VtkDump (Draw_Interpretor& theDI,
1055 Standard_Integer theArgNum,
1056 const char** theArgs)
1059 if (!GetInteractor()->IsEnabled())
1061 std::cout << theArgs[0] << " error : call ivtkinit before \n";
1067 theDI << theArgs[0] << " error : wrong number of parameters. Type 'help"
1068 << theArgs[0] << "' for more information.\n";
1070 vtkSmartPointer<vtkWindowToImageFilter> anImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
1072 anImageFilter->SetInput (GetInteractor()->GetRenderWindow());
1073 // Set custom buffer type
1076 TCollection_AsciiString aBufferType (theArgs[2]);
1077 aBufferType.LowerCase();
1078 if (aBufferType.IsEqual ("rgb"))
1080 anImageFilter->SetInputBufferTypeToRGB();
1082 else if (aBufferType.IsEqual ("rgba"))
1084 anImageFilter->SetInputBufferTypeToRGBA();
1086 else if (aBufferType.IsEqual ("depth"))
1088 anImageFilter->SetInputBufferTypeToZBuffer();
1091 anImageFilter->Update();
1093 // Set custom stereo projection options
1094 if (theArgNum > 5 && GetRenderer()->GetRenderWindow()->GetStereoRender())
1096 Standard_CString aStereoProjStr = theArgs[5];
1098 Standard_Integer aStereoType = GetRenderer()->GetRenderWindow()->GetStereoType();
1099 if (strcasecmp (aStereoProjStr, "L"))
1101 GetRenderer()->GetRenderWindow()->SetStereoTypeToLeft();
1102 GetRenderer()->GetRenderWindow()->StereoUpdate();
1103 anImageFilter->Update();
1104 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1106 else if (strcasecmp (aStereoProjStr, "R"))
1108 GetRenderer()->GetRenderWindow()->SetStereoTypeToRight();
1109 GetRenderer()->GetRenderWindow()->StereoUpdate();
1110 anImageFilter->Update();
1111 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1115 theDI << theArgs[0] << " error: unknown value for stereo projection.\n";
1120 // Set parameters for image writer
1121 vtkSmartPointer<vtkImageWriter> anImageWriter;
1122 TCollection_AsciiString aFilename (theArgs[1]);
1123 Standard_Integer anExtStart = aFilename.SearchFromEnd (TCollection_AsciiString("."));
1124 TCollection_AsciiString aFormat = (anExtStart == -1) ? TCollection_AsciiString("")
1125 : aFilename.SubString (anExtStart + 1, aFilename.Length());
1126 aFormat.LowerCase();
1128 if (aFormat.IsEqual ("png"))
1130 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1132 else if (aFormat.IsEqual ("bmp"))
1134 anImageWriter = vtkSmartPointer<vtkBMPWriter>::New();
1136 else if (aFormat.IsEqual ("jpeg"))
1138 anImageWriter = vtkSmartPointer<vtkJPEGWriter>::New();
1140 else if (aFormat.IsEqual ("tiff"))
1142 anImageWriter = vtkSmartPointer<vtkTIFFWriter>::New();
1144 else if (aFormat.IsEqual ("pnm"))
1146 anImageWriter = vtkSmartPointer<vtkPNMWriter>::New();
1148 else // aFormat is unsupported or not set.
1150 if (aFormat.IsEmpty())
1152 theDI << theArgs[0] << " warning: the image format is not set.\n"
1153 << "The image will be saved into PNG format.\n";
1154 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1155 aFormat = TCollection_AsciiString ("png");
1156 if (anExtStart != -1)
1158 aFilename.Split (anExtStart);
1164 aFilename += aFormat;
1168 theDI << theArgs[0] << " error: the image format "
1169 << aFormat.ToCString() <<" is not supported.\n";
1175 anImageWriter->SetFileName (aFilename.ToCString());
1177 Standard_Integer aWidth = (theArgNum > 3) ? atoi (theArgs[3]) : 0;
1178 Standard_Integer aHeight = (theArgNum > 4) ? atoi (theArgs[4]) : 0;
1179 if (aWidth >= 0 || aHeight >= 0)
1182 vtkSmartPointer<vtkImageResize> anImageResize = vtkSmartPointer<vtkImageResize>::New();
1183 #if VTK_MAJOR_VERSION <= 5
1184 anImageResize->SetInput (anImageFilter->GetOutput());
1186 anImageResize->SetInputData (anImageFilter->GetOutput());
1189 anImageResize->SetOutputDimensions (aWidth, aHeight, 1);
1190 anImageResize->Update();
1191 anImageWriter->SetInputConnection (anImageResize->GetOutputPort());
1195 anImageWriter->SetInputConnection (anImageFilter->GetOutputPort());
1197 anImageWriter->Write();
1202 //===================================================================
1203 // Fubction : VtkBackgroundColor
1205 // Draw args : ivtkbgcolor r g b
1207 //===================================================================
1208 static Standard_Integer VtkBackgroundColor (Draw_Interpretor& theDI,
1209 Standard_Integer theArgNum,
1210 const char** theArgs)
1212 if (theArgNum != 4 && theArgNum != 7)
1214 theDI << theArgs[0] << " error : wrong number of parameters.\n"
1215 << "Type 'help " << theArgs[0] << "' for more information.\n";
1220 if (!GetInteractor()->IsEnabled())
1222 std::cout << theArgs[0] << " error : call ivtkinit before \n";
1226 Standard_Real aR = Draw::Atof(theArgs[1])/255.0;
1227 Standard_Real aG = Draw::Atof(theArgs[2])/255.0;
1228 Standard_Real aB = Draw::Atof(theArgs[3])/255.0;
1230 GetRenderer()->SetGradientBackground(false);
1231 GetRenderer()->SetBackground (aR, aG, aB);
1235 Standard_Real aR2 = Draw::Atof(theArgs[4])/255.0;
1236 Standard_Real aG2 = Draw::Atof(theArgs[5])/255.0;
1237 Standard_Real aB2 = Draw::Atof(theArgs[6])/255.0;
1239 GetRenderer()->SetBackground2(aR2, aG2, aB2);
1240 GetRenderer()->SetGradientBackground(true);
1243 GetInteractor()->Render();
1248 //================================================================
1249 // Function : Commands
1251 //================================================================
1252 void IVtkDraw::Commands (Draw_Interpretor& theCommands)
1254 const char *group = "VtkViewer";
1256 theCommands.Add("ivtkinit",
1258 "ivtkinit [leftPx topPx widthPx heightPx]"
1259 "\n\t\t: Creates the Vtk window",
1260 __FILE__, VtkInit, group);
1262 theCommands.Add("ivtkdisplay",
1263 "ivtkdisplay usage:\n"
1264 "ivtkdisplay name1 name2 ..."
1265 "\n\t\t: Displayes named objects in current view.",
1266 __FILE__, VtkDisplay, group);
1268 theCommands.Add("ivtkerase",
1269 "ivtkerase usage:\n"
1270 "ivtkerase [name1 name2 ...]"
1271 "\n\t\t: Removes from renderer named objects or all objects.",
1272 __FILE__, VtkErase, group);
1274 theCommands.Add("ivtkremove",
1275 "ivtkremove usage:\n"
1276 "ivtkremove [name1 name2 ...]"
1277 "\n\t\t: Removes from renderer and from memory named objects or all objects.",
1278 __FILE__, VtkRemove, group);
1280 theCommands.Add("ivtksetdispmode",
1281 "ivtksetdispmode usage:\n"
1282 "ivtksetdispmode [name] mode (0,1)"
1283 "\n\t\t: Sets or unsets display mode 'mode' to the object with name 'name' or to all objects"
1284 "if name is not defined.",
1285 __FILE__, VtkSetDisplayMode, group);
1287 theCommands.Add("ivtksetboundingdraw",
1288 "ivtksetboundingdraw usage:\n"
1289 "ivtksetboundingdraw [name] draw on/off (0,1)"
1290 "\n\t\t: Sets or unsets boundaries drawing for shading display mode to the object with name 'name' or to all objects"
1291 "if name is not defined.",
1292 __FILE__, VtkSetBoundaryDraw, group);
1294 theCommands.Add("ivtksetselmode",
1295 "ivtksetselmode usage:\n"
1296 " ivtksetselmode [name] mode on/off(0,1)"
1297 "\n\t\t: Sets or unsets selection mode 'mode' to the object with name 'name' or to the all displayed objects.",
1298 __FILE__, VtkSetSelectionMode, group);
1300 theCommands.Add("ivtkmoveto",
1301 "ivtkmoveto usage:\n"
1303 "\n\t\t: Moves position to the pixel with coordinates (x,y). The object on this position is highlighted.",
1304 __FILE__, VtkMoveTo, group);
1306 theCommands.Add("ivtkselect",
1307 "ivtkselect usage:\n"
1309 "\n\t\t: Selects object which correspond to the pixel with input coordinates (x,y).",
1310 __FILE__, VtkSelect, group);
1312 theCommands.Add("ivtkfit",
1315 "\n\t\t: Fits view according all displayed objects.",
1316 __FILE__, VtkFit, group);
1318 theCommands.Add("ivtkdump",
1320 "ivtkdump <FullFilename>.{png|bmp|jpeg|tiff|pnm} [buffer={rgb|rgba|depth}] [width height] [stereoproj={L|R}]"
1321 "\n\t\t: Dumps contents of viewer window to PNG, BMP, JPEG, TIFF or PNM file",
1322 __FILE__, VtkDump, group);
1324 theCommands.Add("ivtkbgcolor",
1325 "ivtkbgcolor usage:\n"
1326 "ivtkbgcolor r g b [r2 g2 b2]\n"
1327 "\n\t\t: Sets uniform background color or gradient one if second triple of paramers is set."
1328 "Color parameters r,g,b = [0..255].",
1329 __FILE__, VtkBackgroundColor, group);
1333 //================================================================
1334 // Function : Factory
1336 //================================================================
1337 void IVtkDraw::Factory (Draw_Interpretor& theDI)
1339 // definition of Viewer Commands
1340 IVtkDraw::Commands (theDI);
1343 // Declare entry point PLUGINFACTORY