SOLIDWORKS PDM API to get referenced files

Programming and macros
dnascimento
Posts: 2
Joined: Thu Mar 18, 2021 12:41 pm
Answers: 0
x 2

SOLIDWORKS PDM API to get referenced files

Unread post by dnascimento »

I would like to create a doc file from PDM templates capabilities. The ideia is create a ECO where all infomations regarding this ECO was provided by user in a datacard and the new document would be filled with these informations. One field in this ECO doc should be to list the referenced files displayed in contain tab. In my searches i have found the API described below but doesn't work. I'm not fluent in API but i would like to know if somone can help me.
If ThisDocument.ReadOnly = False Then
Dim strDocumentPath As String
strDocumentPath = ThisDocument.FullName

Dim oVault As New EdmVault5
oVault.LoginAuto oVault.GetVaultNameFromPath(strDocumentPath), 0

Dim oFile As IEdmFile6
Dim oFolder As IEdmFolder6
Set oFile = oVault.GetFileFromPath(strDocumentPath, oFolder)

Dim projName As String

If oFile Is Nothing Then
MsgBox "File not in vault", vbCritical
Else
Dim oRef As IEdmReference5
Set oRef = oFile.GetReferenceTree(oFolder.ID, 0)
Dim mytable As Table
Set mytable = ActiveDocument.Tables(2)
mytable.Cell(1, 1).Select
Selection.Delete

Selection.InsertAfter Text:=AddReferences(oRef, 0, projName)


End If
End If

End Sub
Private Function AddReferences(file As IEdmReference5, ByVal indent As Long, ByRef projName As String) As String
Dim msg As String

msg = String(indent, " ")



msg = msg + IIf(indent > 0, file.Name, "") + vbLf


Dim isTop As Boolean
isTop = True
If indent > 0 Then isTop = False
indent = indent + 4

Dim pos As IEdmPos5
Set pos = file.GetFirstChildPosition(projName, isTop, True, 0)
Dim ref As IEdmReference5
While Not pos.IsNull
Set ref = file.GetNextChild(pos)
msg = msg + AddReferences(ref, indent, projName)
msg = msg + ref.file.Name + vbLf
Wend

AddReferences = msg
End Function

Private Sub Document_Open()
If ThisDocument.ReadOnly = False Then
Selection.WholeStory
Selection.Fields.Update

AutoOpen
End If
End Sub
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: SOLIDWORKS PDM API to get referenced files

Unread post by AlexB »

You'll want to look at the following example to get the "Contains" files for a PDM file.

https://help.solidworks.com/2022/englis ... _VBNET.htm
revenki
Posts: 24
Joined: Fri Sep 24, 2021 11:17 pm
Answers: 0
x 7

Re: SOLIDWORKS PDM API to get referenced files

Unread post by revenki »

This may be useful as a go-by example. I've stripped out some of the code for anonymity, but what this does is recurses through a passed file (in our case a PDM virtual document used as an ECO) and returns three lists: the PDFs of electronic signoffs, the design change records documenting the changes to engineering, and the affected parts/assemblies/drawings. The files it lists out are those which have been attached to the virtual document via Paste As Reference.

Code is in VBA, as I originally wrote it inside an Excel spreadsheet.

== VBA CODE ==========

Code: Select all

Sub GetReferencedFiles(ByVal Reference As IEdmReference10, ByVal iPath As String, ByVal Level As Integer, ByVal ProjectName As String)
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' PURPOSE: finds first level only of files referenced by file in iPath, populates the two block
'          variables with the file names sorted by PDF and not-PDF, with any DCRs also put in DCR block'
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Dim Top As Boolean
    Top = False
    
    If TypeName(eVault) = "Nothing" Then loginPDM
    
    [make a file object here and point it to the file whose path is iPath]
    
    If Reference Is Nothing Then
        ' -- this is the first time this function is called for this file reference tree; i.e., this is the root
        ' -- this gets the Reference object (the ESO itself), so doesn't add anything to the file lists
        Top = True
        
        Dim File As IEdmFile5
        Set File = Nothing
        Dim ParentFolder As IEdmFolder5
        Set ParentFolder = Nothing
        
        pdfBlock = ""
        fileBlock = ""
        pdfCount = 0
        FileCount = 0
        
        Set File = eVault.GetFileFromPath(iPath, ParentFolder)
        ' -- Get the file reference tree for this file
        Set Reference = File.GetReferenceTree(ParentFolder.ID)
        GetReferencedFiles Reference, "", Level + 1, ProjectName

    Else
        ' -- Execute this code when this function is called recursively;
        ' ---- i.e., this is not the top-level IEdmReference in the tree
        Dim pos As IEdmPos5
        Set pos = Reference.GetFirstChildPosition4(ProjectName, Top, True, True, EdmRefFlags.EdmRef_File, "", 0)
        Dim ref As IEdmReference10
        Dim refFile As IEdmFile16
        Dim ppoFldr As IEdmFolder12
        Dim varEnum As IEdmEnumeratorVariable10
        Dim refMdl As IEdmFile9

        
        While (Not pos.IsNull)
            Set ref = Reference.GetNextChild(pos)
           
            ' -- add to PDFs list if it's an approval PDF
            If Left(UCase(Trim(ref.Name)), 19) = "ELECTRONIC APPROVAL" And Right(UCase(Trim(ref.Name)), 4) = ".PDF" Then
                pdfCount = pdfCount + 1
                pdfBlock = pdfBlock & "[" & Right("00" & pdfCount, 2) & "]: " & ref.Name & vbCrLf & vbCrLf
            Else       ' ---- add anything else to the regular file list, including DCRs; add stripped name to the DCR list if the file is a DCR
                FileCount = FileCount + 1
                refName = ref.Name
                
                Set refMdl = eVault.GetFileFromPath(ref.FoundPath)
                Set varEnum = refMdl.GetEnumeratorVariable
                configName = "@"
                If InStr(UCase(ref.FoundPath), ".SLD") = 0 Then configName = ""
                varEnum.GetVar "Revision", configName, refRev
                
                If refRev = "-" Then refRev = "NEW"
                                
                fileBlock = fileBlock & "[" & Right("00" & FileCount, 2) & "]: " & UCase(refName) & " - REV " & refRev & vbCrLf & vbCrLf
                
                If InStr(refName, "DCR") <> 0 Then
                    If Len(Trim(dcrBlock)) = 0 Then
                        dcrBlock = Left(refName, InStr(refName, ".") - 1)
                    Else
                        dcrBlock = dcrBlock & ", " & Left(refName, InStr(refName, ".") - 1)
                    End If
                End If
            End If
        Wend
    End If
    
    If dcrBlock = "" Then
        dcrBlock = "None"        
    ElseIf Left(dcrBlock, 1) = "," Then
        dcrBlock = Trim(Right(dcrBlock, Len(dcrBlock) - 1))
    End If

  End Sub
Post Reply