|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. |
| 3 | +# SPDX-FileContributor: Jacques Franc |
| 4 | +# ruff: noqa: E402 # disable Module level import not at top of file |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | +from typing import Union |
| 8 | + |
| 9 | +from paraview.util.vtkAlgorithm import ( # type: ignore[import-not-found] |
| 10 | + VTKPythonAlgorithmBase, smdomain, smhint, smproperty, smproxy, |
| 11 | +) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/util/vtkAlgorithm.py |
| 12 | +from paraview.detail.loghandler import ( # type: ignore[import-not-found] |
| 13 | + VTKHandler, |
| 14 | +) # source: https://github.com/Kitware/ParaView/blob/master/Wrapping/Python/paraview/detail/loghandler.py |
| 15 | + |
| 16 | +from vtkmodules.vtkCommonDataModel import ( |
| 17 | + vtkMultiBlockDataSet, |
| 18 | + vtkUnstructuredGrid, |
| 19 | +) |
| 20 | + |
| 21 | +from vtkmodules.vtkCommonCore import ( |
| 22 | + vtkInformation, |
| 23 | + vtkInformationVector, |
| 24 | +) |
| 25 | + |
| 26 | +# update sys.path to load all GEOS Python Package dependencies |
| 27 | +geos_pv_path: Path = Path( __file__ ).parent.parent.parent.parent.parent |
| 28 | +sys.path.insert( 0, str( geos_pv_path / "src" ) ) |
| 29 | +from geos.pv.utils.config import update_paths |
| 30 | + |
| 31 | +update_paths() |
| 32 | + |
| 33 | +from geos.mesh.processing.ClipToMainFrame import ClipToMainFrame |
| 34 | + |
| 35 | +__doc__ = """ |
| 36 | +Clip the input mesh to the main frame applying the correct LandmarkTransform |
| 37 | +
|
| 38 | +To use it: |
| 39 | +
|
| 40 | +* Load the module in Paraview: Tools>Manage Plugins...>Load new>PVClipToMainFrame. |
| 41 | +* Apply. |
| 42 | +
|
| 43 | +""" |
| 44 | + |
| 45 | + |
| 46 | +@smproxy.filter( name="PVClipToMainFrame", label="Clip to the main frame" ) |
| 47 | +@smhint.xml( '<ShowInMenu category="4- Geos Utils"/>' ) |
| 48 | +@smproperty.input( name="Input", port_index=0 ) |
| 49 | +@smdomain.datatype( |
| 50 | + dataTypes=[ "vtkMultiBlockDataSet", "vtkUnstructuredGrid" ], |
| 51 | + composite_data_supported=True, |
| 52 | +) |
| 53 | +class PVClipToMainFrame( VTKPythonAlgorithmBase ): |
| 54 | + |
| 55 | + def __init__( self ) -> None: |
| 56 | + """Init motherclass, filter and logger.""" |
| 57 | + VTKPythonAlgorithmBase.__init__( self, |
| 58 | + nInputPorts=1, |
| 59 | + nOutputPorts=1, |
| 60 | + inputType="vtkDataObject", |
| 61 | + outputType="vtkDataObject" ) |
| 62 | + |
| 63 | + self._realFilter = ClipToMainFrame( speHandler=True ) |
| 64 | + if not self._realFilter.logger.hasHandlers(): |
| 65 | + self._realFilter.SetLoggerHandler( VTKHandler() ) |
| 66 | + |
| 67 | + #ensure I/O consistency |
| 68 | + def RequestDataObject( self, request: vtkInformation, inInfoVec: list[ vtkInformationVector ], |
| 69 | + outInfoVec: vtkInformationVector ) -> int: |
| 70 | + """Inherited from VTKPythonAlgorithmBase::RequestDataObject. |
| 71 | +
|
| 72 | + Args: |
| 73 | + request (vtkInformation): request |
| 74 | + inInfoVec (list[vtkInformationVector]): input objects |
| 75 | + outInfoVec (vtkInformationVector): output objects |
| 76 | +
|
| 77 | + Returns: |
| 78 | + int: 1 if calculation successfully ended, 0 otherwise. |
| 79 | + """ |
| 80 | + inData = self.GetInputData( inInfoVec, 0, 0 ) |
| 81 | + outData = self.GetOutputData( outInfoVec, 0 ) |
| 82 | + assert inData is not None |
| 83 | + if outData is None or ( not outData.IsA( inData.GetClassName() ) ): |
| 84 | + outData = inData.NewInstance() |
| 85 | + outInfoVec.GetInformationObject( 0 ).Set( outData.DATA_OBJECT(), outData ) |
| 86 | + return super().RequestDataObject( request, inInfoVec, outInfoVec ) # type: ignore[no-any-return] |
| 87 | + |
| 88 | + def RequestData( self, request: vtkInformation, inInfo: list[ vtkInformationVector ], |
| 89 | + outInfo: vtkInformationVector ) -> int: |
| 90 | + """Inherited from VTKPythonAlgorithmBase::RequestData. Apply ClipToMainFrame filter. |
| 91 | +
|
| 92 | + Args: |
| 93 | + request (vtkInformation): Request |
| 94 | + inInfo (list[vtkInformationVector]): Input objects |
| 95 | + outInfo (vtkInformationVector): Output objects |
| 96 | +
|
| 97 | + Returns: |
| 98 | + int: 1 if calculation successfully ended, 0 otherwise. |
| 99 | + """ |
| 100 | + inputMesh: Union[ vtkMultiBlockDataSet, vtkUnstructuredGrid ] = self.GetInputData( inInfo, 0, 0 ) |
| 101 | + outputMesh: Union[ vtkMultiBlockDataSet, vtkUnstructuredGrid ] = self.GetOutputData( outInfo, 0 ) |
| 102 | + |
| 103 | + # struct |
| 104 | + self._realFilter.SetInputData( inputMesh ) |
| 105 | + self._realFilter.ComputeTransform() |
| 106 | + self._realFilter.Update() |
| 107 | + outputMesh.ShallowCopy( self._realFilter.GetOutputDataObject( 0 ) ) |
| 108 | + |
| 109 | + return 1 |
0 commit comments