Creating best matching plane using several(more than 3) points script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I'm trying to create a plane using several points.Reason for this is i'm working on surveyers point cloud data and using random 3 points in a surface doesn't always creates the best plane because of the slight surface inperfections or bad readings.Instead what i want is selecting several points in a what i know is a surface and calculating best matching plane.I know resulting plane could not intersect all the point but thats okay.I am not good at python but i tried chatgpt and i think the code it gave me could work.But it gives an error.I think the highligted section in the below code is the problem.If anyone gives me a direction iwill be greatfull.Thank you.
selected_points = []
for point in design.activeProduct.activeSelections:
if isinstance(point.entity, adsk.fusion.SketchPoint):
selected_points.append(point.entity.worldGeometry)
import adsk.core, adsk.fusion
def create_best_fit_plane():
# Get the current active document and root component
app = adsk.core.Application.get()
design = app.activeProduct
root_comp = design.rootComponent
# Get the selected sketch points
selected_points = []
for point in design.activeProduct.activeSelections:
if isinstance(point.entity, adsk.fusion.SketchPoint):
selected_points.append(point.entity.worldGeometry)
num_points = len(selected_points)
if num_points < 3:
# At least 3 points are required to create a plane
return
# Calculate the centroid of the selected points
centroid = adsk.core.Point3D.create(0, 0, 0)
for point in selected_points:
centroid += point
centroid /= num_points
# Calculate the covariance matrix
covariance = adsk.core.Matrix3D.create()
for point in selected_points:
x = point.x - centroid.x
y = point.y - centroid.y
z = point.z - centroid.z
covariance.data[0][0] += x * x
covariance.data[0][1] += x * y
covariance.data[0][2] += x * z
covariance.data[1][0] += y * x
covariance.data[1][1] += y * y
covariance.data[1][2] += y * z
covariance.data[2][0] += z * x
covariance.data[2][1] += z * y
covariance.data[2][2] += z * z
# Find the eigenvector corresponding to the smallest eigenvalue
eigenvalues, eigenvectors = covariance.eigenVectors()
min_eigenvalue = min(eigenvalues)
min_eigenvector = eigenvectors[eigenvalues.index(min_eigenvalue)]
# Create the plane through the centroid with the normal defined by the eigenvector
plane_input = root_comp.constructionPlanes.createInput()
plane_input.setByPlaneAndPoint(
adsk.core.Plane.create(centroid, min_eigenvector),
centroid
)
plane = root_comp.constructionPlanes.add(plane_input)
return plane
# Run the function to create the best-fit plane
create_best_fit_plane()