Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Extrude some profiles

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
dinocoglitore
1587 Views, 4 Replies

Extrude some profiles

Hi 

After an hard work and many  attempts I wrote a Python script to draw the sketch attached.

Now I would like to continue writing the script so that I can  extrude the pofiles as in the second file attached. I would like programmatically select the triangles but I have some problem because I'm a novice on Api and Python.

 

Someone can help me and give me a cue or idea ?

Thanks 

Dino

 

 

4 REPLIES 4
Message 2 of 5
liujac
in reply to: dinocoglitore

Hi,

 

You can filter profiles with provided sketch entities. The script below demo how to find sketch profiles with one provided sketch curve. Take your picture for example, you can run the script and select the outer sketch circle, the expected profiles will be selected.

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

ui = None
def findProfiles(containedCurve):
    sketchCurve = adsk.fusion.SketchCurve.cast(containedCurve)
    sketch = sketchCurve.parentSketch
    profiles = sketch.profiles
    for profile in profiles:
        selected = False
        loops = profile.profileLoops
        for loop in loops:
            if selected == True:
                break
            profileCurves = loop.profileCurves
            for profileCurve in profileCurves:
                if profileCurve.sketchEntity == containedCurve:
                    ui.activeSelections.add(profile)
                    selected = True
                    break;

def run(context):
    try:
        app = adsk.core.Application.get()
        global ui
        ui  = app.userInterface
        
        curve = ui.selectEntity('Select a sketch curve', 'SketchCurves').entity
        ui.activeSelections.clear()
        findProfiles(curve)
        
        ui.messageBox(str(ui.activeSelections.count))

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

 

Thanks,

Jack

Message 3 of 5
dinocoglitore
in reply to: liujac

Hi Jack, 

I thank you very much for your answer. That is almost what i'm looking for.

 

Your code will let me better  understand the mysteries of Fusion Api.

It will take me long time to understand the complexity of your code.

But it will be useful for my knowledge.  

 

Can you spend few words to explain the three for loop inside your code ?

 

I attached a picture to let you see what append when I use your code on a inner circle and in another attach what I expect to be from ....my future code....

Instead of selecting a curve may I select a triangle ?  

 

I thank you for your time 

Dino

Message 4 of 5
liujac
in reply to: dinocoglitore

Hi,

 

The first loop is to visit all the profiles on the sketch. The second loop is to visit all the profile loops of one profile. The third loop is to visit all the profile curves of one profile loop. The intention is to check if the input sketch curve is contained in the profiles curves of the profile, if yes, select the profile.

 

I add a new function ‘isInsideCircle’ in the script to check if the profile is inside the circle. That would be better to match your requirements.

 

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

near_zero = 0.000001
ui = None
#If all points in the profile are inside the circle, then the profile is inside the circle.
def isInsideCircle(profile, sketchCircle):
    # Get center and radius from sketch circle
    circle = sketchCircle.geometry
    center = circle.center
    radius = circle.radius
    
    loops = profile.profileLoops
    for loop in loops:
        profileCurves = loop.profileCurves
        for profileCurve in profileCurves:
            #Get the start point and end point
            curve = profileCurve.geometry
            eva = curve.evaluator
            (retVal, startPoint, endPoint) = eva.getEndPoints()
            
            #Check if the points are outside the circle
            if startPoint.distanceTo(center) - radius > near_zero:
                return False
            if endPoint.distanceTo(center) - radius > near_zero:
                return False
    return True
            
    
def findProfiles(containedCurve):
    sketchCurve = adsk.fusion.SketchCurve.cast(containedCurve)
    sketch = sketchCurve.parentSketch
    profiles = sketch.profiles
    for profile in profiles:
        found = False
        loops = profile.profileLoops
        for loop in loops:
            if found:
                break
            profileCurves = loop.profileCurves
            for profileCurve in profileCurves:
                if profileCurve.sketchEntity == containedCurve:
                    # Check if the profile is inside the circle
                    sketchCircle = adsk.fusion.SketchCircle.cast(containedCurve)
                    if sketchCircle:
                        if isInsideCircle(profile, sketchCircle):
                            ui.activeSelections.add(profile)
                    else:
                        ui.activeSelections.add(profile)
                    found = True
                    break;

def run(context):
    try:
        app = adsk.core.Application.get()
        global ui
        ui  = app.userInterface
        
        curve = ui.selectEntity('Select a sketch curve', 'SketchCurves').entity
        ui.activeSelections.clear()
        findProfiles(curve)
        
        ui.messageBox(str(ui.activeSelections.count))

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

About your another question, the triangle is a profile. You can add the filter to select profiles. 

ui.selectEntity('Select a sketch curve', 'SketchCurves,Profiles')

Thanks,

Jack

 

Message 5 of 5
dinocoglitore
in reply to: liujac

Hi Jack, you have solved the problem. 

Now my proble is understand what have you wrote.

Thank you very much. I'm learning a lot.

Dino

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report