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: 

Sketch from dxf not in timeline, using import manager example

6 REPLIES 6
Reply
Message 1 of 7
metd01567
481 Views, 6 Replies

Sketch from dxf not in timeline, using import manager example

I cut down the import manager example to import a dxf file.  The sketch appears in the object browser, but the sketch does not show up in the timeline.  Is this correct?  Maybe it's non-parametric in some way?

 

For my first pass, I created a subcomponent and tried to insert the sketch into it, but that gave me errors at the "importToTarget" step.  That's what I really want to do.  And I'm able to do that through the user interface.

 

Note I'm on a mac, the dxf file path shown below is from the example.  I've tried a few different .dxf files, always the same.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Get import manager
        importManager = app.importManager
        
        # Get active design
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # Get root component
        rootComp = design.rootComponent
        
        # Get dxf import options
        dxfFileName = 'C:\\APISampleImportNeutralDxf2D.dxf'
        dxfOptions = importManager.createDXF2DImportOptions(dxfFileName, rootComp.xZConstructionPlane)
        dxfOptions.isViewFit = False
        
        # Import dxf file to root component
        importManager.importToTarget(dxfOptions, rootComp)        
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
6 REPLIES 6
Message 2 of 7
goyals
in reply to: metd01567

I just tried that but did not notice any problem. If possible please share the complete script where you are getting problem. Sketch is also displayed in timeline unless it is consumed by a feature. For example in case of Box command, sketch is consumed by box feature hence not displayed in timeline but otherwise sketch is always displayed in timeline.



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 7
metd01567
in reply to: goyals

Thanks, good to know that it should work.  Most likely my second script will work for you too, but we should give it a try.  I've pasted in a simplified version of the second script below, and attached one of the .dxf files that I've tried.

 

The problem shows up before anything else is done.  I start with an empty design, run the "insertDxf" script from my previous post, and the inserted sketch does not appear in the timeline.  Here's a screen shot that shows the empty timeline:

Screen Shot 2019-04-29 at 6.45.06 AM.png

 

# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 07:56:27 2018

@author: metd01567
"""

import adsk.core, adsk.fusion, traceback

# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []

# Event handler that reacts to when the command is destroyed. This terminates the script.
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            app = adsk.core.Application.get()
            ui = app.userInterface
            # When the command is done, terminate the script
            # This will release all globals which will remove all event handlers
            adsk.terminate()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Event handler that reacts when the command definition is executed which
# results in the command being created and this event being fired.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            # check workspace, must be in Model
            app = adsk.core.Application.get()
            ui = app.userInterface
            product = app.activeProduct
            design = adsk.fusion.Design.cast(product)
            if not design:
                ui.messageBox('This script is not supported in current workspace, please change to MODEL workspace and try again.')
                return False

            # Get the command that was created.
            cmd = adsk.core.Command.cast(args.command)

            ###################################
            # create and register event handlers
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)

            ###################################
            # add controls

            # Create a selection input for sweep profiles
            profileSelectionInput = cmd.commandInputs.addSelectionInput('profileSelection', 'Profiles', 'The selected profiles should intersect x,y: 0,0, they will be swept along the selected path')
            profileSelectionInput.setSelectionLimits(1)
            profileSelectionInput.addSelectionFilter("Profiles")

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

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

        # Get the existing command definition or create it if it doesn't already exist.
        cmdDef = ui.commandDefinitions.itemById('simpleSweep')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('simpleSweep', 'Simple Sweep', 'Command to sweep a profile along a selected path.')

        # Connect to the command created event.
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        # Execute the command definition.
        cmdDef.execute()

        # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
        adsk.autoTerminate(False)


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

# Event handler that reacts to any changes the user makes to any of the command inputs.
class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            app = adsk.core.Application.get()
            ui = app.userInterface
            product = app.activeProduct
            design = adsk.fusion.Design.cast(product)
            activeComp = design.activeComponent

            eventArgs = adsk.core.CommandEventArgs.cast(args)
            inputs = eventArgs.command.commandInputs

            ###############################################
            # verify selections

            profileSelectionInput = inputs.itemById('profileSelection')
            if not profileSelectionInput:
                return False

            ###############################################
            # copy the profiles into a collection
            selectedProfiles = adsk.core.ObjectCollection.create()
            for thisEntity in range(profileSelectionInput.selectionCount):
                selectedProfiles.add(profileSelectionInput.selection(thisEntity).entity)
            if selectedProfiles == None:
                ui.messageBox('software error: profile selection could not be used')
                return False

            ###############################################
            #  derive usable entities and parameters, and verify inputs meet requirements
            ###############################################

            # determine the reference plane based on one of the profiles
            referencePlane = selectedProfiles[0].parentSketch.referencePlane
            if not referencePlane:
                ui.messageBox("invalid profile reference plane")
                return False

            ###############################################
            # create a thin extrusion
            ###############################################

            # extrude a thin body from the reference profile.  thickness is arbitrary, but keep it well above the model's resolution
            thickness = adsk.core.ValueInput.createByReal(0.1)
            extrude = activeComp.features.extrudeFeatures.addSimple(selectedProfiles, thickness, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

            # name the body for user convenience
            extrudeBody = extrude.bodies.item(0)
            if extrudeBody == None:
                ui.messageBox('temp extrude failed\n\noperation not complete')
                return False
            extrudeBody.name = "generatedSweepProfile"

            adsk.terminate()

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

 

Message 4 of 7
metd01567
in reply to: metd01567

I tried a reinstall, using the Fusion 360 cleaner to delete everything first.  Then I installed from the Autodesk website.  I got the same result, the sketch is not in the timeline after the insert dxf script runs.  Also there are no options to edit or redefine the reference plane for the sketch.

 

This is probably a mac thing, I'll try it on my wife's mac.  It will take a little more time but I will try on a Windows machine.

Message 5 of 7
goyals
in reply to: metd01567

I am able to get the error when trying to call importManwger.importToTarget API. I will create a defect for it and try to investigate it.

Regarding the second issue where sketch is not shown in timeline after opening exported dxf file in fusion, I saw base feature in timeline than sketch. What I noticed is when I open exported dxf file in Fusion, it starts in Direct modelling mode and when I switch to parametric mode, I saw base feature in timeline. Are you seeing the same behaviour?



Shyam Goyal
Sr. Software Dev. Manager
Message 6 of 7
metd01567
in reply to: goyals

That doesn't happen for me.  Even though it's empty, the timeline remains shown after I run the insert dxf script.  When I right click the root component the bottom menu item is: "Do not capture Design History".  So I'm still in parametric mode.

 

But I think you're right about it being non-parametric.  I could recreate the conditions by manually inserting a dxf via the user interface, with parametric modeling turned off.  Then when I run my extrude script I get the same error.  Which is:

 

Screen Shot 2019-04-30 at 4.16.00 PM.png

Message 7 of 7
metd01567
in reply to: metd01567

By the way, inserting a .dxf file wasn't my first choice.  The inserted sketch isn't linked to the original sketch, and isn't dimensioned.  I'm very open to alternative approaches.

 

I tried inserting or deriving from the sketch's original design.  That almost worked, but the inserted components were damaged after extrusion, or generated a dcSketch InternalValidationError:

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/python-extrude-component-s-sketch-goes-inv...

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