Setting Up the Common Parts Library

From Team 449 Wiki

Due to the nature of GrabCAD, using a single Common Parts Library to be shared among the different projects is not as easy as it sounds. Since the path to the Common Parts Library (CPL) is outside the scope of each project's project file (.ipj), the path is global, not relative, and would likely not work between shared computers unless the GrabCAD folder has the exact same directory between all of them. A macro can resolve this by reading an environment variable and setting the location of the Common Parts Library according to it.

The Problem

GrabCAD projects each have a folder within the GrabCAD folder set by GrabCAD Workbench, and the Inventor project files for each project are in the top level of the project folders. Each project folder is outside the scope of all other Inventor project files. If the Common Parts Library is copied into a project folder, its library path in the Inventor project file could be set to ".\Common Parts Library", but this leads to an excessive use of hard drive space and makes it more difficult to update the Common Parts Library across multiple projects when changes are made. If the Common Parts Library is outside the project folder, in this case as its own GrabCAD project, the library path referring to it can no longer be local and would instead be a global path like "C:\Users\<Username>\Documents\GrabCAD\Common Parts Library". This works well on a single computer, but when the Inventor project file is pushed to GrabCAD and pulled by someone else, the library path is still the same as it was on the original computer, pointing to a directory which does not exist on this computer. The second user could change the path manually, but as soon as they push the issue starts all over again for the rest of the users.

The Macro

Inventor allows its users to create macros using Visual Basic for Applications (VBA) for some more advanced integrations. Inventor macros are stored in either the Default VBA project (defined at Tools > Application Options > File > Default VBA Project) or within an Inventor part file (.ipt) or Inventor assembly file (.iam). Macros stored in the Default VBA project can be accessed from anywhere and can be placed on the tool ribbon, while file-specific macros are used when macros are only needed for a specific part to avoid clutter. Since this macro needs to be executed before any file is opened it must be stored in the Default VBA project.

When executed, the macro clears the active project's libraries and then creates a new one named "Common Parts Library" at "%GRABCAD_DIR%\Common Parts Library", where GRABCAD_DIR is an environmental variable set by the user which stores the directory of the GrabCAD directory.

Macro Code

'Clears all library paths in the active project, then creates one named "CPL" from the directory %GRABCAD_DIR%\Common Parts Library Sub SetCommonPartsLibrary()

   Dim oProjectMgr As DesignProjectManager
   Set oProjectMgr = ThisApplication.DesignProjectManager
   ' Get the active project
   Dim oProject As DesignProject
   Set oProject = oProjectMgr.ActiveDesignProject
   'Get project's library paths
   Dim oLibraryPaths As ProjectPaths
   Set oLibraryPaths = oProject.LibraryPaths
   'Clear library paths
   Call oLibraryPaths.Clear
   Dim path As String
   path = Environ("GRABCAD_DIR") & "\Common Parts Library"
   'Set CPL path
   Call oLibraryPaths.Add("Common Parts Library", path)
   ' Print all library names and paths
   Dim oLibraryPath As ProjectPath
   For Each oLibraryPath In oLibraryPaths
       Debug.Print "Name     " & oLibraryPath.Name & " Path     " & oLibraryPath.path

End Sub


This setup requires the user to have access to the Common Parts Library, and instructions are written for Windows 10.

Setting the Environment Variable

Open Windows Explorer. Right click This PC and select Properties. In the new Control Panel window, select Advanced System Settings in the left menu. Select Environment Variables. In the bottom half (System Variables, not User Variables) select New and set the variable name to GRABCAD_DIR and the value to the GrabCAD directory (e.g. C:\Users\Username\Documents\GrabCAD). Inventor will not process this change until it is reopened.

Changing the Default VBA Project

In Inventor, go to Tools > Options > Application Options > File and set Default VBA Project to C:\...\GrabCAD\Common Parts Library\Macros\Default.ivb. The environment variable cannot be used for this, but this only has to be set once.

Running the Macro

In Inventor, go to Tools > Options > Macros. Select CPL.SetCommonPartsLibrary, then Run. The Common Parts Library should now be set as a library in the active Inventor project. Remember that this macro should be run after selecting the project, but before opening any files.

Setting up the Macro Shortcut

Since the macro will have to be run after every time the user pulls from GrabCAD, it is recommended to make a shortcut to the macro from the ribbon. In Inventor's project Home page with no parts open, go to Tools > Options > Customize. On the left, change the dropdown from "All Commands" to "Macros". On the right, select the location where the shortcut should appear (I use Zero Document | Get Started). On the left, select SetCommonPartsLibrary, then click the right pointing arrows in the middle. Customize the appearance. Select Apply. The macro should now appear as a button in the selected location.