Copy a component from one document to another

espablo
Enthusiast

Copy a component from one document to another

espablo
Enthusiast
Enthusiast

Hello everyone

I have two documents open. I only have one component in one document and I want to copy it to another document. How to do this using API?

0 Likes
Reply
273 Views
2 Replies
Replies (2)

j4n.vokurka
Advocate
Advocate

Hello,


this is a sample script that prompts the user for a file name and then copies the design from that file to the active document. You only need to have the file that is to receive the design data opened.

You could further adjust the script to copy only selected components from the parent design or place them in a specific spot. Now, by default, the script uses origin for placement.

Hope this helps

import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        project = _app.data.activeProject

        fileToInsert = None
        fileNameToLookFor = None

        # get input from the user
        isValid = False
        input = ''  # placeholder
        while not isValid:

            (fileNameToLookFor, isCancelled) = _ui.inputBox('Enter a file name to look for:', 'File Name', input)
            if isCancelled:
                return                      

            for file in project.rootFolder.dataFiles:
                if file.name == fileNameToLookFor:
                    fileToInsert = file
                    isValid = True
                    break

            # check if desired file was found
            if fileToInsert == None:
                _ui.messageBox(f"File with name {fileNameToLookFor} was not found. Try again.")

        des = adsk.fusion.Design.cast(_app.activeProduct)
        root = des.rootComponent
        occ = root.occurrences.addByInsert(fileToInsert, adsk.core.Matrix3D.create(), True) 

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


 

0 Likes

espablo
Enthusiast
Enthusiast

Maybe I'll explain exactly what I mean. I have a document open and I'm working in it. I also have a ready-made program that creates a new document. In this newly created document, a component and solids are created. Up to this point everything is fine. Now I want to copy this newly created component to the document I am working on and close the newly created document. For now, I'll manage by saving the f3d file locally on HDD and then importing it to the document I'm working on. I thought there was another way to do it

0 Likes