Importer: rename sketch feature + replace/delete old sketch on update

This commit is contained in:
2026-02-16 22:00:18 +00:00
parent 1bfc747cf9
commit c4d98ee97c

View File

@@ -75,6 +75,59 @@ def unproject_point_to_3d(
# NX sketch creation
# ---------------------------------------------------------------------------
def _find_old_sketch_feature(part: Any, sketch_name: str, lister: Any):
"""
Find an existing sketch feature by Name attribute.
Returns the Feature object (not Sketch), or None.
"""
try:
for feat in part.Features:
try:
if feat.Name == sketch_name:
lister.WriteLine(f"[import] Found existing sketch feature: {sketch_name} ({feat.JournalIdentifier})")
return feat
except Exception:
continue
except Exception:
pass
return None
def _replace_and_delete_old_sketch(part: Any, old_feature: Any, new_feature: Any, lister: Any):
"""
Replace all references from old sketch feature to new one, then delete old.
Uses NX Edit > Feature > Replace Feature approach.
"""
import NXOpen
session = NXOpen.Session.GetSession()
# Try to replace references from old → new
try:
# Get all parent features that reference the old sketch (e.g. Extrude)
parents = old_feature.GetParents()
if parents:
lister.WriteLine(f"[import] Old sketch has {len(parents)} parent feature(s) — replacing references")
for parent in parents:
try:
parent.ReplaceEntity(old_feature, new_feature)
lister.WriteLine(f"[import] Replaced reference in {parent.JournalIdentifier}")
except Exception as exc:
lister.WriteLine(f"[import] Could not replace in {parent.JournalIdentifier}: {exc}")
except Exception as exc:
lister.WriteLine(f"[import] Warning getting parents: {exc}")
# Delete the old sketch feature
try:
mark = session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete old isogrid sketch")
nErrs = session.UpdateManager.AddToDeleteList(old_feature)
nErrs2 = session.UpdateManager.DoUpdate(mark)
lister.WriteLine(f"[import] Deleted old sketch feature ({nErrs + nErrs2} warnings)")
except Exception as exc:
lister.WriteLine(f"[import] Warning deleting old sketch: {exc}")
lister.WriteLine(f"[import] You may need to manually delete the old sketch")
def _find_or_create_sketch(
part: Any,
sketch_name: str,
@@ -82,14 +135,16 @@ def _find_or_create_sketch(
lister: Any,
) -> Any:
"""
Always create a new sketch on the sandbox plane and name it.
NX will auto-number internally (e.g. SKETCH_008).
The sketch Name attribute is set to sketch_name for identification.
Returns the Sketch object.
Always create a new sketch on the sandbox plane.
After creation, rename the sketch feature to sketch_name.
If an old sketch with the same name exists, replace references and delete it.
Returns the (new) Sketch object.
"""
import NXOpen
# Always create new sketch — NX handles duplicate names fine
# Check for existing sketch with this name (will be replaced later)
old_feature = _find_old_sketch_feature(part, sketch_name, lister)
lister.WriteLine(f"[import] Creating new sketch: {sketch_name}")
origin = transform["origin"]
@@ -166,12 +221,28 @@ def _find_or_create_sketch(
if sketch is None:
raise RuntimeError("Could not get Sketch object after commit")
# Rename the sketch feature (not the sketch object — the feature in the tree)
# The sketch's owning feature is what shows in the Part Navigator
new_feature = None
try:
sketch.Name = sketch_name
except Exception:
pass
new_feature = sketch.Feature
new_feature.SetName(sketch_name)
lister.WriteLine(f"[import] Renamed feature to: {sketch_name} ({new_feature.JournalIdentifier})")
except Exception as exc:
lister.WriteLine(f"[import] Warning renaming feature: {exc}")
# Fallback: try setting Name on the sketch directly
try:
sketch.Name = sketch_name
except Exception:
pass
lister.WriteLine(f"[import] Created sketch: {sketch_name}")
# If old sketch existed, replace references and delete it
if old_feature is not None and new_feature is not None:
lister.WriteLine(f"[import] Replacing old sketch with new one...")
_replace_and_delete_old_sketch(part, old_feature, new_feature, lister)
return sketch