Special Tcl commands>Print>File
GiD_File fopen|fclose|fprintf|fflush|list
To allow print data from a Tcl procedure with standard fprintf command, specially to write the calculation file from Tcl or a mix of .bas template and Tcl procedures

Open a file named <filename> for writting access. By default access is "w", but is possible to use "a" to append to a previous file (and "wb" or "ab" to open the file in binary mode). It returns a long integer <file_id> representing the channel

Close a channel

Print data from a Tcl procedure in a file opened with GiD_File fopen and returns the number of printed characters.
(a .bas template implictly open/close a file with this command, and the file_id could be send to a tcl procedure as a parameter with the *FileId template keyword)
<file_id> must be a valid file descriptor, that could be obtained in a .bas template with the *FileId command (or with GiD_File fopen)
<format> must be a valid C/C++ format, according with the arguments
return

Force unwritten buffered data to be written to the file

It returns a list of currently open file_ids
Example:
.bas file:
Number of points and lines: *tcl(MyMethod *FileId)
.tcl file:
proc MyMethod { channel } {
GiD_File fprintf -nonewline $channel {%d %d} [GiD_Info Geometry NumPoints] [GiD_Info Geometry NumLines]
}
Special Tcl commands>Print>WriteCalculationFile
GiD_WriteCalculationFile
The command called GiD_WriteCalculationFile facilitate the creation of the output calculation file.
See also Writing the input file for calculation
Note: This command is provided to allow efficiency and relative flexibility writing the mesh and tree data related to groups, traversing the data structures without the cost of create a serialized copy in memory.
It is not compulsory to use this command, it is possible, and sometimes necesary, to use other Tcl commands to get mesh, groups and other data and reorder the information in order to be written.
GiD_WriteCalculationFile init|end|puts|coordinates|all_connectivities|connectivities|nodes|elements|has_elements ?-elemtype <etype>? ?-localaxes <formatLADict> ""? ?-elements_faces all|elements|faces? ?-number_ranges <NRDict>? ?-unique? ?-multiple? ?-all_entities? ?-do_subst? ?-sorted? ?-count? ?-return? ?-print_faces_conecs ? -connec_ordering? corner_face_corner_face <groupsDict>

To open for writting the calculation file.
Before to print any information to the file it must be opened with this command. Next prints of GiD_WriteCalculationFile will use implicilty this opened channel.
GiD internal strings are utf-8 codified,
-encoding external : (default) strings to print are converted to the current external encoding.
-encoding utf-8: strings are not converted.
It returns an file identifier that could be used with with 'GiD_File fprintf', but must not be used with Tcl standard print commands like 'puts' or 'write'
Example:
set file_id [GiD_WriteCalculationFile init -mode append {c:/temp/my output.dat}]
lassign {1.5 2.3} x y
GiD_File fprintf $file_id "x=%15.5f y=%15.5f" $x $y

To close the calculation file
Example:
GiD_WriteCalculationFile end

Print the string in the calculation file and a carriage return


Example:
GiD_WriteCalculationFile puts "hello world"

This command must be used to print all nodes of the mesh. It prints <num> <x> <y> <z> for each node.
<format> must be a "C-like" format for an integer and three doubles.
Are only printed the values with supplied format, e.g. if the format is "%d %f" only the node number and its x will be printed.
If a %.0s is specified then the corresponding value is not printed (trick to avoid print some values)


Example:
set num_coordinates [GiD_WriteCalculationFile coordinates -count ""] ;#with -count the format doesn't matter, "" could be used
GiD_WriteCalculationFile puts "num coordinates: $num_coordinates"
set unit_origin [gid_groups_conds::give_mesh_unit]
set unit_destination [gid_groups_conds::give_active_unit L]
set mesh_factor [gid_groups_conds::convert_unit_value L 1.0 m mm]
GiD_WriteCalculationFile coordinates -factor $mesh_factor "%d %g %g %g"

This command must be used to print all elements of the mesh. It prints the element number and its connectivities for each element of type <etype> of the mesh (all types if -elemtype is not set)
<format> must be an integer for the element id and as much integers as connectivities to be printed.

by default the order is corners_faces (first are printed the corners and then the quadratic nodes)
Example:
GiD_WriteCalculationFile all_connectivities -elemtype Triangle "id: %d connectivities: %d %d %d"


To get entities information related to groups: connectivities, nodes, elements of the group names specified in the <groupsDict> dictionary
for connectivities it prints the element number and its connectivities
for nodes it prints the node number and optionally (if the provided format expect it) its x y z real coordinates
for elements it prints the element number (for element faces prints element numer and face number, element from 1, face from 0)

A -do_subst flag should be required in order to replace the previous formulas in the GiD_WriteCalculationFile.
<groupsLADict> is a dictionary (list of pairs key value) of local axes, with key=LA_name and value=format

<NRDict> is a dictionary with key the format associated to a group name in <groupsDict> and value another format with and extra integer, typically the first format prefixed by "%d:"

if key is "" then is considered like a wildcard and match all entities that are not matched by other keys.

Example: to print the id and connectivities of the triangle elements of the groups named $group_a or $group_b
set format_by_group [dict create $group_a "A: %d - %d %d %d" $group_b "B: %d - %d %d %d"]
GiD_WriteCalculationFile connectivities -elemtype Triangle $format_by_group
Example: to print the node id of the nodes belonging to $group_name and a text with the group name
set format_by_group [dict create $group_name "%d of group $group_name"]
GiD_WriteCalculationFile nodes $format_by_group
or to print also its x y z coordinates:
set format_by_group [dict create $group_name "%d of group $group_name with coordinates %g %g %g"]
GiD_WriteCalculationFile nodes $format_by_group
or as a trick to avoid printing the z coordinate could use "%.0s" to jump this extra argument
set format_by_group [dict create $group_name "%d of group $group_name with coordinates %g %g %.0s"]
GiD_WriteCalculationFile nodes $format_by_group
Example: to print the element id of the triangle elements belonging to $group_name
GiD_WriteCalculationFile elements -elemtype Triangle [dict create $group_name "element id:%d node ids:%d %d %d\n"]
Example: to print the faces of tetrahedra belonging to $group_name, printing its 3 face node ids (the %.0s is to not pring the tetrahedra id)
GiD_WriteCalculationFile elements -elemtype Tetrahedra -elements_faces faces -print_faces_conecs [dict create $group_name "%.0snode ids: %d %d %d\n"]
Example: to print the faces of tetrahedra belonging to $group_name, printing the tetrahedra id and the local face index (0 to 4)
GiD_WriteCalculationFile elements -elemtype Tetrahedra -elements_faces faces [dict create $group_name "element id:%d face index:%d\n"]