Multi-Offset Sketch Curves Script for Fusion 360

Multi-Offset Sketch Curves Script for Fusion 360

pengkaiwei88
Explorer Explorer
94 Views
0 Replies
Message 1 of 1

Multi-Offset Sketch Curves Script for Fusion 360

pengkaiwei88
Explorer
Explorer

Multi-Offset Sketch Curves Script for Fusion 360

Fusion360 多次 重复 偏移草图 脚本

 

pengkaiwei88_0-1745560276870.png

 

Overview:

Sometimes on some surfaces, if you want to make a CD-like texture, you need to repeat the equidistant offset sketch many times.

This script allows users to quickly offset selected sketch curves multiple times in Fusion 360. It is especially useful for creating multiple concentric shapes, such as rings or other offset patterns, with a single command. The number of offsets and the distance between each offset can be customized by the user, making it a flexible tool for sketch-based design.

Features:

  • Allows selection of any closed or open sketch curves (lines, arcs, circles, splines).

  • Offsets can be applied multiple times with customizable distance and number of repetitions.

  • Ideal for creating concentric designs like rings, wave patterns, or decorative textures.

  • Fully integrated with the Fusion 360 interface as an Add-in, with a button in the Plugins menu.

  • Includes an intuitive user interface for easy selection and configuration of parameters.

Usage:

  1. Copy the code provided below and save it as a new Python file (e.g., MultiOffsetSketch.py).

  2. Install the script in Fusion 360 by following these steps:

    • Open Fusion 360.

    • Go to Tools > Scripts and Add-Ins.

    • In the Scripts and Add-Ins dialog, click Add and select the Python file (MultiOffsetSketch.py) you just saved.

  3. The script will appear in the Scripts and Add-Ins list. Select it and click Run.

  4. Access the plugin from the Plugins menu, and start using the tool.

  5. Select the sketch curves you want to offset, configure the number of offsets and the distance between each one, and run the tool.

  6. The script will automatically generate the desired offsets with the specified parameters.

Installation Instructions:

  1. Copy the code below.

  2. Create a new Python file (e.g., MultiOffsetSketch.py) and paste the code into it.

  3. Save the file to your computer.

  4. In Fusion 360, go to Tools > Scripts and Add-Ins.

  5. Click Add, select the file you saved, and run it.

 

import adsk.core, adsk.fusion, traceback

app = adsk.core.Application.get()
ui = app.userInterface
handlers = []

# 偏移命令执行事件处理器
class OffsetCommandExecuteHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        try:
            eventArgs = adsk.core.CommandEventArgs.cast(args)
            inputs = eventArgs.command.commandInputs

            # 获取用户输入
            selectionInput = inputs.itemById('sketchCurves')
            offsetCount = inputs.itemById('offsetCount').value
            offsetDist = inputs.itemById('offsetDist').value

            # 验证是否在草图编辑模式
            activeSketch = adsk.fusion.Sketch.cast(app.activeEditObject)
            if not activeSketch:
                ui.messageBox("请先进入草图编辑模式后运行此命令。")
                return

            # 收集用户选中的草图曲线
            curves = adsk.core.ObjectCollection.create()
            for i in range(selectionInput.selectionCount):
                sel = selectionInput.selection(i)
                curves.add(sel.entity)

            if curves.count == 0:
                ui.messageBox("未选择任何曲线。")
                return

            # 执行多次偏移
            for i in range(1, offsetCount + 1):
                try:
                    val = offsetDist * i
                    activeSketch.offset(curves, adsk.core.Point3D.create(0, 0, 0), val)
                except Exception as e:
                    ui.messageBox(f"第 {i} 次偏移失败: {str(e)}")
                    continue

            ui.messageBox(f" 成功完成 {offsetCount} 次偏移。")

        except:
            ui.messageBox('执行时发生错误:\n{}'.format(traceback.format_exc()))

# 命令创建时触发(添加输入控件等)
class OffsetCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def notify(self, args):
        try:
            cmd = adsk.core.Command.cast(args.command)
            inputs = cmd.commandInputs

            # 选择草图曲线(多选)
            selInput = inputs.addSelectionInput(
                'sketchCurves',
                '选择草图曲线',
                '选择要偏移的线段、圆、样条等'
            )
            selInput.setSelectionLimits(1, 0)
            selInput.addSelectionFilter('SketchCurves')

            # 偏移次数输入
            inputs.addIntegerSpinnerCommandInput(
                'offsetCount',
                '偏移次数',
                1, 100, 1, 10
            )

            # 偏移间距输入
            inputs.addValueInput(
                'offsetDist',
                '偏移间距',
                'mm',
                adsk.core.ValueInput.createByString('1')
            )

            # 绑定执行事件
            onExecute = OffsetCommandExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)

            #  使用 destroy 替代 terminated
            onDestroy = OffsetCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            handlers.append(onDestroy)

        except:
            ui.messageBox('命令创建失败:\n{}'.format(traceback.format_exc()))

# 命令销毁时触发,用于自动终止脚本
class OffsetCommandDestroyHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        adsk.terminate()

# 脚本启动点
def run(context):
    try:
        cmdId = 'multiOffsetSketch'
        existing = ui.commandDefinitions.itemById(cmdId)
        if existing:
            existing.deleteMe()

        cmdDef = ui.commandDefinitions.addButtonDefinition(
            cmdId,
            '多次偏移草图曲线',
            '多次偏移选中的草图曲线',
            ''
        )

        onCommandCreated = OffsetCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

    except:
        ui.messageBox('脚本运行错误:\n{}'.format(traceback.format_exc()))

 

Known Issues / Limitations:

  • Currently, the script works best with closed curves, but open curves may also be used with certain configurations.

  • If any offsets fail due to invalid curves or sketch conditions, the script will notify you and continue with the next offset.

Future Improvements:

  • Support for bidirectional offsets.

  • Option to group offsets for better visual clarity.

  • Ability to save and recall previous offset configurations.

 

概述:

有时候在一些曲面想要做类似CD纹理的团,需要多次重复等距离偏移草图。

这个脚本允许用户在 Fusion 360 中快速对选定的草图曲线进行多次偏移。它对于创建多个同心形状,如环形或其他偏移图案,非常有用。用户可以自定义偏移的次数和每次偏移之间的距离,使其成为一个灵活的草图设计工具。

功能特点:

  • 允许选择任何封闭或开放的草图曲线(直线、圆弧、圆形、样条曲线等)。

  • 可以应用多次偏移,且用户可自定义偏移的距离和次数。

  • 非常适合创建同心图案,如环形、波纹或装饰性纹理。

  • 完全集成到 Fusion 360 界面中,作为插件,出现在 插件 菜单中。

  • 包括直观的用户界面,便于选择和配置参数。

使用方法:

  1. 复制下面的代码将其保存为新的 Python 文件(例如:MultiOffsetSketch.py)。

  2. 在 Fusion 360 中安装脚本,按照以下步骤操作:

    • 打开 Fusion 360

    • 进入 工具 > 脚本与插件

    • 脚本与插件 对话框中,点击 添加,选择你刚保存的 Python 文件(MultiOffsetSketch.py)。

  3. 脚本将出现在 脚本与插件 列表中。选择它并点击 运行

  4. 插件 菜单中访问插件,开始使用此工具。

  5. 选择要偏移的草图曲线,配置偏移次数和每次偏移的距离,然后运行工具。

  6. 脚本将自动生成符合指定参数的偏移结果。

安装说明:

  1. 复制下面的代码

  2. 创建一个新的 Python 文件(例如:MultiOffsetSketch.py)并将代码粘贴到该文件中

  3. 保存文件到你的计算机。

  4. 在 Fusion 360 中,进入 工具 > 脚本与插件

  5. 点击添加,选择你保存的文件并运行它。

 

已知问题/限制:

  • 当前,脚本最好应用于封闭曲线,但开放曲线也可以在某些配置下使用。

  • 如果偏移失败(由于无效的曲线或草图条件),脚本将通知您并继续进行下一个偏移。

未来改进:

  • 支持双向偏移。

  • 提供偏移分组选项,以便更清晰地查看。

  • 允许保存和调用以前的偏移配置。

0 Likes
95 Views
0 Replies
Replies (0)