AddLayer only temporarily adds the layer to map

3594
11
07-15-2010 09:37 AM
ChristianHalsted
New Contributor II
When I run the following code:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
addLayer = arcpy.mapping.Layer(r'G:\data_layers\political\Towns_Maine_polys.lyr')
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")

from the Python Window in ArcMap it works fine.  But when I put it in a script tool and run it from ArcToolbox I see the layer get added to the data frame but then it immediately disappears.
I've tried using InsertLayer but get the same result.

Any idea what is happening?
0 Kudos
11 Replies
ChristianHalsted
New Contributor II
Turns out this is a known bug.  [#NIM052741  Feature layer fails to persist in ArcMap table of contents when added through the AddLayer function in script tool]  It was logged in final beta but a fix wasn't made for the final release. 

Too bad, this is a pretty important method for map automation.
0 Kudos
TomWillis
New Contributor
According to the Resource Centre, this bug was fixed with Service Pack 1 for 10.0, and with version 10.1. Anyone know where to download SP1?
0 Kudos
ChrisMathers
Occasional Contributor III
SP1 isnt out yet. Its expected within the next 3 weeks according to our local rep.
0 Kudos
gis4fungis4fun
New Contributor
Any confirmation this bug is fixed with 10SP1?
0 Kudos
MikeHunter
Occasional Contributor
Any confirmation this bug is fixed with 10SP1?


It seems to be fixed.  I was getting the same behavior (layer quickly disappears) before loading sp1, now it works fine.

Mike
0 Kudos
WadimKlincov
New Contributor
Hi,

I'm very sorry, but I still get this error with 10.1 SP1.

def CreateTestLayer(self, img, trainshp):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        lyrFile = arcpy.mapping.Layer(img + '.lyr')
        arcpy.mapping.AddLayer(df, lyrFile)
        arcpy.MakeRasterLayer_management(img, "rdlayer", "#", trainshp , "1")
        mxd.save()

Any ideas on why it still disappears?

E: If there already is a layer, it adds new layers. But when I do not have any layers then they disappear again.
0 Kudos
ionarawilson1
Occasional Contributor III
Hi Wadim,

Did you figure out why this is happening? I have 10.1 sp1, and I am having the same problem. Thanks
0 Kudos
AndrewTewkesbury
New Contributor
I'm also having this exact problem in 10.1 SP1. Does anyone know of a fix, or a patch?
0 Kudos
ionarawilson1
Occasional Contributor III
Robert, can you tell me how I can change the code so the filter also works for filters that have only one value? For example, if I type "Ref", I get two values, so it works, I get the  qText2 and qText3 (the codes used for the definition expression). However if  I type "Vendor", I don't get the codes (confirmed by using the Alert.show method) but I can get the name (itemname, confirmed by the Alert.show method), and I don't get an alert to show that the array collection is of length = 1. I am guessing it is because the filter does not work if there is only one value on the array collection? Is it possible to change that? Here is my code. Thank you so much! This is great!

   
       [Bindable] private var ac:ArrayCollection;
   
   
   public function filter(item:Object):Boolean
   {
    var name:String = String(item["name"]);
    var beginsWithString:String = qText.text.toLowerCase();
    
    return name.toLowerCase().indexOf(beginsWithString) > -1;
    
   }
   
   
   private function doSearch():void
   {
    myAttributeTable.refresh();
    myAttributeTable.visible = false
    ac = new ArrayCollection();
    ac.addItem({name:"Alpine",code:"AL"});
    ac.addItem({name:"Austin",code:"AU"});
    ac.addItem({name:"Referral - Consulting Forester",code:"RC"});
    ac.addItem({name:"Referral Vendor", code:"RV"});
    ac.addItem({name:"Incidental Rural Forestry",code:"AIR"});
    ac.addItem({name:"Prevention and Reduction of Pest Losses",code:"AP"});
    ac.addItem({name:"Forest Health Monitoring - Survey",code:"FHS"});
    myAttributeTable.featureLayer = myFeatureLayer
    
   
    myAttributeTable.featureLayer = yourTable
    myAttributeTable.visible = true
      
    
    ac.filterFunction = filter;
    ac.refresh()
     
    var itemname:String = ac.getItemAt(0).name
    Alert.show(itemname)
    var itemname1:String = ac.getItemAt(1).name
    Alert.show(itemname1)
    
       var qText2:String  = ac.getItemAt(0).code
          var qText3:String = ac.getItemAt(1).code
    
    Alert.show(qText2)
    Alert.show(qText3)

    yourTable.refresh()
    
    
    if (ac.length == 1)
    {
     Alert.show("TEST AC LENGTH 1")
    var defexpr:String =  searchattribute +  "like" +  "'"  +  qText2  + "'";
    yourTable.definitionExpression = defexpr

  
    }
    
    else if (ac.length == 2)
    {
     
     Alert.show("TEST AC LENGTH 2")
     
     var defexpr2:String =  searchattribute +  eqsymbol +  "'"  +  qText2  + "'" + " OR " + searchattribute +  eqsymbol +  "'"  +  qText3  + "'";
    
     yourTable.definitionExpression = defexpr2
     
     
     myAttributeTable.featureLayer = yourTable
     myAttributeTable.visible = true
    }
    else if (ac.length > 2)
    {
     Alert.show("TEST AC LENGTH ELSE")
     myAttributeTable.visible = false
       Alert.show("Please review your search and try again")
    }
    
    
 }
0 Kudos