Adding annotation feature class with Python script

554
4
08-09-2018 08:09 AM
Dailey_Don_L_
New Contributor II

I've created annotation feature classes from Microstation DGN files and I need to add those to a series of mxd's using arcpy. I've been able to do that, but the layers are added as polygon features not as annotation features. I've dug around and found this solutionAnnotation classes not visible after adding an annotation layer , but it doesn't work for me. The feature class still adds as a polygon layer. Has anyone else run into this? Below is the relevant code. 

for df in arcpy.mapping.ListDataFrames(mxd):
            print df.name
            if df.name == "Layers":
                featclasses = arcpy.ListFeatureClasses("","",County_Nam)
                print featclasses
                for fc in featclasses:
                    print fc
                    addLayer = arcpy.mapping.Layer(fc)
                    reflayer = arcpy.mapping.ListLayers(mxd,"Private",df)[0]
                    if fc == County_Nam + "_Anno_Ins":
                        print fc
                        temp = County_Nam + "_Anno_Ins"
                        arcpy.MakeFeatureLayer_management(fc,temp)
                        annoLayer = arcpy.mapping.Layer(temp)
                        arcpy.mapping.AddLayer(df, annoLayer, "BOTTOM")
0 Kudos
4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

That code... listed below... loads from disk

# ---- code from that post
import arcpy
my_mxd = arcpy.mapping.MapDocument("C:/PATH/TO/blank.mxd"
df = arcpy.mapping.ListDataFrames(my_mxd)[0]
anno_path = "C:/PATH/TO/some.gdb/BuildingAnno"
temp_layer = "temp_anno"
arcpy.MakeFeatureLayer_management(anno_path,temp_layer)
anno_layer= arcpy.mapping.Layer(temp_layer)
arcpy.mapping.AddLayer(df,anno_layer)
my_mxd.saveACopy("C:/PATH/TO/saved.mxd") 

Do they load without issue if you do it manually?

Dailey_Don_L_
New Contributor II

It does load properly when done manually. Seeing your repost of that code made me think to try to load a single feature class into an empty mxd, which worked. It took a few hours, but I finally figured out the problem had to do with the fact that I was trying to create the layer in the mxd to have the same name as the feature class in the gdb. Each feature class is named <some county>_Anno_Ins and when I changed this line in my script:

temp = County_Nam + "_Anno_Ins"‍

to

temp = County_Nam + "_DPatterson"‍

they were added to the mxd's properly as annotation. So problem solved. Thanks for the help.  

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

ahhh immortalized as an annotation

0 Kudos
Dailey_Don_L_
New Contributor II

I'm sure it's not the first time.

0 Kudos