[VBA] Add new library to project file and (some weirdness) getting it active

[VBA] Add new library to project file and (some weirdness) getting it active

mr_ensing
Advocate Advocate
351 Views
0 Replies
Message 1 of 1

[VBA] Add new library to project file and (some weirdness) getting it active

mr_ensing
Advocate
Advocate

First of all, i think i've got it to work, but is was a journey.

 

So i'm adding and new material and appearance libraries to multiple project files. This seemed pretty straightforward, but it ended up challenging. It was the setting active of a library, that got weird.

 

Manually, no problem:

 

Activate_library_manual.png

 

 

 

But doing the same thing from VBA, this happened:

 

Activate_library_vba.png

 

And when asked for the active library, the old version was returned.

 

The solution for this weird behavior was -apparently- to delete the active library, then add the new library and finally re-adding the deleted one: 

 

 

 

Private Sub FixProject()

    Dim oProject As DesignProject
    Set oProject = ThisApplication.DesignProjectManager.DesignProjects.AddExisting("C:\Temp\Temp\Test_project.ipj")
        
    Dim newLibPath As String
    newLibPath = "I:\Inventor_Data\Materials\Test_library_new.adsklib"
    
    Dim newLib As ProjectAssetLibrary
    Dim oLib As ProjectAssetLibrary
    Dim activeLib As ProjectAssetLibrary
    Dim activeLibPath As String
    
    'MaterialLibraries
    'note active library, to delete and add back later
    Set activeLib = oProject.ActiveMaterialLibrary
    activeLibPath = activeLib.LibraryFilename
    'check if new library already exists
    For Each oLib In oProject.MaterialLibraries
        If StrComp(oLib.LibraryFilename, newLibPath, vbTextCompare) = 0 Then
            Set newLib = oLib
            Exit For
        End If
    Next oLib
    'not found, add
    If newLib Is Nothing Then Set newLib = oProject.MaterialLibraries.Add(newLibPath)
    'delete active
    If Not StrComp(activeLib.LibraryFilename, newLib.LibraryFilename, vbTextCompare) = 0 Then activeLib.Delete
    'set active
    Call newLib.Activate
    're-add deleted
    If Not StrComp(activeLib.LibraryFilename, activeLibPath, vbTextCompare) = 0 Then _
        Call oProject.MaterialLibraries.Add(activeLibPath)
    
    'reset
    Set newLib = Nothing
    Set oLib = Nothing
    Set activeLib = Nothing
    activeLibPath = ""
    
    'AppearanceLibraries
    'note active library, to delete and add back later
    Set activeLib = oProject.ActiveAppearanceLibrary
    activeLibPath = activeLib.LibraryFilename
    'check if new library already exists
    For Each oLib In oProject.AppearanceLibraries
        If StrComp(oLib.LibraryFilename, newLibPath, vbTextCompare) = 0 Then
            Set newLib = oLib
            Exit For
        End If
    Next oLib
    'not found, add
    If newLib Is Nothing Then Set newLib = oProject.AppearanceLibraries.Add(newLibPath)
    'delete active
    If Not StrComp(activeLib.LibraryFilename, newLib.LibraryFilename, vbTextCompare) = 0 Then activeLib.Delete
    'set active
    Call newLib.Activate
    're-add deleted
    If Not StrComp(activeLib.LibraryFilename, activeLibPath, vbTextCompare) = 0 Then _
        Call oProject.MaterialLibraries.Add(activeLibPath)
    
End Sub

 

 

 

0 Likes
352 Views
0 Replies
Replies (0)