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})")
|
lister.WriteLine(f"[import] WARNING: Could not set {label} ({attr})")
|
||||||
|
|
||||||
# Commit to create the sketch
|
# Commit — Builder.Commit() returns NXObject
|
||||||
sketch_feature = sketch_builder.CommitFeature()
|
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()
|
sketch_builder.Destroy()
|
||||||
|
|
||||||
# Get the sketch object from the feature
|
# Find the Sketch object from committed objects or return value
|
||||||
sketch = None
|
sketch = None
|
||||||
try:
|
if isinstance(sketch_obj, NXOpen.Sketch):
|
||||||
entities = sketch_feature.GetEntities()
|
sketch = sketch_obj
|
||||||
for e in entities:
|
else:
|
||||||
if isinstance(e, NXOpen.Sketch):
|
# Search committed objects
|
||||||
sketch = e
|
for obj in committed:
|
||||||
|
if isinstance(obj, NXOpen.Sketch):
|
||||||
|
sketch = obj
|
||||||
break
|
break
|
||||||
if sketch is None and entities:
|
# Try GetObject
|
||||||
sketch = entities[0]
|
if sketch is None:
|
||||||
except Exception:
|
try:
|
||||||
# Try alternate: find sketch by feature
|
sketch = sketch_builder.GetObject()
|
||||||
try:
|
except Exception:
|
||||||
sketch = sketch_feature.Sketch
|
pass
|
||||||
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:
|
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:
|
try:
|
||||||
sketch_feature.Name = sketch_name
|
sketch.Name = sketch_name
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -270,24 +284,32 @@ def _draw_polyline_in_sketch(
|
|||||||
start_pt = NXOpen.Point3d(p1[0], p1[1], p1[2])
|
start_pt = NXOpen.Point3d(p1[0], p1[1], p1[2])
|
||||||
end_pt = NXOpen.Point3d(p2[0], p2[1], p2[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 = part.Sketches.CreateLineBuilder()
|
||||||
line_builder.SetStartPoint(start_pt)
|
line_builder.SetStartPoint(start_pt)
|
||||||
line_builder.SetEndPoint(end_pt)
|
line_builder.SetEndPoint(end_pt)
|
||||||
line = line_builder.Commit()
|
line_builder.Commit()
|
||||||
line_builder.Destroy()
|
line_builder.Destroy()
|
||||||
|
|
||||||
lines_created += 1
|
lines_created += 1
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
# Fallback: try creating a curve and adding to sketch
|
if lines_created == 0:
|
||||||
try:
|
lister.WriteLine(f"[import] Line creation failed: {exc}")
|
||||||
start_obj = part.Points.CreatePoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
# Try fallback: create curve + add to sketch
|
||||||
end_obj = part.Points.CreatePoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
try:
|
||||||
line = part.Curves.CreateLine(start_obj, end_obj)
|
start_obj = part.Points.CreatePoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||||
sketch.AddGeometry(line, NXOpen.Sketch.InferConstraintsOption.DoNotInferConstraints)
|
end_obj = part.Points.CreatePoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
||||||
lines_created += 1
|
line = part.Curves.CreateLine(start_obj, end_obj)
|
||||||
except Exception as exc2:
|
sketch.AddGeometry(
|
||||||
if lines_created == 0:
|
NXOpen.Sketch.InferConstraintsOption.DoNotInferConstraints,
|
||||||
lister.WriteLine(f"[import] Line creation failed: {exc2}")
|
NXOpen.Sketch.AddEllipseOption.None_,
|
||||||
|
[line],
|
||||||
|
)
|
||||||
|
lines_created += 1
|
||||||
|
except Exception as exc2:
|
||||||
|
lister.WriteLine(f"[import] Fallback line also failed: {exc2}")
|
||||||
|
|
||||||
return lines_created
|
return lines_created
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user