Fusion delete my sketch if it is created in a event

Fusion delete my sketch if it is created in a event

maurizio_manzi
Advocate Advocate
89 Views
0 Replies
Message 1 of 1

Fusion delete my sketch if it is created in a event

maurizio_manzi
Advocate
Advocate

Hello,
If I call the function drawVector() for exymple in run, then all is okay.
If I call the function drawVector() in a event, for example in the function:

class MyCommandInputChangedHandler(adsk.core.InputChangedEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(self, args😞

then fusion delete the created sketch immediately if the function (event) ist terminated.

 

 

 

def drawVector():
    global app, ui, xVector, yVector, zVector, language
    ws: adsk.core.Workspace = ui.workspaces.itemById('FusionSolidEnvironment') # Referenz ws to design-Workspace
    if ws:
        ws.activate() # Activate design-Workspace
   
    design = adsk.fusion.Design.cast(app.activeProduct)
    rootComp = design.rootComponent

    # Origin of line (X, Y, Z)
    originX, originY, originZ = 0, 0, 10

    # Vector direction (-1 to +1 for X, Y, Z)
    vectorX, vectorY, vectorZ = xVector, yVector, zVector

   
    # Sketch for creating origin and axis
    sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
    sketch.sketchPoints.add(adsk.core.Point3D.create(originX, originY, originZ))

    # Draw diagonal vector
    draw_axis(sketch, originX, originY, originZ, vectorX, vectorY, vectorZ)

    # Draw an arrowhead (3 circles in decreasing size) at the end of the vector
    draw_vector_end_marker(sketch, originX, originY, originZ, vectorX, vectorY, vectorZ)





def draw_axis(sketch, originX, originY, originZ, offsetX, offsetY, offsetZ, scale=15😞
    startPoint = adsk.core.Point3D.create(originX, originY, originZ)
    endPoint = adsk.core.Point3D.create(
        originX + offsetX * scale,
        originY + offsetY * scale,
        originZ + offsetZ * scale
    )
    sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)



def draw_vector_end_marker(sketch, originX, originY, originZ, offsetX, offsetY, offsetZ, scale=15, radius_start=1, num_circles=5😞
    # Calculate the length of the vector
    vector_length = (offsetX**2 + offsetY**2 + offsetZ**2)**0.5

    # Calculate the direction of the vector and normalize it
    unit_vectorX = offsetX / vector_length
    unit_vectorY = offsetY / vector_length
    unit_vectorZ = offsetZ / vector_length

    # Calculate the end of the vector
    endX = originX + offsetX * scale
    endY = originY + offsetY * scale
    endZ = originZ + offsetZ * scale

    # Offset for the circles (shift smaller circles upward along the vector)
    vertical_offset = 1

    # Draw the circles in decreasing size and offset along the vector
    for i in range(num_circles):
        # Calculate the position of the circle along the vector
        circle_offset = vertical_offset * (i + 0.3)
       
        # Calculate the new radius for the circle
        radius = radius_start * (1 - 0.2 * i)  # The radius decreases by 20% for each circle
       
        # Calculate the offset point along the vector
        circle_centerX = endX + unit_vectorX * circle_offset
        circle_centerY = endY + unit_vectorY * circle_offset
        circle_centerZ = endZ + unit_vectorZ * circle_offset
       
        # Draw the circle
        circle_center = adsk.core.Point3D.create(circle_centerX, circle_centerY, circle_centerZ)
        circle = sketch.sketchCurves.sketchCircles.addByCenterRadius(circle_center, radius)



0 Likes
90 Views
0 Replies
Replies (0)