fix: Builder.Commit() not CommitFeature(), correct AddGeometry signature, verbose commit logging
This commit is contained in:
@@ -201,33 +201,47 @@ def _find_or_create_sketch(
|
||||
|
||||
lister.WriteLine(f"[import] WARNING: Could not set {label} ({attr})")
|
||||
|
||||
# Commit to create the sketch
|
||||
sketch_feature = sketch_builder.CommitFeature()
|
||||
# Commit — Builder.Commit() returns NXObject
|
||||
sketch_obj = sketch_builder.Commit()
|
||||
lister.WriteLine(f"[import] Commit returned: {sketch_obj} (type={type(sketch_obj).__name__})")
|
||||
|
||||
# Also get committed objects for inspection
|
||||
committed = sketch_builder.GetCommittedObjects()
|
||||
lister.WriteLine(f"[import] Committed objects: {len(committed)}")
|
||||
for i, obj in enumerate(committed):
|
||||
lister.WriteLine(f"[import] [{i}] {type(obj).__name__}: {obj}")
|
||||
|
||||
sketch_builder.Destroy()
|
||||
|
||||
# Get the sketch object from the feature
|
||||
# Find the Sketch object from committed objects or return value
|
||||
sketch = None
|
||||
try:
|
||||
entities = sketch_feature.GetEntities()
|
||||
for e in entities:
|
||||
if isinstance(e, NXOpen.Sketch):
|
||||
sketch = e
|
||||
if isinstance(sketch_obj, NXOpen.Sketch):
|
||||
sketch = sketch_obj
|
||||
else:
|
||||
# Search committed objects
|
||||
for obj in committed:
|
||||
if isinstance(obj, NXOpen.Sketch):
|
||||
sketch = obj
|
||||
break
|
||||
if sketch is None and entities:
|
||||
sketch = entities[0]
|
||||
except Exception:
|
||||
# Try alternate: find sketch by feature
|
||||
try:
|
||||
sketch = sketch_feature.Sketch
|
||||
except Exception:
|
||||
pass
|
||||
# Try GetObject
|
||||
if sketch is None:
|
||||
try:
|
||||
sketch = sketch_builder.GetObject()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Last resort: find by name in part sketches
|
||||
if sketch is None:
|
||||
for s in part.Sketches:
|
||||
sketch = s # take the last one (just created)
|
||||
lister.WriteLine(f"[import] Found sketch by iteration: {sketch}")
|
||||
|
||||
if sketch is None:
|
||||
raise RuntimeError("Could not get Sketch object from feature")
|
||||
raise RuntimeError("Could not get Sketch object after commit")
|
||||
|
||||
# Rename the feature
|
||||
# Rename
|
||||
try:
|
||||
sketch_feature.Name = sketch_name
|
||||
sketch.Name = sketch_name
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -270,24 +284,32 @@ def _draw_polyline_in_sketch(
|
||||
start_pt = NXOpen.Point3d(p1[0], p1[1], p1[2])
|
||||
end_pt = NXOpen.Point3d(p2[0], p2[1], p2[2])
|
||||
|
||||
# SketchCollection.CreateLineBuilder() -> SketchLineBuilder
|
||||
# SketchLineBuilder.SetStartPoint(Point3d), .SetEndPoint(Point3d)
|
||||
# Builder.Commit() -> NXObject, Builder.Destroy()
|
||||
line_builder = part.Sketches.CreateLineBuilder()
|
||||
line_builder.SetStartPoint(start_pt)
|
||||
line_builder.SetEndPoint(end_pt)
|
||||
line = line_builder.Commit()
|
||||
line_builder.Commit()
|
||||
line_builder.Destroy()
|
||||
|
||||
lines_created += 1
|
||||
except Exception:
|
||||
# Fallback: try creating a curve and adding to sketch
|
||||
try:
|
||||
start_obj = part.Points.CreatePoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||
end_obj = part.Points.CreatePoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
||||
line = part.Curves.CreateLine(start_obj, end_obj)
|
||||
sketch.AddGeometry(line, NXOpen.Sketch.InferConstraintsOption.DoNotInferConstraints)
|
||||
lines_created += 1
|
||||
except Exception as exc2:
|
||||
if lines_created == 0:
|
||||
lister.WriteLine(f"[import] Line creation failed: {exc2}")
|
||||
except Exception as exc:
|
||||
if lines_created == 0:
|
||||
lister.WriteLine(f"[import] Line creation failed: {exc}")
|
||||
# Try fallback: create curve + add to sketch
|
||||
try:
|
||||
start_obj = part.Points.CreatePoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||
end_obj = part.Points.CreatePoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
||||
line = part.Curves.CreateLine(start_obj, end_obj)
|
||||
sketch.AddGeometry(
|
||||
NXOpen.Sketch.InferConstraintsOption.DoNotInferConstraints,
|
||||
NXOpen.Sketch.AddEllipseOption.None_,
|
||||
[line],
|
||||
)
|
||||
lines_created += 1
|
||||
except Exception as exc2:
|
||||
lister.WriteLine(f"[import] Fallback line also failed: {exc2}")
|
||||
|
||||
return lines_created
|
||||
|
||||
|
||||
Reference in New Issue
Block a user