Extrude some profiles

Extrude some profiles

dinocoglitore
Advocate Advocate
2,621 Views
4 Replies
Message 1 of 5

Extrude some profiles

dinocoglitore
Advocate
Advocate

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

 

 

0 Likes
Accepted solutions (2)
2,622 Views
4 Replies
Replies (4)
Message 2 of 5

liujac
Alumni
Alumni

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

0 Likes
Message 3 of 5

dinocoglitore
Advocate
Advocate

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

0 Likes
Message 4 of 5

liujac
Alumni
Alumni
Accepted solution

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
Advocate
Advocate
Accepted solution

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

0 Likes