diff --git a/kratos.gid/scripts/Custom/python_writer.py b/kratos.gid/scripts/Custom/python_writer.py new file mode 100644 index 000000000..f4762b79d --- /dev/null +++ b/kratos.gid/scripts/Custom/python_writer.py @@ -0,0 +1,40 @@ +import numpy as np +import tohil +from objarray_numpy_tools import objarray_to_nparray,nparray_to_objarray +from pathlib import Path + +#to create functions and variables for all tcl available ones +tcl=tohil.import_tcl() + + +def my_meshio_write_mesh2(filename): + # Gets the nodes in the GID Mesh + info_nodes=tuple(tcl.GiD_Info('mesh','nodes','-array2')) + node_ids,node_xyzs=info_nodes + #tcl.W(node_xyzs) + + for element_type in ['line','triangle','quadrilateral','tetrahedra','pyramid','prism','hexahedra']: + info_elements=tuple(tcl.GiD_Info('mesh','elements',element_type,'-array2')) + if (len(info_elements)): + elements_data=info_elements[0] + element_type_ret,element_ids_original,connectivities_original,materials=elements_data + element_ids=objarray_to_nparray(element_ids_original) + connectivities=objarray_to_nparray(connectivities_original) + tcl.W(element_type_ret) + tcl.W(element_ids) + + group_names=tcl.GiD_Groups("list") + for group_name in group_names: + group_name=str(group_name) # convert from tohil.tclobj to Python str + # get nodes of the group (returns "" when empty, so convert first then check length) + group_node_ids=objarray_to_nparray(tcl.GiD_EntitiesGroups("get", group_name, "nodes")) + + + # get elements of the group (returns "" when empty, so convert first then check length) + group_element_ids=objarray_to_nparray(tcl.GiD_EntitiesGroups("get", group_name, "elements")) + + return 0 + +# main +def start(filename): + return my_meshio_write_mesh2(filename) \ No newline at end of file diff --git a/kratos.gid/scripts/Menus.tcl b/kratos.gid/scripts/Menus.tcl index a3ae1203f..f9c6c641c 100644 --- a/kratos.gid/scripts/Menus.tcl +++ b/kratos.gid/scripts/Menus.tcl @@ -148,6 +148,11 @@ proc Kratos::ChangeMenus { } { GiDMenu::InsertOption "Kratos" [list "---"] [incr pos] PRE "" "" "" replace = GiDMenu::InsertOption "Kratos" [list "Write calculation files - No run" ] [incr pos] PRE [list Kratos::WriteCalculationFilesEvent] "" "" replace = GiDMenu::InsertOption "Kratos" [list "Run - No write" ] [incr pos] PRE [list Kratos::ForceRun] "" "" replace = + GiDMenu::InsertOption "Kratos" [list "Custom scripts"] [incr pos] PRE "" "" "" replace _ + foreach custom_script [Kratos::GetCustomScripts] { + GiDMenu::InsertOption "Kratos" [list "Custom scripts" "$custom_script"] [incr pos] PRE [list Kratos::ExecuteCustomScript $custom_script] "" "" replace _ + } + # GiDMenu::InsertOption "Kratos" [list "---"] [incr pos] PRE "" "" "" replace = # GiDMenu::InsertOption "Kratos" [list "You are in $fromode" ] [incr pos] PRE [list ] "" "" replace = # GiDMenu::InsertOption "Kratos" [list "Switch to $tomode" ] [incr pos] PRE [list Kratos::SwitchMode] "" "" replace = @@ -187,6 +192,18 @@ proc Kratos::About {} { Splash } +proc Kratos::GetCustomScripts { } { + # Custom scripts are defined in the folder scipts/Custom/*.py, and they are added to the menu with the name of the file (without extension) + variable kratos_private + set custom_scripts [list ] + if {[file exists [file join $kratos_private(Path) scripts Custom]]} { + set files [glob -nocomplain -directory [file join $kratos_private(Path) scripts Custom] *.py] + foreach file $files { + lappend custom_scripts [file rootname [file tail $file]] + } + } + return $custom_scripts +} proc Kratos::Splash { } { variable kratos_private diff --git a/kratos.gid/scripts/Utils.tcl b/kratos.gid/scripts/Utils.tcl index 374c3a9b0..333492f5e 100644 --- a/kratos.gid/scripts/Utils.tcl +++ b/kratos.gid/scripts/Utils.tcl @@ -409,3 +409,18 @@ if { ![GidUtils::IsTkDisabled] } { } } + +proc Kratos::ExecuteCustomScript {script_name} { + variable kratos_private + set script_path [file join $kratos_private(Path) scripts Custom ${script_name}.py] + if {[file exists $script_path]} { + # first version will assume that its for writing meshes in mdpa format + # must be python + # W "Executing custom script: $script_path" + GiD_Python_Source $script_path + GiD_Python_Call ${script_name}.start "[GiD_Info project ModelName].mdpa" + # [GiD_Python_Call gid_meshio.my_meshio_write_mesh2 $filename] + } else { + W "Custom script $script_name not found in path $script_path" + } +} \ No newline at end of file