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: 

pattern inputEntities failing

15 REPLIES 15
Reply
Message 1 of 16
DWhiteley
1028 Views, 15 Replies

pattern inputEntities failing

Can anyone tell me why this fails?

 

         for rectangularPatternFeature in patterns:
            
                              ients = rectangularPatternFeature.inputEntities # this line
15 REPLIES 15
Message 2 of 16
goyals
in reply to: DWhiteley

There could be multiple reasons for failure. May not be the obvious one but you can check if the entry in patterns collection is null for some reason. If not then you can find out the one entry which is leading to failure probably by adding your code in try/catch bock. We usually throw the exception if the API method fails for some reason on our side and catch block would be able to let you know what message you are getting on your side. This might help to know the reason of failure. Thanks.



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 16
DWhiteley
in reply to: goyals

Thanks for your reply. So I made a new model with a hole and patterned the hole.

I ran this simple script and it failed at the .inputEntities line:

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
         
        allComps = design.allComponents
                 
        for comp in allComps:
            
            features = comp.features
            patterns = features.rectangularPatternFeatures
                 
            for rectangularPatternFeature in patterns:
            
                 ient = rectangularPatternFeature.inputEntities # this fails


    except:
        if ui:
           ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 4 of 16
DWhiteley
in reply to: goyals

I would really appreciate an answer on this. It looks like there is a problem with the API.

(I'm trying to get access to the inputEntities for patterns).

 

Dave

Message 5 of 16
goyals
in reply to: DWhiteley

@DWhiteley I am able to notice the problem. I need to check why it is implemented like that but the workaround is just rolled back the timeline just before rectangular pattern feature. I will get back once I am having more information available. Thanks.



Shyam Goyal
Sr. Software Dev. Manager
Message 6 of 16
DWhiteley
in reply to: goyals

Thanks for the feedback. I need this to work for me to finish an app off to integrate F360 with FeatureCAM.
Message 7 of 16
BrianEkins
in reply to: DWhiteley

This failure is as-designed.  You'll see this with most of the features whenever you try to query or edit the entities that were provided as input when the feature was created.  You can still get the entities but you need to roll the timeline back to just before the feature you're querying was created.  This is because the input entities are only guaranteed to exist at that point in time.

 

The best example to illustrate this is the fillet feature.  If you try to query for edges that were input to the fillet feature after the fillet has been created, they can't be returned because they were consumed by the fillet feature and no longer exist.  However, if your roll back the timeline to just before the fillet feature, the edges are now available again.

 

I know you weren't creating a fillet but similar cases can exist with any feature.  In most cases, it's not the feature you created that would consume the inputs, but subsequent features could.  It's also possible that the input entities do still exist, but Fusion doesn't check for that and plays it safe by forcing a roll back.

 

This is the same behavior you see in the UI too.  Whenever you edit a feature, Fusion rolls the timeline back to a point just before that feature existed.  You can now edit the input and then clicking OK, which moves the timeline back to where it was.  Here's how to do it in code.

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        sk = root.sketches.add(root.xYConstructionPlane)
        circle = sk.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), 5)
        prof = sk.profiles.item(0)

        ext = root.features.extrudeFeatures.addSimple(prof, adsk.core.ValueInput.createByString('2 in'), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

        filletInput = root.features.filletFeatures.createInput()
        edges = adsk.core.ObjectCollection.create()
        edges.add(ext.bodies.item(0).edges.item(0))

        filletInput.addConstantRadiusEdgeSet(edges, adsk.core.ValueInput.createByString("0.5 in"), False)
        fillet = root.features.filletFeatures.add(filletInput)

        # Get the current position of the stop node in the timeline.
        position = des.timeline.markerPosition

        # Roll the timeline to before the feature to edit.
        fillet.timelineObject.rollTo(True)

        # Edit the feature
        edgeSet = adsk.fusion.ConstantRadiusFilletEdgeSet.cast(fillet.edgeSets.item(0))
        edges = adsk.core.ObjectCollection.create()
        edges.add(ext.bodies.item(0).edges.item(0))
        edges.add(ext.bodies.item(0).edges.item(1))
        edgeSet.edges = edges

        # Move the node to the original position.
        des.timeline.markerPosition = position
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 8 of 16
DWhiteley
in reply to: BrianEkins

Many thanks for the response @BrianEkins , I'm sure it will also assist anyone who wants to EDIT the feature. However, I do not, I only want to view. At no point do I want to edit the component.

Here's what I'm trying to do and what I have tried so far:

 

I want to access ALL the threaded holes in the component (find their thread data and positions - all thanks to your help). I can do that until a threaded hole is in a pattern.

From the API, I cannot find out if the feature has a pattern as a parent. So I thought that I would iterate through all the rectangular & circular patterns and see if the input entity is a threaded hole (or hole). I cannot do that unless I roll back each and every pattern in the component.

 

Is there no easier way?

 

Also the documentation needs updating:

 

Annotation 2020-06-26 100547.png

Thanks in advance, Dave Whiteley

 

Message 9 of 16
BrianEkins
in reply to: DWhiteley

Currently, there isn't any other way.  You have to roll back to be able to query the inputs.  I don't see that changing in the future but there are a couple of things that would be nice to have in the API that would help.  The first is some control of transactions.  You would still be doing all the roll backs but instead of moving the marker back to where it was you could abort the transaction where you moved the marker to before the feature.  This would do two things; first it wouldn't pollute the undo list and second it would be faster because it would skip the recompute of the model.

 

The other thing that would be nice is something that Inventor supports. No matter how the thread is created, you can get if there is a thread and the thread information directly from the face that has the thread.

 

Unfortunately, neither one of these currently exist.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 10 of 16
DWhiteley
in reply to: BrianEkins

Thanks for your fast response Brian. That’s a real shame. I’ll just have to roll back.
I’m used to the mature Inventor API and probably expecting too much at the moment.

Dave
Message 11 of 16
DWhiteley
in reply to: BrianEkins

Thanks Brian, I can now rollback to before the pattern. So now the last feature is either a hole (that I want) or something else. How do I now access the last feature in the timeline to critigue it?

 

Dave

Message 12 of 16
BrianEkins
in reply to: DWhiteley

I'm not sure why you would need to get the feature just before the current timeline position.  As far as the pattern feature, any feature could have been used as input, not just the feature before the pattern.  You can get the input features directly from the pattern using the inputEntities property of the RectangularPattern object.

 

However, it is possible to get the feature just before the current timeline position too.  The following code should report the name of that feature.

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        timeline = des.timeline
        lastActive = timeline.item(timeline.markerPosition - 1)

        message = ""
        try:
            entity = lastActive.entity
            message = entity.name + ' (' + entity.objectType + ')'
        except:
            message = "Unknown"

        ui.messageBox('Last active node: ' + message)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 13 of 16
DWhiteley
in reply to: BrianEkins

Looks like we're going round in circles here.

The InputEntities are what I wanted in the first place, but that doesn't work. Take a look at the start of this forum trail.

 

Very frustrating....Dave

Message 14 of 16
BrianEkins
in reply to: DWhiteley

Here's a sample that demonstrates the complete workflow.  Select a rectangular pattern and it will get the input entities and list them out in the text command window.  You can see it does this by rolling back the timeline, getting the inputs, and then repositioning back to where it was.

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

_ui = None
def run(context):
    global _ui
    try:
        app = adsk.core.Application.get()
        _ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        sel = _ui.selectEntity('Select rectangular pattern feature.', 'Features')
        pattern = adsk.fusion.RectangularPatternFeature.cast(sel.entity)

        if pattern:
            # Get current position of timeline.'
            timeline = des.timeline
            lastActive = timeline.item(timeline.markerPosition - 1)

            # Roll it to just before the rectangular pattern feature.'
            pattern.timelineObject.rollTo(True)

            # Get the inputs.
            WriteToTextWindow('Input Entities')
            for input in pattern.inputEntities:
                if getattr(input, "name", None):
                    name = ', (' + input.name + ')'
                else:
                    name = ""

                WriteToTextWindow('   ' + input.objectType + name)

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


def WriteToTextWindow(text):
    # Get the palette that represents the TEXT COMMANDS window. 
    textPalette = _ui.palettes.itemById('TextCommands')

    # Make sure the palette is visible. 
    if not textPalette.isVisible: 
        textPalette.isVisible = True

    # Write the text. 
    textPalette.writeText(text)   
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 15 of 16
DWhiteley
in reply to: BrianEkins

It's now all making sense. Many thanks for you guidance and patience Brian.

 

Dave

Message 16 of 16
DWhiteley
in reply to: BrianEkins

Thanks Brian,

I can now get to the input Entities of the pattern.

I want to see if this is a threaded hole & to get the thread details.

However, the 'input entities' gives me a brep face.

My question is how to I determine if the 'input entity' of a pattern is a threaded hole?

 

Many thanks

Dave W

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