2

I would like to add an AutoCad .dwg file to Arcmap using python the same way that the +ADD Data button does.

I can add an Annotation layer with python but only as a feature layer not as an annotation layer... even thought in the properties it says it's a CAD Annotation Feature Class

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "ACAD", df)[0]
addLayer = arcpy.mapping.Layer(PlanPath)
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")

The files share the same properties, but are different types (Feature, Annotation)...

enter image description here

enter image description here

NOTE: I realize in my screencaps the files have different R1, R2 numbers ignore this.

The documentation for layer says:

"There are essentially three categories of layers in a map document: feature layers, group layers, and raster layers. There are a few specialized layers and datasets that don't fall into one of these three categories: annotation subclasses Group layers and other sublayers (e.g., annotation classes) are treated just like ordinary layers."

Is there any way to add it as an Annotation layer? The same way the +Add Data button works?

EDIT:

This is what I tried doing.

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "ACAD", df)[0]
arcpy.MakeFeatureLayer_management(PlanPath,"CAD")

for lyr in arcpy.mapping.ListLayers(mxd,"CAD",df):
    lyr2 = lyr
    #Include the Add Layer to Group code as part of the For Loop
    arcpy.mapping.AddLayerToGroup(df,targetGroupLayer,lyr2)

From what I could tell from your code (Get Spatial). The annotation layer is added to a temp feature layer. You then search for that temp feature layer "cadname" and then add it to the target grouplayer.I tried to recreate this but using and for loop to loop through each layer and find the one called "CAD"

@GetSpatial I ran the new code and looks like it made the feature layer yet did not correctly add it to the map.

In the report below is the "CAD" after "\Annotation" part of the file name it is trying to find, or is it simply the name of the Layer and has no effect on the file path?

Report

1
  • I made an edit to my answer based on a different method that might be closer to what you are trying to accomplish. Commented Jun 27, 2012 at 1:19

1 Answer 1

2

The answer to your question lies in understanding that even though it is referred to as Annotation, it is still coming from a CAD drawing.

This ESRI Help document lists the Supported AutoCAD entities.

This Help document talks about ArcGIS Annotation: What is annotation?

There are a lot of differences. The most important being that all CAD data is Read-Only in ArcGIS. You can load in data and symbolize in different ways, but if you want to edit anything it has to be converted to a different spatial format first.

ArcGIS Annotation on the other hand is editable like any other feature class in a geodatabase, along with the ability to add different attributes.

In short, if you want to add CAD Annotation as an Annotation Feature Class, you first have to convert it to Geodatabase Annotation. Here is a help document that describes this process: Import CAD Annotation (Conversion)

-------- Edit

With a clearer head, I reread your question, and realized there are two different ways of understanding it. 1. Is like above where I thought you wanted to bring it in as an actual ArcGIS Annotation layer.
2. Is that you simply want to load the layer in to show the CAD annotation as text, and not have it represented as a point featureclass as in your first diagram.

The way to do this requires using a different type of Layer function, not the one in arcpy.mapping, but instead the geoprocessing type, with this command: Make Feature Layer
This command creates a Feature Layer in memory, but also adds it to your current Map. The difference between this and the arcpy.mapping Layer class is that you are able to specify not only the Annotation Layer of the CAD drawing, but also specify that this dataset is of type CAD. This changes the way that ArcGIS reads the file.

Once you have added the layer to the map, you may simply add it to your Group Layer. Here is some code that takes a referenced CAD drawing, pulls out the Annotation portion, creates a Feature Layer and adds it to the Group Layer

import arcpy

#Reference current MXD
mxd = arcpy.mapping.MapDocument("CURRENT")

#Directory containing CAD File
cadpath = r"C:\City_GIS\Projects\Python"

#Set environment workspace
arcpy.env.workspace = cadpath

#Return specific drawing name from list of datasets
dwgname = arcpy.ListDatasets("C-6*","CAD")[0]

#Returns desired dataframe from current MXD
dataframe = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

#Return desired group layer to add Annotation to
grouplayer = arcpy.mapping.ListLayers(mxd,"CADLayers",dataframe)[0]

#Name of new layer to be created
cadname = dwgname + "_anno"

#Create Temporary Feature Layer, which also adds to Map document
arcpy.MakeFeatureLayer_management(dwgname + "/Annotation",cadname)

#Refresh Table of Contents
arcpy.RefreshTOC

#Search MXD list to return Layer
layertogroup = arcpy.mapping.ListLayers(mxd,cadname)[0]

#Add this returned layer to your group layer
arcpy.mapping.AddLayerToGroup(dataframe,grouplayer,layertogroup)

This will create two copies of the layer on your Map. The one will be outside and one inside the Group layer. You can then set up code to remove the one that is outside the group layer.

It is a bit convoluted, but I hope it approaches what you were trying to accomplish. Here is a screenshot of the result.

Screenshot of CAD Annotation Layer

6
  • Thanks alot, I'll upvote your question when I can, right now I'm logged in but the when I click to upvote says "Must login..." Commented Jun 27, 2012 at 17:41
  • I've tried doing the following but was not working. arcpy.MakeFeatureLayer_management(PlanPath,"CAD") for lyr in arcpy.mapping.ListLayers(mxd, "CAD"): lyr2 = lyr arcpy.mapping.AddLayerToGroup(df,targetGroupLayer,lyr2) Commented Jun 27, 2012 at 23:50
  • @TForward - It is hard from the code you added to see what you are trying to do. Why don't you edit your initial question, and add in the code you are working with now so I am able to see it better. Commented Jun 28, 2012 at 0:23
  • Edited to previous post Commented Jun 28, 2012 at 14:16
  • @TForward - Based on the edit, it looks like you need to make the line where you add the layer to the group layer, indented so that it is included in the "For" loop. Right now, it is not part of the loop, so not all the possible layers would be added. I'll edit your question to show what I mean. Look at the changes to see what the difference is. Commented Jun 28, 2012 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.