<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating best matching plane using several(more than 3) points script in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/13041331#M3326</link>
    <description>&lt;P&gt;i used the code above but nothing happens when I run the code...&lt;/P&gt;&lt;P&gt;dou you heve the same issue?&lt;/P&gt;</description>
    <pubDate>Tue, 24 Sep 2024 13:51:56 GMT</pubDate>
    <dc:creator>karelQUS5R</dc:creator>
    <dc:date>2024-09-24T13:51:56Z</dc:date>
    <item>
      <title>Creating best matching plane using several(more than 3) points script</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12080627#M3322</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;selected_points = []&lt;BR /&gt;&lt;STRONG&gt;for point in design.activeProduct.activeSelections:&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if isinstance(point.entity, adsk.fusion.SketchPoint):&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;selected_points.append(point.entity.worldGeometry)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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 &amp;lt; 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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 10:26:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12080627#M3322</guid>
      <dc:creator>caner-korkmaz</dc:creator>
      <dc:date>2023-07-05T10:26:48Z</dc:date>
    </item>
    <item>
      <title>Betreff: Creating best matching plane using several(more than 3) points script</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12081380#M3323</link>
      <description>You can find the Object Reference Documentation at (Expand Objects) &lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7B5A90C8-E94C-48DA-B16B-430729B734DC" target="_blank"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7B5A90C8-E94C-48DA-B16B-430729B734DC&lt;/A&gt;&lt;BR /&gt;You need to do at least some minimal research to get that script working. Have a look at the Code Samples as well: &lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=SampleList" target="_blank"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=SampleList&lt;/A&gt;</description>
      <pubDate>Wed, 05 Jul 2023 16:04:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12081380#M3323</guid>
      <dc:creator>felix85SA3</dc:creator>
      <dc:date>2023-07-05T16:04:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating best matching plane using several(more than 3) points script</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12081611#M3324</link>
      <description>&lt;P&gt;I think at the present time, ChatGPT isn't a dependable resource for creating Fusion programs. It will create code, but it will often have little errors that will be difficult for a new programmer to find and fix. It also seems that when it can't determine if Fusion has certain functionality, it just makes up functionality. For example, it calls the&amp;nbsp;eigenVectors function on the Matrix3D object, but it doesn't support that function. It's also treating a Matrix3D object as a Python List, which it is not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suspect the algorithm it uses to find the points' average and the plane normal is valid, but it will take some work to convert it into usable code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 17:30:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12081611#M3324</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2023-07-05T17:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating best matching plane using several(more than 3) points script</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12089237#M3325</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4935405"&gt;@caner-korkmaz&lt;/a&gt;&amp;nbsp;-San.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I asked ChatGPT how to do planar fitting without external modules, but they replied that it is difficult.&lt;BR /&gt;Therefore, we created a sample using numpy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

import os
import sys
import inspect
import random

script_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
script_name = os.path.splitext(os.path.basename(script_path))[0]
script_dir = os.path.dirname(script_path)
sys.path.append(script_dir + "/site-packages")

import numpy as np

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        points = create_sample(200)
        dump_points(points, "sample data")

        create_fit_plane(points)

        ui.messageBox("Done")

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def create_fit_plane(
    pointList: list[core.Point3D]
) -&amp;gt; fusion.ConstructionPlane:

    def fit_plane(pointList: list[core.Point3D]):
        point_cloud = np.array(pointList)

        com = np.sum(point_cloud, axis=0) / len(point_cloud)
        q = point_cloud - com
        Q = np.dot(q.T, q)
        la, vectors = np.linalg.eig(Q)
        plane_v = vectors.T[np.argmin(la)]

        return plane_v, com
    # *************

    normal, origin = fit_plane(
        [p.asArray() for p in pointList]
    )

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    plane: core.Plane = core.Plane.create(
        core.Point3D.create(*origin.tolist()),
        core.Vector3D.create(*normal.tolist()),
    )

    baseFeat: fusion.BaseFeature = None
    if des.designType == fusion.DesignTypes.ParametricDesignType:
        baseFeat = root.features.baseFeatures.add()

    planes: fusion.ConstructionPlanes = root.constructionPlanes
    planeIpt: fusion.ConstructionPlaneInput = planes.createInput()
    constPlane: fusion.ConstructionPlane = None
    if baseFeat:
        try:
            baseFeat.startEdit()
            planeIpt.setByPlane(plane)
            constPlane = planes.add(planeIpt)
        except:
            pass
        finally:
            baseFeat.finishEdit()
    else:
        planeIpt.setByPlane(plane)
        constPlane = planes.add(planeIpt)
    constPlane.name = "Fit Plane"

    return constPlane


def create_sample(
    count: int
) -&amp;gt; list[core.Point3D]:

    return [
        core.Point3D.create(
            random.uniform(-10, 10),
            random.uniform(-10, 10),
            random.uniform(-1, 1),
        )
        for _ in range(count)
    ]


def dump_points(
    points: list,
    name: str = '',
) -&amp;gt; fusion.Sketch:

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    skt: fusion.Sketch = root.sketches.add(
        root.xYConstructionPlane
    )

    if len(name) &amp;gt; 0:
        skt.name = name

    sktPoints: fusion.SketchPoints = skt.sketchPoints

    skt.isComputeDeferred = True
    [sktPoints.add(p) for p in points]
    skt.isComputeDeferred = False

    return skt&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;200 random sketch points are created and the plane obtained by plane fitting is created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fit_plane function that does the plane fitting is a modified version of one I found on the web, so I do not understand the content itself.&lt;BR /&gt;I think it is probably a simple least squares method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The attached file also includes numpy, so perhaps you can run it in your environment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a side note, in the CAD software I use for my work (CATIA V5), there is a command to create such a plane.&lt;BR /&gt;It would be useful to have a similar command in Fusion360.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2023 13:52:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/12089237#M3325</guid>
      <dc:creator>kandennti</dc:creator>
      <dc:date>2023-07-09T13:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating best matching plane using several(more than 3) points script</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/13041331#M3326</link>
      <description>&lt;P&gt;i used the code above but nothing happens when I run the code...&lt;/P&gt;&lt;P&gt;dou you heve the same issue?&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 13:51:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/creating-best-matching-plane-using-several-more-than-3-points/m-p/13041331#M3326</guid>
      <dc:creator>karelQUS5R</dc:creator>
      <dc:date>2024-09-24T13:51:56Z</dc:date>
    </item>
  </channel>
</rss>

