fix(import): use SketchLineBuilder instead of model curves + AddGeometry
Model curves (part.Curves.CreateLine) are SmartObjects that can't be added to a sketch via AddGeometry. Switch to SketchLineBuilder which creates native sketch geometry directly (SetStartPoint/SetEndPoint/Commit).
This commit is contained in:
@@ -254,16 +254,15 @@ def _draw_polylines_batch(
|
||||
close: bool = True,
|
||||
) -> int:
|
||||
"""
|
||||
Draw multiple closed polylines in the sketch efficiently.
|
||||
Draw multiple closed polylines directly in the active sketch using
|
||||
SketchLineBuilder (creates native sketch geometry, not model curves).
|
||||
|
||||
Creates all lines as NX curves first (fast), then adds them
|
||||
to the sketch in one batch call (much faster than per-line builders).
|
||||
The sketch must be Activate'd before calling this.
|
||||
|
||||
Returns total number of lines created.
|
||||
Returns total number of line segments created.
|
||||
"""
|
||||
import NXOpen
|
||||
|
||||
all_lines = []
|
||||
total_lines = 0
|
||||
|
||||
for points_3d in polylines_3d:
|
||||
@@ -284,58 +283,21 @@ def _draw_polylines_batch(
|
||||
p2 = points_3d[(i + 1) % n]
|
||||
|
||||
try:
|
||||
# CurveCollection.CreateLine(start: Point, end: Point) -> Line
|
||||
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)
|
||||
all_lines.append(line)
|
||||
lb = part.Sketches.CreateLineBuilder()
|
||||
lb.SetStartPoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||
lb.SetEndPoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
||||
lb.Commit()
|
||||
lb.Destroy()
|
||||
total_lines += 1
|
||||
except Exception as exc:
|
||||
if total_lines == 0:
|
||||
lister.WriteLine(f"[import] CreateLine failed: {exc}")
|
||||
# Try SketchLineBuilder as fallback for first line
|
||||
try:
|
||||
lb = part.Sketches.CreateLineBuilder()
|
||||
lb.SetStartPoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||
lb.SetEndPoint(NXOpen.Point3d(p2[0], p2[1], p2[2]))
|
||||
lb.Commit()
|
||||
lb.Destroy()
|
||||
total_lines += 1
|
||||
lister.WriteLine("[import] SketchLineBuilder fallback worked")
|
||||
except Exception as exc2:
|
||||
lister.WriteLine(f"[import] Both methods failed: {exc2}")
|
||||
return 0
|
||||
lister.WriteLine(f"[import] SketchLineBuilder failed on first line: {exc}")
|
||||
return 0
|
||||
|
||||
# Add all curves to sketch in one batch call
|
||||
if all_lines:
|
||||
try:
|
||||
# Sketch.AddGeometry(infer, ellipse, curves)
|
||||
# Enums are nested under NXOpen.Sketch, not NXOpen directly
|
||||
infer = NXOpen.Sketch.InferConstraintsOption.InferNoConstraints
|
||||
ellipse = NXOpen.Sketch.AddEllipseOption.TreatAsEllipse
|
||||
sketch.AddGeometry(infer, ellipse, all_lines)
|
||||
lister.WriteLine(f"[import] Added {len(all_lines)} lines to sketch via AddGeometry")
|
||||
except Exception as exc:
|
||||
lister.WriteLine(f"[import] AddGeometry batch failed: {exc}")
|
||||
# Try adding one by one
|
||||
added = 0
|
||||
try:
|
||||
infer = NXOpen.Sketch.InferConstraintsOption.InferNoConstraints
|
||||
ellipse = NXOpen.Sketch.AddEllipseOption.TreatAsEllipse
|
||||
except Exception:
|
||||
infer = None
|
||||
if infer is not None:
|
||||
for line in all_lines:
|
||||
try:
|
||||
sketch.AddGeometry(infer, ellipse, [line])
|
||||
added += 1
|
||||
except Exception:
|
||||
pass
|
||||
lister.WriteLine(f"[import] Added {added}/{len(all_lines)} lines individually")
|
||||
# Progress every 50 polylines
|
||||
if (total_lines > 0) and (len(polylines_3d) > 50) and \
|
||||
(polylines_3d.index(points_3d) % 50 == 49):
|
||||
lister.WriteLine(f"[import] ... {total_lines} lines so far")
|
||||
|
||||
return total_lines
|
||||
|
||||
|
||||
Reference in New Issue
Block a user