With ArcPy I have created a tool that creates an empty feature class with pre-defined fields and adds it to the TOC. It works quite well, however if I use the tool to generate a feature class in an unsaved MXD it hangs. I can get around it by naming and saving the MXD (it'S a bit cumbersome).
I'd like to change the code so that I can add a feature class to the TOC without necessarily creating an MXD project each time.
#------------------------------------------------------
# currently only works on a saved mxd. needs to be able to add to an unsaved docuement.
#----------------------------------------------------------------
import arcpy
#import time feature - to insert date into field, but not functional yet.
from time import strftime
from arcpy import env
env.workspace = "CURRENT"
#--------------DEFINE VARIABLES---------------
in_wrkspace = arcpy.GetParameterAsText(0)
projectNumber = arcpy.GetParameterAsText(1)
FeatureDescription = arcpy.GetParameterAsText(2)
in_GeomType = arcpy.GetParameterAsText(3)
spatialRefCode = arcpy.GetParameterAsText(4)
#-----------------DEFINE EXPRESSIONS-----------------
FeatureClass = '_'+ str(projectNumber) + '_cm_' + str(FeatureDescription) + '_' + strftime("%y%m%d")
#Create feature class within workspace and add fields
FC = arcpy.CreateFeatureclass_management(in_wrkspace,FeatureClass,in_GeomType,"","DISABLED","DISABLED",spatialRefCode)
for FeatureClass in FC:
# Process: classe
arcpy.AddField_management(FeatureClass, "classe", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")
# Process: objet
arcpy.AddField_management(FeatureClass, "objet", "TEXT", "", "", "200", "", "NULLABLE", "NON_REQUIRED", "")
#Process: source
arcpy.AddField_management(FeatureClass, "source", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
#Process: date
arcpy.AddField_management(FeatureClass, "date", "TEXT", "", "", "10", "", "NULLABLE", "NON_REQUIRED", "")
#Process: remarque
arcpy.AddField_management(FeatureClass, "remarque", "TEXT", "", "", "250", "", "NULLABLE", "NON_REQUIRED", "")
#Process: traitment
arcpy.AddField_management(FeatureClass, "traitment", "TEXT", "", "", "250", "", "NULLABLE", "NON_REQUIRED", "")
#Process: no_seq
arcpy.AddField_management(FeatureClass,"NO_SEQ", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#make feature layer
#Create a layer file to add to MXD
temp = '_'+ str(projectNumber) + '_cm_' + str(FeatureDescription) + '_' + strftime("%y%m%d")
arcpy.MakeFeatureLayer_management(FeatureClass,temp)
#-----------------add the layer to data frame--------------------------
#source: http://gis.stackexchange.com/questions/62896/how-to-add-feature-class-to-mxd-with-arcpy-python
#arcpy.MakeFeatureLayer_management("in_memory",FeatureClass)
MXD = arcpy.mapping.MapDocument("CURRENT")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
Layer = arcpy.mapping.Layer(temp)
arcpy.mapping.AddLayer(DF,Layer,"AUTO_ARRANGE")
MXD.save()
#delete in memory cache layer
del MXD #, DF, Layer
arcpy.Delete_management(Layer)
Many thanks!