Skip to content

PaulGronwald/Femap-Python-API-Linting

Repository files navigation

Femap Python Linting & Type Stubs Generator

Generate type stubs and IntelliSense support for Siemens Femap Python API development.

Based on: https://github.com/vsdsantos/PyFemap

Why This Project?

Writing Python scripts for Femap's COM API is challenging because:

  1. No IntelliSense - The standard win32com.client.makepy output (Pyfemap.py) loses all type information, making method signatures opaque
  2. No Autocomplete - IDEs can't suggest available methods, properties, or constants
  3. Magic Numbers Everywhere - Femap uses hundreds of enum constants (e.g., FT_NODE = 7) that are hard to remember
  4. No Error Detection - Typos in method names or wrong parameter types aren't caught until runtime

This repository generates:

  • Pyfemap.pyi - Type stub file with accurate interface types (INode, IMatl, IElement) and method signatures
  • femap_constants.py - Type-safe IntEnum classes for all Femap constants with readable aliases
  • Pyfemap.py - Standard COM wrapper (regenerated from your Femap version)

Features

  • Full IntelliSense - Autocomplete for all 400+ Femap interfaces and 10,000+ methods
  • Type-Safe Constants - ReturnCode.OK instead of 0, Entity.NODE instead of 7
  • Accurate Signatures - Method parameters with proper types, not just Any
  • Version Hints - Deprecation warnings when newer API methods exist (e.g., GetNodeList2 -> GetNodeList3)
  • Auto-Detection - Automatically finds your Femap installation
  • Multi-Version Support - Works with different Femap versions

Notes

  • The stub generation is not 100% deterministic - some type mappings may be imperfect. If you find issues, please report them.
  • The constant aliases (e.g., ReturnCode instead of zReturnCode, Entity instead of zDataType) were manually chosen to be more readable and intuitive. The original Femap enum names and prefixes can be cryptic.

Requirements

  • Python 3.8+ (with tkinter for file dialogs)
  • Siemens Femap installed (for femap.tlb type library)
  • pywin32 (pip install pywin32)

Installation

Option 1: pip install (recommended)

Install as a Python package using the included pyproject.toml:

cd Femap-Linting
pip install . 

This makes Pyfemap, femap_constants, and femap_path_utils importable from anywhere in your Python environment.

Option 2: Direct Use

Simply copy Pyfemap.py, Pyfemap.pyi, and femap_constants.py to your project.

Usage (Regenerating Files)

Run these scripts if you need to regenerate files for a different Femap version.

1. Generate Pyfemap.py (required)

Generate the COM wrapper from your Femap type library:

python generate_Pyfemap.py

2. Generate Constants Module (optional)

Generate type-safe enum constants for readable code:

python generate_constants_tlb.py

Options:

python generate_constants_tlb.py --list-enums  # List all available enums
python generate_constants_tlb.py --output femap_constants.py  # default: current directory

3. Generate Type Stubs

Generate the .pyi stub file for IDE IntelliSense:

python generate_stubs_tlb.py

Options:

python generate_stubs_tlb.py --tlb "C:\Program Files\Siemens\Femap 2024\femap.tlb"
python generate_stubs_tlb.py --output Pyfemap.pyi  # default: current directory

TLB Path Resolution

All scripts automatically find femap.tlb using this order:

  1. Command-line argument: --tlb "path/to/femap.tlb"
  2. Environment variable: FEMAP_TLB_PATH
  3. Cached path: Stored in %TEMP%\.femap_tlb_cache from previous user selection
  4. Auto-detect: Searches C:\Program Files\Siemens\Femap *
  5. File dialog: Prompts you to select the file (saves to cache)

Example: Before & After

Without Type Stubs (standard Pyfemap.py)

import Pyfemap

app = Pyfemap.model  # No autocomplete, no type info
node = app.feNode    # What methods are available?

# Magic numbers everywhere
rc = node.Get(7)     # What does 7 mean?
node.color = 15      # Is 15 a valid color?

With Type Stubs & Constants

import Pyfemap
from femap_constants import ReturnCode, Entity, Color

app: Pyfemap.model
node: Pyfemap.INode = app.feNode  # Full autocomplete

# Type-safe, readable constants
rc = node.Get(Entity.NODE)        # Self-documenting
if rc != ReturnCode.OK:
    print("Failed to get node")

node.color = Color.RED            # Autocomplete for colors

Generated Files

File Purpose
Pyfemap.py COM wrapper generated by makepy (required at runtime)
Pyfemap.pyi Type stub for IDE IntelliSense (not needed at runtime)
femap_constants.py Type-safe IntEnum classes for all constants

IDE Setup

VS Code

  1. Copy Pyfemap.pyi to your project or add to python.analysis.extraPaths
  2. Import femap_constants for type-safe constants

PyCharm

  1. Mark the folder containing Pyfemap.pyi as a Sources Root
  2. PyCharm automatically picks up .pyi stubs

Supported Enums

The constants generator creates readable aliases for common enums:

Enum Alias Example
zReturnCode ReturnCode ReturnCode.OK, ReturnCode.FAIL
zDataType Entity Entity.NODE, Entity.ELEMENT
zElementType ElemType ElemType.LINE2, ElemType.QUAD4
zColor Color Color.RED, Color.BLUE
zAnalysisType Analysis Analysis.STATIC, Analysis.MODAL

Run python generate_constants_tlb.py --list-enums to see all available enums.

License

See LICENSE for details.

About

Python stubs and typeinformation for FEMAP-API

Topics

Resources

License

Stars

Watchers

Forks

Languages