### Start, Get, and Stop Logging with Gmsh
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Demonstrates how to use the gmsh.logger module to start capturing messages, retrieve logged messages, and stop the logging process. This is essential for debugging and understanding Gmsh operations.
```python
import gmsh
gmsh.initialize()
# Start logging
gmsh.logger.start()
gmsh.model.add("logging")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Get logged messages
messages = gmsh.logger.get()
for msg in messages:
print(msg)
# Stop logging
gmsh.logger.stop()
gmsh.finalize()
```
```python
import gmsh
gmsh.initialize()
gmsh.logger.start()
# ... perform operations ...
# Retrieve all log messages
logs = gmsh.logger.get()
print(f"Total messages: {len(logs)}")
gmsh.logger.stop()
gmsh.finalize()
```
--------------------------------
### Install Gmsh Documentation Files (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
Installs various documentation files, including READMEs, licenses, changelogs, tutorials, demos, and man pages. It organizes these files into specific directories within the installation path.
```cmake
if(INSTALL_SDK_README)
configure_file(${SDK_FILE} ${CMAKE_CURRENT_BINARY_DIR}/README.txt)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/README.txt DESTINATION .)
endif()
install(FILES ${WELCOME_FILE} DESTINATION ${GMSH_DOC} RENAME README.txt)
install(FILES ${LICENSE_FILE} DESTINATION ${GMSH_DOC})
install(FILES ${CREDITS_FILE} DESTINATION ${GMSH_DOC})
install(FILES ${CHANGELOG_FILE} DESTINATION ${GMSH_DOC})
install(FILES ${TUTORIAL_FILES} DESTINATION ${GMSH_DOC}/tutorial)
foreach(DIR ${DEMOS_DIRS})
get_filename_component(DEMOS_DIR_NAME ${DIR} NAME)
file(GLOB DEMO_FILES ${DIR}/?*.*)
install(FILES ${DEMO_FILES} DESTINATION ${GMSH_DOC}/demos/${DEMOS_DIR_NAME})
endforeach()
if(UNIX AND NOT CYGWIN)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/doc/gmsh.1 DESTINATION ${GMSH_MAN})
endif()
```
--------------------------------
### Install gmsh Python Package Components
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/gmshpy/CMakeLists.txt
This section details the installation of various components for the gmsh Python package. It installs the __init__.py file, the setup.py file, C++ wrapper files, Python module files, and target libraries, ensuring the package is correctly structured in the destination directory.
```cmake
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/__init__.py.orig DESTINATION gmshpy/src RENAME __init__.py)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/setup.py DESTINATION gmshpy)
foreach(module ${SWIG_MODULES})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${module}PYTHON_wrap.cxx DESTINATION gmshpy/src)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${module}.py DESTINATION gmshpy/src)
install(TARGETS _${module} DESTINATION gmshpy/src)
endforeach(module)
```
--------------------------------
### Install Gmsh Targets and Libraries (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
Installs the main Gmsh executable, libraries (static and shared), and optional components based on build configurations. It handles destination paths for binaries, libraries, and includes, ensuring flexibility in installation.
```cmake
install(TARGETS gmsh DESTINATION ${GMSH_BIN} OPTIONAL)
if(ENABLE_BUILD_LIB)
install(TARGETS lib DESTINATION ${GMSH_LIB} OPTIONAL)
endif()
if(ENABLE_BUILD_SHARED OR ENABLE_BUILD_DYNAMIC)
install(TARGETS shared DESTINATION ${GMSH_LIB} OPTIONAL)
# FIXME once we cleanup the installation of the python module
install(TARGETS shared DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/api OPTIONAL)
endif()
if(ENABLE_ONELAB)
install(FILES ${ONELAB_PY} DESTINATION ${GMSH_BIN})
endif()
if(ENABLE_BUILD_LIB OR ENABLE_BUILD_SHARED OR ENABLE_BUILD_DYNAMIC)
install(FILES ${GMSH_API} DESTINATION ${GMSH_INC})
install(FILES ${GMSH_PY} DESTINATION ${GMSH_LIB})
install(FILES ${GMSH_JL} DESTINATION ${GMSH_LIB})
if(ENABLE_PRIVATE_API)
install(FILES ${GMSH_PRIVATE_API} DESTINATION ${GMSH_INC}/gmsh)
endif()
endif()
```
--------------------------------
### Installation Targets (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/java/CMakeLists.txt
This CMake snippet defines installation rules for the Gmsh Java wrapper. It specifies that the dynamic library (if applicable based on OS) and the generated JAR files should be installed into a designated library directory.
```cmake
install(TARGETS WrapGmsh DESTINATION ${GMSH_LIB} OPTIONAL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
DESTINATION ${GMSH_LIB})
```
--------------------------------
### Run FLTK GUI
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Starts the FLTK event loop, displaying the Gmsh GUI. This function is blocking and runs until the GUI window is closed.
```APIDOC
## gmsh.fltk.run()
### Description
Runs the FLTK event loop (blocking until the window is closed).
### Method
`gmsh.fltk.run()`
### Endpoint
`/fltk/run`
### Parameters
No parameters.
### Request Example
```python
import gmsh
gmsh.initialize()
gmsh.model.add("interactive")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Initialize and run the GUI
gmsh.fltk.initialize()
gmsh.fltk.run() # This call is blocking
gmsh.finalize()
```
### Response
#### Success Response (200)
FLTK GUI event loop finished.
#### Response Example
(No explicit response body, operation runs the GUI and returns upon closure.)
```
--------------------------------
### Define Installation Directories (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake code block defines installation directories (bin, lib, doc, man, inc) based on the operating system (WIN32, CYGWIN, APPLE) and build options (e.g., ENABLE_OS_SPECIFIC_INSTALL). It either uses predefined paths or includes GNUInstallDirs for standard locations.
```cmake
if(WIN32 OR CYGWIN)
if(ENABLE_OS_SPECIFIC_INSTALL)
set(GMSH_BIN .)
set(GMSH_LIB .)
set(GMSH_DOC .)
set(GMSH_MAN .)
set(GMSH_INC .)
else()
include(GNUInstallDirs)
set(GMSH_BIN ${CMAKE_INSTALL_BINDIR})
set(GMSH_LIB ${CMAKE_INSTALL_LIBDIR})
set(GMSH_DOC ${CMAKE_INSTALL_DOCDIR})
set(GMSH_MAN ${CMAKE_INSTALL_MANDIR}/man1)
set(GMSH_INC ${CMAKE_INSTALL_INCLUDEDIR})
endif()
if(CYGWIN)
unix2dos(GMSH_API)
if(ENABLE_PRIVATE_API)
unix2dos(GMSH_PRIVATE_API)
endif()
unix2dos(WELCOME_FILE)
unix2dos(SDK_FILE)
unix2dos(LICENSE_FILE)
unix2dos(CREDITS_FILE)
unix2dos(CHANGELOG_FILE)
unix2dos(TUTORIAL_FILES)
foreach(DIR ${DEMOS_DIRS})
file(GLOB DEMO_FILES ${DIR}/?*.*)
unix2dos(DEMO_FILES)
endforeach()
endif()
elseif(APPLE AND ENABLE_OS_SPECIFIC_INSTALL)
# set these so that the files get installed nicely in the MacOSX
# .app bundle
set(GMSH_BIN ../MacOS)
set(GMSH_LIB ../MacOS)
set(GMSH_DOC ../../..)
set(GMSH_MAN ../../..)
set(GMSH_INC ../MacOS)
else()
include(GNUInstallDirs)
set(GMSH_BIN ${CMAKE_INSTALL_BINDIR})
set(GMSH_LIB ${CMAKE_INSTALL_LIBDIR})
set(GMSH_DOC ${CMAKE_INSTALL_DOCDIR})
set(GMSH_MAN ${CMAKE_INSTALL_MANDIR}/man1)
set(GMSH_INC ${CMAKE_INSTALL_INCLUDEDIR})
endif()
```
--------------------------------
### Python Package Initialization and Installation (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/gmshpy/CMakeLists.txt
This snippet configures the Python package's `__init__.py` file using a template. It then installs this file to the designated Python package directory. This ensures that the Gmsh Python bindings can be imported correctly.
```cmake
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/__init__.py.in ${CMAKE_CURRENT_BINARY_DIR}/__init__.py)
if (NOT ENABLE_GMSHPY_SOURCE_PACKAGE)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/__init__.py DESTINATION ${GMSHPY_INSTALL_DIRECTORY})
```
--------------------------------
### Define Source Files for Gmsh Build (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/contrib/bamg/CMakeLists.txt
This snippet defines the list of C++ source files required for the Gmsh build using CMake's set command. It specifies core mesh-related files and files within the bamglib subdirectory.
```cmake
set(SRC
Mesh2d.cpp
bamg-gmsh.cpp
bamglib/Mesh2.cpp
bamglib/MeshGeom.cpp
bamglib/MeshRead.cpp
bamglib/Metric.cpp
bamglib/R2.cpp
bamglib/MeshDraw.cpp
bamglib/MeshQuad.cpp
bamglib/MeshWrite.cpp
bamglib/Meshio.cpp
bamglib/QuadTree.cpp
bamglib/SetOfE4.cpp
)
```
--------------------------------
### Discover Header Files and Append Source (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/contrib/bamg/CMakeLists.txt
This CMake code snippet discovers all header files (*.hpp) recursively within the project and then appends both the source and discovered header files to the Gmsh build, specifically for the 'contrib/bamg' module.
```cmake
file(GLOB_RECURSE HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.hpp)
append_gmsh_src(contrib/bamg "${SRC};${HDR}")
```
--------------------------------
### Generate gmsh Python Package Setup
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/gmshpy/CMakeLists.txt
This snippet configures the setup.py file for the gmsh Python package when building from source. It replaces semicolons with comma-space delimiters in the SWIG_MODULES list and then uses configure_file to generate the final setup.py from a template.
```cmake
if(ENABLE_GMSHPY_SOURCE_PACKAGE)
string(REPLACE ";" "\\", "` GMSH_PYTHON_MODULES "${SWIG_MODULES}")
set(GMSH_PYTHON_MODULES "`"${GMSH_PYTHON_MODULES}"`")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/setup.py)
endif(ENABLE_GMSHPY_SOURCE_PACKAGE)
```
--------------------------------
### Copying Frameworks for XCode Project Configuration
Source: https://github.com/sasobadovinac/gmsh/blob/master/contrib/mobile/CMakeLists.txt
Copies specified framework directories (PETSC, SLEPC, OCCT) to the application's framework directory within the build output. This is typically part of an XCode project setup process.
```cmake
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PETSC_FRAMEWORK}/ ${CMAKE_CURRENT_BINARY_DIR}/${APPNAME}/${APPNAME}/frameworks/petsc.framework/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${SLEPC_FRAMEWORK}/ ${CMAKE_CURRENT_BINARY_DIR}/${APPNAME}/${APPNAME}/frameworks/slepc.framework/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${OCCT_FRAMEWORK}/ ${CMAKE_CURRENT_BINARY_DIR}/${APPNAME}/${APPNAME}/frameworks/occt.framework/
```
--------------------------------
### Find and Configure PETSc Solver Library (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
Configures the PETSc library for use as a solver. It attempts to locate PETSc installation directories and parse configuration files to determine include paths. Handles different PETSc installation styles and versions.
```cmake
if(ENABLE_PETSC)
if(PETSC_DIR)
set(ENV_PETSC_DIR ${PETSC_DIR})
else()
set(ENV_PETSC_DIR $ENV{PETSC_DIR})
endif()
if(PETSC_ARCH)
set(ENV_PETSC_ARCH ${PETSC_ARCH})
else()
set(ENV_PETSC_ARCH $ENV{PETSC_ARCH})
endif()
set(PETSC_POSSIBLE_CONF_FILES
${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/conf/petscvariables
${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib/petsc-conf/petscvariables
${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/lib/petsc/conf/petscvariables)
foreach(FILE ${PETSC_POSSIBLE_CONF_FILES})
if(EXISTS ${FILE})
# old-style PETSc installations (using PETSC_DIR and PETSC_ARCH)
message(STATUS "Using PETSc dir: ${ENV_PETSC_DIR}")
message(STATUS "Using PETSc arch: ${ENV_PETSC_ARCH}")
# find includes by parsing the petscvariables file
file(STRINGS ${FILE} PETSC_VARIABLES NEWLINE_CONSUME)
endif()
endforeach()
if(PETSC_VARIABLES)
# try to find PETSC_CC_INCLUDES for PETSc >= 3.4
string(REGEX MATCH "PETSC_CC_INCLUDES = [^\n\r]*" PETSC_PACKAGES_INCLUDES
${PETSC_VARIABLES})
if(PETSC_PACKAGES_INCLUDES)
string(REPLACE "PETSC_CC_INCLUDES = " "" PETSC_PACKAGES_INCLUDES
${PETSC_PACKAGES_INCLUDES})
else()
# try to find PETSC_PACKAGES_INCLUDES in older versions
list(APPEND EXTERNAL_INCLUDES ${ENV_PETSC_DIR}/include)
list(APPEND EXTERNAL_INCLUDES ${ENV_PETSC_DIR}/${ENV_PETSC_ARCH}/include)
string(REGEX MATCH "PACKAGES_INCLUDES = [^\n\r]*" PETSC_PACKAGES_INCLUDES
${PETSC_VARIABLES})
string(REPLACE "PACKAGES_INCLUDES = " "" PETSC_PACKAGES_INCLUDES
${PETSC_PACKAGES_INCLUDES})
endif()
if(PETSC_PACKAGES_INCLUDES)
if(PETSC_PACKAGES_INCLUDES)
string(REPLACE "-I" "" PETSC_PACKAGES_INCLUDES ${PETSC_PACKAGES_INCLUDES})
string(REPLACE " " ";" PETSC_PACKAGES_INCLUDES ${PETSC_PACKAGES_INCLUDES})
foreach(VAR ${PETSC_PACKAGES_INCLUDES})
# seem to include unexisting directories (/usr/include/lib64)
# check to avoid warnings
if(EXISTS ${VAR})
list(APPEND EXTERNAL_INCLUDES ${VAR})
endif()
endforeach()
endif()
endif()
endif()
endif()
endif()
```
--------------------------------
### Configure MathEx Integration in CMake
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake snippet configures the MathEx library, prioritizing a system-wide installation if available and enabled. If not found system-wide, it falls back to adding the 'MathEx' subdirectory from the contrib folder.
```cmake
if(ENABLE_MATHEX)
find_library(MATHEX_LIB mathex PATH_SUFFIXES lib)
find_path(MATHEX_INC "mathex.h" PATH_SUFFIXES src include)
if(ENABLE_SYSTEM_CONTRIB AND MATHEX_LIB AND MATHEX_INC)
list(APPEND EXTERNAL_LIBRARIES ${MATHEX_LIB})
list(APPEND EXTERNAL_INCLUDES ${MATHEX_INC})
set_config_option(HAVE_MATHEX "MathEx[system]")
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/MathEx)
add_subdirectory(contrib/MathEx)
include_directories(contrib/MathEx)
set_config_option(HAVE_MATHEX "MathEx")
endif()
endif()
```
--------------------------------
### Generate Gmsh Documentation (makeinfo, texi2pdf) (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
Defines custom targets for generating Gmsh documentation in info, plain text, HTML, and PDF formats using 'makeinfo' and 'texi2pdf'. It handles dependencies and installation of the generated HTML and PDF files.
```cmake
find_program(MAKEINFO makeinfo)
if(MAKEINFO)
add_custom_command(OUTPUT ${TEX_DIR}/gmsh.info DEPENDS ${TEX_SRC}
COMMAND ${MAKEINFO} --split-size 1000000
ARGS ${TEX_DIR}/gmsh.texi WORKING_DIRECTORY ${TEX_DIR})
add_custom_target(info DEPENDS ${TEX_DIR}/gmsh.info)
add_custom_command(OUTPUT ${TEX_DIR}/gmsh.txt DEPENDS ${TEX_SRC}
COMMAND ${MAKEINFO} --plaintext -o gmsh.txt
ARGS ${TEX_DIR}/gmsh.texi WORKING_DIRECTORY ${TEX_DIR})
add_custom_target(txt DEPENDS ${TEX_DIR}/gmsh.txt)
add_custom_command(OUTPUT ${TEX_DIR}/gmsh.html DEPENDS ${TEX_SRC}
COMMAND ${MAKEINFO} --html --css-ref=http://gmsh.info/gmsh.css
--no-split --set-customization-variable
EXTRA_HEAD=''
ARGS ${TEX_DIR}/gmsh.texi WORKING_DIRECTORY ${TEX_DIR})
add_custom_target(html DEPENDS ${TEX_DIR}/gmsh.html)
install(FILES ${TEX_DIR}/gmsh.html DESTINATION ${GMSH_DOC} OPTIONAL)
else()
add_custom_target(html COMMAND ${CMAKE_COMMAND} -E touch ${TEX_DIR}/gmsh.html)
endif()
find_program(TEXI2PDF texi2pdf)
if(TEXI2PDF)
add_custom_command(OUTPUT ${TEX_DIR}/gmsh.pdf DEPENDS ${TEX_SRC}
COMMAND ${TEXI2PDF} ARGS gmsh.texi
WORKING_DIRECTORY ${TEX_DIR})
add_custom_target(pdf DEPENDS ${TEX_DIR}/gmsh.pdf)
install(FILES ${TEX_DIR}/gmsh.pdf DESTINATION ${GMSH_DOC} OPTIONAL)
endif()
```
--------------------------------
### SWIG Module and Java Wrapping Setup (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/java/CMakeLists.txt
This snippet configures SWIG to generate Java wrapper code for Gmsh. It sets up include directories, defines SWIG source files, specifies the output directory for generated Java files, and sets the Java package name. The `swig_add_module` command initiates the SWIG process to create C++ wrapper code from the interface file.
```cmake
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils)
# flag used to compilation during Java wrapping
set(CMAKE_CXX_FLAGS "-fno-strict-aliasing")
# find SWIG and include the swig file
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# find Java and JNI and include files required
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${JAVA_INCLUDE_PATH})
include_directories(${JAVA_INCLUDE_PATH2})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# define where the interface file (used by SWIG) are
set(SWIG_SOURCES WrapGmsh.i)
# let swig know that example.i is c++
set_source_files_properties(${SWIG_SOURCES} PROPERTIES CPLUSPLUS ON)
# define where the Java files generated by SWIG will be stored
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/)
# defines the packaging of Java files generated by SWIG
set(JAVAPACKAGENAME "org.geuz.gmsh.generated")
set(CMAKE_SWIG_FLAGS -package ${JAVAPACKAGENAME})
# create the swig module called WrapGmsh
# using the SWIG_SOURCE flag defined previously
# swig will be used to create WrapGmshJAVA_wrap.cxx from SWIG_SOURCE
swig_add_module(WrapGmsh java ${SWIG_SOURCES})
swig_link_libraries(WrapGmsh shared)
```
--------------------------------
### CMake: Setup Julia Tests
Source: https://github.com/sasobadovinac/gmsh/blob/master/demos/api/CMakeLists.txt
This CMake script locates the Julia executable. If Julia is found on the system, the script iterates through all Julia files (*.jl) and configures a CMake test for each, invoking the Julia interpreter to run the script. This integrates Julia script execution into the testing framework.
```cmake
find_program(JULIA julia)
if(JULIA)
file(GLOB DEMOS *.jl)
foreach(DEMO ${DEMOS})
get_filename_component(DEMONAME ${DEMO} NAME_WE)
add_test(${DEMONAME}_jl ${JULIA} ${DEMO})
endforeach()
endif()
```
--------------------------------
### Initialize FLTK GUI
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Initializes the FLTK graphical user interface for Gmsh, allowing for interactive visualization and operations.
```APIDOC
## gmsh.fltk.initialize()
### Description
Initializes the FLTK graphical user interface.
### Method
`gmsh.fltk.initialize()`
### Endpoint
`/fltk/initialize`
### Parameters
No parameters.
### Request Example
```python
import gmsh
gmsh.initialize()
gmsh.model.add("gui_example")
gmsh.model.occ.addSphere(0, 0, 0, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Initialize and run the GUI
gmsh.fltk.initialize()
gmsh.fltk.run()
gmsh.finalize()
```
### Response
#### Success Response (200)
FLTK GUI initialized.
#### Response Example
(No explicit response body, operation initializes the GUI.
```
--------------------------------
### Set Shared Library Install Name Directory on macOS (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake code configures the installation path for shared libraries on macOS. If dynamic or shared build is enabled, it sets the INSTALL_NAME_DIR property for the 'shared' target to the specified library installation directory.
```cmake
if(APPLE)
if(ENABLE_BUILD_DYNAMIC OR ENABLE_BUILD_SHARED)
set_target_properties(shared PROPERTIES INSTALL_NAME_DIR
${CMAKE_INSTALL_PREFIX}/${GMSH_LIB})
endif()
endif()
```
--------------------------------
### Run Gmsh GUI and Finalize
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
This snippet shows how to launch the Gmsh GUI and keep it open until the user closes it, followed by finalizing the Gmsh instance. It's useful for interactive model viewing and manipulation.
```python
import gmsh
gmsh.fltk.run()
gmsh.finalize()
```
--------------------------------
### Initialize FLTK GUI using gmsh.fltk.initialize()
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Initializes the FLTK graphical user interface. This function should be called after GMSH initialization and model creation, before running the GUI.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("gui_example")
gmsh.model.occ.addSphere(0, 0, 0, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Initialize and run the GUI
gmsh.fltk.initialize()
gmsh.fltk.run()
gmsh.finalize()
```
--------------------------------
### Determine Gmsh Python Wrapper Installation Path
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/gmshpy/CMakeLists.txt
This CMake code determines the installation directory for Gmsh Python wrappers. It executes a Python script to find the site-packages directory and appends 'gmshpy' to it, ensuring Python modules are installed in the correct location. This path is cached for subsequent runs.
```cmake
if (NOT GMSHPY_INSTALL_DIRECTORY)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print (sysconfig.get_python_lib(1,0,prefix='${CMAKE_INSTALL_PREFIX}'))" OUTPUT_VARIABLE PYTHON_MODULE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
set(GMSHPY_INSTALL_DIRECTORY ${PYTHON_MODULE_PATH}/gmshpy CACHE STRING "python wrappers installation directory")
endif (NOT GMSHPY_INSTALL_DIRECTORY)
```
--------------------------------
### Modifying Gmsh Source for QuadTri Patch
Source: https://github.com/sasobadovinac/gmsh/blob/master/contrib/QuadTri/README.txt
Instructions for integrating the QuadTri patch into the Gmsh source code. This involves copying the patch files into the appropriate Gmsh directories and recompiling the source code. Users are cautioned to ensure the correct CMakeLists.txt file is used during the compilation process.
```Shell Script
# Copy QuadTri patch files directly into Gmsh directories
# Replace existing copies with patched versions
# Compile Gmsh source as usual, being careful with CMakeLists.txt files
```
--------------------------------
### Invoke TransfQuadTri for Transfinite Volumes
Source: https://github.com/sasobadovinac/gmsh/blob/master/benchmarks/extrude_quadtri/READMEQUADTRI.txt
The TransfQuadTri command is used to apply the transfinite QuadTri algorithm to specified volumes. It can be applied to a list of volume numbers or to all existing volumes using '{:}'. This command only affects transfinite volumes; it has no effect on non-transfinite volumes.
```gmsh
TransfQuadTri { expression-list } | {:};
// Example: Apply to volume 1
TransfQuadTri { 1 };
// Example: Apply to all volumes
TransfQuadTri {:};
```
--------------------------------
### Get Number Option
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves the current value of a numerical option within Gmsh.
```APIDOC
## gmsh.option.getNumber(name)
### Description
Gets the current value of a numerical option.
### Method
`gmsh.option.getNumber(name)`
### Endpoint
`/option/getNumber`
### Parameters
#### Path Parameters
- **name** (string) - Required - The name of the option to retrieve (e.g., "Mesh.Algorithm").
### Request Example
```python
import gmsh
gmsh.initialize()
# Check current mesh algorithm
algo = gmsh.option.getNumber("Mesh.Algorithm")
print(f"Current mesh algorithm: {algo}")
gmsh.finalize()
```
### Response
#### Success Response (200)
- **value** (float) - The current numerical value of the specified option.
#### Response Example
```json
{
"value": 6.0
}
```
```
--------------------------------
### Get Mesh Elements
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves information about the elements in the mesh. Can be filtered by dimension and entity tag.
```APIDOC
## gmsh.model.mesh.getElements(dim=-1, tag=-1)
### Description
Returns the elements of the mesh, optionally filtered by entity.
### Method
`gmsh.model.mesh.getElements(dim, tag)`
### Endpoint
`/model/mesh/getElements`
### Parameters
#### Query Parameters
- **dim** (integer) - Optional - The dimension of the entity to filter elements by. Defaults to -1 (all dimensions).
- **tag** (integer) - Optional - The tag of the entity to filter elements by. Defaults to -1 (all entities).
### Request Example
```python
import gmsh
gmsh.initialize()
gmsh.model.add("get_elements")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Get all mesh elements
elementTypes, elementTags, nodeTags = gmsh.model.mesh.getElements()
for i, elemType in enumerate(elementTypes):
name, dim, order, numNodes, localCoords, numPrimNodes = \
gmsh.model.mesh.getElementProperties(elemType)
print(f"Element type: {name}, count: {len(elementTags[i])}")
gmsh.finalize()
```
### Response
#### Success Response (200)
- **elementTypes** (list of integers) - The types of the mesh elements.
- **elementTags** (list of lists of integers) - The tags of the mesh elements, grouped by type.
- **nodeTags** (list of lists of integers) - The node tags forming each element, grouped by type.
#### Response Example
```json
{
"elementTypes": [1, 2, 3, ...],
"elementTags": [[1, 2, 3, ...], [4, 5, 6, ...], ...],
"nodeTags": [[1, 2, 3, 4], [1, 5, 8, 4], ...]
}
```
```
--------------------------------
### Get Mesh Nodes
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves information about the nodes in the mesh. Can be filtered by dimension and entity tag.
```APIDOC
## gmsh.model.mesh.getNodes(dim=-1, tag=-1)
### Description
Returns the nodes of the mesh, optionally filtered by entity.
### Method
`gmsh.model.mesh.getNodes(dim, tag)`
### Endpoint
`/model/mesh/getNodes`
### Parameters
#### Query Parameters
- **dim** (integer) - Optional - The dimension of the entity to filter nodes by. Defaults to -1 (all dimensions).
- **tag** (integer) - Optional - The tag of the entity to filter nodes by. Defaults to -1 (all entities).
### Request Example
```python
import gmsh
gmsh.initialize()
gmsh.model.add("get_nodes")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Get all mesh nodes
nodeTags, nodeCoords, nodeParams = gmsh.model.mesh.getNodes()
print(f"Number of nodes: {len(nodeTags)}")
# nodeCoords is flattened: [x1, y1, z1, x2, y2, z2, ...]
for i in range(min(5, len(nodeTags))):
x, y, z = nodeCoords[3*i:3*i+3]
print(f"Node {nodeTags[i]}: ({x}, {y}, {z})")
gmsh.finalize()
```
### Response
#### Success Response (200)
- **nodeTags** (list of integers) - The unique tags of the mesh nodes.
- **nodeCoords** (list of floats) - The coordinates of the nodes, flattened in (x, y, z) order.
- **nodeParams** (list of floats) - Additional parameters associated with each node (e.g., parametric coordinates).
#### Response Example
```json
{
"nodeTags": [1, 2, 3, 4, 5, ...],
"nodeCoords": [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, ...],
"nodeParams": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...]
}
```
```
--------------------------------
### Create Torus using Gmsh OpenCASCADE Kernel
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Demonstrates creating a torus using `gmsh.model.occ.addTorus`. It initializes Gmsh, adds a torus, synchronizes, generates a 3D mesh, and writes the mesh file. Requires Gmsh library.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("torus")
# Create a torus at origin with major radius 1 and minor radius 0.3
torus = gmsh.model.occ.addTorus(0, 0, 0, 1, 0.3, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
gmsh.write("torus.msh")
gmsh.finalize()
```
--------------------------------
### Clear Gmsh Models and Data
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Clears all currently loaded models and post-processing data from Gmsh. This function allows for starting with a clean state.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("temp")
# ... create geometry ...
gmsh.clear() # Remove all models
gmsh.model.add("new_model") # Start fresh
gmsh.finalize()
```
--------------------------------
### Get Numerical Options using gmsh.option.getNumber()
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves the current value of a numerical GMSH option. This function is useful for checking configuration settings before or after modifying them.
```python
import gmsh
gmsh.initialize()
# Check current mesh algorithm
algo = gmsh.option.getNumber("Mesh.Algorithm")
print(f"Current mesh algorithm: {algo}")
gmsh.finalize()
```
--------------------------------
### Get Gmsh Model Entities by Dimension
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves all entities within the current Gmsh model. Optionally filters entities based on their dimension (e.g., points, curves, surfaces, volumes).
```python
import gmsh
gmsh.initialize()
gmsh.model.add("test")
# ... create geometry ...
gmsh.model.geo.synchronize()
# Get all entities
all_entities = gmsh.model.getEntities()
# Get only surfaces (dim=2)
surfaces = gmsh.model.getEntities(dim=2)
# Get only volumes (dim=3)
volumes = gmsh.model.getEntities(dim=3)
gmsh.finalize()
```
--------------------------------
### Post-Build Java Compilation and JAR Creation (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/wrappers/java/CMakeLists.txt
This snippet defines post-build commands in CMake to compile and package Java code after the SWIG module is built. It includes commands to compile generated Java files, utility files, and demonstration files, then creates JAR archives for the Gmsh wrapper and sample applications. It also copies necessary resources like a `.geo` file and `build.xml`.
```cmake
# define a good name for the dynamic libraty which depends on the OS
if(WIN32)
set_target_properties(WrapGmsh PROPERTIES PREFIX "")
endif(WIN32)
if(UNIX)
set_target_properties(WrapGmsh PROPERTIES PREFIX "lib")
endif(UNIX)
add_custom_command(TARGET WrapGmsh
POST_BUILD # do the rest of the command after the build period
# write on the output consol
COMMAND cmake -E echo "Compiling Java files..."
#compilation of Java files generated by SWIG
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used by wrapping
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used as demonstration
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/com/artenum/sample/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# create a jar file which contains the files with a .class extension
COMMAND cmake -E echo "Creating jar file..."
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ org
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/Sample.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ com
# create a jar from Sample
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/t5.geo
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# copy the build.xml file to run the ant script
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/build.xml
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/)
```
--------------------------------
### Get Mesh Nodes using gmsh.model.mesh.getNodes()
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves the nodes of the mesh. Nodes can be filtered by dimension or entity tag. Returns node tags, coordinates, and parameters. Node coordinates are returned as a flattened list.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("get_nodes")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Get all mesh nodes
nodeTags, nodeCoords, nodeParams = gmsh.model.mesh.getNodes()
print(f"Number of nodes: {len(nodeTags)}")
# nodeCoords is flattened: [x1, y1, z1, x2, y2, z2, ...]
for i in range(min(5, len(nodeTags))):
x, y, z = nodeCoords[3*i:3*i+3]
print(f"Node {nodeTags[i]}: ({x}, {y}, {z})")
gmsh.finalize()
```
--------------------------------
### Configure Voro++ Integration in CMake
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake snippet configures the Voro++ library for Voronoi diagrams. It prioritizes a system-wide installation if enabled, otherwise it adds the 'voro++' subdirectory from contrib, setting appropriate include directories.
```cmake
if(ENABLE_VOROPP)
find_library(VOROPP_LIB voro++ PATH_SUFFIXES lib)
find_path(VOROPP_INC "voro++.hh" PATH_SUFFIXES include)
if(ENABLE_SYSTEM_CONTRIB AND VOROPP_LIB AND VOROPP_INC)
message(STATUS "Using system version of voro++")
list(APPEND EXTERNAL_LIBRARIES ${VOROPP_LIB})
list(APPEND EXTERNAL_INCLUDES ${VOROPP_INC})
set_config_option(HAVE_VOROPP "Voro++[system]")
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/voro++)
add_subdirectory(contrib/voro++)
include_directories(contrib/voro++/src)
set_config_option(HAVE_VOROPP "Voro++")
endif()
endif()
```
--------------------------------
### Project Configuration and Build Instructions (CMake)
Source: https://github.com/sasobadovinac/gmsh/blob/master/contrib/mobile/CMakeLists.txt
This snippet provides informational messages to the user after the CMake configuration is complete. It outlines the subsequent steps required to build the Android project, specifically instructing the user to run 'make androidProject' and then use Gradle's 'gradle assembleRelease' command to finalize the application build.
```cmake
message(STATUS "")
message(STATUS "ONELAB for Android successfully configured:")
message(STATUS " * Run 'make androidProject' to create the project")
message(STATUS " * Finally build the app with gradle 'gradle assembleRelease'")
message(STATUS "")
```
--------------------------------
### Run STL to Gmsh Converter Application
Source: https://github.com/sasobadovinac/gmsh/blob/master/utils/converters/stl_to_cartesian/README.txt
Executes the compiled 'stlToGmsh' application. It takes an input STL file and three integers (nx, ny, nz) representing grid dimensions as arguments.
```bash
./stlToGmsh
```
--------------------------------
### Get Mesh Elements using gmsh.model.mesh.getElements()
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Retrieves the elements of the mesh, optionally filtered by entity. Returns element types, element tags, and the node tags associated with each element. It can also retrieve element properties.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("get_elements")
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Get all mesh elements
elementTypes, elementTags, nodeTags = gmsh.model.mesh.getElements()
for i, elemType in enumerate(elementTypes):
name, dim, order, numNodes, localCoords, numPrimNodes = \
gmsh.model.mesh.getElementProperties(elemType)
print(f"Element type: {name}, count: {len(elementTags[i])}")
gmsh.finalize()
```
--------------------------------
### Create Cone using Gmsh OpenCASCADE Kernel
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Shows how to create a cone with specified base and top radii using `gmsh.model.occ.addCone`. It initializes Gmsh, adds a cone, synchronizes, generates a 3D mesh, and writes the mesh file. Requires Gmsh library.
```python
import gmsh
gmsh.initialize()
gmsh.model.add("cone")
# Create a cone with base radius 1, top radius 0.2, height 2
cone = gmsh.model.occ.addCone(0, 0, 0, 0, 0, 2, 1, 0.2, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
gmsh.write("cone.msh")
gmsh.finalize()
```
--------------------------------
### Configure Metis Integration in CMake
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake snippet configures the Metis library for partitioning meshes. It prioritizes a system-wide installation if enabled, otherwise it adds the 'metis' subdirectory from contrib, setting appropriate include directories and definitions.
```cmake
if(HAVE_MESH OR HAVE_SOLVER)
if(ENABLE_METIS)
find_library(METIS_LIB metis PATH_SUFFIXES lib)
find_path(METIS_INC "metis.h" PATH_SUFFIXES include)
if(ENABLE_SYSTEM_CONTRIB AND METIS_LIB AND METIS_INC)
message(STATUS "Using system version of METIS")
list(APPEND EXTERNAL_LIBRARIES ${METIS_LIB})
list(APPEND EXTERNAL_INCLUDES ${METIS_INC})
set_config_option(HAVE_METIS "Metis[system]")
elseif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/metis)
add_definitions(-DUSE_GKREGEX)
add_subdirectory(contrib/metis)
include_directories(contrib/metis/include contrib/metis/libmetis
contrib/metis/GKlib)
set_config_option(HAVE_METIS "Metis")
endif()
endif()
endif()
```
--------------------------------
### Initialize and Finalize Gmsh API
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Initializes the Gmsh API before use and finalizes it to release resources. These functions are mandatory for any Gmsh API interaction.
```python
import gmsh
# Initialize Gmsh - required before any other calls
gmsh.initialize()
# Enable terminal output for messages
gmsh.option.setNumber("General.Terminal", 1)
# Your meshing code here...
# Always finalize when done
gmsh.finalize()
```
```python
import gmsh
gmsh.initialize()
gmsh.model.add("example")
# ... perform operations ...
gmsh.finalize() # Clean up resources
```
--------------------------------
### Core Functions
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
This section details the essential functions for initializing, finalizing, opening, merging, writing, and clearing models within the Gmsh API.
```APIDOC
## Gmsh API Core Functions
### Description
Provides fundamental functions for managing the Gmsh API session, including initialization, finalization, file operations, and model clearing.
### `gmsh.initialize()`
Initializes the Gmsh API. This function must be called before any other API functions.
### `gmsh.finalize()`
Finalizes the Gmsh API, releasing all resources. This function should be called when Gmsh operations are complete.
### `gmsh.open(fileName)`
Opens a specified file (geometry, mesh, or post-processing data) and creates a new model based on its content.
- **fileName** (string) - The path to the file to open.
### `gmsh.merge(fileName)`
Merges the content of a specified file into the current model without creating a new model. This is useful for combining multiple geometry or mesh files.
- **fileName** (string) - The path to the file to merge.
### `gmsh.write(fileName)`
Writes the current model to a file. The file format is determined by the file extension.
- **fileName** (string) - The path and name for the output file.
### `gmsh.clear()`
Clears all loaded models and post-processing data from memory, freeing up resources and allowing for a fresh start.
### Request Example (Initialization and Finalization)
```python
import gmsh
gmsh.initialize()
# Perform Gmsh operations...
gmsh.finalize()
```
### Request Example (Opening a file)
```python
import gmsh
gmsh.initialize()
gmsh.open("model.geo")
gmsh.finalize()
```
### Request Example (Writing a file)
```python
import gmsh
gmsh.initialize()
# ... create geometry and mesh ...
gmsh.model.mesh.generate(2)
gmsh.write("output.msh")
gmsh.finalize()
```
```
--------------------------------
### Create View
Source: https://context7.com/sasobadovinac/gmsh/llms.txt
Creates a new post-processing view for visualization purposes.
```APIDOC
## gmsh.view.add(name, tag=-1)
### Description
Creates a new post-processing view.
### Method
`gmsh.view.add(name, tag)`
### Endpoint
`/view/add`
### Parameters
#### Path Parameters
- **name** (string) - Required - The name to assign to the new view.
- **tag** (integer) - Optional - A specific tag to assign to the view. If -1, Gmsh assigns a tag automatically.
### Request Example
```python
import gmsh
gmsh.initialize()
gmsh.model.add("view_example")
# Create geometry and mesh
gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1)
gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(3)
# Create a view for visualization
view_tag = gmsh.view.add("Temperature Field")
gmsh.finalize()
```
### Response
#### Success Response (200)
- **tag** (integer) - The tag of the newly created view.
#### Response Example
```json
{
"tag": 1
}
```
```
--------------------------------
### Configure Kbipack and GMP Integration in CMake
Source: https://github.com/sasobadovinac/gmsh/blob/master/CMakeLists.txt
This CMake code configures the 'kbipack' module and optionally integrates the GMP library. It finds GMP's library and header files, setting configuration options and external library/include paths if found.
```cmake
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/kbipack AND ENABLE_KBIPACK)
set_config_option(HAVE_KBIPACK "Kbipack")
add_subdirectory(contrib/kbipack)
include_directories(contrib/kbipack)
if(ENABLE_GMP)
find_library(GMP_LIB gmp)
find_path(GMP_INC "gmp.h" PATH_SUFFIXES src include)
endif()
if(GMP_LIB AND GMP_INC)
set_config_option(HAVE_GMP "GMP")
list(APPEND EXTERNAL_LIBRARIES ${GMP_LIB})
list(APPEND EXTERNAL_INCLUDES ${GMP_INC})
else()
message(STATUS "GMP not found: Kbipack uses long int")
endif()
endif()
```