<?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 [bug]Getting wrong SketchEntities from ProfileLoop in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/bug-getting-wrong-sketchentities-from-profileloop/m-p/9574956#M11443</link>
    <description>&lt;P&gt;After two updates of Fusion 360 this bug still remains unsolved.&lt;/P&gt;&lt;P&gt;I didn't received any response.&lt;/P&gt;&lt;P&gt;Python script below reproduces the behaviour.&lt;/P&gt;&lt;P&gt;The script run successfully&amp;nbsp; only in a sketch mode with valid profile.&lt;/P&gt;&lt;P&gt;To really reproduce the bug, the profile must be created specific way. The bug occurs if sketch profile is extended by joining two loops and trimmed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;A guaranteed&amp;nbsp; way is (look also at the picture attached)&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;create&amp;nbsp; new sketch on XY plane&lt;/LI&gt;&lt;LI&gt;draw fit spline&lt;/LI&gt;&lt;LI&gt;add 3 line segments&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;add a rectangle crossing bottom line&lt;/LI&gt;&lt;LI&gt;trim the segments to extend the profile&lt;/LI&gt;&lt;LI&gt;run the script&lt;/LI&gt;&lt;LI&gt;script creates 'curves.txt' file with a listing of the Sketch Curves with start and end point.&lt;BR /&gt;Define a path for 'curves.txt' to find the file easier.&lt;BR /&gt;You will see that one Sketch Curve is missing and another is doubled. (If you follow exactly described&amp;nbsp; way, it is always 1 and 5. This is wrong.&lt;/LI&gt;&lt;LI&gt;If you export this sketch as dxf, and import it again this effect disappears. The sketch is somehow corrected.&lt;BR /&gt;There is also no problem to work with this profile in Fusion.&amp;nbsp; The problem has developer, who receives wrong data for another processing.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;If you run this script without sketch and profile loop it will fail. (its only for test)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Author-JiriManak
# Description-Demonstration of the bug

import adsk.core, adsk.fusion, adsk.cam, traceback


def point2str(p):
    return "[{:4.2f}, {:4.2f}, {:4.2f}]".format(p.x, p.y, p.z)


def get_curve_info(profile_curve):
    sketch_entity = profile_curve.sketchEntity
    class_type = sketch_entity.objectType
    sketch_curve = None
    start_p = None
    end_p = None
    if class_type == adsk.fusion.SketchLine.classType():
        sketch_curve = adsk.fusion.SketchLine.cast(sketch_entity)
    if class_type == adsk.fusion.SketchArc.classType():
        sketch_curve = adsk.fusion.SketchArc.cast(sketch_entity)
    if class_type == adsk.fusion.SketchConicCurve.classType():
        sketch_curve = adsk.fusion.SketchConicCurve.cast(sketch_entity)
    if class_type == adsk.fusion.SketchEllipticalArc.classType():
        sketch_curve = adsk.fusion.SketchEllipticalArc.cast(sketch_entity)
    if class_type == adsk.fusion.SketchFittedSpline.classType():
        sketch_curve = adsk.fusion.SketchFittedSpline.cast(sketch_entity)
    if class_type == adsk.fusion.SketchFixedSpline.classType():
        sketch_curve = adsk.fusion.SketchFixedSpline.cast(sketch_entity)

    if sketch_curve:
        start_p = adsk.core.Point3D.cast(sketch_curve.startSketchPoint.geometry)
        end_p = adsk.core.Point3D.cast(sketch_curve.endSketchPoint.geometry)
    return class_type, start_p, end_p


def publish_curve_info(index, curve_info):
    s = ""
    class_type, start_p, end_p = curve_info
    s += "[{:02d}] Sketch Curve: {}\n".format(index, class_type)
    s += "     start point: {}\n".format(point2str(start_p))
    s += "     end point:   {}\n".format(point2str(end_p))
    s += "\n"
    return s


def find_duplicates(collection):
    a_index = 0

    for info_a in collection:
        b_index = 0
        for info_b in collection:
            if a_index == b_index:
                continue
            a_class_type, a_start_p, a_end_p = info_a
            b_class_type, b_start_p, b_end_p = info_b
            if adsk.core.Point3D.cast(a_start_p).isEqualToByTolerance(b_start_p, 0.001):
                if adsk.core.Point3D.cast(a_end_p).isEqualToByTolerance(b_end_p, 0.001):
                    return a_index, b_index,
            b_index += 1
        a_index += 1
    return -1, -1


def run(context):
    ui = None

    try:
        s = "HandDrawnCurvesBug\n"
        s += "List of curves:\n"
        app = adsk.core.Application.get()
        ui = app.userInterface
        active_sketch = app.activeEditObject
        profiles = active_sketch.profiles

        collection = list()

        txtCmd = 'Debug.EntitlementString'
        ui.messageBox(app.executeTextCommand(txtCmd))

        # Individual: Personal / Hobbyist
        # Startup: Startup / Enthusiast
        # Commercial / EE: Commercial
        # Educational
        # Institution: Student

        if profiles.count &amp;lt; 1:
            ui.messageBox("Profile not found !")
        else:
            profile = profiles.item(0)
            profile_loops = profile.profileLoops
            if profile_loops.count &amp;gt; 0:
                profile_loop = profile_loops.item(0)
                profile_curves = profile_loop.profileCurves
                index = 0
                for profile_curve in profile_curves:
                    curve_info = get_curve_info(profile_curve)
                    collection.append(curve_info)
                    s += publish_curve_info(index, curve_info)
                    index += 1

        d0, d2 = find_duplicates(collection)
        if d0 &amp;gt; -1:
            s += "DUPLICATES FOUND: [{:02d}] == [{:02d}]\n".format(d0, d2)

        output = open("D:\\tmp\\curves.txt", 'w')
        output.writelines(s)
        output.close()


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Jiri&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jun 2020 18:02:40 GMT</pubDate>
    <dc:creator>jiri.manak</dc:creator>
    <dc:date>2020-06-11T18:02:40Z</dc:date>
    <item>
      <title>[bug]Getting wrong SketchEntities from ProfileLoop</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/bug-getting-wrong-sketchentities-from-profileloop/m-p/9574956#M11443</link>
      <description>&lt;P&gt;After two updates of Fusion 360 this bug still remains unsolved.&lt;/P&gt;&lt;P&gt;I didn't received any response.&lt;/P&gt;&lt;P&gt;Python script below reproduces the behaviour.&lt;/P&gt;&lt;P&gt;The script run successfully&amp;nbsp; only in a sketch mode with valid profile.&lt;/P&gt;&lt;P&gt;To really reproduce the bug, the profile must be created specific way. The bug occurs if sketch profile is extended by joining two loops and trimmed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;A guaranteed&amp;nbsp; way is (look also at the picture attached)&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;create&amp;nbsp; new sketch on XY plane&lt;/LI&gt;&lt;LI&gt;draw fit spline&lt;/LI&gt;&lt;LI&gt;add 3 line segments&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;add a rectangle crossing bottom line&lt;/LI&gt;&lt;LI&gt;trim the segments to extend the profile&lt;/LI&gt;&lt;LI&gt;run the script&lt;/LI&gt;&lt;LI&gt;script creates 'curves.txt' file with a listing of the Sketch Curves with start and end point.&lt;BR /&gt;Define a path for 'curves.txt' to find the file easier.&lt;BR /&gt;You will see that one Sketch Curve is missing and another is doubled. (If you follow exactly described&amp;nbsp; way, it is always 1 and 5. This is wrong.&lt;/LI&gt;&lt;LI&gt;If you export this sketch as dxf, and import it again this effect disappears. The sketch is somehow corrected.&lt;BR /&gt;There is also no problem to work with this profile in Fusion.&amp;nbsp; The problem has developer, who receives wrong data for another processing.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;If you run this script without sketch and profile loop it will fail. (its only for test)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Author-JiriManak
# Description-Demonstration of the bug

import adsk.core, adsk.fusion, adsk.cam, traceback


def point2str(p):
    return "[{:4.2f}, {:4.2f}, {:4.2f}]".format(p.x, p.y, p.z)


def get_curve_info(profile_curve):
    sketch_entity = profile_curve.sketchEntity
    class_type = sketch_entity.objectType
    sketch_curve = None
    start_p = None
    end_p = None
    if class_type == adsk.fusion.SketchLine.classType():
        sketch_curve = adsk.fusion.SketchLine.cast(sketch_entity)
    if class_type == adsk.fusion.SketchArc.classType():
        sketch_curve = adsk.fusion.SketchArc.cast(sketch_entity)
    if class_type == adsk.fusion.SketchConicCurve.classType():
        sketch_curve = adsk.fusion.SketchConicCurve.cast(sketch_entity)
    if class_type == adsk.fusion.SketchEllipticalArc.classType():
        sketch_curve = adsk.fusion.SketchEllipticalArc.cast(sketch_entity)
    if class_type == adsk.fusion.SketchFittedSpline.classType():
        sketch_curve = adsk.fusion.SketchFittedSpline.cast(sketch_entity)
    if class_type == adsk.fusion.SketchFixedSpline.classType():
        sketch_curve = adsk.fusion.SketchFixedSpline.cast(sketch_entity)

    if sketch_curve:
        start_p = adsk.core.Point3D.cast(sketch_curve.startSketchPoint.geometry)
        end_p = adsk.core.Point3D.cast(sketch_curve.endSketchPoint.geometry)
    return class_type, start_p, end_p


def publish_curve_info(index, curve_info):
    s = ""
    class_type, start_p, end_p = curve_info
    s += "[{:02d}] Sketch Curve: {}\n".format(index, class_type)
    s += "     start point: {}\n".format(point2str(start_p))
    s += "     end point:   {}\n".format(point2str(end_p))
    s += "\n"
    return s


def find_duplicates(collection):
    a_index = 0

    for info_a in collection:
        b_index = 0
        for info_b in collection:
            if a_index == b_index:
                continue
            a_class_type, a_start_p, a_end_p = info_a
            b_class_type, b_start_p, b_end_p = info_b
            if adsk.core.Point3D.cast(a_start_p).isEqualToByTolerance(b_start_p, 0.001):
                if adsk.core.Point3D.cast(a_end_p).isEqualToByTolerance(b_end_p, 0.001):
                    return a_index, b_index,
            b_index += 1
        a_index += 1
    return -1, -1


def run(context):
    ui = None

    try:
        s = "HandDrawnCurvesBug\n"
        s += "List of curves:\n"
        app = adsk.core.Application.get()
        ui = app.userInterface
        active_sketch = app.activeEditObject
        profiles = active_sketch.profiles

        collection = list()

        txtCmd = 'Debug.EntitlementString'
        ui.messageBox(app.executeTextCommand(txtCmd))

        # Individual: Personal / Hobbyist
        # Startup: Startup / Enthusiast
        # Commercial / EE: Commercial
        # Educational
        # Institution: Student

        if profiles.count &amp;lt; 1:
            ui.messageBox("Profile not found !")
        else:
            profile = profiles.item(0)
            profile_loops = profile.profileLoops
            if profile_loops.count &amp;gt; 0:
                profile_loop = profile_loops.item(0)
                profile_curves = profile_loop.profileCurves
                index = 0
                for profile_curve in profile_curves:
                    curve_info = get_curve_info(profile_curve)
                    collection.append(curve_info)
                    s += publish_curve_info(index, curve_info)
                    index += 1

        d0, d2 = find_duplicates(collection)
        if d0 &amp;gt; -1:
            s += "DUPLICATES FOUND: [{:02d}] == [{:02d}]\n".format(d0, d2)

        output = open("D:\\tmp\\curves.txt", 'w')
        output.writelines(s)
        output.close()


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Jiri&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jun 2020 18:02:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/bug-getting-wrong-sketchentities-from-profileloop/m-p/9574956#M11443</guid>
      <dc:creator>jiri.manak</dc:creator>
      <dc:date>2020-06-11T18:02:40Z</dc:date>
    </item>
  </channel>
</rss>

