Major Enhancement: - Implemented .exp file-based expression updates via NX journal scripts - Fixes critical issue with feature-linked expressions (e.g., hole_count) - Supports ALL NX expression types including binary-stored ones - Full 4D design space validation completed successfully New Components: 1. import_expressions.py - NX journal for .exp file import - Uses NXOpen.ExpressionCollection.ImportFromFile() - Replace mode overwrites existing values - Automatic model update and save - Comprehensive error handling 2. export_expressions.py - NX journal for .exp file export - Exports all expressions to text format - Used for unit detection and verification 3. Enhanced nx_updater.py - New update_expressions_via_import() method - Automatic unit detection from .exp export - Creates study-variable-only .exp files - Replaces fragile binary .prt editing Technical Details: - .exp Format: [Units]name=value (e.g., [MilliMeter]beam_length=5000) - Unitless expressions: name=value (e.g., hole_count=10) - Robustness: Native NX functionality, no regex failures - Performance: < 1 second per update operation Validation: - Simple Beam Optimization study (4D design space) * beam_half_core_thickness: 10-40 mm * beam_face_thickness: 10-40 mm * holes_diameter: 150-450 mm * hole_count: 5-15 (integer) Results: ✅ 3-trial validation completed successfully ✅ All 4 variables update correctly in all trials ✅ Mesh adaptation verified (hole_count: 6, 15, 11 → different mesh sizes) ✅ Trial 0: 5373 CQUAD4 elements (6 holes) ✅ Trial 1: 5158 CQUAD4 + 1 CTRIA3 (15 holes) ✅ Trial 2: 5318 CQUAD4 (11 holes) Problem Solved: - hole_count expression was not updating with binary .prt editing - Expression stored in feature parameter, not accessible via text regex - Binary format prevented reliable text-based updates Solution: - Use NX native expression import/export - Works for ALL expressions (text and binary-stored) - Automatic unit handling - Model update integrated in journal Documentation: - New: docs/NX_EXPRESSION_IMPORT_SYSTEM.md (comprehensive guide) - Updated: CHANGELOG.md with Phase 3.2 progress - Study: studies/simple_beam_optimization/ (complete example) Files Added: - optimization_engine/import_expressions.py - optimization_engine/export_expressions.py - docs/NX_EXPRESSION_IMPORT_SYSTEM.md - studies/simple_beam_optimization/ (full study) Files Modified: - optimization_engine/nx_updater.py - CHANGELOG.md Compatibility: - NX 2412 tested and verified - Python 3.10+ - Works with all NX expression types 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
"""
|
|
NX Journal: Import expressions from .exp file
|
|
|
|
Usage: run_journal.exe import_expressions.py -args <prt_file> <exp_file>
|
|
"""
|
|
import sys
|
|
import NXOpen
|
|
|
|
|
|
def main(args):
|
|
if len(args) < 2:
|
|
print("[ERROR] Usage: import_expressions.py <prt_file> <exp_file>")
|
|
sys.exit(1)
|
|
|
|
prt_file = args[0]
|
|
exp_file = args[1]
|
|
|
|
theSession = NXOpen.Session.GetSession()
|
|
|
|
# Open the part file
|
|
partLoadStatus1 = None
|
|
try:
|
|
workPart, partLoadStatus1 = theSession.Parts.OpenActiveDisplay(
|
|
prt_file,
|
|
NXOpen.DisplayPartOption.AllowAdditional
|
|
)
|
|
finally:
|
|
if partLoadStatus1:
|
|
partLoadStatus1.Dispose()
|
|
|
|
print(f"[JOURNAL] Opened part: {prt_file}")
|
|
|
|
# Import expressions from .exp file
|
|
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Import Expressions")
|
|
|
|
try:
|
|
expModified, errorMessages = workPart.Expressions.ImportFromFile(
|
|
exp_file,
|
|
NXOpen.ExpressionCollection.ImportMode.Replace
|
|
)
|
|
|
|
print(f"[JOURNAL] Imported expressions from: {exp_file}")
|
|
|
|
# expModified can be either a bool or an array depending on NX version
|
|
if isinstance(expModified, bool):
|
|
print(f"[JOURNAL] Import completed: {expModified}")
|
|
else:
|
|
print(f"[JOURNAL] Expressions modified: {len(expModified)}")
|
|
|
|
if errorMessages:
|
|
print(f"[JOURNAL] Import errors: {errorMessages}")
|
|
|
|
# Update the part to apply expression changes
|
|
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "NX update")
|
|
nErrs = theSession.UpdateManager.DoUpdate(markId2)
|
|
theSession.DeleteUndoMark(markId2, "NX update")
|
|
|
|
print(f"[JOURNAL] Part updated (errors: {nErrs})")
|
|
|
|
# Save the part
|
|
partSaveStatus = workPart.Save(
|
|
NXOpen.BasePart.SaveComponents.TrueValue,
|
|
NXOpen.BasePart.CloseAfterSave.FalseValue
|
|
)
|
|
partSaveStatus.Dispose()
|
|
|
|
print(f"[JOURNAL] Part saved: {prt_file}")
|
|
|
|
except Exception as e:
|
|
print(f"[ERROR] Failed to import expressions: {e}")
|
|
sys.exit(1)
|
|
|
|
print("[JOURNAL] Expression import complete!")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv[1:])
|