SeanYeatts / SolidWrap

Wrapper for SolidWorks and PDM APIs for a streamlined, Pythonic workflow.
https://github.com/SeanYeatts
MIT License
1 stars 0 forks source link

[Feature Request] Vault - Get Reference Tree #39

Open callumjo opened 2 months ago

callumjo commented 2 months ago

Great work with this. Its saved me a lot of time.

Feature: Add the PDM API method GetReferenceTree for assemblies.

What I am trying to achieve: I want to get all the components in an assembly tree from PDM so I can perform action on all the components at once. I can do this using solidworks. The issue is when working with very large assemblies is becomes very slow to use solidworks.

I have been trying in implement it myself but no matter what I try I can not get it to work https://help.solidworks.com/2021/english/api/epdmapi/EPDM.Interop.epdm~EPDM.Interop.epdm.IEdmFile5~GetReferenceTree.html

This was what I was working off: https://help.solidworks.com/2021/English/api/epdmapi/Get_File_References_for_File_Example_VBNET.htm

What I came up with so far:

    def getreferencetree(self, filepath: Filepath) -> Dict[str, List[str]]:
        '''Gets the reference tree for a document from PDM.'''
        log.info(f"Getting PDM reference tree for document: '{filepath.name}'")

        try:
            # Get PDM-API objects
            directory = Vault.client.GetFolderFromPath(filepath.directory)  # IEdmFolder
            file = directory.GetFile(filepath.name)                         # IEdmFile5

            # Get reference tree
            ref_tree = file.GetReferenceTree(directory.ID, 0)  # 0 for latest version

            # Process references
            references = {
                "referenced_by": [],
                "references": []
            }

            # Get files that reference this file
            ref_by = ref_tree.GetFirstReferencePosition(True)
            while not ref_by.IsNull:
                ref_file = ref_tree.GetNextReference(ref_by, True)
                references["referenced_by"].append(ref_file.GetFullPath())

            # Get files that this file references
            refs = ref_tree.GetFirstReferencePosition(False)
            while not refs.IsNull:
                ref_file = ref_tree.GetNextReference(refs, False)
                references["references"].append(ref_file.GetFullPath())

            log.info(f"Found {len(references['referenced_by'])} files referencing '{filepath.name}'")
            log.info(f"Found {len(references['references'])} files referenced by '{filepath.name}'")
            return references
        except Exception as exception:
            log.error(f"Failed to get PDM reference tree for '{filepath.name}'")
            log.error(exception)
            return {"referenced_by": [], "references": []}

Thanks!

SeanYeatts commented 2 weeks ago

Thanks for the feedback @callumjo! I'll look into this.