Last month, we presented you with a way to migrate scripts from VBA to Python as a means of reflecting the push away from Visual Basic in ArcGIS. The goal of the article was to continue to encourage scripting despite this change, and to be able to provide you with useful scripts in this new environment. To that end, we will be modifying our initial scripts and translating them to Python so that you will be able to utilize them regardless of the version of ArcGIS you happen to be running.
You will need a little background on this process before digging in. If you don’t know about installing comtypes on the machine you’ll be scripting with, for example, or you don’t know how to add a custom script to ArcToolbox, be sure to read the article on Python migration to get the full scoop.
Before we can massively convert our remaining VBA scripts for you, there’s an issue we haven’t yet discussed - using ArcFM™ with Python. ArcFM™’s COM objects are located elsewhere, so Snippets.py’s GetModule() function doesn’t actually work for ArcFM™ objects. We’ve created a new file that will allow ArcFM™ libraries to be imported.
We’ve released a new script called ArcFM.py that allows these scripts to easily interact with ArcFM™ objects. This will be necessary for certain scripts below. Download that here.
Move your newly-downloaded ArcFM.py to your appropriate ArcGIS Python folder - most likely something along these lines: C:\Python[version]\ArcGIS[version]\Lib\
You can access ArcFM™ components the same way as you would with ArcObjects components. First, start with an import:
Then, just like snippets, use GetModule() to manually import a library. For example, to import the Miner & Miner Geodatabase Object Library:
(For reference, you can find the folder name with all of the COM library files by running ArcFM.GetLibPath().)
Once you’ve retrieved the library, it’s available in comtypes.gen, alongside any ESRI objects you’ve imported via Snippets.py. For example, the Miner & Miner Geodatabase Object Library could be accessed via comtypes.gen.mmGeodatabase.
With this code, you can fetch and manipulate ArcFM™ objects using Python.
I’ve rewritten the original "useful script" to showcase this ArcFM™ functionality.