1

I'm trying to add a CAD layer (point, polyline, annotation...etc) into ArcMap by python code, i'm using Arcgis 10.1, IDLE(Python 2.7), I faced many codes but they all gives errors, so i'm blocked here !

I tried this :

arcpy.AddCADFields_conversion("C:/Saadia/SAFTOP/Parcelle et rivrains/simulation arcgis/python-script python/Progr_work/plan.dwg","ADD_ENTITY_PROPERTIES")

And also this :

arcgisscripting gb = arcgisscripting.create()
gb.ImportCAD_conversion("'C:/plan.dwg'", "C:/workspace")

But no one works !

I tried this :

import arcpy
arcpy.env.workspace = "D:/Users/saadiya/Documents/ArcGIS/Default.gdb"
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] 
addPoint = arcpy.mapping.Layer(r"C:\plan.dwg\Point") # reference to point layer
addPolyline = arcpy.mapping.Layer(r"C:\plan.dwg\Polyline") # reference to Polyline layer
arcpy.mapping.AddLayer(df, addPoint, "BOTTOM")
arcpy.mapping.AddLayer(df, addPolyline, "BOTTOM")

But i got this error :

RuntimeError: Objet\xa0: CreateObject can not open the map

I changed it to this :

mxd = arcpy.mapping.MapDocument(r"C:\Saadia\BD\Projet_Marrakech.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] 
addPoint = arcpy.mapping.Layer(r"C:\plan.dwg\Point") # reference to point layer
addPolyline = arcpy.mapping.Layer(r"C:\plan.dwg\Polyline") # reference to Polyline layer
arcpy.mapping.AddLayer(df, addPoint, "BOTTOM")
arcpy.mapping.AddLayer(df, addPolyline, "BOTTOM")

it fixed this error :

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
IndexError: list index out of range

closed as unclear what you're asking by PolyGeo Sep 11 '16 at 8:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Can you share some of the code you used? Also, can you clarify what you mean by "cad code"? – dassouki Dec 9 '14 at 14:15
  • Thank you dassouki for answering. This is the code i used import arcgisscripting gb = arcgisscripting.create() gb.ImportCAD_conversion("'C:/plan.dwg'", "C:/workspace") And about CAD feature class, it results from AUTOCAD ! – geosevda Dec 9 '14 at 14:26
  • you can click on the "edit" button and edit your question by providing perhaps a bit more detailed explanation – dassouki Dec 9 '14 at 14:27
  • I used to this one : £ arcpy.AddCADFields_conversion("C:/Saadia/SAFTOP/Parcelle et rivrains/simulation arcgis/python-script python/Progr_work/plan.dwg","ADD_ENTITY_PROPERTIES") – geosevda Dec 9 '14 at 14:30
  • So no one knows the answer, i'm sure that is an easy task ! – geosevda Dec 10 '14 at 13:15
2

You may use the AddLayer method:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
addPoint = arcpy.mapping.Layer(r"C:\Temp\my.dwg\Point") # reference to point layer
addPolyline = arcpy.mapping.Layer(r"C:\Temp\my.dwg\Polyline") # reference to Polyline layer
arcpy.mapping.AddLayer(df, addPoint, "BOTTOM")
arcpy.mapping.AddLayer(df, addPolyline, "BOTTOM")
  • It gives me this error : Object: CreateObject Layer invalid data source. While i'm trying to executing the third line ! – geosevda Dec 11 '14 at 9:09
  • Can you edit your question with your updated code? – artwork21 Dec 11 '14 at 13:13
  • I did it artwork ... – geosevda Dec 11 '14 at 13:32
  • Your problem seems to be on this line, mxd = arcpy.mapping.MapDocument("CURRENT"). My example was with running the code in a opened ArcMap session within the python window. Instead of using CURRENT value you can set this path to whatever map document you want the layers added to. – artwork21 Dec 11 '14 at 13:38
  • I edited my question again ! please check the change ! – geosevda Dec 11 '14 at 13:48

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