Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy/past multiple lines/splines in 2d sketch

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
richop88
737 Views, 3 Replies

Copy/past multiple lines/splines in 2d sketch

Hi,

 

I'm looking into making a script where I can take a line or spline as input and spread them out equally, and then spread the distances using a function.

 

I'm searching and getting conflicting results, is it possible to use a spline as an input to clone? Is it possible to clone sketch lines programmatically? Obviously there's a script for rectangular and circular, I was just wondering if I could hack the rectangular distribution script, although I can't get the script I've found to work(I've placed after import/def run): https://forums.autodesk.com/t5/fusion-360-api-and-scripts/add-rectangular-patterns-to-the-sketch-via...

Is it even possible in the first place? Before I invest myself in this project!

3 REPLIES 3
Message 2 of 4
kandennti
in reply to: richop88

Hi @richop88 .

 

Although not very practical, I created sample code.

・ Only a single curve can be selected.
・ All cloned sketch curves are NurbsCurve.

# Fusion360API Python script
# SketchCurve RectangularPatternLike sample

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        # set pattern param
        x_count = 5
        y_count = 3
        x_pich = 2.0
        y_pich = 3.0

        # get obj
        app  :adsk.core.Application = adsk.core.Application.get()
        ui   :adsk.core.UserInterface = app.userInterface
        des  :adsk.fusion.Design = app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # Select SketchCurve
        msg :str = 'Select SketchCurve'
        selFiltter :str = 'SketchCurves'
        sel :adsk.core.Selection = selectEnt(ui, msg ,selFiltter)
        if not sel: return

        # get select obj
        crv :adsk.fusion.SketchCurve = sel.entity
        skt :adsk.fusion.Sketch = crv.parentSketch

        # get Vector
        x_vec = skt.xDirection
        y_vec = skt.yDirection

        # get Matrix
        vec3d = adsk.core.Vector3D
        mat3d = adsk.core.Matrix3D
        matLst = []
        for x in range(x_count):
            for y in range(y_count):
                vec = vec3d.create(x_pich * x, y_pich * y, 0.0)
                mat = mat3d.create()
                mat.translation = vec
                matLst.append(mat)
        matLst = matLst[1:]

        # get Geometry info
        geo = crv.worldGeometry
        if hasattr(geo, 'asNurbsCurve'):
            geo = geo.asNurbsCurve

        (returnValue, controlPoints, degree, knots, isRational, weights, isPeriodic) = geo.getData()

        # init sketch curve
        nurbs3d = adsk.core.NurbsCurve3D
        fitCrv = skt.sketchCurves.sketchFittedSplines
        for mat in matLst:
            nurbs = nurbs3d.createRational(controlPoints, degree, knots, weights, isPeriodic)
            nurbs.transformBy(mat)
            fitCrv.addByNurbsCurve(nurbs)
        
        # fin
        ui.messageBox('Done')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def selectEnt(
        ui :adsk.core.UserInterface,
        msg :str, 
        filtterStr :str) -> adsk.core.Selection :

    try:
        sel = ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None
Message 3 of 4
richop88
in reply to: kandennti

Thank you so much 

 

 

 

Message 4 of 4
kandennti
in reply to: richop88

I'm sorry.
Because the confirmation was not enough, the support surface of sketch was not correctly processed other than the XY plane of RootComponent.
In addition, there were many unnecessary codes.
・・・
        # get obj
        app  :adsk.core.Application = adsk.core.Application.get()
        ui   :adsk.core.UserInterface = app.userInterface
        # des  :adsk.fusion.Design = app.activeProduct # unused
        # root :adsk.fusion.Component = des.rootComponent # unused

        # Select SketchCurve
        msg :str = 'Select SketchCurve'
        selFiltter :str = 'SketchCurves'
        sel :adsk.core.Selection = selectEnt(ui, msg ,selFiltter)
        if not sel: return

        # get select obj
        crv :adsk.fusion.SketchCurve = sel.entity
        skt :adsk.fusion.Sketch = crv.parentSketch

        # get Vector
        # x_vec = skt.xDirection # unused
        # y_vec = skt.yDirection # unused

        # get Matrix
        vec3d = adsk.core.Vector3D
        mat3d = adsk.core.Matrix3D
        matLst = []
        for x in range(x_count):
            for y in range(y_count):
                vec = vec3d.create(x_pich * x, y_pich * y, 0.0)
                mat = mat3d.create()
                mat.translation = vec
                matLst.append(mat)
        matLst = matLst[1:]

        # get Geometry info
        # geo = crv.worldGeometry # NG
        geo = crv.geometry
        if hasattr(geo, 'asNurbsCurve'):
            geo = geo.asNurbsCurve
・・・


 


I think that's probably the case.
Please change and enjoy.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report