rotating sketch that is on another plane around ConstractionPlaneAxis

rotating sketch that is on another plane around ConstractionPlaneAxis

adminTCYL2
Enthusiast Enthusiast
812 Views
5 Replies
Message 1 of 6

rotating sketch that is on another plane around ConstractionPlaneAxis

adminTCYL2
Enthusiast
Enthusiast

On the end of a path sits a plane with a circle: 

adminTCYL2_2-1625680168749.png

 

Now I want to rotate and copy the circle around its basis, so it looks like flowers growing from one Point. 
Doing it by hand is no problem:

adminTCYL2_3-1625680202012.png

But in my api-Script only the lines get rotated right. The circles rotate around their plane. This is not what I want. 
How can I fix it?
(PS: Workarounds like rotating just the line and draw new planes or the use of pattern feature do not help me, because this ist just a simplified example of a much more complex script where I need to rotate a sketch that way.)

adminTCYL2_4-1625680523604.png

 

import adsk.coreadsk.fusionadsk.camtracebackmath

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        root_comp = design.rootComponent

        # Create a 3 identical lines.
        sketches = root_comp.sketches
        xyPlane = root_comp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        line = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(000), adsk.core.Point3D.create(022))
        line = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(000), adsk.core.Point3D.create(022))
        line = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(000), adsk.core.Point3D.create(022))

        # create plane on the end of the line
        path1 = root_comp.features.createPath(line)
        planes = root_comp.constructionPlanes
        planeInput = planes.createInput()
        one = adsk.core.ValueInput.createByReal(1)
        planeInput.setByDistanceOnPath(path1one)
        plane = planes.add(planeInput)

        # create 3 identical circles on the plane
        sketch1 = sketches.add(plane)
        circle = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), 1)
        circle = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), 1)   
        circle = sketch1.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), 1)   

        # Try to rotate two circles around root_comp.constructionPlane axis (does not work)
        sketch = root_comp.sketches.item(0)
        sketch1 = root_comp.sketches.item(1)
        objects1 = adsk.core.ObjectCollection.create()
        objects1.add(sketch.sketchCurves.item(0))  #that's the line
        objects1.add(sketch1.sketchCurves.item(0)) #that's the circle
        objects2 = adsk.core.ObjectCollection.create()
        objects2.add(sketch.sketchCurves.item(1))  #that's another line
        objects2.add(sketch1.sketchCurves.item(1)) #that's another circle

        normal =  adsk.core.Vector3D.create(0,1,0)   #sketch.xDirection.crossProduct(sketch.yDirection)
        normal.transformBy(sketch.transform)
        origin = sketch.origin
        origin.transformBy(sketch.transform)
        matrix1 = adsk.core.Matrix3D.create()
        matrix2 = adsk.core.Matrix3D.create()
        matrix1.setToRotation( 2*math.pi/3normalorigin)
        matrix2.setToRotation(-2*math.pi/3normalorigin)
        sketch.move(objects1matrix1)
        sketch.move(objects2matrix2)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
813 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @adminTCYL2 .

 

I think it's because the Line and Circle are drawn on different sketches and "sketch.move" is used to move them, so the result is not what I want.

 

I tried various Matrix3D calculations, but my mind got confused and I gave up...

 

Instead, I created Lines and Circles in the same sketch and used the Copy method.

# Fusion360API Python script
import adsk.core, adsk.fusion, adsk.cam, traceback, math

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        root_comp = design.rootComponent

        # create Skecth
        sketches = root_comp.sketches
        xyPlane = root_comp.xYConstructionPlane
        sketch :adsk.fusion.Sketch  = sketches.add(xyPlane)
        sktCrvs :adsk.fusion.SketchCurves = sketch.sketchCurves

        # create Line
        line :adsk.fusion.SketchLine = sktCrvs.sketchLines.addByTwoPoints(
            adsk.core.Point3D.create(0, 0, 0),
            adsk.core.Point3D.create(0, 2, 2)
        )

        # create Circle
        circle :adsk.fusion.SketchCircle = sktCrvs.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(0,0,0),
            1
        )

        # create Plane
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9fdb167b-77dd-457c-a1be-2df1ea7e1c29
        vec :adsk.core.Vector3D = line.startSketchPoint.geometry.vectorTo(line.endSketchPoint.geometry)
        vec.normalize()
        plane :adsk.core.Plane = adsk.core.Plane.create(line.endSketchPoint.geometry, vec)

        # get Matrix
        mat :adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        mat.setWithCoordinateSystem(
            line.endSketchPoint.geometry,
            plane.uDirection,
            plane.vDirection,
            vec
        )

        # move Circle
        objs :adsk.core.ObjectCollection = adsk.core.ObjectCollection.create()
        objs.add(circle)
        sketch.move(objs, mat)


        # get Matrix
        normal =  adsk.core.Vector3D.create(0,1,0)
        normal.transformBy(sketch.transform)
        origin = sketch.origin
        origin.transformBy(sketch.transform)
        matrix1 = adsk.core.Matrix3D.create()
        matrix2 = adsk.core.Matrix3D.create()
        matrix1.setToRotation( 2*math.pi/3, normal, origin)
        matrix2.setToRotation(-2*math.pi/3, normal, origin)

        # create clone Line Circle
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-899a5797-8bc8-4060-9321-5f81697e0dec
        objs.clear()
        objs.add(line)
        objs.add(circle)

        for mat in [matrix1, matrix2]:
            sketch.copy(
                objs,
                mat)

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

1.png

 

0 Likes
Message 3 of 6

adminTCYL2
Enthusiast
Enthusiast

I found a work-around ... but it does not satisfy me, because it would be much afford, if the sketch on the plane is not just a simple circle. The Idea is to construct three points of the circle, transform them into worldGeometry and construct the circle in worldGeometry not on the plane. Now it is rotateable. You can see the three points in the sketch: 
This helped me: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-do-i-create-circular-pattern-in-sketch...

adminTCYL2_2-1625733297154.png

 

 

import adsk.coreadsk.fusionadsk.camtracebackmath
def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        root_comp = design.rootComponent

        # Create a line in three dimensions.
        sketches = root_comp.sketches
        xyPlane = root_comp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        line = sketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(000), adsk.core.Point3D.create(022))

        # create plane on the end of the line
        path1 = root_comp.features.createPath(line)
        planes = root_comp.constructionPlanes
        planeInput = planes.createInput()
        one = adsk.core.ValueInput.createByReal(1)
        planeInput.setByDistanceOnPath(path1one)
        plane = planes.add(planeInput)

        # create 3 Points on the plane, transform them into worldGeometry and 
        # sketch a circle trough, witch is in WorldGeometry and not on the plane.
        sketch1 = sketches.add(plane)
        p1 = sketch1.sketchPoints.add(adsk.core.Point3D.create(01,0))
        p2 = sketch1.sketchPoints.add(adsk.core.Point3D.create(0,-1,0))
        p3 = sketch1.sketchPoints.add(adsk.core.Point3D.create(10,0))
        p1a = p1.worldGeometry
        p2a = p2.worldGeometry
        p3a = p3.worldGeometry  
        circle = sketch.sketchCurves.sketchCircles.addByThreePoints(p1a,p2a,p3a)    
        
        # Rotate all two times
        sketch = root_comp.sketches.item(0)
        objects = adsk.core.ObjectCollection.create()
        objects.add(sketch.sketchCurves.item(0))  #that's the line
        objects.add(sketch.sketchCurves.item(1))  #that's the circle
        normal =  adsk.core.Vector3D.create(0,1,0)#that's the axis 
        normal.transformBy(sketch.transform)
        origin = sketch.origin
        origin.transformBy(sketch.transform)
        matrix = adsk.core.Matrix3D.create()
        for i in range(13😞
            matrix.setToRotation(2*math.pi/3 * inormalorigin)
            sketch.copy(objectsmatrix)
        # #if just one rotation is needed use this instead:
        # matrix.setToRotation( 2*math.pi/3, normal, origin)
        # sketch.copy(objects, matrix) # or sketch.move(objects, matrix)
   
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Message 4 of 6

adminTCYL2
Enthusiast
Enthusiast

Is there someone who realy knows how to rotate sketches in worldGeometry that are on other planes?
I mean: There must be a way, because I can do it in Fusion by hand. So why not in the api?
Rotations allways should use the geometry of the axes not of the sketches. Otherwise rotations lead to unexpected results. Rotate-feature in the api is not programmed user-friendly. 

0 Likes
Message 5 of 6

adminTCYL2
Enthusiast
Enthusiast

Thank you Kandennti, that is a nice pice of code. I will have to play around with it, before I fully understand what is going on. As far as I see you create the circle in the center of the xy-plane and somehow it gets up to the end of the lines by the roation?
Good to hear, that your mind got confused... so I am not the only one 🙂

0 Likes
Message 6 of 6

kandennti
Mentor
Mentor
0 Likes