""" Extract mass from NX measure expression in Bracket.prt This script reads the mass value directly from the model's measure expression, bypassing the need for GRDPNT output in the OP2 file. """ import sys from pathlib import Path import NXOpen def extract_mass_from_prt(prt_file: Path) -> float: """ Extract mass from NX .prt file measure expression. Args: prt_file: Path to .prt file with mass measure expression Returns: Mass in kg """ theSession = NXOpen.Session.GetSession() # Open the part file print("[1/2] Opening part file: " + str(prt_file)) try: basePart, partLoadStatus = theSession.Parts.OpenBaseDisplay(str(prt_file)) partLoadStatus.Dispose() except Exception as e: raise RuntimeError("Failed to open part file: " + str(e)) # Get all expressions print("[2/2] Reading expressions...") expressions = basePart.Expressions # Search for mass expression (common names: "mass", "bracket_mass", "total_mass", etc.) mass_value = None mass_expr_name = None for expr in expressions: expr_name = expr.Name.lower() if 'mass' in expr_name: # Found a mass expression mass_expr_name = expr.Name mass_value = expr.Value print(" Found mass expression: '" + expr.Name + "' = " + str(mass_value)) break if mass_value is None: # List all expressions to help debug print("\n Available expressions:") for expr in expressions: print(" - " + expr.Name + " = " + str(expr.Value)) raise ValueError("No mass expression found in part file") # Close the part theSession.Parts.CloseAll(NXOpen.BasePart.CloseWholeTree.False, None) print("\n[OK] Mass extracted: {:.6f} kg".format(mass_value)) return mass_value if __name__ == "__main__": if len(sys.argv) > 1: prt_file = Path(sys.argv[1]) else: # Default to Bracket.prt in same directory prt_file = Path(__file__).parent / "Bracket.prt" if not prt_file.exists(): print(f"ERROR: Part file not found: {prt_file}") sys.exit(1) try: mass_kg = extract_mass_from_prt(prt_file) print(f"\nMass: {mass_kg:.6f} kg ({mass_kg * 1000:.2f} g)") except Exception as e: print(f"\nERROR: {e}") import traceback traceback.print_exc() sys.exit(1)