Generate type stubs and IntelliSense support for Siemens Femap Python API development.
Based on: https://github.com/vsdsantos/PyFemap
Writing Python scripts for Femap's COM API is challenging because:
- No IntelliSense - The standard
win32com.client.makepyoutput (Pyfemap.py) loses all type information, making method signatures opaque - No Autocomplete - IDEs can't suggest available methods, properties, or constants
- Magic Numbers Everywhere - Femap uses hundreds of enum constants (e.g.,
FT_NODE = 7) that are hard to remember - 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 signaturesfemap_constants.py- Type-safeIntEnumclasses for all Femap constants with readable aliasesPyfemap.py- Standard COM wrapper (regenerated from your Femap version)
- Full IntelliSense - Autocomplete for all 400+ Femap interfaces and 10,000+ methods
- Type-Safe Constants -
ReturnCode.OKinstead of0,Entity.NODEinstead of7 - 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
- 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.,
ReturnCodeinstead ofzReturnCode,Entityinstead ofzDataType) were manually chosen to be more readable and intuitive. The original Femap enum names and prefixes can be cryptic.
- Python 3.8+ (with tkinter for file dialogs)
- Siemens Femap installed (for
femap.tlbtype library) pywin32(pip install pywin32)
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.
Simply copy Pyfemap.py, Pyfemap.pyi, and femap_constants.py to your project.
Run these scripts if you need to regenerate files for a different Femap version.
Generate the COM wrapper from your Femap type library:
python generate_Pyfemap.pyGenerate type-safe enum constants for readable code:
python generate_constants_tlb.pyOptions:
python generate_constants_tlb.py --list-enums # List all available enums
python generate_constants_tlb.py --output femap_constants.py # default: current directoryGenerate the .pyi stub file for IDE IntelliSense:
python generate_stubs_tlb.pyOptions:
python generate_stubs_tlb.py --tlb "C:\Program Files\Siemens\Femap 2024\femap.tlb"
python generate_stubs_tlb.py --output Pyfemap.pyi # default: current directoryAll scripts automatically find femap.tlb using this order:
- Command-line argument:
--tlb "path/to/femap.tlb" - Environment variable:
FEMAP_TLB_PATH - Cached path: Stored in
%TEMP%\.femap_tlb_cachefrom previous user selection - Auto-detect: Searches
C:\Program Files\Siemens\Femap * - File dialog: Prompts you to select the file (saves to cache)
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?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| 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 |
- Copy
Pyfemap.pyito your project or add topython.analysis.extraPaths - Import
femap_constantsfor type-safe constants
- Mark the folder containing
Pyfemap.pyias a Sources Root - PyCharm automatically picks up
.pyistubs
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.
See LICENSE for details.