diff --git a/tools/adaptive-isogrid/src/nx/import_profile.py b/tools/adaptive-isogrid/src/nx/import_profile.py index 2a22ad5f..e8cdd91a 100644 --- a/tools/adaptive-isogrid/src/nx/import_profile.py +++ b/tools/adaptive-isogrid/src/nx/import_profile.py @@ -107,10 +107,8 @@ def _replace_and_delete_old_sketch(part: Any, old_feature: Any, new_feature: Any "Replace isogrid sketch") try: - # Create the ReplaceFeatureBuilder - builder = part.Features.CreateReplaceFeatureBuilder( - NXOpen.Features.Feature.Null - ) + # Create the ReplaceFeatureBuilder (takes no args) + builder = part.Features.CreateReplaceFeatureBuilder() # Set the original feature (old sketch) to be replaced added_old = builder.SelectFeature.Add(old_feature) @@ -459,10 +457,11 @@ def _delete_feature(session: Any, feature: Any, lister: Any, label: str = "featu def _extrude_sketch(part: Any, sketch: Any, thickness: float, normal: List[float], extrude_name: str, lister: Any): """ - Extrude all curves in the sketch by `thickness` along the face normal. - Creates a new Extrude feature named `extrude_name`. - - If an old extrude with the same name exists, delete it first. + Extrude a sketch using the standard NXOpen pattern: + - Create ExtrudeBuilder with null feature + - Build a Section from the sketch feature rule + - Set direction + limits + - Commit """ import NXOpen import NXOpen.Features @@ -470,149 +469,64 @@ def _extrude_sketch(part: Any, sketch: Any, thickness: float, session = NXOpen.Session.GetSession() - # Delete old extrude if it exists old_extrude = _find_extrude_feature(part, extrude_name, lister) if old_extrude is not None: _delete_feature(session, old_extrude, lister, f"old extrude {extrude_name}") lister.WriteLine(f"[import] Extruding sketch by {thickness} mm along normal") - mark = session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, - f"Extrude {extrude_name}") + session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, + f"Extrude {extrude_name}") + builder = None + rule_options = None try: - # Create ExtrudeBuilder (null feature = new extrude) - builder = part.Features.CreateExtrudeBuilder( - NXOpen.Features.Feature.Null + builder = part.Features.CreateExtrudeBuilder(NXOpen.Features.Feature.Null) + + # Build section directly from the sketch feature + section = builder.Section + section.AllowSelfIntersection(True) + + rule_options = part.ScRuleFactory.CreateRuleOptions() + curve_rule = part.ScRuleFactory.CreateRuleCurveFeature( + [sketch.Feature], + NXOpen.DisplayableObject.Null, + rule_options, ) - # Set the section: use sketch curves - # Get all curves from the sketch - sketch_feature = sketch.Feature - curves = [] - try: - # Try getting sketch geometry objects - geom = sketch.GetAllGeometry() - for obj in geom: - curves.append(obj) - except Exception: - pass - - if not curves: - # Fallback: get curves from feature's entities - try: - entities = sketch_feature.GetEntities() - for ent in entities: - if hasattr(ent, 'StartPoint') or hasattr(ent, 'GetLength'): - curves.append(ent) - except Exception: - pass - - lister.WriteLine(f"[import] Found {len(curves)} curves in sketch for extrude") - - # Create section and add curves - section = part.Sections.CreateSection( - 0.0095, # chaining tolerance - 0.01, # distance tolerance - 0.5, # angle tolerance - ) - section.SetAllowedEntityTypes( - NXOpen.Section.AllowTypes.OnlyCurves + help_pt = NXOpen.Point3d(0.0, 0.0, 0.0) + section.AddToSection( + [curve_rule], + NXOpen.NXObject.Null, + NXOpen.NXObject.Null, + NXOpen.NXObject.Null, + help_pt, + NXOpen.Section.Mode.Create, + False, ) - # Add sketch curves to the section - sketch_feature_tag = sketch.Feature - curves_added = False - - # Method 1: Add the sketch feature directly as section input - try: - feature_rule = part.ScRuleFactory.CreateRuleFeatureCurves( - [sketch_feature_tag] - ) - section.AllowSelfIntersection(True) - help_pt = NXOpen.Point3d(0, 0, 0) - section.AddToSection( - [feature_rule], - NXOpen.NXObject.Null, # seed - NXOpen.NXObject.Null, # start connector - NXOpen.NXObject.Null, # end connector - help_pt, - NXOpen.Section.Mode.Create, - False, # go to end - ) - curves_added = True - lister.WriteLine("[import] Added sketch curves to section via FeatureCurves rule") - except Exception as exc: - lister.WriteLine(f"[import] FeatureCurves rule failed: {exc}") - - if not curves_added and curves: - # Method 2: Add individual curves - try: - curve_rule = part.ScRuleFactory.CreateRuleCurveDumb(curves) - help_pt = NXOpen.Point3d(0, 0, 0) - section.AddToSection( - [curve_rule], - curves[0], - NXOpen.NXObject.Null, - NXOpen.NXObject.Null, - help_pt, - NXOpen.Section.Mode.Create, - False, - ) - curves_added = True - lister.WriteLine("[import] Added curves via CurveDumb rule") - except Exception as exc: - lister.WriteLine(f"[import] CurveDumb rule failed: {exc}") - - if not curves_added: - lister.WriteLine("[import] ERROR: Could not add curves to extrude section") - builder.Destroy() - return None - - builder.Section = section - - # Set direction (along face normal) direction = part.Directions.CreateDirection( - NXOpen.Point3d(0, 0, 0), - NXOpen.Vector3d(normal[0], normal[1], normal[2]), + NXOpen.Point3d(0.0, 0.0, 0.0), + NXOpen.Vector3d(float(normal[0]), float(normal[1]), float(normal[2])), NXOpen.SmartObject.UpdateOption.WithinModeling, ) builder.Direction = direction - # Set limits: start=0, end=thickness - builder.Limits.StartExtend.Value.RightHandSide = "0" - builder.Limits.EndExtend.Value.RightHandSide = str(thickness) - - # Boolean: create (no unite/subtract for now — idealized model) + builder.Limits.StartExtend.Value.RightHandSide = "0.0" + builder.Limits.EndExtend.Value.RightHandSide = str(float(thickness)) builder.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create - # Commit extrude_obj = builder.Commit() - builder.Destroy() - - # Rename the extrude feature - committed = builder.GetCommittedObjects() if hasattr(builder, 'GetCommittedObjects') else [] - extrude_feature = None - - if isinstance(extrude_obj, NXOpen.Features.Feature): - extrude_feature = extrude_obj - else: - # Find the most recently created extrude feature - for feat in part.Features: - try: - if "Extrude" in feat.FeatureType and feat.Name == "": - extrude_feature = feat - except Exception: - continue + extrude_feature = extrude_obj if isinstance(extrude_obj, NXOpen.Features.Feature) else None if extrude_feature is not None: try: extrude_feature.SetName(extrude_name) lister.WriteLine(f"[import] Extrude created and named: {extrude_name}") except Exception: - lister.WriteLine(f"[import] Extrude created (could not rename)") + lister.WriteLine("[import] Extrude created (could not rename)") else: - lister.WriteLine(f"[import] Extrude committed (feature reference unclear)") + lister.WriteLine("[import] Extrude committed (feature reference unclear)") return extrude_feature @@ -621,6 +535,17 @@ def _extrude_sketch(part: Any, sketch: Any, thickness: float, import traceback lister.WriteLine(traceback.format_exc()) return None + finally: + try: + if rule_options is not None: + rule_options.Dispose() + except Exception: + pass + try: + if builder is not None: + builder.Destroy() + except Exception: + pass # ---------------------------------------------------------------------------