fix: use Plane (SmartObject) not DatumPlane, method calls not property setters (verified from MCP stubs)

This commit is contained in:
2026-02-16 19:05:12 +00:00
parent 7a2c002672
commit bf1f461e2b

View File

@@ -118,32 +118,55 @@ def _find_or_create_sketch(
origin_pt = NXOpen.Point3d(origin[0], origin[1], origin[2]) origin_pt = NXOpen.Point3d(origin[0], origin[1], origin[2])
normal_vec = NXOpen.Vector3d(normal[0], normal[1], normal[2]) normal_vec = NXOpen.Vector3d(normal[0], normal[1], normal[2])
x_vec = NXOpen.Vector3d(x_axis[0], x_axis[1], x_axis[2]) x_vec = NXOpen.Vector3d(x_axis[0], x_axis[1], x_axis[2])
y_axis = transform["y_axis"]
y_vec = NXOpen.Vector3d(y_axis[0], y_axis[1], y_axis[2])
# Build Matrix3x3 from x_axis, y_axis, normal (row-major: Xx,Xy,Xz, Yx,Yy,Yz, Zx,Zy,Zz) # Create a Plane (SmartObject) — NOT a DatumPlane (DisplayableObject)
mtx = NXOpen.Matrix3x3() # PlaneCollection.CreatePlane(method, alternate, origin, normal, expr, flip, percent, geometry)
mtx.Xx = x_axis[0]; mtx.Xy = x_axis[1]; mtx.Xz = x_axis[2] plane = part.Planes.CreatePlane(
mtx.Yx = y_axis[0]; mtx.Yy = y_axis[1]; mtx.Yz = y_axis[2] NXOpen.PlaneTypes.MethodType.Fixed, # Fixed plane
mtx.Zx = normal[0]; mtx.Zy = normal[1]; mtx.Zz = normal[2] NXOpen.PlaneTypes.AlternateType.One,
origin_pt,
normal_vec,
"", # expression
False, # flip
False, # percent
[], # geometry refs
)
lister.WriteLine(f"[import] Created plane: {plane} (type={type(plane).__name__})")
# Create fixed datum plane if plane is None:
datum_plane = part.Datums.CreateFixedDatumPlane(origin_pt, mtx) # Fallback: try Distance method
lister.WriteLine(f"[import] Created datum plane: {datum_plane}") plane = part.Planes.CreatePlane(
NXOpen.PlaneTypes.MethodType.Distance,
NXOpen.PlaneTypes.AlternateType.One,
origin_pt, normal_vec, "", False, False, [],
)
lister.WriteLine(f"[import] Fallback plane: {plane}")
if plane is None:
# Fallback 2: CreateFixedTypePlane
y_axis_t = transform["y_axis"]
mtx = NXOpen.Matrix3x3()
mtx.Xx = x_axis[0]; mtx.Xy = x_axis[1]; mtx.Xz = x_axis[2]
mtx.Yx = y_axis_t[0]; mtx.Yy = y_axis_t[1]; mtx.Yz = y_axis_t[2]
mtx.Zx = normal[0]; mtx.Zy = normal[1]; mtx.Zz = normal[2]
plane = part.Planes.CreateFixedTypePlane(
origin_pt, mtx, NXOpen.SmartObject.UpdateOption.WithinModeling,
)
lister.WriteLine(f"[import] Fallback2 plane: {plane}")
# Create sketch-in-place builder # Create sketch-in-place builder
sketch_builder = part.Sketches.CreateSketchInPlaceBuilder2(NXOpen.Sketch.Null) sketch_builder = part.Sketches.CreateSketchInPlaceBuilder2(NXOpen.Sketch.Null)
# Set plane — PlaneReference is a property setter # These are method calls (verified from MCP .pyi stubs), not property setters
sketch_builder.PlaneReference = datum_plane sketch_builder.PlaneReference(plane)
# Set sketch origin
origin_point = part.Points.CreatePoint(origin_pt) origin_point = part.Points.CreatePoint(origin_pt)
sketch_builder.SketchOrigin = origin_point sketch_builder.SketchOrigin(origin_point)
# Set axis reference axis_dir = part.Directions.CreateDirection(
axis_dir = part.Directions.CreateDirection(origin_pt, x_vec, NXOpen.SmartObject.UpdateOption.WithinModeling) origin_pt, x_vec, NXOpen.SmartObject.UpdateOption.WithinModeling,
sketch_builder.AxisReference = axis_dir )
sketch_builder.AxisReference(axis_dir)
# Commit to create the sketch # Commit to create the sketch
sketch_feature = sketch_builder.CommitFeature() sketch_feature = sketch_builder.CommitFeature()