Struggling to get script-generated sketch to line up with existing sketch in design

Struggling to get script-generated sketch to line up with existing sketch in design

darrenPH8TL
Explorer Explorer
1,351 Views
7 Replies
Message 1 of 8

Struggling to get script-generated sketch to line up with existing sketch in design

darrenPH8TL
Explorer
Explorer

I'm trying to create my first script. I've made it as far as creating a sketch with two nested, constrained rectangles in it. Now I want that sketch to line up with an existing sketch.

 

I can get it on the same plane, but the coordinate system is rotated vs what I expect.  The bottom sketch's geometry is supposed to be in the positive XY quadrant, but as you can see it gets inserted rotated.  This is a clean test design with a sketch with one rectangle on it.

 

How can I copy the existing sketch's origin, size and coordinate system?  Ideally I want to pick any existing sketch and place my generated sketch and its geometry on the same plane, or on an offset plane directly "above" the existing plane and sized exactly the same. It's my intent to generate patterned geometry to intersect and cut the existing geometry.

 

        # Create a new sketch.
        thisComp = design.rootComponent
        sketches = thisComp.sketches

        masterSketch = sketches.item(0)

        # Get the profile
        prof = masterSketch.profiles.item(0)

        # Get construction planes
        planes = thisComp.constructionPlanes
        
        # Create construction plane input
        planeInput = planes.createInput()
        
        # Add construction plane by offset
        offsetValue = adsk.core.ValueInput.createByReal(0)
        planeInput.setByOffset(prof, offsetValue)
        planeOne = planes.add(planeInput)


        sketch = sketches.add(planeOne)

 

fusion360.png

0 Likes
Accepted solutions (1)
1,352 Views
7 Replies
Replies (7)
Message 2 of 8

BrianEkins
Mentor
Mentor

I'm not following exactly what you're trying to do, but hopefully, this will help.

 

Each sketch has its coordinate system, and all the geometry created within it is relative to that coordinate system. Fusion doesn't give you any control over the position or orientation of a sketch. Sketches are created by selecting a construction plane or planar face. Fusion has some internal algorithm to create the sketch and keep it associatively linked to the selected construction plane or face.

 

There are some functions on the Sketch object that let you get information about the sketch space and go between sketch and model space. These are modelToSketchSpace, sketchToModelSpace, transform, xDirection, and yDirection.

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 8

darrenPH8TL
Explorer
Explorer

What I am trying to do is pretty simple. I want to programmatically generate some geometry and place it in its own sketch that is aligned with an existing sketch. Then I want to use that geometry as a cutting tool. 

 

Specifically, I am creating my own custom patterns to cut the vertical sides of 3D printed objects. The patterns are self-supporting so that no support material is needed. The purpose is for creating lighter parts that require less filament to make, and thus cost less to produce and print faster.

 

I can achieve the result manually by drawing in Fusion and then repeating as a pattern, but it is very tedious. Doing it in a script will save me countless hours and allow me to quickly iterate through different options.

0 Likes
Message 4 of 8

kandennti
Mentor
Mentor
Accepted solution

Hi @darrenPH8TL -San.

 

Is this what it's like?

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        msg: str = 'Select Sketch'
        selFilter: str = "Sketches,SketchCurves,SketchPoints"
        sel: core.Selection = select_ent(msg, selFilter)
        if not sel:
            return

        create_clone_sketch(sel.entity)

        ui.messageBox('Done')

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


def create_clone_sketch(
        sktEntity: fusion.SketchEntity
) -> fusion.Sketch:

    skt: fusion.Sketch = None
    if sktEntity.classType() == fusion.Sketch.classType():
        skt = sktEntity
    else:
        skt = sktEntity.parentSketch

    comp: fusion.Component = skt.parentComponent
    refPlane = skt.referencePlane

    # get sketch entities
    sktEnts: core.ObjectCollection = core.ObjectCollection.createWithArray(
        list(skt.sketchCurves),
    )
    [sktEnts.add(pnt) for pnt in skt.sketchPoints]

    # create offset plane
    planes: fusion.ConstructionPlanes = comp.constructionPlanes
    planeIpt: fusion.ConstructionPlaneInput = planes.createInput()
    planeIpt.setByOffset(
        refPlane,
        core.ValueInput.createByReal(0),
    )
    offsetPlane: fusion.ConstructionPlane = planes.add(planeIpt)

    # create sketch
    cloneSkt: fusion.Sketch = comp.sketches.add(offsetPlane)

    # copy
    cloneSkt.copy(
        sktEnts,
        core.Matrix3D.create(),
    )

    return cloneSkt


def select_ent(
        msg: str,
        filterStr: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filterStr)
        return sel
    except:
        return None
0 Likes
Message 5 of 8

darrenPH8TL
Explorer
Explorer

That's getting there.  As soon as I call design.rootComponent.sketches.add(clonedSketch), the add call throws an exception: "3 : not a planar entity".


But what's really odd is that even with the exception, the cloned sketch is indeed added to the design and matches the position and dimension of the original.  

0 Likes
Message 6 of 8

kandennti
Mentor
Mentor

@darrenPH8TL -San.

 

We have not been able to reproduce such a condition.
It may be an inherent problem with the data you are trying.

0 Likes
Message 7 of 8

darrenPH8TL
Explorer
Explorer

I can make up a package to send to you with the script and the design. The sample design file is a single sketch with a single rectangle in it. Where would I upload that?

0 Likes
Message 8 of 8

darrenPH8TL
Explorer
Explorer

After some more investigation, the clone_sketch function provided is already inserting the cloned sketch into the design,which I didn't realize until I read the code. So my code was trying to insert it a second time.  I took out my code to add it a second time and now it's good. No idea why that throws a "non planar entity" exception. :shrug:

 

Now I just need to access some parameters from the design file so I can use them in my script. They appear to be in Design->allParameters.