[S0045] Get the number of mesh elements and nodes

The automatic mesh generation function automatically creates a mesh based on analysis and mesh generation settings.
Alternatively, a mesh can be created manually by using the JMAG-Designer Geometry Editor with specifying the number of divisions and element sizes.
This script retrieves the number of nodes and elements for a mesh created in a study.

Preconditions

  • One or more studies have been created.
    This script runs on the active study in the project tree.

Script Function

  • If a mesh has not yet been created for the specified study and case, it executes automatic mesh generation.
  • Display the number of nodes and elements of the generated mesh.
# Copyright (c) 2026 JSOL CORPORATION
#
# This script is released under the MIT License.
# See the full license text at:
# https://www.jmag-international.com/scriptlibrary/jmag_script_library_mit/


def generateMesh(study, caseNo):
    """If a mesh has not yet been generated for the case of the study specified in the argument, it will be generated"""
    # The argument for SetCurrentCase is a zero-based index, so it should be case number - 1.
    caseIndex = caseNo - 1
    study.SetCurrentCase(caseIndex)
    if not study.HasMesh():
        study.CreateMesh()


def checkNumNodesAndElems(study, caseNo):
    """Get and display the number of nodes and elements for the case of the study specified in the argument."""
    caseIndex = caseNo - 1
    study.SetCurrentCase(caseIndex)
    if study.HasMesh():
        report = study.GetReport()
        num_nodes = report.NumVertices()
        num_elements = report.NumElements()
        
        print(f"Nodes of Case{caseNo}: {num_nodes}")
        print(f"Elements of Case{caseNo}: {num_elements}")
    else:
        print(u"No mesh generated for the current case")

app = designer.GetApplication()
targetCase = 2

study = app.GetCurrentStudy()
if targetCase > study.GetDesignTable().NumCases():
    print(u"The case", targetCase, u"doesn't exist in this study.")
else:
    generateMesh(study, targetCase)
    checkNumNodesAndElems(study, targetCase)

Download Python source code

How to use script file

Use the JMAG Script Library after reading and agreeing to the following terms of use.

Search Filter
  • All Categories

An engineer's diary
JMAG-Express Online