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,
|
close: bool = True,
|
||||||
) -> int:
|
) -> 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
|
The sketch must be Activate'd before calling this.
|
||||||
to the sketch in one batch call (much faster than per-line builders).
|
|
||||||
|
|
||||||
Returns total number of lines created.
|
Returns total number of line segments created.
|
||||||
"""
|
"""
|
||||||
import NXOpen
|
import NXOpen
|
||||||
|
|
||||||
all_lines = []
|
|
||||||
total_lines = 0
|
total_lines = 0
|
||||||
|
|
||||||
for points_3d in polylines_3d:
|
for points_3d in polylines_3d:
|
||||||
@@ -283,21 +282,6 @@ def _draw_polylines_batch(
|
|||||||
p1 = points_3d[i]
|
p1 = points_3d[i]
|
||||||
p2 = points_3d[(i + 1) % n]
|
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)
|
|
||||||
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:
|
try:
|
||||||
lb = part.Sketches.CreateLineBuilder()
|
lb = part.Sketches.CreateLineBuilder()
|
||||||
lb.SetStartPoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
lb.SetStartPoint(NXOpen.Point3d(p1[0], p1[1], p1[2]))
|
||||||
@@ -305,37 +289,15 @@ def _draw_polylines_batch(
|
|||||||
lb.Commit()
|
lb.Commit()
|
||||||
lb.Destroy()
|
lb.Destroy()
|
||||||
total_lines += 1
|
total_lines += 1
|
||||||
lister.WriteLine("[import] SketchLineBuilder fallback worked")
|
except Exception as exc:
|
||||||
except Exception as exc2:
|
if total_lines == 0:
|
||||||
lister.WriteLine(f"[import] Both methods failed: {exc2}")
|
lister.WriteLine(f"[import] SketchLineBuilder failed on first line: {exc}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# Add all curves to sketch in one batch call
|
# Progress every 50 polylines
|
||||||
if all_lines:
|
if (total_lines > 0) and (len(polylines_3d) > 50) and \
|
||||||
try:
|
(polylines_3d.index(points_3d) % 50 == 49):
|
||||||
# Sketch.AddGeometry(infer, ellipse, curves)
|
lister.WriteLine(f"[import] ... {total_lines} lines so far")
|
||||||
# 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")
|
|
||||||
|
|
||||||
return total_lines
|
return total_lines
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user