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(Standard_Transient)& IVtkDraw::WClass()
257 static Handle(Standard_Transient) aWindowClass;
259 if (aWindowClass.IsNull())
261 aWindowClass = new WNT_WClass ("GWVTK_Class", DefWindowProc,
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",
309 Handle(WNT_WClass)::DownCast (WClass()),
314 GetWindow()->SetVirtual (Draw_VirtualWindows);
318 if (GetWindow().IsNull())
320 GetWindow() = new Xw_Window (GetDisplayConnection(),
323 aPxWidth, aPxHeight);
324 GetWindow()->SetVirtual (Draw_VirtualWindows);
328 GetRenderer() = vtkSmartPointer<vtkRenderer>::New();
330 vtkSmartPointer<vtkRenderWindow> aRenWin = vtkSmartPointer<vtkRenderWindow>::New();
331 aRenWin->AddRenderer (GetRenderer());
332 GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
333 aRenWin->SetSize (aPxWidth, aPxHeight);
336 aRenWin->SetWindowId((void*)GetWindow()->HWindow());
338 Window aWindowId = GetWindow()->XWindow();
339 aRenWin->SetWindowId ((void*)aWindowId);
340 Display *aDisplayId = GetDisplayConnection()->GetDisplay();
341 aRenWin->SetDisplayId (aDisplayId);
344 XSynchronize (aDisplayId, 1);
347 // X11 : For keyboard on SUN
349 wmhints.flags = InputHint;
352 XSetWMHints (aDisplayId, aWindowId, &wmhints);
354 XSelectInput (aDisplayId, aWindowId, ExposureMask | KeyPressMask |
355 ButtonPressMask | ButtonReleaseMask |
356 StructureNotifyMask |
358 Button1MotionMask | Button2MotionMask |
362 XSynchronize (aDisplayId, 0);
367 GetInteractor() = vtkSmartPointer<IVtkDraw_Interactor>::New();
368 GetInteractor()->SetRenderWindow (aRenWin);
369 GetInteractor()->SetOCCWindow (GetWindow());
371 vtkSmartPointer<vtkInteractorStyleTrackballCamera>
372 aStyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
373 GetInteractor()->SetInteractorStyle (aStyle);
376 GetPicker() = vtkSmartPointer<IVtkTools_ShapePicker>::New();
377 GetPicker()->SetTolerance (0.025f);
378 GetPicker()->SetRenderer (GetRenderer());
380 GetInteractor()->SetShapePicker (GetPicker());
381 GetInteractor()->SetPipelines (GetPipelines());
382 GetInteractor()->Initialize();
384 aRenWin->SetOffScreenRendering(Draw_VirtualWindows);
387 isFirst = Standard_False;
393 //================================================================
394 // Function : VtkInit
396 //================================================================
397 static Standard_Integer VtkInit (Draw_Interpretor& ,
398 Standard_Integer theArgNum,
399 const char** theArgs)
401 Standard_Integer aPxLeft = (theArgNum > 1) ? atoi(theArgs[1]) : 0;
402 Standard_Integer aPxTop = (theArgNum > 2) ? atoi(theArgs[2]) : 0;
403 Standard_Integer aPxWidth = (theArgNum > 3) ? atoi(theArgs[3]) : 0;
404 Standard_Integer aPxHeight = (theArgNum > 4) ? atoi(theArgs[4]) : 0;
406 IVtkDraw::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight);
411 //================================================================
412 // Function : CreateActor
414 //================================================================
415 vtkActor* CreateActor (const Standard_Integer theId,
416 const TopoDS_Shape& theShape)
418 if ( theShape.IsNull() )
423 Handle(PipelinePtr) aPL = new PipelinePtr (theShape, theId);
424 GetPipelines()->Bind (theId, aPL);
429 //================================================================
430 // Function : VtkDisplay
432 //================================================================
434 static Standard_Integer VtkDisplay (Draw_Interpretor& theDI,
435 Standard_Integer theArgNum,
436 const char** theArgs)
439 if (!GetInteractor()->IsEnabled())
441 theDI << theArgs[0] << " error : call ivtkinit before\n";
442 return 1; // TCL_ERROR
448 theDI << theArgs[0] << " error : expects at least 1 argument\n";
449 return 1; // TCL_ERROR
452 TCollection_AsciiString aName;
453 TopoDS_Shape anOldShape, aNewShape;
454 vtkSmartPointer<vtkRenderer>& aRenderer = GetRenderer();
455 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
458 aName = theArgs[anIndex];
459 // Get shape from DRAW
460 aNewShape = DBRep::Get (theArgs[anIndex]);
462 // The shape is already in the map
463 if (GetMapOfShapes().IsBound2 (aName))
465 // Get shape from map
466 anOldShape = GetMapOfShapes().Find2 (aName);
468 if (anOldShape.IsEqual (aNewShape))
470 // Get actor from map and display it
471 PipelineByActorName (aName)->AddToRenderer (aRenderer);
476 if (aNewShape.IsNull()) continue;
477 // Create actor from DRAW shape
478 vtkActor* anActor = CreateActor (GenerateId(), aNewShape);
479 // Remove old actor from render
480 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
482 GetMapOfShapes().UnBind2 (aName);
483 GetMapOfShapes().Bind (aNewShape, aName);
484 GetMapOfActors().UnBind2 (aName);
485 GetMapOfActors().Bind (anActor, aName);
487 PipelineByActorName (aName)->AddToRenderer (aRenderer);
488 // Compute selection for displayed actors
489 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
492 // There is no shape with given name in map
495 if (aNewShape.IsNull()) continue;
496 // Create actor from DRAW shape
497 Standard_Integer anId = GenerateId();
498 vtkSmartPointer<vtkActor> anActor = CreateActor (anId, aNewShape);
500 GetMapOfShapes().Bind (aNewShape, aName);
501 GetMapOfActors().Bind (anActor, aName);
503 GetPipeline(anId)->AddToRenderer(aRenderer);
505 // Compute selection for displayed actors
506 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
511 aRenderer->ResetCamera();
512 GetInteractor()->GetRenderWindow()->Render();
517 //================================================================
518 // Function : VtkErase
520 //================================================================
521 static Standard_Integer VtkErase (Draw_Interpretor& theDI,
522 Standard_Integer theArgNum,
523 const char** theArgs)
526 if (!GetInteractor()->IsEnabled())
528 theDI << theArgs[0] << " error : call ivtkinit before\n";
529 return 1; // TCL_ERROR
532 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
536 DoubleMapOfActorsAndNames::Iterator anIterator (GetMapOfActors());
537 while (anIterator.More())
539 PipelineByActor (anIterator.Key1())->RemoveFromRenderer (aRenderer);
543 // Erase named objects
546 TCollection_AsciiString aName;
547 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
549 aName = theArgs[anIndex];
550 if (GetMapOfActors().IsBound2 (aName))
552 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
558 aRenderer->ResetCamera();
559 GetInteractor()->GetRenderWindow()->Render();
563 //================================================================
564 // Function : VtkRemove
565 // Purpose : Remove the actor from memory.
566 //================================================================
567 static Standard_Integer VtkRemove(Draw_Interpretor& theDI,
568 Standard_Integer theArgNum,
569 const char** theArgs)
572 if (!GetInteractor()->IsEnabled())
574 theDI << theArgs[0] << " error : call ivtkinit before\n";
575 return 1; // TCL_ERROR
578 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
580 // Remove all objects
583 // Remove all actors from the renderer
584 DoubleMapOfActorsAndNames::Iterator anIterator(GetMapOfActors());
585 while (anIterator.More())
587 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
588 IVtkTools_ShapeObject::GetShapeSource(anIterator.Key1());
589 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
591 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
595 aRenderer->RemoveActor(anIterator.Key1());
599 // Remove all pipelines from the renderer
600 for (ShapePipelineMap::Iterator anIt(*GetPipelines()); anIt.More(); anIt.Next())
602 anIt.Value()->RemoveFromRenderer(aRenderer);
604 // Clear maps and remove all TopoDS_Shapes, actors and pipelines
605 GetMapOfShapes().Clear();
606 GetMapOfActors().Clear();
607 GetPipelines()->Clear();
609 // Remove named objects
612 TCollection_AsciiString aName;
613 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
615 aName = theArgs[anIndex];
616 if (GetMapOfActors().IsBound2(aName))
618 // Remove the actor and its pipeline (if found) from the renderer
619 vtkSmartPointer<vtkActor> anActor = GetMapOfActors().Find2(aName);
620 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
621 IVtkTools_ShapeObject::GetShapeSource(anActor);
622 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
624 IVtk_IdType aShapeID = aSrc->GetShape()->GetId();
625 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
626 GetPipeline(aSrc->GetShape()->GetId())->RemoveFromRenderer(aRenderer);
627 GetPipelines()->UnBind(aShapeID); // Remove a pipepline
631 aRenderer->RemoveActor(anActor);
633 // Remove the TopoDS_Shape and the actor
634 GetMapOfShapes().UnBind2(aName); // Remove a TopoDS shape
635 GetMapOfActors().UnBind2(aName); // Remove an actor
641 aRenderer->ResetCamera();
642 GetInteractor()->GetRenderWindow()->Render();
646 //================================================================
647 // Function : VtkSetDisplayMode
649 // Draw args : ivtksetdispmode [name] mode(0,1)
650 //================================================================
651 static Standard_Integer VtkSetDisplayMode (Draw_Interpretor& theDI,
652 Standard_Integer theArgNum,
653 const char** theArgs)
656 if (!GetInteractor()->IsEnabled())
658 theDI << theArgs[0] << " error: call ivtkinit before\n";
659 return 1; // TCL_ERROR
663 if (theArgNum != 2 && theArgNum != 3)
665 theDI << theArgs[0] << " error: expects 1 or 2 arguments\n";
666 return 1; // TCL_ERROR
669 vtkSmartPointer<vtkActor> anActor;
670 // Set disp mode for all objects
674 Standard_Integer aMode = Draw::Atoi (theArgs[1]);
675 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
676 while (anIter.More())
678 anActor = anIter.Key1();
679 IVtkTools_ShapeDataSource* aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
682 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
683 if (!anOccShape.IsNull())
685 IVtkTools_DisplayModeFilter* aFilter = GetPipeline ( anOccShape->GetId() )->GetDisplayModeFilter();
686 aFilter->SetDisplayMode((IVtk_DisplayMode)aMode);
694 // Set disp mode for named object
697 Standard_Integer aMode = atoi(theArgs[2]);
698 TCollection_AsciiString aName = theArgs[1];
699 if (GetMapOfActors().IsBound2 (aName))
701 anActor = GetMapOfActors().Find2 (aName);
702 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
705 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
706 if (!anOccShape.IsNull())
708 IVtkTools_DisplayModeFilter* aFilter = GetPipeline (anOccShape->GetId())->GetDisplayModeFilter();
709 aFilter->SetDisplayMode ((IVtk_DisplayMode)aMode);
718 GetInteractor()->Render();
722 //================================================================
723 // Function : VtkSetSelectionMode
725 // Draw args : ivtksetselmode [name] mode on/off(0,1)
726 //================================================================
727 static Standard_Integer VtkSetSelectionMode (Draw_Interpretor& theDI,
728 Standard_Integer theArgNum,
729 const char** theArgs)
732 if (!GetInteractor()->IsEnabled())
734 theDI << theArgs[0] << " error: call ivtkinit before\n";
735 return 1; // TCL_ERROR
739 if (theArgNum != 3 && theArgNum != 4)
741 theDI << theArgs[0] << " error: expects 2 or 3 arguments\n";
742 return 1; // TCL_ERROR
745 vtkSmartPointer<vtkActor> anActor;
746 Standard_Integer aMode;
747 Standard_Boolean isTurnOn;
748 // Set sel mode for all objects
751 aMode = atoi (theArgs[1]);
752 isTurnOn = (atoi (theArgs[2]) != 0);
753 if (aMode < 0 || aMode > 8)
755 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
756 return 1; // TCL_ERROR
758 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
759 while (anIter.More())
761 anActor = anIter.Key1();
763 if (aMode == SM_Shape && isTurnOn)
765 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
766 IVtk_SelectionModeList::Iterator anIt (aList);
767 // Turn off all sel modes differed from SM_Shape
770 IVtk_SelectionMode aCurMode = anIt.Value();
771 if (SM_Shape != aCurMode)
773 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
777 GetPicker()->SetSelectionMode (anActor, SM_Shape);
780 if (aMode != SM_Shape)
784 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
785 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
786 if (aMode == SM_Vertex)
788 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
793 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
794 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
797 GetPicker()->SetSelectionMode(anActor, SM_Shape);
799 if (aMode == SM_Vertex)
801 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
809 // Set sel mode for named object
812 aMode = atoi (theArgs[2]);
813 isTurnOn = (atoi (theArgs[3]) != 0);
815 if (aMode < 0 || aMode > 8)
817 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
818 return 1; // TCL_ERROR
821 TCollection_AsciiString aName = theArgs[1];
822 if (GetMapOfActors().IsBound2 (aName))
824 anActor = GetMapOfActors().Find2 (aName);
826 if (aMode == SM_Shape && isTurnOn)
828 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
829 IVtk_SelectionModeList::Iterator anIt (aList);
830 // Turn off all sel modes differed from SM_Shape
833 IVtk_SelectionMode aCurMode = anIt.Value();
834 if (SM_Shape != aCurMode)
836 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
840 GetPicker()->SetSelectionMode (anActor, SM_Shape);
843 if (aMode != SM_Shape)
847 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
848 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
849 if (aMode == SM_Vertex)
851 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
856 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
857 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
860 GetPicker()->SetSelectionMode(anActor, SM_Shape);
862 if (aMode == SM_Vertex)
864 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
872 GetInteractor()->Render();
877 //================================================================
878 // Function : VtkMoveTo
880 // Draw args : ivtkmoveto x y
881 //================================================================
882 static Standard_Integer VtkMoveTo(Draw_Interpretor& theDI,
883 Standard_Integer theArgNum,
884 const char** theArgs)
887 if (!GetInteractor()->IsEnabled())
889 theDI << theArgs[0] << " error: call ivtkinit before\n";
890 return 1; // TCL_ERROR
896 theDI << theArgs[0] << " error: expects 2 arguments\n";
897 return 1; // TCL_ERROR
900 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
901 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
905 //================================================================
906 // Function : VtkSelect
908 // Draw args : ivtkselect x y
909 //================================================================
910 static Standard_Integer VtkSelect (Draw_Interpretor& theDI,
911 Standard_Integer theArgNum,
912 const char** theArgs)
915 if (!GetInteractor()->IsEnabled())
917 theDI << theArgs[0] << " error: call ivtkinit before\n";
918 return 1; // TCL_ERROR
924 theDI << theArgs[0] << " error: expects 3 arguments\n";
925 return 1; // TCL_ERROR
928 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
929 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
930 GetInteractor()->OnSelection();
935 //===================================================================
938 // Draw args : ivtkfit
939 //===================================================================
941 static Standard_Integer VtkFit (Draw_Interpretor& theDI,
943 const char** theArgs)
946 if (!GetInteractor()->IsEnabled())
948 theDI << theArgs[0] << " error : call ivtkinit before \n";
949 return 1; //TCL_ERROR
952 GetRenderer()->ResetCamera();
953 GetInteractor()->Render();
957 //===================================================================
958 // Fubction : VtkDump
960 // Draw args : ivtkdump FullFilename.{png|bmp|jpeg|tiff|pnm}
961 // [buffer={rgb|rgba|depth}] [width height]
962 // [stereoproj={L|R}]
963 //===================================================================
964 static Standard_Integer VtkDump (Draw_Interpretor& theDI,
965 Standard_Integer theArgNum,
966 const char** theArgs)
969 if (!GetInteractor()->IsEnabled())
971 std::cout << theArgs[0] << " error : call ivtkinit before \n";
977 theDI << theArgs[0] << " error : wrong number of parameters. Type 'help"
978 << theArgs[0] << "' for more information.\n";
980 vtkSmartPointer<vtkWindowToImageFilter> anImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
982 anImageFilter->SetInput (GetInteractor()->GetRenderWindow());
983 // Set custom buffer type
986 TCollection_AsciiString aBufferType (theArgs[2]);
987 aBufferType.LowerCase();
988 if (aBufferType.IsEqual ("rgb"))
990 anImageFilter->SetInputBufferTypeToRGB();
992 else if (aBufferType.IsEqual ("rgba"))
994 anImageFilter->SetInputBufferTypeToRGBA();
996 else if (aBufferType.IsEqual ("depth"))
998 anImageFilter->SetInputBufferTypeToZBuffer();
1001 anImageFilter->Update();
1003 // Set custom stereo projection options
1004 if (theArgNum > 5 && GetRenderer()->GetRenderWindow()->GetStereoRender())
1006 Standard_CString aStereoProjStr = theArgs[5];
1008 Standard_Integer aStereoType = GetRenderer()->GetRenderWindow()->GetStereoType();
1009 if (strcasecmp (aStereoProjStr, "L"))
1011 GetRenderer()->GetRenderWindow()->SetStereoTypeToLeft();
1012 GetRenderer()->GetRenderWindow()->StereoUpdate();
1013 anImageFilter->Update();
1014 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1016 else if (strcasecmp (aStereoProjStr, "R"))
1018 GetRenderer()->GetRenderWindow()->SetStereoTypeToRight();
1019 GetRenderer()->GetRenderWindow()->StereoUpdate();
1020 anImageFilter->Update();
1021 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1025 theDI << theArgs[0] << " error: unknown value for stereo projection.\n";
1030 // Set parameters for image writer
1031 vtkSmartPointer<vtkImageWriter> anImageWriter;
1032 TCollection_AsciiString aFilename (theArgs[1]);
1033 Standard_Integer anExtStart = aFilename.SearchFromEnd (TCollection_AsciiString("."));
1034 TCollection_AsciiString aFormat = (anExtStart == -1) ? TCollection_AsciiString("")
1035 : aFilename.SubString (anExtStart + 1, aFilename.Length());
1036 aFormat.LowerCase();
1038 if (aFormat.IsEqual ("png"))
1040 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1042 else if (aFormat.IsEqual ("bmp"))
1044 anImageWriter = vtkSmartPointer<vtkBMPWriter>::New();
1046 else if (aFormat.IsEqual ("jpeg"))
1048 anImageWriter = vtkSmartPointer<vtkJPEGWriter>::New();
1050 else if (aFormat.IsEqual ("tiff"))
1052 anImageWriter = vtkSmartPointer<vtkTIFFWriter>::New();
1054 else if (aFormat.IsEqual ("pnm"))
1056 anImageWriter = vtkSmartPointer<vtkPNMWriter>::New();
1058 else // aFormat is unsupported or not set.
1060 if (aFormat.IsEmpty())
1062 theDI << theArgs[0] << " warning: the image format is not set.\n"
1063 << "The image will be saved into PNG format.\n";
1064 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1065 aFormat = TCollection_AsciiString ("png");
1066 if (anExtStart != -1)
1068 aFilename.Split (anExtStart);
1074 aFilename += aFormat;
1078 theDI << theArgs[0] << " error: the image format "
1079 << aFormat.ToCString() <<" is not supported.\n";
1085 anImageWriter->SetFileName (aFilename.ToCString());
1087 Standard_Integer aWidth = (theArgNum > 3) ? atoi (theArgs[3]) : 0;
1088 Standard_Integer aHeight = (theArgNum > 4) ? atoi (theArgs[4]) : 0;
1089 if (aWidth >= 0 || aHeight >= 0)
1092 vtkSmartPointer<vtkImageResize> anImageResize = vtkSmartPointer<vtkImageResize>::New();
1093 #if VTK_MAJOR_VERSION <= 5
1094 anImageResize->SetInput (anImageFilter->GetOutput());
1096 anImageResize->SetInputData (anImageFilter->GetOutput());
1099 anImageResize->SetOutputDimensions (aWidth, aHeight, 1);
1100 anImageResize->Update();
1101 anImageWriter->SetInputConnection (anImageResize->GetOutputPort());
1105 anImageWriter->SetInputConnection (anImageFilter->GetOutputPort());
1107 anImageWriter->Write();
1112 //===================================================================
1113 // Fubction : VtkBackgroundColor
1115 // Draw args : ivtkbgcolor r g b
1117 //===================================================================
1118 static Standard_Integer VtkBackgroundColor (Draw_Interpretor& theDI,
1119 Standard_Integer theArgNum,
1120 const char** theArgs)
1122 if (theArgNum != 4 && theArgNum != 7)
1124 theDI << theArgs[0] << " error : wrong number of parameters.\n"
1125 << "Type 'help " << theArgs[0] << "' for more information.\n";
1130 if (!GetInteractor()->IsEnabled())
1132 std::cout << theArgs[0] << " error : call ivtkinit before \n";
1136 Standard_Real aR = Draw::Atof(theArgs[1])/255.0;
1137 Standard_Real aG = Draw::Atof(theArgs[2])/255.0;
1138 Standard_Real aB = Draw::Atof(theArgs[3])/255.0;
1140 GetRenderer()->SetGradientBackground(false);
1141 GetRenderer()->SetBackground (aR, aG, aB);
1145 Standard_Real aR2 = Draw::Atof(theArgs[4])/255.0;
1146 Standard_Real aG2 = Draw::Atof(theArgs[5])/255.0;
1147 Standard_Real aB2 = Draw::Atof(theArgs[6])/255.0;
1149 GetRenderer()->SetBackground2(aR2, aG2, aB2);
1150 GetRenderer()->SetGradientBackground(true);
1153 GetInteractor()->Render();
1158 //================================================================
1159 // Function : Commands
1161 //================================================================
1162 void IVtkDraw::Commands (Draw_Interpretor& theCommands)
1164 const char *group = "VtkViewer";
1166 theCommands.Add("ivtkinit",
1168 "ivtkinit [leftPx topPx widthPx heightPx]"
1169 "\n\t\t: Creates the Vtk window",
1170 __FILE__, VtkInit, group);
1172 theCommands.Add("ivtkdisplay",
1173 "ivtkdisplay usage:\n"
1174 "ivtkdisplay name1 name2 ..."
1175 "\n\t\t: Displayes named objects in current view.",
1176 __FILE__, VtkDisplay, group);
1178 theCommands.Add("ivtkerase",
1179 "ivtkerase usage:\n"
1180 "ivtkerase [name1 name2 ...]"
1181 "\n\t\t: Removes from renderer named objects or all objects.",
1182 __FILE__, VtkErase, group);
1184 theCommands.Add("ivtkremove",
1185 "ivtkremove usage:\n"
1186 "ivtkremove [name1 name2 ...]"
1187 "\n\t\t: Removes from renderer and from memory named objects or all objects.",
1188 __FILE__, VtkRemove, group);
1190 theCommands.Add("ivtksetdispmode",
1191 "ivtksetdispmode usage:\n"
1192 "ivtksetdispmode [name] mode (0,1)"
1193 "\n\t\t: Sets or unsets display mode 'mode' to the object with name 'name' or to all objects"
1194 "if name is not defined.",
1195 __FILE__, VtkSetDisplayMode, group);
1197 theCommands.Add("ivtksetselmode",
1198 "ivtksetselmode usage:\n"
1199 " ivtksetselmode [name] mode on/off(0,1)"
1200 "\n\t\t: Sets or unsets selection mode 'mode' to the object with name 'name' or to the all displayed objects.",
1201 __FILE__, VtkSetSelectionMode, group);
1203 theCommands.Add("ivtkmoveto",
1204 "ivtkmoveto usage:\n"
1206 "\n\t\t: Moves position to the pixel with coordinates (x,y). The object on this position is highlighted.",
1207 __FILE__, VtkMoveTo, group);
1209 theCommands.Add("ivtkselect",
1210 "ivtkselect usage:\n"
1212 "\n\t\t: Selects object which correspond to the pixel with input coordinates (x,y).",
1213 __FILE__, VtkSelect, group);
1215 theCommands.Add("ivtkfit",
1218 "\n\t\t: Fits view according all displayed objects.",
1219 __FILE__, VtkFit, group);
1221 theCommands.Add("ivtkdump",
1223 "ivtkdump <FullFilename>.{png|bmp|jpeg|tiff|pnm} [buffer={rgb|rgba|depth}] [width height] [stereoproj={L|R}]"
1224 "\n\t\t: Dumps contents of viewer window to PNG, BMP, JPEG, TIFF or PNM file",
1225 __FILE__, VtkDump, group);
1227 theCommands.Add("ivtkbgcolor",
1228 "ivtkbgcolor usage:\n"
1229 "ivtkbgcolor r g b [r2 g2 b2]\n"
1230 "\n\t\t: Sets uniform background color or gradient one if second triple of paramers is set."
1231 "Color parameters r,g,b = [0..255].",
1232 __FILE__, VtkBackgroundColor, group);
1236 //================================================================
1237 // Function : Factory
1239 //================================================================
1240 void IVtkDraw::Factory (Draw_Interpretor& theDI)
1242 // definition of Viewer Commands
1243 IVtkDraw::Commands (theDI);
1246 // Declare entry point PLUGINFACTORY