Select to view content in your preferred language

AddLayer only temporarily adds the layer to map

4429
11
07-15-2010 09:37 AM
ChristianHalsted
Occasional Contributor
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
ionarawilson1
Deactivated User
I think it might not have to do with the lenght of the array collection. When I add another if clause ( else if (ac.length == 3)), that filter that I had before (when I typed "ref" and filtered two values to the array collection) stopped working. So it  might have to do with the creation of another variable actually. I noticed that if I don't create qText4, then the filter for the two values work. So maybe the function is not executing anymore since when it filters to two values, it cannot get the third value and it therefore cannot create that 3rd variable that goes in the if clause. Do you have any idea why this is happening? Thanks!

 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
   
    var qText4:String = ac.getItemAt(2).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 == 3)
        {
         
         Alert.show("TEST AC LENGTH 3")
         
         var defexpr3:String =  searchattribute +  eqsymbol +  "'"  +  qText2  + "'" + " OR " + searchattribute +  eqsymbol +  "'"  +  qText3  + "'" + " OR " + searchattribute +  eqsymbol +  "'"  +  qText4  + "'";
         
         yourTable.definitionExpression = defexpr3
         Alert.show(defexpr3)
         
         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
MathiasKP
Deactivated User
These lines will add a layer with SP10.1:
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")

but if you try other things like using the Layer.replaceDataSource method the layer will disappear again. I have not tried all methods/properties combinations but they might fail as well as some of you have experienced.

/Mathias
0 Kudos