Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions kratos.gid/scripts/Custom/python_writer.py
Original file line number Diff line number Diff line change
@@ -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

Comment on lines +3 to +5
#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)
17 changes: 17 additions & 0 deletions kratos.gid/scripts/Menus.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions kratos.gid/scripts/Utils.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment on lines +420 to +421
# [GiD_Python_Call gid_meshio.my_meshio_write_mesh2 $filename]
} else {
W "Custom script $script_name not found in path $script_path"
Comment on lines +415 to +424
}
}