2

I captured (stopped) a Layer (featureclass) from being deleted from the Table of contents of arcmap. When i re-add it back, the datasource is broken.

(See question :Using ArcObjects to stop items from being deleted from Table of Contents?)

I'm wondering what I need to cast my ILayer object to, in order to just activate the datasource?

The workspace is sde, but i find it odd that if the whole object(layer) is available when caught by the itemdeleted handlers, why is the datasource off?

I'm having trouble finding the right interface to use.


Here is the code that I have implemented: It readds the layer back to the ToC but the connection is not connected. (Exten is the Addin.extension object for the addin.)

 public void On_TOCItemDeleted(object Item)
        {
            IWorkspaceName workspace;
            var msgText = "You tried to delete an item from the TOC.";

        // check that the item is of type ILayer (lyr file, raster, etc) before doing anything.
        var DeletedLayer = Item as ILayer;

        if (DeletedLayer == null)
            throw new Exception("Not iLayer file");

        IDataLayer2 src = DeletedLayer as IDataLayer2;
        var groupname = FindParentGroup(DeletedLayer.Name);

        if (src.DataSourceName != null)
        {
            workspace = (src.DataSourceName as IDatasetName).WorkspaceName;
        }
        else
        {
            switch (groupname)
            {
                case BLayerName: // basemaps etc
                    workspace = DeletedLayer.SetLayerSDEConnection(Exten.Analysis_WIP);
                    break;

                default: //analysis connection
                    workspace = DeletedLayer.SetAnalysisSDEConnection(Exten.Analysis_WIP);
                    break;
            }
        }
        src.Connect((workspace as IName));
        msgText = string.Format("You tried to delete the layer '{0}'. This operation was stopped.", DeletedLayer.Name);
        //Read the item

        var grouplyr = GetGroupByName(ArcMap.Document.FocusMap.Layers, groupname);

        grouplyr.Add(src as ILayer);

        ArcMap.Document.UpdateContents();
        ArcMap.Document.ActiveView.Refresh();

        Exten.InformUser("App", msgText);
    }
  • 1
    can you show some code? What is the workspace type? – George Silva Jan 7 '16 at 13:38
  • 1
    Cast the layer to IDatalayer2. Maybe the DataSourceName is still populated, and you can just re-connect it. Typically this points to an IFeatureclassName for a featurelayer. – Kirk Kuykendall Jan 7 '16 at 18:15
1

So, I found out that the COM object itelf has already be disposed of; only the metadata is available. Due to this, any attempt to re connect the IPropertySet causes a E_FAIL.

To avoid writing too much 'dumb' code, I have changed my approach to just have internal metadata look up, to find the Lyr file location on the hard disk. Once found from a OpenFileDialog(), I then re-added the layer to the existing Group Layer, if applicable.

The method is able to see if the 'source' to be added is of type IFeatureLayer or IRasterLayer and adds to the map using arcobjects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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