how to draw polyline and transform lines. I want to draw Hexagon,

nkc.mech
Explorer

how to draw polyline and transform lines. I want to draw Hexagon,

nkc.mech
Explorer
Explorer

I want to draw Hexagon, & follow step:

1. draw line

2. Copy line

3. translate line by line length

4. rotate by 60 deg

 

0 Likes
Reply
Accepted solutions (1)
787 Views
1 Reply
Reply (1)

kandennti
Mentor
Mentor
Accepted solution

Hi @nkc.mech .

 

Why don't you calculate the vertex coordinates of the polygon and draw a line between the two points?

 

 

#Fusion360API Python Script

import adsk.core, adsk.fusion, traceback
import math

# Circumscribed circle center XY - unit Cm
_center = [1.0, 2.0]

# Circumscribed circle radius - unit Cm
_radius = 5.0

# Vertex count
_vertex_count = 6

# Vertex start angle - unit deg
_ang = 30

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        # get Vertexs
        pnts = getVertexPoints()

        # sketch
        skt = root.sketches.add(root.xYConstructionPlane)

        # draw Polyline
        drawPolyline(skt, pnts)

        # finish
        ui.messageBox('done')

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

def drawPolyline(
    skt :adsk.fusion.Sketch,
    pnts :list):

    count = len(pnts)
    pnts.append(pnts[0])

    lines = skt.sketchCurves.sketchLines

    skt.isComputeDeferred = True
    [lines.addByTwoPoints(pnts[i], pnts[i + 1]) for i in range(count)]
    skt.isComputeDeferred = False

def getVertexPoints() -> adsk.core.Point3D:

    global _center, _radius, _vertex_count, _ang
    rad = math.radians(_ang)
    unit_rad = math.pi * 2 / _vertex_count
    rads = [rad + (unit_rad * idx) for idx in range(1, _vertex_count + 1)]

    pnt3D = adsk.core.Point3D
    pnts = [pnt3D.create(math.cos(rad) * _radius, math.sin(rad) * _radius, 0) 
        for rad in rads]

    targetPnt = pnt3D.create(_center[0], _center[1], 0)
    vec = pnt3D.create(0,0,0).vectorTo(targetPnt)

    [pnt.translateBy(vec) for pnt in pnts]
    return pnts
2 Likes