Create centerpoint rectangles from sketch centers

Create centerpoint rectangles from sketch centers

Anonymous
Not applicable
1,914 Views
5 Replies
Message 1 of 6

Create centerpoint rectangles from sketch centers

Anonymous
Not applicable

Hey there -

 

New to Fusion scripting, and I am 90% sure I am getting tangled up in object types, but thought this would be a good place to start.

 

I wrote a script already that imports a pile of DXF files separately. Now, what I would like to do is create a rectangle of fixed size around the *individual sketch* origin, not the origin of the entire project.

 

I am having 2 issues:

1) I can't create another point translated from the first for some reason

2) Trying to retrieve the origin of the sketch is proving to be difficult - I keep getting the global origin, *not* the origin on the sketch. I have a demo file attached, and code is below. Thanks for the help!

 

import traceback

import adsk.core
import adsk.fusion


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

        design = app.activeProduct

        cornerTrans = adsk.core.Vector3D.create(24, 24, 0)

        # Get the sketch out of the root component of the active design.
        rootComp = design.rootComponent
        sketches = rootComp.sketches;
        sketch = sketches[0]
        wO = sketch.origin

        xzPlane = rootComp.xZConstructionPlane
        sketchNew = sketches.add(xzPlane)

        sketchPoints = sketchNew.sketchPoints
        lines = sketchNew.sketchCurves.sketchLines;
        workOrigin = sketchPoints.add(wO)

        rC = wO.translateBy(cornerTrans)
        recCorner = sketchPoints.add(rC)
        recLines = lines.addCenterPointRectangle(workOrigin, recCorner)

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

kandennti
Mentor
Mentor

Hi @Anonymous .

 

It is possible that I am not understanding what you want.


The translateBy method of the Point3D object is a destructive method that changes the calling instance itself.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-29830e60-05c0-4c88-8ef7-2bfe7af6bcc1 


The first parameter of the addCenterPointRectangle method is the Point3D object.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-cdc75402-9c1c-4134-bd01-e95eb9c9b8ee 


So if you do it this way, the error will no longer

・・・
        sketchPoints = sketchNew.sketchPoints
        lines = sketchNew.sketchCurves.sketchLines;
        workOrigin = sketchPoints.add(wO)

        # rC = wO.translateBy(cornerTrans)
        rC = wO.copy()
        rC.translateBy(cornerTrans)

        recCorner = sketchPoints.add(rC)
        
        # recLines = lines.addCenterPointRectangle(workOrigin, recCorner)
        recLines = lines.addCenterPointRectangle(workOrigin.geometry, recCorner)
・・・
0 Likes
Message 3 of 6

Anonymous
Not applicable

Thank you for this help! I will check it out on Monday and report back. I think at the least this will solve one of the issues. 

0 Likes
Message 4 of 6

Anonymous
Not applicable

This definitely got a step closer, but is not quite what I am looking for. Here is the goal that I am aiming for:

 

If I select an imported DXF sketch in Fusion that is off the part origin:

1.png

...and then hit "Create sketch"...

 

2.png

 

I am given a sketch plane where its local origin corresponds to the center of the previously selected sketch. If I draw from that point, I can create fully defined sketches:

 

3.png

...the long goal here being I am trying to automate a very repetitive CAM process by importing a bunch of DXFs from a folder, create a center rectangle representing the stock, and then automatically create CAM profile data and export a pile of NC code. 

 

Does that make a bit more sense?

0 Likes
Message 5 of 6

kandennti
Mentor
Mentor
Accepted solution

@Anonymous .

 

In this case, if you don't have this problem, I think it is easy to use BoundingBox3D.

https://modthemachine.typepad.com/my_weblog/2017/06/getting-the-overall-size-of-parts.html 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9ce5053e-72b6-4a47-8172-e4223fb4caff 


Although there is not enough processing, we have created such a sample.

 

 

#Fusion360API Python script
import traceback
import adsk.core
import adsk.fusion

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # create Bounding BoxSketch
        # Default modeling orientation "Y up" only
        createBoundingBoxSketch(root.sketches[0],root.xZConstructionPlane)

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


def createBoundingBoxSketch(
    targetSkt :adsk.fusion.Sketch,
    supportPlane :adsk.core.Base = None
    ) -> adsk.fusion.Sketch:

    # check profile
    if targetSkt.profiles.count < 1:
        return None

    # create new sketch
    comp :adsk.fusion.Component = targetSkt.parentComponent
    skt = adsk.fusion.Sketch.cast(None)
    if supportPlane:
        skt = comp.sketches.add(supportPlane)
    else:
        skt = comp.sketches.add(targetSkt.referencePlane)

    # get boundingBox
    prof :adsk.fusion.Profile
    bound :adsk.core.BoundingBox3D = targetSkt.profiles[0].boundingBox
    for prof in targetSkt.profiles:
        bound.combine(prof.boundingBox)


    # get min/max point
    minPnt :adsk.core.Point3D = bound.minPoint
    maxPnt :adsk.core.Point3D = bound.maxPoint

    # get mid point
    midPnt :adsk.core.Point3D = adsk.core.Point3D.create(
                                (maxPnt.x + minPnt.x) * 0.5,
                                (maxPnt.y + minPnt.y) * 0.5,
                                (maxPnt.z + minPnt.z) * 0.5)

    # create boundingBox
    lines = skt.sketchCurves.sketchLines
    lines.addCenterPointRectangle(midPnt, maxPnt)

    skt.name = f'BoundingBox of {targetSkt.name}'

    return skt

 

 

However, you need to set "Y up" in the Fusion360 preferences to get the correct result.

 

Isn't the attached f3d file created with "Y up"?

1.png

0 Likes
Message 6 of 6

Anonymous
Not applicable

This is great! Yes, this was the enabling bit to get my code to work. Posting here for a little more help in case anyone else needs this. Essentially, I have updated this to iterate through all available sketches, create a fixed size bounding box, and place a point at the lower left bounding box corner to help automate CAM setup creation.

 

Thank you!!

 

#Author-S. Cusack
#Description-CORE: Create Setup CAM Boundaries
#Units are in *CM*, thus the weird scale factors

import traceback
import adsk.core
import adsk.fusion

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # Get number of existing sketches
        sketchLen = root.sketches.count
        i = 0

        # create Bounding BoxSketch
        # Default modeling orientation "Y up" only
        while i < sketchLen:
            createBoundingBoxSketch(root.sketches[i],root.xZConstructionPlane)
            i += 1

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


def createBoundingBoxSketch(
    targetSkt :adsk.fusion.Sketch,
    supportPlane :adsk.core.Base = None
    ) -> adsk.fusion.Sketch:

    # check profile
    if targetSkt.profiles.count < 1:
        return None

    # create new sketch
    comp :adsk.fusion.Component = targetSkt.parentComponent
    skt = adsk.fusion.Sketch.cast(None)
    if supportPlane:
        skt = comp.sketches.add(supportPlane)
    else:
        skt = comp.sketches.add(targetSkt.referencePlane)

    # create sizing vector
    cornerTrans = adsk.core.Vector3D.create(60.96, 60.96, 0)
    cT2 = cornerTrans.copy()
    cT2.scaleBy(-2)

    # get boundingBox
    prof :adsk.fusion.Profile
    bound :adsk.core.BoundingBox3D = targetSkt.profiles[0].boundingBox
    for prof in targetSkt.profiles:
        bound.combine(prof.boundingBox)


    # get min/max point
    minPnt :adsk.core.Point3D = bound.minPoint
    maxPnt :adsk.core.Point3D = bound.maxPoint

    # get mid point
    midPnt :adsk.core.Point3D = adsk.core.Point3D.create(
                                (maxPnt.x + minPnt.x) * 0.5,
                                (maxPnt.y + minPnt.y) * 0.5,
                                (maxPnt.z + minPnt.z) * 0.5)

    # translate midpoint to exterior corner
    rC = midPnt.copy()
    rC.translateBy(cornerTrans)
    
    # create boundingBox
    lines = skt.sketchCurves.sketchLines
    lines.addCenterPointRectangle(midPnt, rC)

    # create boundary corner
    rC.translateBy(cT2)
    sketchPoints = skt.sketchPoints
    sketchPoints.add(rC)

    skt.name = f'Boundary for {targetSkt.name}'

    return skt

1.png

2.png 

0 Likes