56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
|
|
"""
|
||
|
|
Extract expression value from NX .prt file
|
||
|
|
Used for extracting computed values like mass, volume, etc.
|
||
|
|
|
||
|
|
This extractor reads expressions using the .exp export method for accuracy.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Dict, Any
|
||
|
|
from optimization_engine.nx_updater import NXParameterUpdater
|
||
|
|
|
||
|
|
|
||
|
|
def extract_expression(prt_file: Path, expression_name: str):
|
||
|
|
"""
|
||
|
|
Extract an expression value from NX .prt file.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
prt_file: Path to .prt file
|
||
|
|
expression_name: Name of expression to extract (e.g., 'p173' for mass)
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Dict with expression value and units
|
||
|
|
"""
|
||
|
|
updater = NXParameterUpdater(prt_file, backup=False)
|
||
|
|
expressions = updater.get_all_expressions(use_exp_export=True)
|
||
|
|
|
||
|
|
if expression_name not in expressions:
|
||
|
|
raise ValueError(f"Expression '{expression_name}' not found in {prt_file}")
|
||
|
|
|
||
|
|
expr_info = expressions[expression_name]
|
||
|
|
|
||
|
|
# If expression is a formula (value is None), we need to evaluate it
|
||
|
|
# For now, we'll raise an error if it's a formula - user should use the computed value
|
||
|
|
if expr_info['value'] is None and expr_info['formula'] is not None:
|
||
|
|
raise ValueError(
|
||
|
|
f"Expression '{expression_name}' is a formula: {expr_info['formula']}. "
|
||
|
|
f"This extractor requires a computed value, not a formula reference."
|
||
|
|
)
|
||
|
|
|
||
|
|
return {
|
||
|
|
expression_name: expr_info['value'],
|
||
|
|
f'{expression_name}_units': expr_info['units']
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
# Example usage
|
||
|
|
import sys
|
||
|
|
if len(sys.argv) > 2:
|
||
|
|
prt_file = Path(sys.argv[1])
|
||
|
|
expression_name = sys.argv[2]
|
||
|
|
result = extract_expression(prt_file, expression_name)
|
||
|
|
print(f"Extraction result: {result}")
|
||
|
|
else:
|
||
|
|
print(f"Usage: python {sys.argv[0]} <prt_file> <expression_name>")
|