PDM variable from attribute

PDM API is written so bad that all AIs are clueless. Good job dassault. No wonder solidworks is crashing so often. Also try to at least compile the code with summaries included.

Anyway how can I retrieve PDM variable name from attribute name and extension?

Can someone create function with signature:

Public Shared Function GetVariableByAttribute(vault As IEdmVault5, attributeName As String, fileExt As String) As String


Also include caching if all variables need to be retrieved.
 

Answers 2
DG
DG 2026-03-11 Best Answer

holy s...
you need to do .isnull checking.

        Public Shared Function GetVariableByAttribute(
               vault As IEdmVault5,
               attributeName As String,
               fileExt As String) As String

           vault = If(vault, Login())
           If vault Is Nothing Then Return Nothing

           Dim key As String = fileExt & "|" & attributeName

           'Fast path (O(1))
           SyncLock _cacheLock
               Dim cached As String = Nothing
               If _variableCache.TryGetValue(key, cached) Then
                   Return cached
               End If
           End SyncLock

           Try
               Dim varMgr As IEdmVariableMgr5 = CType(vault, IEdmVariableMgr5)

               Dim posVar As IEdmPos5 = varMgr.GetFirstVariablePosition()

               While Not posVar Is Nothing AndAlso Not posVar.IsNull

                   Dim variable As IEdmVariable5 = varMgr.GetNextVariable(posVar)
                   If variable Is Nothing Then Exit While

                   Dim posAttr = variable.GetFirstAttributePosition(fileExt)

                   While posAttr IsNot Nothing AndAlso Not posAttr.IsNull

                       Dim attr As IEdmAttribute5 = variable.GetNextAttribute(posAttr)
                       If attr Is Nothing Then Exit While

                       If String.Equals(attr.Name, attributeName, StringComparison.InvariantCultureIgnoreCase) Then

                           Dim result = variable.Name

                           SyncLock _cacheLock
                               _variableCache(key) = result
                           End SyncLock

                           Return result
                       End If

                   End While

               End While

           Catch ex As Exception
               ShowMessage($"[GetVariableByAttribute] {ex.Message}")
           End Try

           'Cache negative result too (prevents repeated scans)
           SyncLock _cacheLock
               _variableCache(key) = Nothing
           End SyncLock

           Return Nothing

       End Function

MD
MD 2026-03-10

The PDM API has nothing to do with SOLIDWORKS Design if that is what you’re referring to as crashing. Do not rely on AI to think for you.