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 #include <vtkAlgorithmOutput.h>
89 #include <vtkAppendPolyData.h>
90 #include <vtkBMPWriter.h>
91 #include <vtkCamera.h>
92 #include <vtkCellData.h>
93 #include <vtkCommand.h>
94 #include <vtkGeometryFilter.h>
95 #include <vtkIdTypeArray.h>
96 #include <vtkImageResize.h>
97 #include <vtkImageWriter.h>
98 #include <vtkInteractorStyleTrackballCamera.h>
99 #include <vtkJPEGWriter.h>
100 #include <vtkPNGWriter.h>
101 #include <vtkPNMWriter.h>
102 #include <vtkPolyDataMapper.h>
103 #include <vtkProperty.h>
104 #include <vtkRenderWindow.h>
105 #include <vtkRenderer.h>
106 #include <vtkRenderWindowInteractor.h>
107 #include <vtkSmartPointer.h>
108 #include <vtkTIFFWriter.h>
109 #include <vtkWindowToImageFilter.h>
113 #include <X11/Shell.h>
114 #include <X11/Xlib.h>
116 #include <Xw_Window.hxx>
117 #include <vtkXRenderWindowInteractor.h>
118 #include <vtkXOpenGLRenderWindow.h>
119 #include <X11/Xutil.h>
123 // workaround name conflicts with OCCT methods (in class TopoDS_Shape for example)
131 //================================================================
133 //================================================================
135 typedef NCollection_DoubleMap<TopoDS_Shape, TCollection_AsciiString> DoubleMapOfShapesAndNames;
136 typedef NCollection_DoubleMap<vtkSmartPointer<vtkActor>, TCollection_AsciiString> DoubleMapOfActorsAndNames;
138 typedef IVtkDraw_HighlightAndSelectionPipeline PipelinePtr;
140 //================================================================
142 //================================================================
144 Standard_IMPORT Standard_Boolean Draw_VirtualWindows;
146 static Handle(Aspect_DisplayConnection)& GetDisplayConnection()
148 static Handle(Aspect_DisplayConnection) aDisplayConnection;
149 return aDisplayConnection;
152 static void SetDisplayConnection (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
154 GetDisplayConnection() = theDisplayConnection;
157 static DoubleMapOfShapesAndNames& GetMapOfShapes()
159 static DoubleMapOfShapesAndNames aMap;
163 static DoubleMapOfActorsAndNames& GetMapOfActors()
165 static DoubleMapOfActorsAndNames aMap;
169 static vtkSmartPointer<vtkRenderer>& GetRenderer()
171 static vtkSmartPointer<vtkRenderer> aRenderer;
175 static Handle(ShapePipelineMap)& GetPipelines()
177 static Handle(ShapePipelineMap) aPLMap;
180 aPLMap = new ShapePipelineMap();
186 static Handle(PipelinePtr) GetPipeline (const IVtk_IdType& theShapeID)
188 Handle(PipelinePtr) aPtr;
189 GetPipelines()->Find (theShapeID, aPtr);
193 //! Get VTK render pipeline with shape ID got from actor.
194 static Handle(PipelinePtr) PipelineByActor (const vtkSmartPointer<vtkActor>& theActor)
196 IVtk_IdType aShapeID = IVtkTools_ShapeObject::GetShapeSource (theActor)->GetShape()->GetId();
197 return GetPipeline (aShapeID);
200 //! Get VTK render pipeline with actor that has the input name.
201 static Handle(PipelinePtr) PipelineByActorName (const TCollection_AsciiString& theName)
203 const vtkSmartPointer<vtkActor>& anActor = GetMapOfActors().Find2 (theName);
204 return PipelineByActor (anActor);
209 static Handle(WNT_Window)& GetWindow()
211 static Handle(WNT_Window) aWindow;
217 static Handle(Xw_Window)& GetWindow()
219 static Handle(Xw_Window) aXWWin;
225 static vtkSmartPointer<IVtkDraw_Interactor>& GetInteractor()
227 static vtkSmartPointer<IVtkDraw_Interactor> anInteractor;
231 static vtkSmartPointer<IVtkTools_ShapePicker>& GetPicker()
233 static vtkSmartPointer<IVtkTools_ShapePicker> aPicker;
237 //! Generate identical number for shape
238 Standard_Integer GenerateId()
240 static unsigned int aShapesCounter = (unsigned int )-1;
241 return (Standard_Integer )++aShapesCounter;
244 //=========================================================
247 //=========================================================
248 const Handle(Standard_Transient)& IVtkDraw::WClass()
250 static Handle(Standard_Transient) aWindowClass;
252 if (aWindowClass.IsNull())
254 aWindowClass = new WNT_WClass ("GWVTK_Class", DefWindowProc,
255 CS_VREDRAW | CS_HREDRAW, 0, 0,
256 ::LoadCursorW (NULL, IDC_ARROW));
262 //==============================================================
263 // Function : ViewerInit
265 //==============================================================
266 void IVtkDraw::ViewerInit (Standard_Integer thePxLeft,
267 Standard_Integer thePxTop,
268 Standard_Integer thePxWidth,
269 Standard_Integer thePxHeight)
271 static Standard_Boolean isFirst = Standard_True;
273 Standard_Integer aPxLeft = 0;
274 Standard_Integer aPxTop = 460;
275 Standard_Integer aPxWidth = 409;
276 Standard_Integer aPxHeight = 409;
288 aPxWidth = thePxWidth;
290 if (thePxHeight != 0)
292 aPxHeight = thePxHeight;
297 SetDisplayConnection (new Aspect_DisplayConnection ());
299 if (GetWindow().IsNull())
301 GetWindow() = new WNT_Window ("IVtkTest",
302 Handle(WNT_WClass)::DownCast (WClass()),
307 GetWindow()->SetVirtual (Draw_VirtualWindows);
311 if (GetWindow().IsNull())
313 GetWindow() = new Xw_Window (GetDisplayConnection(),
316 aPxWidth, aPxHeight);
317 GetWindow()->SetVirtual (Draw_VirtualWindows);
321 GetRenderer() = vtkSmartPointer<vtkRenderer>::New();
323 vtkSmartPointer<vtkRenderWindow> aRenWin = vtkSmartPointer<vtkRenderWindow>::New();
324 aRenWin->AddRenderer (GetRenderer());
325 GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
326 aRenWin->SetSize (aPxWidth, aPxHeight);
329 aRenWin->SetWindowId((void*)GetWindow()->HWindow());
331 Window aWindowId = GetWindow()->XWindow();
332 aRenWin->SetWindowId ((void*)aWindowId);
333 Display *aDisplayId = GetDisplayConnection()->GetDisplay();
334 aRenWin->SetDisplayId (aDisplayId);
337 XSynchronize (aDisplayId, 1);
340 // X11 : For keyboard on SUN
342 wmhints.flags = InputHint;
345 XSetWMHints (aDisplayId, aWindowId, &wmhints);
347 XSelectInput (aDisplayId, aWindowId, ExposureMask | KeyPressMask |
348 ButtonPressMask | ButtonReleaseMask |
349 StructureNotifyMask |
351 Button1MotionMask | Button2MotionMask |
355 XSynchronize (aDisplayId, 0);
360 GetInteractor() = vtkSmartPointer<IVtkDraw_Interactor>::New();
361 GetInteractor()->SetRenderWindow (aRenWin);
362 GetInteractor()->SetOCCWindow (GetWindow());
364 vtkSmartPointer<vtkInteractorStyleTrackballCamera>
365 aStyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
366 GetInteractor()->SetInteractorStyle (aStyle);
369 GetPicker() = vtkSmartPointer<IVtkTools_ShapePicker>::New();
370 GetPicker()->SetTolerance (0.025f);
371 GetPicker()->SetRenderer (GetRenderer());
373 GetInteractor()->SetShapePicker (GetPicker());
374 GetInteractor()->SetPipelines (GetPipelines());
375 GetInteractor()->Initialize();
377 aRenWin->SetOffScreenRendering(Draw_VirtualWindows);
380 isFirst = Standard_False;
386 //================================================================
387 // Function : VtkInit
389 //================================================================
390 static Standard_Integer VtkInit (Draw_Interpretor& ,
391 Standard_Integer theArgNum,
392 const char** theArgs)
394 Standard_Integer aPxLeft = (theArgNum > 1) ? atoi(theArgs[1]) : 0;
395 Standard_Integer aPxTop = (theArgNum > 2) ? atoi(theArgs[2]) : 0;
396 Standard_Integer aPxWidth = (theArgNum > 3) ? atoi(theArgs[3]) : 0;
397 Standard_Integer aPxHeight = (theArgNum > 4) ? atoi(theArgs[4]) : 0;
399 IVtkDraw::ViewerInit (aPxLeft, aPxTop, aPxWidth, aPxHeight);
404 //================================================================
405 // Function : CreateActor
407 //================================================================
408 vtkActor* CreateActor (const Standard_Integer theId,
409 const TopoDS_Shape& theShape)
411 if ( theShape.IsNull() )
416 Handle(PipelinePtr) aPL = new PipelinePtr (theShape, theId);
417 GetPipelines()->Bind (theId, aPL);
422 //================================================================
423 // Function : VtkDisplay
425 //================================================================
427 static Standard_Integer VtkDisplay (Draw_Interpretor& theDI,
428 Standard_Integer theArgNum,
429 const char** theArgs)
432 if (!GetInteractor()->IsEnabled())
434 theDI << theArgs[0] << " error : call ivtkinit before\n";
435 return 1; // TCL_ERROR
441 theDI << theArgs[0] << " error : expects at least 1 argument\n";
442 return 1; // TCL_ERROR
445 TCollection_AsciiString aName;
446 TopoDS_Shape anOldShape, aNewShape;
447 vtkSmartPointer<vtkRenderer>& aRenderer = GetRenderer();
448 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
451 aName = theArgs[anIndex];
452 // Get shape from DRAW
453 aNewShape = DBRep::Get (theArgs[anIndex]);
455 // The shape is already in the map
456 if (GetMapOfShapes().IsBound2 (aName))
458 // Get shape from map
459 anOldShape = GetMapOfShapes().Find2 (aName);
461 if (anOldShape.IsEqual (aNewShape))
463 // Get actor from map and display it
464 PipelineByActorName (aName)->AddToRenderer (aRenderer);
469 if (aNewShape.IsNull()) continue;
470 // Create actor from DRAW shape
471 vtkActor* anActor = CreateActor (GenerateId(), aNewShape);
472 // Remove old actor from render
473 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
475 GetMapOfShapes().UnBind2 (aName);
476 GetMapOfShapes().Bind (aNewShape, aName);
477 GetMapOfActors().UnBind2 (aName);
478 GetMapOfActors().Bind (anActor, aName);
480 PipelineByActorName (aName)->AddToRenderer (aRenderer);
481 // Compute selection for displayed actors
482 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
485 // There is no shape with given name in map
488 if (aNewShape.IsNull()) continue;
489 // Create actor from DRAW shape
490 Standard_Integer anId = GenerateId();
491 vtkSmartPointer<vtkActor> anActor = CreateActor (anId, aNewShape);
493 GetMapOfShapes().Bind (aNewShape, aName);
494 GetMapOfActors().Bind (anActor, aName);
496 GetPipeline(anId)->AddToRenderer(aRenderer);
498 // Compute selection for displayed actors
499 GetPicker()->SetSelectionMode (SM_Shape, Standard_True);
504 aRenderer->ResetCamera();
505 GetInteractor()->GetRenderWindow()->Render();
510 //================================================================
511 // Function : VtkErase
513 //================================================================
514 static Standard_Integer VtkErase (Draw_Interpretor& theDI,
515 Standard_Integer theArgNum,
516 const char** theArgs)
519 if (!GetInteractor()->IsEnabled())
521 theDI << theArgs[0] << " error : call ivtkinit before\n";
522 return 1; // TCL_ERROR
525 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
529 DoubleMapOfActorsAndNames::Iterator anIterator (GetMapOfActors());
530 while (anIterator.More())
532 PipelineByActor (anIterator.Key1())->RemoveFromRenderer (aRenderer);
536 // Erase named objects
539 TCollection_AsciiString aName;
540 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
542 aName = theArgs[anIndex];
543 if (GetMapOfActors().IsBound2 (aName))
545 PipelineByActorName (aName)->RemoveFromRenderer (aRenderer);
551 aRenderer->ResetCamera();
552 GetInteractor()->GetRenderWindow()->Render();
556 //================================================================
557 // Function : VtkRemove
558 // Purpose : Remove the actor from memory.
559 //================================================================
560 static Standard_Integer VtkRemove(Draw_Interpretor& theDI,
561 Standard_Integer theArgNum,
562 const char** theArgs)
565 if (!GetInteractor()->IsEnabled())
567 theDI << theArgs[0] << " error : call ivtkinit before\n";
568 return 1; // TCL_ERROR
571 vtkSmartPointer<vtkRenderer> aRenderer = GetRenderer();
573 // Remove all objects
576 // Remove all actors from the renderer
577 DoubleMapOfActorsAndNames::Iterator anIterator(GetMapOfActors());
578 while (anIterator.More())
580 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
581 IVtkTools_ShapeObject::GetShapeSource(anIterator.Key1());
582 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
584 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
588 aRenderer->RemoveActor(anIterator.Key1());
592 // Remove all pipelines from the renderer
593 for (ShapePipelineMap::Iterator anIt(*GetPipelines()); anIt.More(); anIt.Next())
595 anIt.Value()->RemoveFromRenderer(aRenderer);
597 // Clear maps and remove all TopoDS_Shapes, actors and pipelines
598 GetMapOfShapes().Clear();
599 GetMapOfActors().Clear();
600 GetPipelines()->Clear();
602 // Remove named objects
605 TCollection_AsciiString aName;
606 for (Standard_Integer anIndex = 1; anIndex < theArgNum; ++anIndex)
608 aName = theArgs[anIndex];
609 if (GetMapOfActors().IsBound2(aName))
611 // Remove the actor and its pipeline (if found) from the renderer
612 vtkSmartPointer<vtkActor> anActor = GetMapOfActors().Find2(aName);
613 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc =
614 IVtkTools_ShapeObject::GetShapeSource(anActor);
615 if (aSrc.GetPointer() != NULL && !(aSrc->GetShape().IsNull()))
617 IVtk_IdType aShapeID = aSrc->GetShape()->GetId();
618 GetPicker()->RemoveSelectableObject(aSrc->GetShape());
619 GetPipeline(aSrc->GetShape()->GetId())->RemoveFromRenderer(aRenderer);
620 GetPipelines()->UnBind(aShapeID); // Remove a pipepline
624 aRenderer->RemoveActor(anActor);
626 // Remove the TopoDS_Shape and the actor
627 GetMapOfShapes().UnBind2(aName); // Remove a TopoDS shape
628 GetMapOfActors().UnBind2(aName); // Remove an actor
634 aRenderer->ResetCamera();
635 GetInteractor()->GetRenderWindow()->Render();
639 //================================================================
640 // Function : VtkSetDisplayMode
642 // Draw args : ivtksetdispmode [name] mode(0,1)
643 //================================================================
644 static Standard_Integer VtkSetDisplayMode (Draw_Interpretor& theDI,
645 Standard_Integer theArgNum,
646 const char** theArgs)
649 if (!GetInteractor()->IsEnabled())
651 theDI << theArgs[0] << " error: call ivtkinit before\n";
652 return 1; // TCL_ERROR
656 if (theArgNum != 2 && theArgNum != 3)
658 theDI << theArgs[0] << " error: expects 1 or 2 arguments\n";
659 return 1; // TCL_ERROR
662 vtkSmartPointer<vtkActor> anActor;
663 // Set disp mode for all objects
667 Standard_Integer aMode = Draw::Atoi (theArgs[1]);
668 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
669 while (anIter.More())
671 anActor = anIter.Key1();
672 IVtkTools_ShapeDataSource* aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
675 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
676 if (!anOccShape.IsNull())
678 IVtkTools_DisplayModeFilter* aFilter = GetPipeline ( anOccShape->GetId() )->GetDisplayModeFilter();
679 aFilter->SetDisplayMode((IVtk_DisplayMode)aMode);
687 // Set disp mode for named object
690 Standard_Integer aMode = atoi(theArgs[2]);
691 TCollection_AsciiString aName = theArgs[1];
692 if (GetMapOfActors().IsBound2 (aName))
694 anActor = GetMapOfActors().Find2 (aName);
695 vtkSmartPointer<IVtkTools_ShapeDataSource> aSrc = IVtkTools_ShapeObject::GetShapeSource (anActor);
698 IVtkOCC_Shape::Handle anOccShape = aSrc->GetShape();
699 if (!anOccShape.IsNull())
701 IVtkTools_DisplayModeFilter* aFilter = GetPipeline (anOccShape->GetId())->GetDisplayModeFilter();
702 aFilter->SetDisplayMode ((IVtk_DisplayMode)aMode);
711 GetInteractor()->Render();
715 //================================================================
716 // Function : VtkSetSelectionMode
718 // Draw args : ivtksetselmode [name] mode on/off(0,1)
719 //================================================================
720 static Standard_Integer VtkSetSelectionMode (Draw_Interpretor& theDI,
721 Standard_Integer theArgNum,
722 const char** theArgs)
725 if (!GetInteractor()->IsEnabled())
727 theDI << theArgs[0] << " error: call ivtkinit before\n";
728 return 1; // TCL_ERROR
732 if (theArgNum != 3 && theArgNum != 4)
734 theDI << theArgs[0] << " error: expects 2 or 3 arguments\n";
735 return 1; // TCL_ERROR
738 vtkSmartPointer<vtkActor> anActor;
739 Standard_Integer aMode;
740 Standard_Boolean isTurnOn;
741 // Set sel mode for all objects
744 aMode = atoi (theArgs[1]);
745 isTurnOn = (atoi (theArgs[2]) != 0);
746 if (aMode < 0 || aMode > 8)
748 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
749 return 1; // TCL_ERROR
751 DoubleMapOfActorsAndNames::Iterator anIter (GetMapOfActors());
752 while (anIter.More())
754 anActor = anIter.Key1();
756 if (aMode == SM_Shape && isTurnOn)
758 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
759 IVtk_SelectionModeList::Iterator anIt (aList);
760 // Turn off all sel modes differed from SM_Shape
763 IVtk_SelectionMode aCurMode = anIt.Value();
764 if (SM_Shape != aCurMode)
766 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
770 GetPicker()->SetSelectionMode (anActor, SM_Shape);
773 if (aMode != SM_Shape)
777 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
778 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
779 if (aMode == SM_Vertex)
781 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
786 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
787 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
790 GetPicker()->SetSelectionMode(anActor, SM_Shape);
792 if (aMode == SM_Vertex)
794 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
802 // Set sel mode for named object
805 aMode = atoi (theArgs[2]);
806 isTurnOn = (atoi (theArgs[3]) != 0);
808 if (aMode < 0 || aMode > 8)
810 theDI << theArgs[0] << " error: only 0-8 selection modes are supported\n";
811 return 1; // TCL_ERROR
814 TCollection_AsciiString aName = theArgs[1];
815 if (GetMapOfActors().IsBound2 (aName))
817 anActor = GetMapOfActors().Find2 (aName);
819 if (aMode == SM_Shape && isTurnOn)
821 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
822 IVtk_SelectionModeList::Iterator anIt (aList);
823 // Turn off all sel modes differed from SM_Shape
826 IVtk_SelectionMode aCurMode = anIt.Value();
827 if (SM_Shape != aCurMode)
829 GetPicker()->SetSelectionMode (anActor, aCurMode, Standard_False);
833 GetPicker()->SetSelectionMode (anActor, SM_Shape);
836 if (aMode != SM_Shape)
840 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode);
841 GetPicker()->SetSelectionMode (anActor, SM_Shape, Standard_False);
842 if (aMode == SM_Vertex)
844 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOn();
849 GetPicker()->SetSelectionMode (anActor, (IVtk_SelectionMode)aMode, Standard_False);
850 IVtk_SelectionModeList aList = GetPicker()->GetSelectionModes (anActor);
853 GetPicker()->SetSelectionMode(anActor, SM_Shape);
855 if (aMode == SM_Vertex)
857 GetPipeline( IVtkTools_ShapeObject::GetShapeSource(anActor)->GetShape()->GetId() )->SharedVerticesSelectionOff();
865 GetInteractor()->Render();
870 //================================================================
871 // Function : VtkMoveTo
873 // Draw args : ivtkmoveto x y
874 //================================================================
875 static Standard_Integer VtkMoveTo(Draw_Interpretor& theDI,
876 Standard_Integer theArgNum,
877 const char** theArgs)
880 if (!GetInteractor()->IsEnabled())
882 theDI << theArgs[0] << " error: call ivtkinit before\n";
883 return 1; // TCL_ERROR
889 theDI << theArgs[0] << " error: expects 2 arguments\n";
890 return 1; // TCL_ERROR
893 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
894 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
898 //================================================================
899 // Function : VtkSelect
901 // Draw args : ivtkselect x y
902 //================================================================
903 static Standard_Integer VtkSelect (Draw_Interpretor& theDI,
904 Standard_Integer theArgNum,
905 const char** theArgs)
908 if (!GetInteractor()->IsEnabled())
910 theDI << theArgs[0] << " error: call ivtkinit before\n";
911 return 1; // TCL_ERROR
917 theDI << theArgs[0] << " error: expects 3 arguments\n";
918 return 1; // TCL_ERROR
921 Standard_Integer anY = GetInteractor()->GetRenderWindow()->GetSize()[1] - atoi (theArgs[2]) - 1;
922 GetInteractor()->MoveTo (atoi (theArgs[1]), anY);
923 GetInteractor()->OnSelection();
928 //===================================================================
931 // Draw args : ivtkfit
932 //===================================================================
934 static Standard_Integer VtkFit (Draw_Interpretor& theDI,
936 const char** theArgs)
939 if (!GetInteractor()->IsEnabled())
941 theDI << theArgs[0] << " error : call ivtkinit before \n";
942 return 1; //TCL_ERROR
945 GetRenderer()->ResetCamera();
946 GetInteractor()->Render();
950 //===================================================================
951 // Fubction : VtkDump
953 // Draw args : ivtkdump FullFilename.{png|bmp|jpeg|tiff|pnm}
954 // [buffer={rgb|rgba|depth}] [width height]
955 // [stereoproj={L|R}]
956 //===================================================================
957 static Standard_Integer VtkDump (Draw_Interpretor& theDI,
958 Standard_Integer theArgNum,
959 const char** theArgs)
962 if (!GetInteractor()->IsEnabled())
964 std::cout << theArgs[0] << " error : call ivtkinit before \n";
970 theDI << theArgs[0] << " error : wrong number of parameters. Type 'help"
971 << theArgs[0] << "' for more information.\n";
973 vtkSmartPointer<vtkWindowToImageFilter> anImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
975 anImageFilter->SetInput (GetInteractor()->GetRenderWindow());
976 // Set custom buffer type
979 TCollection_AsciiString aBufferType (theArgs[2]);
980 aBufferType.LowerCase();
981 if (aBufferType.IsEqual ("rgb"))
983 anImageFilter->SetInputBufferTypeToRGB();
985 else if (aBufferType.IsEqual ("rgba"))
987 anImageFilter->SetInputBufferTypeToRGBA();
989 else if (aBufferType.IsEqual ("depth"))
991 anImageFilter->SetInputBufferTypeToZBuffer();
994 anImageFilter->Update();
996 // Set custom stereo projection options
997 if (theArgNum > 5 && GetRenderer()->GetRenderWindow()->GetStereoRender())
999 Standard_CString aStereoProjStr = theArgs[5];
1001 Standard_Integer aStereoType = GetRenderer()->GetRenderWindow()->GetStereoType();
1002 if (strcasecmp (aStereoProjStr, "L"))
1004 GetRenderer()->GetRenderWindow()->SetStereoTypeToLeft();
1005 GetRenderer()->GetRenderWindow()->StereoUpdate();
1006 anImageFilter->Update();
1007 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1009 else if (strcasecmp (aStereoProjStr, "R"))
1011 GetRenderer()->GetRenderWindow()->SetStereoTypeToRight();
1012 GetRenderer()->GetRenderWindow()->StereoUpdate();
1013 anImageFilter->Update();
1014 GetRenderer()->GetRenderWindow()->SetStereoType (aStereoType);
1018 theDI << theArgs[0] << " error: unknown value for stereo projection.\n";
1023 // Set parameters for image writer
1024 vtkSmartPointer<vtkImageWriter> anImageWriter;
1025 TCollection_AsciiString aFilename (theArgs[1]);
1026 Standard_Integer anExtStart = aFilename.SearchFromEnd (TCollection_AsciiString("."));
1027 TCollection_AsciiString aFormat = (anExtStart == -1) ? TCollection_AsciiString("")
1028 : aFilename.SubString (anExtStart + 1, aFilename.Length());
1029 aFormat.LowerCase();
1031 if (aFormat.IsEqual ("png"))
1033 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1035 else if (aFormat.IsEqual ("bmp"))
1037 anImageWriter = vtkSmartPointer<vtkBMPWriter>::New();
1039 else if (aFormat.IsEqual ("jpeg"))
1041 anImageWriter = vtkSmartPointer<vtkJPEGWriter>::New();
1043 else if (aFormat.IsEqual ("tiff"))
1045 anImageWriter = vtkSmartPointer<vtkTIFFWriter>::New();
1047 else if (aFormat.IsEqual ("pnm"))
1049 anImageWriter = vtkSmartPointer<vtkPNMWriter>::New();
1051 else // aFormat is unsupported or not set.
1053 if (aFormat.IsEmpty())
1055 theDI << theArgs[0] << " warning: the image format is not set.\n"
1056 << "The image will be saved into PNG format.\n";
1057 anImageWriter = vtkSmartPointer<vtkPNGWriter>::New();
1058 aFormat = TCollection_AsciiString ("png");
1059 if (anExtStart != -1)
1061 aFilename.Split (anExtStart);
1067 aFilename += aFormat;
1071 theDI << theArgs[0] << " error: the image format "
1072 << aFormat.ToCString() <<" is not supported.\n";
1078 anImageWriter->SetFileName (aFilename.ToCString());
1080 Standard_Integer aWidth = (theArgNum > 3) ? atoi (theArgs[3]) : 0;
1081 Standard_Integer aHeight = (theArgNum > 4) ? atoi (theArgs[4]) : 0;
1082 if (aWidth >= 0 || aHeight >= 0)
1085 vtkSmartPointer<vtkImageResize> anImageResize = vtkSmartPointer<vtkImageResize>::New();
1086 #if VTK_MAJOR_VERSION <= 5
1087 anImageResize->SetInput (anImageFilter->GetOutput());
1089 anImageResize->SetInputData (anImageFilter->GetOutput());
1092 anImageResize->SetOutputDimensions (aWidth, aHeight, 1);
1093 anImageResize->Update();
1094 anImageWriter->SetInputConnection (anImageResize->GetOutputPort());
1098 anImageWriter->SetInputConnection (anImageFilter->GetOutputPort());
1100 anImageWriter->Write();
1105 //===================================================================
1106 // Fubction : VtkBackgroundColor
1108 // Draw args : ivtkbgcolor r g b
1110 //===================================================================
1111 static Standard_Integer VtkBackgroundColor (Draw_Interpretor& theDI,
1112 Standard_Integer theArgNum,
1113 const char** theArgs)
1115 if (theArgNum != 4 && theArgNum != 7)
1117 theDI << theArgs[0] << " error : wrong number of parameters.\n"
1118 << "Type 'help " << theArgs[0] << "' for more information.\n";
1123 if (!GetInteractor()->IsEnabled())
1125 std::cout << theArgs[0] << " error : call ivtkinit before \n";
1129 Standard_Real aR = Draw::Atof(theArgs[1])/255.0;
1130 Standard_Real aG = Draw::Atof(theArgs[2])/255.0;
1131 Standard_Real aB = Draw::Atof(theArgs[3])/255.0;
1133 GetRenderer()->SetGradientBackground(false);
1134 GetRenderer()->SetBackground (aR, aG, aB);
1138 Standard_Real aR2 = Draw::Atof(theArgs[4])/255.0;
1139 Standard_Real aG2 = Draw::Atof(theArgs[5])/255.0;
1140 Standard_Real aB2 = Draw::Atof(theArgs[6])/255.0;
1142 GetRenderer()->SetBackground2(aR2, aG2, aB2);
1143 GetRenderer()->SetGradientBackground(true);
1146 GetInteractor()->Render();
1151 //================================================================
1152 // Function : Commands
1154 //================================================================
1155 void IVtkDraw::Commands (Draw_Interpretor& theCommands)
1157 const char *group = "VtkViewer";
1159 theCommands.Add("ivtkinit",
1161 "ivtkinit [leftPx topPx widthPx heightPx]"
1162 "\n\t\t: Creates the Vtk window",
1163 __FILE__, VtkInit, group);
1165 theCommands.Add("ivtkdisplay",
1166 "ivtkdisplay usage:\n"
1167 "ivtkdisplay name1 name2 ..."
1168 "\n\t\t: Displayes named objects in current view.",
1169 __FILE__, VtkDisplay, group);
1171 theCommands.Add("ivtkerase",
1172 "ivtkerase usage:\n"
1173 "ivtkerase [name1 name2 ...]"
1174 "\n\t\t: Removes from renderer named objects or all objects.",
1175 __FILE__, VtkErase, group);
1177 theCommands.Add("ivtkremove",
1178 "ivtkremove usage:\n"
1179 "ivtkremove [name1 name2 ...]"
1180 "\n\t\t: Removes from renderer and from memory named objects or all objects.",
1181 __FILE__, VtkRemove, group);
1183 theCommands.Add("ivtksetdispmode",
1184 "ivtksetdispmode usage:\n"
1185 "ivtksetdispmode [name] mode (0,1)"
1186 "\n\t\t: Sets or unsets display mode 'mode' to the object with name 'name' or to all objects"
1187 "if name is not defined.",
1188 __FILE__, VtkSetDisplayMode, group);
1190 theCommands.Add("ivtksetselmode",
1191 "ivtksetselmode usage:\n"
1192 " ivtksetselmode [name] mode on/off(0,1)"
1193 "\n\t\t: Sets or unsets selection mode 'mode' to the object with name 'name' or to the all displayed objects.",
1194 __FILE__, VtkSetSelectionMode, group);
1196 theCommands.Add("ivtkmoveto",
1197 "ivtkmoveto usage:\n"
1199 "\n\t\t: Moves position to the pixel with coordinates (x,y). The object on this position is highlighted.",
1200 __FILE__, VtkMoveTo, group);
1202 theCommands.Add("ivtkselect",
1203 "ivtkselect usage:\n"
1205 "\n\t\t: Selects object which correspond to the pixel with input coordinates (x,y).",
1206 __FILE__, VtkSelect, group);
1208 theCommands.Add("ivtkfit",
1211 "\n\t\t: Fits view according all displayed objects.",
1212 __FILE__, VtkFit, group);
1214 theCommands.Add("ivtkdump",
1216 "ivtkdump <FullFilename>.{png|bmp|jpeg|tiff|pnm} [buffer={rgb|rgba|depth}] [width height] [stereoproj={L|R}]"
1217 "\n\t\t: Dumps contents of viewer window to PNG, BMP, JPEG, TIFF or PNM file",
1218 __FILE__, VtkDump, group);
1220 theCommands.Add("ivtkbgcolor",
1221 "ivtkbgcolor usage:\n"
1222 "ivtkbgcolor r g b [r2 g2 b2]\n"
1223 "\n\t\t: Sets uniform background color or gradient one if second triple of paramers is set."
1224 "Color parameters r,g,b = [0..255].",
1225 __FILE__, VtkBackgroundColor, group);
1229 //================================================================
1230 // Function : Factory
1232 //================================================================
1233 void IVtkDraw::Factory (Draw_Interpretor& theDI)
1235 // definition of Viewer Commands
1236 IVtkDraw::Commands (theDI);
1239 // Declare entry point PLUGINFACTORY