// ArcGIS Snippet Title: // Loop Through Layers of Specific UID // // Long Description: // Stub code to loop through layers in a map where a specific UID is supplied. // // Add the following references to the project: // ESRI.ArcGIS.Carto // ESRI.ArcGIS.System // System // // Intended ArcGIS Products for this snippet: // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView) // ArcGIS Engine // ArcGIS Server // // Applicable ArcGIS Product Versions: // 9.2 // 9.3 // 9.3.1 // 10.0 // // Required ArcGIS Extensions: // (NONE) // // Notes: // This snippet is intended to be inserted at the base level of a Class. // It is not intended to be nested within an existing Method. // ///<summary>Stub code to loop through layers in a map where a specific UID is supplied.</summary> /// ///<param name="map">An IMap interface in which the layers reside.</param> ///<param name="layerCLSID">A System.String that is the layer GUID type. For example: "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" is the IGeoFeatureLayer.</param> /// ///<remarks>In order of the code to be useful the user needs to write their own code to use the layer in the TODO section. /// /// The different layer GUID's and Interface's are: /// "{AD88322D-533D-4E36-A5C9-1B109AF7A346}" = IACFeatureLayer /// "{74E45211-DFE6-11D3-9FF7-00C04F6BC6A5}" = IACLayer /// "{495C0E2C-D51D-4ED4-9FC1-FA04AB93568D}" = IACImageLayer /// "{65BD02AC-1CAD-462A-A524-3F17E9D85432}" = IACAcetateLayer /// "{4AEDC069-B599-424B-A374-49602ABAD308}" = IAnnotationLayer /// "{DBCA59AC-6771-4408-8F48-C7D53389440C}" = IAnnotationSublayer /// "{E299ADBC-A5C3-11D2-9B10-00C04FA33299}" = ICadLayer /// "{7F1AB670-5CA9-44D1-B42D-12AA868FC757}" = ICadastralFabricLayer /// "{BA119BC4-939A-11D2-A2F4-080009B6F22B}" = ICompositeLayer /// "{9646BB82-9512-11D2-A2F6-080009B6F22B}" = ICompositeGraphicsLayer /// "{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E}" = ICoverageAnnotationLayer /// "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}" = IDataLayer /// "{0737082E-958E-11D4-80ED-00C04F601565}" = IDimensionLayer /// "{48E56B3F-EC3A-11D2-9F5C-00C04F6BC6A5}" = IFDOGraphicsLayer /// "{40A9E885-5533-11D0-98BE-00805F7CED21}" = IFeatureLayer /// "{605BC37A-15E9-40A0-90FB-DE4CC376838C}" = IGdbRasterCatalogLayer /// "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" = IGeoFeatureLayer /// "{34B2EF81-F4AC-11D1-A245-080009B6F22B}" = IGraphicsLayer /// "{EDAD6644-1810-11D1-86AE-0000F8751720}" = IGroupLayer /// "{D090AA89-C2F1-11D3-9FEF-00C04F6BC6A5}" = IIMSSubLayer /// "{DC8505FF-D521-11D3-9FF4-00C04F6BC6A5}" = IIMAMapLayer /// "{34C20002-4D3C-11D0-92D8-00805F7C28B0}" = ILayer /// "{E9B56157-7EB7-4DB3-9958-AFBF3B5E1470}" = IMapServerLayer /// "{B059B902-5C7A-4287-982E-EF0BC77C6AAB}" = IMapServerSublayer /// "{82870538-E09E-42C0-9228-CBCB244B91BA}" = INetworkLayer /// "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}" = IRasterLayer /// "{AF9930F0-F61E-11D3-8D6C-00C04F5B87B2}" = IRasterCatalogLayer /// "{FCEFF094-8E6A-4972-9BB4-429C71B07289}" = ITemporaryLayer /// "{5A0F220D-614F-4C72-AFF2-7EA0BE2C8513}" = ITerrainLayer /// "{FE308F36-BDCA-11D1-A523-0000F8774F0F}" = ITinLayer /// "{FB6337E3-610A-4BC2-9142-760D954C22EB}" = ITopologyLayer /// "{005F592A-327B-44A4-AEEB-409D2F866F47}" = IWMSLayer /// "{D43D9A73-FF6C-4A19-B36A-D7ECBE61962A}" = IWMSGroupLayer /// "{8C19B114-1168-41A3-9E14-FC30CA5A4E9D}" = IWMSMapLayer ///</remarks> public void LoopThroughLayersOfSpecificUID(ESRI.ArcGIS.Carto.IMap map, System.String layerCLSID) { if (map == null || layerCLSID == null) { return; } ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass(); uid.Value = layerCLSID; // Example: "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" = IGeoFeatureLayer try { ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast enumLayer.Reset(); ESRI.ArcGIS.Carto.ILayer layer = enumLayer.Next(); cboLayers.Items.Clear(); lstLayers.Clear(); while (!(layer == null)) { IFeatureLayer featureLayer = (IFeatureLayer)layer; if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { cboLayers.Items.Add(layer.Name); lstLayers.Add(layer); } layer = enumLayer.Next(); } if (cboLayers.Items.Count > 0) { cboLayers.SelectedIndex = 0; } } catch (System.Exception ex) { //System.Windows.Forms.MessageBox.Show("No layers of type: " + uid.Value.ToString); } }
private ILayer getLayerByName(string name) { UID pid = new UID(); pid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; ESRI.ArcGIS.Carto.IMap map = ArcMap.Document.ActiveView.FocusMap; IEnumLayer layers = map.get_Layers(pid, true); ILayer curLayer = layers.Next(); while (curLayer != null) { //System.Windows.Forms.MessageBox.Show("curLayer name is " + curLayer.Name); if (curLayer.Name == name) { return(curLayer); } curLayer = layers.Next(); } return(null); }
public bool CreateMxd(string sMxdTemplate, string sPathToMXD, string ArcGISServer, string sMxdFile, string sDBConn, string sDataSet, bool bSde) { if (sMxdTemplate.Length > 0) { _sMxdTemplate = sMxdTemplate; } _sPathToMXD = sPathToMXD; _ArcGISServer = ArcGISServer; ESRI.ArcGIS.Carto.IMap pMap = null; IFeatureClass pOldFC = null; string fcName = String.Empty; string sSuffix = String.Empty; IWorkspaceFactory2 wsf = null; IWorkspace2 ws2 = null; IFeatureWorkspace fws = null; IWorkspace ws = null; try { if (bSde) { // Get WS for SDE ws = ArcSdeWorkspaceFromFile(sDBConn); } else { // Get WS from file GDB. wsf = new FileGDBWorkspaceFactoryClass() as IWorkspaceFactory2; //if locks on gdb only path is passed in string fileGdb = sDBConn.Contains(".gdb") ? sDBConn : sDBConn; if (wsf.IsWorkspace(fileGdb)) { ws = wsf.OpenFromFile(fileGdb, 0); } } if (ws == null) { return(false); } // Check if Mxd already exists if (File.Exists(sMxdFile)) { return(false); } // Create a Mxd from Overlays Template pMap = PrivateCreateMxd(sMxdFile); ws2 = (IWorkspace2)ws; fws = (IFeatureWorkspace)ws; // Loop through all layers in MXD and repoint data source to OverlayGDB Features IEnumLayer pEnumLayer = pMap.get_Layers(null, true); pEnumLayer.Reset(); ILayer pLayer = pEnumLayer.Next(); while (pLayer != null) { if (!(pLayer is IFeatureLayer)) { pLayer = pEnumLayer.Next(); continue; } // Cast pLayer to featurelayer IFeatureLayer pMapFeatureLayer = (IFeatureLayer)pLayer; pOldFC = pMapFeatureLayer.FeatureClass; if (pOldFC == null) { pLayer = pEnumLayer.Next(); continue; } // Get FC name IDataset pDS = (IDataset)pOldFC; fcName = pDS.Name; // Feature Class: <Dataset>_osm_pt, <Dataset>_osm_ln, <Dataset>_osm_ply sSuffix = fcName.Substring(fcName.IndexOf("_osm_")); if (String.IsNullOrEmpty(sSuffix)) { continue; } // Check if feature class exists in GDB if (ws2.get_NameExists(esriDatasetType.esriDTFeatureClass, sDataSet + sSuffix)) { // Get feature class IFeatureClass ipFC = fws.OpenFeatureClass(sDataSet + sSuffix); IFeatureLayer ipFL = (IFeatureLayer)pLayer; // Create IMapAdmin2 from pMap IMapAdmin2 pMapAdmin2 = (IMapAdmin2)pMap; // Change FeatureClass of layer to FC in FGDB ipFL.FeatureClass = ipFC; pMapAdmin2.FireChangeFeatureClass(pOldFC, ipFC); COMUtil.ReleaseObject(ipFC); ipFC = null; COMUtil.ReleaseObject(ipFL); ipFL = null; } else { // Remove layer from map pMap.DeleteLayer(pLayer); } pLayer = pEnumLayer.Next(); } SaveMXD(sMxdFile, pMap); return(true); } catch (System.Runtime.InteropServices.COMException cx) { throw cx; } catch (Exception ex) { throw ex; } finally { COMUtil.ReleaseObject(pOldFC); COMUtil.ReleaseObject(fws); COMUtil.ReleaseObject(ws2); COMUtil.ReleaseObject(ws); COMUtil.ReleaseObject(pMap); COMUtil.ReleaseObject(wsf); pOldFC = null; fws = null; ws2 = null; ws = null; wsf = null; pMap = null; _pMapDocument = null; //Do not make any call to ArcObjects after ShutDownApplication() if (m_AOLicenseInitializer != null) { m_AOLicenseInitializer.ShutdownApplication(); } m_AOLicenseInitializer = null; } }