Help with generating threads via existing cylindrical geometry

Help with generating threads via existing cylindrical geometry

Six1Seven1
Advocate Advocate
751 Views
5 Replies
Message 1 of 6

Help with generating threads via existing cylindrical geometry

Six1Seven1
Advocate
Advocate

Hi, Back again...

 

I was wondering if anyone could help me out with generating thread profiles via the thread features object. My program allows the user to generate a cylindrical face (which I hope to add threads).

 

More specifically, I have 4 options for thread standards (Unified) and I am not interested in including the entire library of fusion threads into my addin, only the 4 specific ones that I have (not sure if you need that info or not).

 

I saw the thread sample program but i am a bit confused. I saw that the programmer referenced a thread type property, but I cannot find the list for each thread profile corresponding to the types. (I am still learning how to nav through the API object manual).

 

I have attached my program, which is quite messy (my apologies). If I could even get an small sample program of adding threads to a cylindrical extrusion, that would be all I need.

 

Any help that can be provided is much appreciated.

 

Thank you

0 Likes
752 Views
5 Replies
Replies (5)
Message 2 of 6

ekinsb
Alumni
Alumni

I don't know if you've seen this or not, but I think it should answer your questions.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-796D74F1-8DE9-43D9-84A5-6491A03D4574

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 6

Six1Seven1
Advocate
Advocate

Brian,

 

I am stuck with a problem. I am using the documentation you linked me to. Here is my problem...

 

I create the cylindrical extrusion in one function, then create threads in another. (this is for organization). Problem is, i don't know how to get access to the extrusion in the thread function.

 

For instance...

 

#Call the function-----------------------

 

createCylinder()

 

createThreads()

 

#Define functions-----------------------

 

def createCylinder():

     #creates a cylindrical extrusion

 

    prof1 = sketch3.profiles.item(0)
    extrude1 = rootComp.features.extrudeFeatures
    extrude1_prof = extrude1.createInput(prof1,adsk.fusion.FeatureOperations.NewComponentFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(length_bod - thread_len_body)
    extrude1_prof.setDistanceExtent(False, distance)
    finish_extrude1 = extrude1.add(extrude1_prof)

 

def createThreads():

      .....creates threads 

 

           but i don't know how to pull in the faces data from the previous extrusion. Do I need to return my extrusion to the main program? I tried that but couldn't get it working.

 

--------------------------------------------

 

Furthermore, here is the code in the thread feature example...

 

# get the face the thread will be applied to
        sideface = extrude.sideFaces.item(0)
        faces = adsk.core.ObjectCollection.create()
        faces.add(sideface)

So, my question is, how do I bring in my extrusion from the other function (createCylinder) to be used in this function?

 

 

 

Thank you, much appreciated. 

0 Likes
Message 4 of 6

ekinsb
Alumni
Alumni

Here's an example where I broke out the creation of the extrusion into a separate function and then the creation of thread into another function and call them.  How you'll create the cylinders in your case will be different to solve what you need but this demonstrates getting the geometry needed (the side cylindrical face in this case) and returning it from the drawCylinder function.  The createThread then takes this as input and draws a thread.

 

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # get the design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # get the root component of the active design.
        rootComp = design.rootComponent

        cylinderFace = drawCylinder(rootComp, rootComp.xYConstructionPlane, 5, 8, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) 
        if cylinderFace:
            createThread(rootComp, cylinderFace)
        
        cylinderFace = drawCylinder(rootComp, rootComp.xZConstructionPlane, 7, 10, adsk.fusion.FeatureOperations.JoinFeatureOperation)       
        if cylinderFace:
            createThread(rootComp, cylinderFace)

        cylinderFace = drawCylinder(rootComp, rootComp.yZConstructionPlane, 8, 7, adsk.fusion.FeatureOperations.JoinFeatureOperation)       
        if cylinderFace:
            createThread(rootComp, cylinderFace)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def createThread(comp, cylinderFace):
    import math
    try:
        # define all of the thread information.
        threadFeatures = adsk.fusion.ThreadFeatures.cast(comp.features.threadFeatures)
        
        # query the thread table to get the thread information
        threadDataQuery = threadFeatures.threadDataQuery
        threadTypes = threadDataQuery.allThreadTypes
        threadType = threadTypes[0]
        
        allSizes = threadDataQuery.allSizes(threadType)
        
        # Find the thread size closest to the cylinder size.
        cylinderRadius = cylinderFace.geometry.radius
        des = adsk.fusion.Design.cast(comp.parentDesign)
        uom = des.unitsManager
        
        difference = 50000
        bestSize = ''
        for currentSize in allSizes:
            size = uom.evaluateExpression(currentSize, 'in')
            if math.fabs(cylinderRadius - size) < difference:
                difference = math.fabs(cylinderRadius - size)
                bestSize = currentSize
                
        threadSize = bestSize
        
        allDesignations = threadDataQuery.allDesignations(threadType, threadSize)
        threadDesignation = allDesignations[0]
        
        allClasses = threadDataQuery.allClasses(False, threadType, threadDesignation)
        threadClass = allClasses[0]
        
        # create the threadInfo according to the query result
        threadInfo = threadFeatures.createThreadInfo(False, threadType, threadDesignation, threadClass)
        
        # get the face the thread will be applied to
        faces = adsk.core.ObjectCollection.create()
        faces.add(cylinderFace)
        
        # define the thread input with the lenght 3.5 cm
        threadInput = threadFeatures.createInput(faces, threadInfo)
        threadInput.isFullLength = True
        
        # create the thread feature.
        thread = threadFeatures.add(threadInput)
        
        return thread
    except:
        return None
        

def drawCylinder(comp, plane, radius, length, operation):
    try:
        # create a new sketch on the xy plane.
        sketch = comp.sketches.add(plane)
        
        # create a sketch circle
        sketchCircles = sketch.sketchCurves.sketchCircles
        sketchCircle = sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), radius)
        
        # get the profile defined by the circle.
        prof = sketch.profiles.item(0)
        
        # create extrude input
        extrudes = comp.features.extrudeFeatures
        extrudeInput = extrudes.createInput(prof, operation)
        
        # define that the extent is a distance extent of the input length
        extrudeInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(length))
        
        # extrude the circle to create a cylinder
        extrude = extrudes.add(extrudeInput)
        
        # Get the cylindrical face that was created and return it.
        if extrude.sideFaces.count > 0:
            cylinder = extrude.sideFaces.item(0)
            return cylinder        
        else:
            return None
    except:
        return None

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 6

Six1Seven1
Advocate
Advocate

Hi Brian,

 

So I tried implementing your method into my program. Needless to say, I am not getting any thread generation. Although, I'm not getting any errors.

 

To some degree, I can wrap my head around what is going on in your method that you provided, which is passing in arguments from the drawCylinder to the drawThread. Although, this is quite a lot to digest and fully comprehend.

 

I have attached my code. If you cannot get to looking it over, no worries, as I wouldn't expect you to want to decipher through my entire jumbled program.

 

But to clear things up about my program, here is how I have the section of interest written:

1. User choses Cylindrical profile

2. User specifies various parameters. Body length being one of them.

3. User has the option to select "Add Body Threads"

 

     If user does not select "Add Body Threads" a cylinder at the specified body length is created

 

     if user DOES select "Add Body Threads" a cylinder is drawn at the specified body length MINUS the thread length

     Then, another cylinder is drawn on top of that cylinder at the specified thread length and at a diameter that is defined by the user based upon their thread choice

 

Therefore, the user has the option to select a body length that remains constant regardless of adding threads to the body. I.E., if the user select "8" as a body length then choses to add "2" of threads, a cylinder of "6" is generated, then a cylinder of "2" on top of it at a different diameter (depending on thread size).

 

Anyhow, like I said, I don't really expect you to overlook this issue because I still need to increase my programming experience before diving into a project like this. But if you're feeling adventurous, feel free.

 

 

Thank you for your time, it is greatly appreciated.

Regards

0 Likes
Message 6 of 6

Six1Seven1
Advocate
Advocate

Brian,

 

Upon further inspection, I have managed to get thread generation with your help. However, something really fishy is happening.

 

1) When executing the Add-in, it is creating 2 identical documents (?)

2) After executing Add-in and choosing to add threads, the threads show in the history, but they are not on the actual model. UNTIL, the add-in is generated again, then they pop up. Very strange.

 

Notes:

1) This may have to do with the fact that the cylindrical extrusions which serves as the thread O.D. is not created as a body. It has no parent component. It is just.. there.

2) This also may have to do with the 'best size' calculation loop in the program. This is being designed first as an imperial unit addin, then later will have metric. So I need to be working in standard units. 

3) I would like to choose the exact thread going on the cylindrical body. I need to research how to do this more.

4) There are some other functions and things in the program that are not being called (work-in-progress), just ignore them. 

 

 

Attached is my program. Its beyond my ability to figure out the issues above. If you could take a glance, that would be great.

 

As always,

Thank you

0 Likes