Addlayer error

3488
4
08-08-2014 05:02 AM
CARMETSandrine
New Contributor II

Hello! I'm having the same problem...

Just to better understand, if I'm not using path but defined parameters, I can not write :

   # Add shp to mxd

  mxd =arcpy.mapping.MapDocument("CURRENT")

  df = arcpy.mapping.ListDataFrames(mxd)[0]

  addLayer = arcpy.mapping.Layer(OutputSHPFile)

  arcpy.mapping.AddLayer(df, addLayer, "TOP")

  arcpy.RefreshActiveView()

  arcpy.RefreshTOC()

Where OuputSHPFile = parameters[3].valueAsText  ??

Is there a way to get my OutputSHPfile as a string and then use it in the "addLayer" ?

Thanks.

0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor

Can you explain how and where your parameters list is defined? Does the "OutputSHPFile" exist? If it is a featureclass (type) you could describe it and use the dataSource (which points to the path) instead.

I notice that you are using "CURRENT" keyword to define the mxd (which is the current one). This means your script can only run inside a session of ArcMap. Is this right?

Maybe you can explain a little more about what you are trying to accomplish and reveal the rest of the code. That way it's easier to understand what might be going wrong.

Kind regards, Xander

CARMETSandrine
New Contributor II

Yes, I want to add my OuputSHPFile (contour feature class - param3) to my currend MXD, the script is described below.

So instead of expressing "OutputSHPFile", I should write "str = inputWorkspace + "\\" + OutputSHPFile", or something like that ? Because I don't want to use the path of the class, to allow my script to be used by other users.

Thank you very much.

import arcpy

import os

class Toolbox(object):

    def __init__(self):

        """Define the toolbox (the name of the toolbox is the name of the

        .pyt file)."""

        self.label = "Clip and contour Analysis"

        self.alias = ""

        # List of tool classes associated with this toolbox

        self.tools = [Tool]

class Tool(object):

    def __init__(self):

        """Define the tool (tool name is the name of the class)."""

        self.label = "clip and contour"

        self.description = ""

        self.canRunInBackground = False

    def getParameterInfo(self):

  """Define parameter definitions"""

  param0 = arcpy.Parameter(

       displayName="Select shapefile (output extent):",

       name="in_SourceSHP",

        datatype="GPFeatureLayer",

        parameterType="Required",

        direction="Input")

  param1 = arcpy.Parameter(

       displayName="Raster",

       name="in_TargetRaster",

       datatype="GPRasterLayer",

       parameterType="Required",

       direction="Input")

  param2 = arcpy.Parameter(

       displayName="Raster clip",

       name="out_raster",

       datatype="DERasterDataset",

       parameterType="Required",

       direction="Output")

  param3 = arcpy.Parameter(

       displayName="Contour",

       name="out_features",

       datatype="GPFeatureLayer",

       parameterType="Required",

       direction="Output")

  param4 = arcpy.Parameter(

       displayName="Interval contour:",

       name="in_Interval",

       datatype="GPDouble",

       parameterType="Required",

       direction="Input")

  param4.value = 0.5

  params = [param0, param1, param2, param3, param4]

  return params

    def execute(self, parameters, messages):

       """The source code of the tool."""

        mxd =arcpy.mapping.MapDocument("CURRENT")

        Interval = float(parameters[4].valueAsText)

       OutputSHPFile = parameters[3].valueAsText

       RasterFile = parameters[1].valueAsText

       OutRasterFile = parameters[2].valueAsText

       ShapeFileName = parameters[0].valueAsText

       # Process : Clip

       arcpy.Clip_management(RasterFile, "#", OutRasterFile, ShapeFileName, "0", "ClippingGeometry")

       # Process: Contour

       arcpy.Contour_3d(OutRasterFile, OutputSHPFile, Interval, "0", "1")

       # Add shp to mxd

       mxd =arcpy.mapping.MapDocument("CURRENT")

       df = arcpy.mapping.ListDataFrames(mxd)[0]

       addLayer = arcpy.mapping.Layer(OutputSHPFile)

       arcpy.mapping.AddLayer(df, addLayer, "TOP")

       arcpy.RefreshActiveView()

       arcpy.RefreshTOC()

0 Kudos
XanderBakker
Esri Esteemed Contributor

If you want to create a Shapefile, shouldn't you define the parameter type to be "DEShapefile" instead of "GPFeatureLayer"? Normally when using a tool inside ArcMap, the output result is added by default to ArcMap.

Geoprocessing Menu \ Geoprocessing Options, checkbox "Add results of geoprocessing operations to the display".

0 Kudos
SepheFox
Frequent Contributor

Another idea might be to also have the user define the path to the shapefile by adding a workspace parameter.

0 Kudos