DataFile.hasChildReferences in History

DataFile.hasChildReferences in History

kandennti
Mentor Mentor
561 Views
2 Replies
Message 1 of 3

DataFile.hasChildReferences in History

kandennti
Mentor
Mentor

Hey there everyone.

 

In the Japanese forum, there are sometimes posts saying "I can't delete a document in the Data Panel".

I think there is more than one cause, but recently I found out that one of the causes is the History of the document.

 

In order to delete a document, you need to find the document you are referring to.

 

The attached f3z file looks like this

1.png

If you press the pull-down button showing the version of "Part_A" in the data panel, the information will be displayed, and if you look at "Used In", you can see which document it is used in.

 

The following script will log the ID of the active document, access the DataFiles in the same DataFolder as the active document, and log the ID, name, and version.
DataFile accesses not only the latest version, but all versions, and does the same for the DataFile in the hasChildReferences Property.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-E3934C16-05D5-46D2-A4FF-068786F93C17 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


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

        # datafile
        datafile: adsk.core.DataFile = getActiveDocDataFile()
        if not datafile:
            msg = 'No active documents are saved.'
            ui.messageBox(msg)
            return

        targetId = datafile.id

        # folder
        datafolder: adsk.core.DataFolder = datafile.parentFolder

        # version files
        files = []
        f: adsk.core.DataFile
        for f in datafolder.dataFiles.asArray():
            files.extend(f.versions)

        app.log(f'- Target<ID>{targetId} -')
        for file in files:
            app.log(
                f'\n*Document<ID>{file.id} <Name>{file.name} <Ver>{file.versionNumber}')

            if len(file.childReferences) < 1:
                continue

            app.log('-Child References-')
            for refFile in file.childReferences:
                app.log(
                    f'        <ID>{refFile.id} <Name>{refFile.name} <Ver>{refFile.versionNumber}')

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

 

If you run the script with the attached file, Part_A, open, you will get this result.

 - Target<ID>urn:adsk.wipprod:dm.lineage:NrhoPG0NSKGv4cPrCK4qKg -
 
*Document<ID>urn:adsk.wipprod:dm.lineage:yhUbUYc8ScGNuDe_mfyCMw <Name>Assy <Ver>1
 -Child References-
         <ID>urn:adsk.wipprod:dm.lineage:NrhoPG0NSKGv4cPrCK4qKg <Name>Part_A <Ver>1
 
*Document<ID>urn:adsk.wipprod:dm.lineage:NrhoPG0NSKGv4cPrCK4qKg <Name>Part_A <Ver>1

2.png

 

Documents that cannot be deleted can be created by doing the following.
Open the "Assy" documentation, delete the "Part_A" component (Occurrence) and save it.
3.png

 

If you check "Part_A" in the Data Panel, you will see that it is not referenced.
4.png

 

Now, if you try to close "Part_A" and delete it from the Data Panel, you will not be able to do so.
5.png

 

The reason you can't delete it is the history of "Assy". This is because the history holds Part_A.
6.png

 


I thought I could access "Part_A" if it was a childReferences in the Datafile of an older version of "Assy".
If this is possible, I thought it would be useful to find the document that holds the Link to delete.

 

Open Part_A again and run the previous script.

 - Target<ID>urn:adsk.wipprod:dm.lineage:NrhoPG0NSKGv4cPrCK4qKg -
 
*Document<ID>urn:adsk.wipprod:dm.lineage:yhUbUYc8ScGNuDe_mfyCMw <Name>Assy <Ver>2
 
*Document<ID>urn:adsk.wipprod:dm.lineage:yhUbUYc8ScGNuDe_mfyCMw <Name>Assy <Ver>1
 
*Document<ID>urn:adsk.wipprod:dm.lineage:NrhoPG0NSKGv4cPrCK4qKg <Name>Part_A <Ver>1

Unfortunately, the childReferences in the old Datafile for "Assy" did not have "Part_A".
However, if you open the old "Assy" in the GUI, you will definitely see that "Part_A" is linked.

7.png

 

I am not going to discuss in the API forum how to remove such documents or how to manage the links.

Is it not possible to get the linked documents from an older version of Datafile?

 

If a user performs this operation, there is virtually no way to find the document that is causing the deletion failure.

0 Likes
562 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

I did some additional testing.

 

Assy Ver1: Add Part_A and save.
Assy Ver2: Add Part_B and save.
Assy Ver3: Add Part_C and save.

 

Here is the result of running the above script in this state.

1 (1).png

 

I was expecting this to be the case for hasChildReferences, but it is not.
Assy Ver1: Part_A
Assy Ver2: Part_A & Part_B
Assy Ver3: Part_A & Part_B & Part_C

 

It seems that the historical Datafile.hasChildReferences always returns the latest version of Datafile.hasChildReferences.
Is this a specification?

0 Likes
Message 3 of 3

kandennti
Mentor
Mentor

We were missing one function to execute.

def getActiveDocDataFile() -> adsk.core.DataFile:
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        doc: adsk.core.Document = app.activeDocument
        return doc.dataFile
    except:
        return None