Ejemplo n.º 1
0
        /// <summary>
        /// Gets the features from the selection that are from a given ft class
        /// </summary>
        /// <param name="ftClassName">ft class name</param>
        /// <returns>List of IFeature</returns>
        public List <ESRI.ArcGIS.Geodatabase.IFeature> GetSelectedFeatures(IFeatureLayer layer)
        {
            List <ESRI.ArcGIS.Geodatabase.IFeature> result = new List <ESRI.ArcGIS.Geodatabase.IFeature>();

            if (layer == null)
            {
                return(result);
            }

            ESRI.ArcGIS.Carto.IFeatureSelection ftSelection = layer as ESRI.ArcGIS.Carto.IFeatureSelection;

            if (null != ftSelection &&
                null != ftSelection.SelectionSet)
            {
                ESRI.ArcGIS.Geodatabase.ISelectionSet selectionSet = ftSelection.SelectionSet;
                ESRI.ArcGIS.Geodatabase.ICursor       cursor       = null;

                try
                {
                    selectionSet.Search(null, false, out cursor);
                    ESRI.ArcGIS.Geodatabase.IRow ft = cursor.NextRow();
                    while (null != ft)
                    {
                        result.Add((ESRI.ArcGIS.Geodatabase.IFeature)ft);
                        ft = cursor.NextRow();
                    }
                }
                finally
                {
                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(cursor);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void LoadAnalysisObjectsByGeometry(ESRI.ArcGIS.Geodatabase.IFeatureClass
                                                  inputFeatureClass, string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext
                                                  naContext)
        {
            // Both Initialize and Load take a cursor from the input feature class
            ESRI.ArcGIS.Geodatabase.ICursor cursor = inputFeatureClass.Search(null, false) as
                                                     ESRI.ArcGIS.Geodatabase.ICursor;

            // Initialize the default field mappings.
            // If you want to specify field mappings beyond the default ones, use naClassLoader.FieldMap to retrieve
            //  and edit the mappings between the input class and the naclass.
            ESRI.ArcGIS.NetworkAnalyst.INAClassLoader2 naClassLoader = new
                                                                       ESRI.ArcGIS.NetworkAnalyst.NAClassLoaderClass();
            naClassLoader.Initialize(naContext, naClassName, cursor);

            // Use ExcludeRestrictedElements and CacheRestrictedElements to prevent locations from being placed on restricted elements.
            // Some ways to restrict elements include restriction barriers and restriction attributes.
            // If you are loading barriers into barrier classes, or not loading locations (for example, seedpoints)
            //  then you should not exclude the restricted elements.  Also, if you do have barriers in your analysis problem,
            //  then you should load those first, to make sure the restricted elements are established before loading
            //  non-barrier classes.
            ESRI.ArcGIS.NetworkAnalyst.INALocator3 naLocator3 = naClassLoader.Locator as
                                                                ESRI.ArcGIS.NetworkAnalyst.INALocator3;
            naLocator3.ExcludeRestrictedElements = true;
            naLocator3.CacheRestrictedElements(naContext);

            // After Loading is complete, the rowsIn and rowsLocated variable can be used to verify
            //  that every row from the input feature class has been loaded into the network analysis class
            int rowsIn      = 0;
            int rowsLocated = 0;

            naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated);
        }
        /// <summary>
        /// Gets the fiber record for a given fiber number on a given cable ft
        /// </summary>
        /// <param name="cableFeature"></param>
        /// <param name="fiberNumber"></param>
        /// <returns>IRow</returns>
        private ESRI.ArcGIS.Geodatabase.IRow GetFiberRecord(ESRI.ArcGIS.Geodatabase.IFeature cableFeature, int fiberNumber)
        {
            ESRI.ArcGIS.Geodatabase.IRow result = null;

            ESRI.ArcGIS.Geodatabase.IRelationshipClass fiberRelationship = GdbUtils.GetRelationshipClass(cableFeature.Class, ConfigUtil.FiberCableToFiberRelClassName);
            if (null != fiberRelationship && null != fiberRelationship.DestinationClass)
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberTable = fiberRelationship.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null != fiberTable)
                {
                    ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    filter.WhereClause = string.Format("{0}='{1}' AND {2}={3}",
                                                       fiberRelationship.OriginForeignKey,
                                                       cableFeature.get_Value(cableFeature.Fields.FindField(fiberRelationship.OriginPrimaryKey)),
                                                       ConfigUtil.Fiber_NumberFieldName,
                                                       fiberNumber);

                    ESRI.ArcGIS.Geodatabase.ICursor cursor = null;
                    try
                    {
                        cursor = fiberTable.Search(filter, false);
                        result = cursor.NextRow();
                    }
                    finally
                    {
                        if (null != cursor)
                        {
                            ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(cursor);
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Finds the connected device/port
        /// </summary>
        /// <param name="siblingFtClass">Any feature class from the workspace</param>
        /// <param name="cableId">Cable ID to check connx for</param>
        /// <param name="fiberNumber">Fiber Number to check connx for</param>
        /// <param name="isFromEnd">Whether to check the cable's from or to end</param>
        /// <param name="portRow">(out) result port</param>
        /// <param name="deviceFt">(out) result device</param>
        /// <returns>True if a connx was found</returns>
        private bool GetConnectedPort(ESRI.ArcGIS.Geodatabase.IFeatureClass siblingFtClass, string cableId, int fiberNumber, bool isFromEnd, out ESRI.ArcGIS.Geodatabase.IRow portRow, out ESRI.ArcGIS.Geodatabase.IFeature deviceFt)
        {
            portRow  = null;
            deviceFt = null;

            bool result = false;

            string[] portTableNames = ConfigUtil.PortTableNames;
            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                                                   ConfigUtil.ConnectedCableFieldName,
                                                   cableId,
                                                   ConfigUtil.ConnectedFiberFieldName,
                                                   fiberNumber,
                                                   ConfigUtil.ConnectedEndFieldName,
                                                   isFromEnd ? "T" : "F");
                releaser.ManageLifetime(filter);

                for (int i = 0; i < portTableNames.Length; i++)
                {
                    string portTableName = portTableNames[i];
                    ESRI.ArcGIS.Geodatabase.ITable  portTable = _wkspHelper.FindTable(portTableName);
                    ESRI.ArcGIS.Geodatabase.ICursor cursor    = portTable.Search(filter, false);
                    releaser.ManageLifetime(cursor);

                    portRow = cursor.NextRow();
                    if (null != portRow)
                    {
                        ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetDeviceRelationship(portTable);
                        if (null == deviceHasPorts)
                        {
                            throw new Exception("Device to port relationship is missing or cannot be opened.");
                        }

                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceClass = deviceHasPorts.OriginClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                        if (null == deviceClass)
                        {
                            throw new Exception("Device feature class is missing or cannot be opened.");
                        }

                        filter.WhereClause = string.Format("{0}='{1}'",
                                                           deviceHasPorts.OriginPrimaryKey,
                                                           portRow.get_Value(portTable.FindField(deviceHasPorts.OriginForeignKey)));
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor deviceCursor = deviceClass.Search(filter, false);
                        deviceFt = deviceCursor.NextFeature();

                        result = true;
                        break;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public void LoadAnalysisObjectsByField(ESRI.ArcGIS.Geodatabase.ITable inputClass,
                                               string naClassName, ESRI.ArcGIS.NetworkAnalyst.INAContext naContext)
        {
            // Both Initialize and Load take a cursor from the input class
            ESRI.ArcGIS.Geodatabase.ICursor cursor = inputClass.Search(null, false) as
                                                     ESRI.ArcGIS.Geodatabase.ICursor;
            ESRI.ArcGIS.NetworkAnalyst.INAClassLoader2 naClassLoader = new
                                                                       ESRI.ArcGIS.NetworkAnalyst.NAClassLoaderClass();
            naClassLoader.Initialize(naContext, naClassName, cursor);

            // Store the current set of locator agents, so they can be added back later
            int agentCount   = naContext.Locator.LocatorAgentCount;
            var listOfAgents = new System.Collections.Generic.List <
                ESRI.ArcGIS.NetworkAnalyst.INALocatorAgent>();

            for (int locIndex = 0; locIndex < agentCount; locIndex++)
            {
                listOfAgents.Add(naContext.Locator.get_LocatorAgent(locIndex));
            }

            // Remove the existing locator agents from the locator
            // This for loop is done in reverse order, because agents are being removed as the loop executes
            for (int locIndex = agentCount - 1; locIndex >= 0; locIndex--)
            {
                naContext.Locator.RemoveLocatorAgent(locIndex);
            }

            // Create and add a fields agent
            var fieldsAgent = new
                              ESRI.ArcGIS.NetworkAnalyst.NALocatorLocationFieldsAgentClass() as
                              ESRI.ArcGIS.NetworkAnalyst.INALocatorLocationFieldsAgent2;

            // Set the field names appropriately based on input data and NAClass
            var naClass = naContext.NAClasses.get_ItemByName(naClassName) as
                          ESRI.ArcGIS.NetworkAnalyst.INAClass;
            var naFeatureClass = naClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;

            // Check to see if the NAClass is of type NALocation or NALocationRanges
            ESRI.ArcGIS.esriSystem.UID naLocationFeatureUID = new
                                                              ESRI.ArcGIS.esriSystem.UIDClass();
            naLocationFeatureUID.Value = "esriNetworkAnalyst.NALocationFeature";
            ESRI.ArcGIS.esriSystem.UID naLocationFeatureRangesUID = new
                                                                    ESRI.ArcGIS.esriSystem.UIDClass();
            naLocationFeatureRangesUID.Value = "esriNetworkAnalyst.NALocationRangesFeature";
            if (naFeatureClass.CLSID.Compare(naLocationFeatureUID))
            {
                // The field names listed below are the names used in ArcGIS Network Analyst extension classes to represent NALocations.
                //  These are also the names of fields added by the CalculateLocations geoprocessing tool
                fieldsAgent.OIDFieldName      = "SourceOID";
                fieldsAgent.SourceIDFieldName = "SourceID";
                fieldsAgent.PositionFieldName = "PosAlong";
                fieldsAgent.SideFieldName     = "SideOfEdge";
            }
            else if (naFeatureClass.CLSID.Compare(naLocationFeatureRangesUID))
            {
                // The location ranges input field must be of type BLOB
                fieldsAgent.LocationRangesFieldName = "Locations";
                var blobField = inputClass.Fields.get_Field(inputClass.FindField
                                                                (fieldsAgent.LocationRangesFieldName));
                if (blobField.Type !=
                    ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob)
                {
                    System.Windows.Forms.MessageBox.Show(
                        "Loading location ranges by field requires a blob field");
                    return;
                }
            }
            naContext.Locator.AddLocatorAgent(fieldsAgent as
                                              ESRI.ArcGIS.NetworkAnalyst.INALocatorAgent);

            // After Loading is complete, the rowsIn and rowsLocated variable can be used to verify
            //  that every row from the input feature class has been loaded into the network analysis class
            int rowsIn      = 0;
            int rowsLocated = 0;

            naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated);

            // Now remove the custom fields agent and add back the stored agents
            naContext.Locator.RemoveLocatorAgent(0);
            foreach (var agent in listOfAgents)
            {
                naContext.Locator.AddLocatorAgent(agent);
            }
        }
        /// <summary>
        /// Creates given connections between cable and device
        /// </summary>
        /// <param name="cable">Cable</param>
        /// <param name="device">Device</param>
        /// <param name="units">Units to connect</param>
        /// <param name="isFromEnd">Is it the cable's from end?</param>
        /// <param name="portType">Input or Output?</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in a new edit operation</param>
        /// <returns>Success</returns>
        public bool MakeConnections(FiberCableWrapper cable, DeviceWrapper device, Dictionary <int, int> units, bool isFromEnd, PortType portType, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            if (null == device)
            {
                throw new ArgumentNullException("device");
            }

            if (null == units)
            {
                throw new ArgumentNullException("units");
            }

            if (ESRI.ArcGIS.Editor.esriEditState.esriStateNotEditing == _editor.EditState)
            {
                throw new InvalidOperationException("You must be editing to perform this operation");
            }
            #endregion

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to get port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                int    portIdIdx      = portTable.FindField(ConfigUtil.PortIdFieldName);
                int    fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);
                int    cableIdIdx     = portTable.FindField(ConfigUtil.ConnectedCableFieldName);
                int    isFromEndIdx   = portTable.FindField(ConfigUtil.ConnectedEndFieldName);
                string isFromEndValue = isFromEnd ? "T" : "F";

                Dictionary <int, int> portsAsKeys = units;
                if (PortType.Input == portType)
                {
                    portsAsKeys = new Dictionary <int, int>();
                    foreach (KeyValuePair <int, int> pair in units)
                    {
                        portsAsKeys[pair.Value] = pair.Key;
                    }
                }

                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    releaser.ManageLifetime(filter);

                    string format = "{0}='{1}' AND {2}='{3}'";
                    filter.WhereClause = string.Format(format,
                                                       deviceHasPorts.OriginForeignKey,
                                                       device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                       ConfigUtil.PortTypeFieldName,
                                                       (PortType.Input == portType ? "1" : "2"));

                    // Non recylcing cursor since we are doing updates.
                    ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Update(filter, false);
                    releaser.ManageLifetime(portCursor);

                    ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();
                    while (null != portRow)
                    {
                        object portIdObj = portRow.get_Value(portIdIdx);
                        if (DBNull.Value != portIdObj)
                        {
                            int portId = System.Convert.ToInt32(portIdObj);
                            if (portsAsKeys.ContainsKey(portId))
                            {
                                portRow.set_Value(cableIdIdx, cable.IPID);
                                portRow.set_Value(isFromEndIdx, isFromEndValue);
                                portRow.set_Value(fiberNumberIdx, portsAsKeys[portId]);
                                portRow.Store();
                            }
                        }

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                        portRow = portCursor.NextRow();
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Create Connections");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch (Exception ex)
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;

                throw new Exception("Save operation failed.");
            }

            return(success);
        }
        /// <summary>
        /// Returns a list of connections between the cable and the device, at the cable's given end, to the device's given port type
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="device">Device to check</param>
        /// <param name="isFromEnd">Digitized end of cable</param>
        /// <param name="portType">Input or output</param>
        /// <returns>List of Connection</returns>
        public List <Connection> GetConnections(FiberCableWrapper cable, DeviceWrapper device, bool isFromEnd, PortType portType)
        {
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            if (null == device)
            {
                throw new ArgumentNullException("device");
            }

            List <Connection> result  = new List <Connection>();
            List <int>        ports   = new List <int>();
            List <int>        strands = new List <int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to find port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4}='{5}' AND {6}='{7}' AND {8} IS NOT NULL AND {9} IS NOT NULL",
                                                   deviceHasPorts.OriginForeignKey,
                                                   device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                   ConfigUtil.ConnectedCableFieldName,
                                                   cable.IPID,
                                                   ConfigUtil.PortTypeFieldName,
                                                   (PortType.Input == portType ? "1" : "2"),
                                                   ConfigUtil.ConnectedEndFieldName,
                                                   (isFromEnd ? "T" : "F"),
                                                   ConfigUtil.ConnectedFiberFieldName,
                                                   ConfigUtil.PortIdFieldName);


                // ORDER BY does not work outside of SDE.
                // Removing for now, should not be important.
                string orderFormat = "ORDER BY {0}";
                if (PortType.Input == portType)
                {
//                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.ConnectedFiberFieldName);
                }
                else
                {
//                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.PortIdFieldName);
                }

                ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                ESRI.ArcGIS.Geodatabase.IRow    portRow    = portCursor.NextRow();

                int portIdIdx      = portTable.FindField(ConfigUtil.PortIdFieldName);
                int fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);

                while (null != portRow)
                {
                    ports.Add((int)portRow.get_Value(portIdIdx));
                    strands.Add((int)portRow.get_Value(fiberNumberIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                    portRow = portCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
            }


            List <Range> portRanges   = SpliceAndConnectionUtils.MergeRanges(ports);
            List <Range> strandRanges = SpliceAndConnectionUtils.MergeRanges(strands);

            if (PortType.Input == portType)
            {
                result = SpliceAndConnectionUtils.MatchUp(strandRanges, portRanges);
            }
            else
            {
                result = SpliceAndConnectionUtils.MatchUp(portRanges, strandRanges);
            }

            return(result);
        }
        private FiberCableWrapper GetConnectedFiber(DeviceWrapper device, int portId, PortType portType, out int fiberNumber)
        {
            FiberCableWrapper result = null;

            fiberNumber = -1;

            ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = (ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Feature.Class;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
            if (null == deviceHasPorts)
            {
                throw new Exception("Device to port relationship is missing or cannot be opened.");
            }

            ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
            if (null == portTable)
            {
                throw new Exception("Port table is missing or cannot be opened.");
            }


            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                                                   deviceHasPorts.OriginForeignKey,
                                                   device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                   ConfigUtil.PortIdFieldName,
                                                   portId,
                                                   ConfigUtil.PortTypeFieldName,
                                                   PortType.Input == portType ? 1 : 2);

                ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, false);
                releaser.ManageLifetime(cursor);
                ESRI.ArcGIS.Geodatabase.IRow portRow = cursor.NextRow();

                if (null != portRow)
                {
                    //releaser.ManageLifetime(portRow);

                    object cableIdValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedCableFieldName));
                    if (DBNull.Value != cableIdValue)
                    {
                        ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = _wkspHelper.FindFeatureClass(ConfigUtil.FiberCableFtClassName);
                        filter.WhereClause = string.Format("{0}='{1}'", ConfigUtil.IpidFieldName, cableIdValue);
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor cableCursor = cableFtClass.Search(filter, false);
                        releaser.ManageLifetime(cableCursor);

                        ESRI.ArcGIS.Geodatabase.IFeature cable = cableCursor.NextFeature();
                        if (null != cable)
                        {
                            result = new FiberCableWrapper(cable);
                            object fiberNumberValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedFiberFieldName));
                            if (DBNull.Value != fiberNumberValue)
                            {
                                int.TryParse(fiberNumberValue.ToString(), out fiberNumber);
                            }
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Looks for a fiber splice record at one end of a given cable
        /// </summary>
        /// <param name="fiberSpliceTable">Fiber splice table</param>
        /// <param name="cableId">Cable ID of the cable we are checking</param>
        /// <param name="fiberNumber">Fiber Number we are checking</param>
        /// <param name="checkFromEnd">Which end of the cable are we checking?</param>
        /// <param name="nextCableId">(out) Cable ID of the cable spliced on this end, or string.Empty if none</param>
        /// <param name="nextFiberNumber">(out) Fiber Number of spliced on this end, or -1 if none</param>
        /// <param name="spliceClosureIpid">(out) IPID of Splice Closure</param>
        /// <param name="isNextFromEnd">(out) Is the result cable spliced on its from end or its to end?</param>
        /// <returns>The splice record</returns>
        private ESRI.ArcGIS.Geodatabase.IRow GetNextSplice(ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable, string cableId, int fiberNumber, bool checkFromEnd, out string nextCableId, out int nextFiberNumber, out string spliceClosureIpid, out bool isNextFromEnd)
        {
            ESRI.ArcGIS.Geodatabase.IRow spliceRow = null;

            spliceClosureIpid = string.Empty;
            nextCableId       = cableId;
            nextFiberNumber   = fiberNumber;
            isNextFromEnd     = checkFromEnd;

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("({0}='{1}' AND {2}={3} AND {4}='{5}')"
                                                   + " OR ({6}='{1}' AND {7}={3} AND {8}='{5}')",
                                                   ConfigUtil.ACableIdFieldName,
                                                   cableId,
                                                   ConfigUtil.AFiberNumberFieldName,
                                                   fiberNumber,
                                                   ConfigUtil.IsAFromEndFieldName,
                                                   (checkFromEnd ? "T" : "F"),
                                                   ConfigUtil.BCableIdFieldName,
                                                   ConfigUtil.BFiberNumberFieldName,
                                                   ConfigUtil.IsBFromEndFieldName);

                releaser.ManageLifetime(filter);

                // TODO: should we give a warning if the rowcount is more than 1? We should technically only find one splice
                // record on this end of the fiber...

                ESRI.ArcGIS.Geodatabase.ICursor search = fiberSpliceTable.Search(filter, false);
                releaser.ManageLifetime(search);

                spliceRow = search.NextRow();
                if (null != spliceRow)
                {
                    object scIpidValue = spliceRow.get_Value(_spliceClosureIpidIdx);
                    if (DBNull.Value != scIpidValue)
                    {
                        spliceClosureIpid = scIpidValue.ToString();
                    }

                    string aCableId = spliceRow.get_Value(_aCableIdx).ToString();
                    if (0 == string.Compare(aCableId, cableId))
                    {
                        // b is the one we want to return
                        nextCableId     = spliceRow.get_Value(_bCableIdx).ToString();
                        nextFiberNumber = (int)spliceRow.get_Value(_bFiberNumIdx);
                        isNextFromEnd   = spliceRow.get_Value(_isBFromIdx).ToString() == "T" ? true : false;
                    }
                    else
                    {
                        // a is the one we want to return
                        nextCableId     = aCableId;
                        nextFiberNumber = (int)spliceRow.get_Value(_aFiberNumIdx);
                        isNextFromEnd   = spliceRow.get_Value(_isAFromIdx).ToString() == "T" ? true : false;
                    }
                }
            }

            return(spliceRow);
        }
        ///// <summary>
        ///// The active view has refreshed. Redraw our results, if we have any
        ///// </summary>
        ///// <param name="Display">Display to draw on</param>
        ///// <param name="phase"></param>
        //private void _arcMapWrapper_ActiveViewAfterDraw(ESRI.ArcGIS.Display.IDisplay Display, ESRI.ArcGIS.Carto.esriViewDrawPhase phase)
        //{
        //    if (phase == ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection)
        //    {
        //        // Draw after the selection
        //        if (null != _currentResults)
        //        {
        //            ESRI.ArcGIS.Display.ILineSymbol lineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbol();
        //            ESRI.ArcGIS.Display.IRgbColor color = new ESRI.ArcGIS.Display.RgbColorClass();
        //            color.Red = 255;
        //            color.Green = 0;
        //            color.Blue = 0;

        //            lineSymbol.Color = color;
        //            lineSymbol.Width = 4;

        //            ESRI.ArcGIS.Display.ISimpleMarkerSymbol markerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
        //            markerSymbol.Color = color;
        //            markerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
        //            markerSymbol.Size = 6;

        //            for (int i = 0; i < _currentResults.Count; i++)
        //            {
        //                ESRI.ArcGIS.Geometry.IGeometry geometry = _currentResults[i];
        //                if (geometry is ESRI.ArcGIS.Geometry.IPolyline)
        //                {
        //                    Display.SetSymbol((ESRI.ArcGIS.Display.ISymbol)lineSymbol);
        //                    Display.DrawPolyline((ESRI.ArcGIS.Geometry.IPolyline)geometry);
        //                }
        //                else if (geometry is ESRI.ArcGIS.Geometry.IPoint)
        //                {
        //                    Display.SetSymbol((ESRI.ArcGIS.Display.ISymbol)markerSymbol);
        //                    Display.DrawPoint((ESRI.ArcGIS.Geometry.IPoint)geometry);
        //                }
        //            }
        //        }
        //    }
        //}

        private List <ESRI.ArcGIS.Geodatabase.IRow> TracePath(ESRI.ArcGIS.Geodatabase.IFeature cableFeature, int fiberNumber, bool isStartingAtFromEnd)
        {
            List <ESRI.ArcGIS.Geodatabase.IRow> result = new List <ESRI.ArcGIS.Geodatabase.IRow>();

            string ipid = cableFeature.get_Value(cableFeature.Fields.FindField(ConfigUtil.IpidFieldName)).ToString();

            ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = (ESRI.ArcGIS.Geodatabase.IFeatureClass)cableFeature.Class;

            ESRI.ArcGIS.Geodatabase.IFeatureClass spliceFtClass    = _wkspHelper.FindFeatureClass(ConfigUtil.SpliceClosureFtClassName);
            ESRI.ArcGIS.Geodatabase.ITable        fiberSpliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);

            ESRI.ArcGIS.Geodatabase.IFields spliceFields = fiberSpliceTable.Fields;

            string fiberClassName = ConfigUtil.FiberTableName;

            ESRI.ArcGIS.Geodatabase.IRelationshipClass fiberRelationship = GdbUtils.GetRelationshipClass(cableFtClass, ConfigUtil.FiberCableToFiberRelClassName);
            if (null != fiberRelationship && null != fiberRelationship.DestinationClass)
            {
                fiberClassName = GdbUtils.ParseTableName(fiberRelationship.DestinationClass as ESRI.ArcGIS.Geodatabase.IDataset);
            }

            ESRI.ArcGIS.Geodatabase.ITable fiberTable = _wkspHelper.FindTable(fiberClassName);

            _aCableIdx            = spliceFields.FindField(ConfigUtil.ACableIdFieldName);
            _bCableIdx            = spliceFields.FindField(ConfigUtil.BCableIdFieldName);
            _aFiberNumIdx         = spliceFields.FindField(ConfigUtil.AFiberNumberFieldName);
            _bFiberNumIdx         = spliceFields.FindField(ConfigUtil.BFiberNumberFieldName);
            _isAFromIdx           = spliceFields.FindField(ConfigUtil.IsAFromEndFieldName);
            _isBFromIdx           = spliceFields.FindField(ConfigUtil.IsBFromEndFieldName);
            _spliceClosureIpidIdx = spliceFields.FindField(ConfigUtil.SpliceClosureIpidFieldName);

            ESRI.ArcGIS.Geodatabase.IQueryFilter spliceFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            spliceFilter.WhereClause = string.Format("({0}='{1}' AND {2}={3})"
                                                     + " OR ({4}='{1}' AND {5}={3})",
                                                     ConfigUtil.ACableIdFieldName,
                                                     ipid,
                                                     ConfigUtil.AFiberNumberFieldName,
                                                     fiberNumber,
                                                     ConfigUtil.BCableIdFieldName,
                                                     ConfigUtil.BFiberNumberFieldName);

            int connections = fiberSpliceTable.RowCount(spliceFilter);

            if (2 < connections)
            {
                // TODO: warning?
                System.Windows.Forms.MessageBox.Show("Less than 2 connections were detected: " + fiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }

            string spliceClosureIpid = string.Empty;
            string nextCableId       = string.Empty;
            int    nextFiberNumber   = -1;
            bool   isNextFromEnd     = false;

            // {{0}} causes the string.format to
            string cableWhereFormat  = string.Format("{0}='{{0}}'", ConfigUtil.IpidFieldName);
            string spliceWhereFormat = string.Format("{0}='{{0}}'", ConfigUtil.IpidFieldName);
            string fiberWhereFormat  = string.Format("{0}='{{0}}' AND {1}={{1}}", fiberRelationship.OriginForeignKey, ConfigUtil.Fiber_NumberFieldName);

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);

                // Ripple down the start cable's to end
                ESRI.ArcGIS.Geodatabase.IRow spliceRow = GetNextSplice(fiberSpliceTable, ipid, fiberNumber, isStartingAtFromEnd, out nextCableId, out nextFiberNumber, out spliceClosureIpid, out isNextFromEnd);
                while (null != spliceRow)
                {
                    ESRI.ArcGIS.Geodatabase.IFeature spliceClosure = null;

                    if (spliceClosureIpid.Equals(""))
                    {
                        System.Windows.Forms.MessageBox.Show("Found Splice with no SpliceClosure (ID/#) " + nextCableId + "/" + nextFiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                    else
                    {
                        filter.WhereClause = string.Format(spliceWhereFormat, spliceClosureIpid);
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor spliceCursor = spliceFtClass.Search(filter, false);
                        releaser.ManageLifetime(spliceCursor);
                        spliceClosure = spliceCursor.NextFeature();
                        if (spliceClosure == null)
                        {
                            System.Windows.Forms.MessageBox.Show("Invalid SpliceClosure referenced: (IPID)" + spliceClosureIpid, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                        }
                    }

                    filter.WhereClause = string.Format(cableWhereFormat, nextCableId);
                    ESRI.ArcGIS.Geodatabase.IFeatureCursor cableCursor = cableFtClass.Search(filter, false);
                    releaser.ManageLifetime(cableCursor);
                    ESRI.ArcGIS.Geodatabase.IFeature cable = cableCursor.NextFeature();
                    if (cable == null)
                    {
                        System.Windows.Forms.MessageBox.Show("Invalid cable ID referenced: (ID)" + nextCableId, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }

                    filter.WhereClause = string.Format(fiberWhereFormat, nextCableId, nextFiberNumber);
                    ESRI.ArcGIS.Geodatabase.ICursor fiberCursor = fiberTable.Search(filter, false);
                    releaser.ManageLifetime(fiberCursor);
                    ESRI.ArcGIS.Geodatabase.IRow fiber = fiberCursor.NextRow();
                    if (fiber == null)
                    {
                        System.Windows.Forms.MessageBox.Show("Invalid Fiber Cable or # referenced: (ID/#) " + nextCableId + "/" + nextFiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }

                    if (isStartingAtFromEnd)
                    {
                        if (spliceRow != null)
                        {
                            result.Add(spliceRow);
                        }
                        if (spliceClosure != null)
                        {
                            result.Add(spliceClosure);
                        }
                        if (fiber != null)
                        {
                            result.Add(fiber);
                        }
                        if (cable != null)
                        {
                            result.Add(cable);
                        }
                    }
                    else
                    {
                        if (spliceClosure != null)
                        {
                            result.Add(spliceClosure);
                        }
                        if (spliceRow != null)
                        {
                            result.Add(spliceRow);
                        }
                        if (cable != null)
                        {
                            result.Add(cable);
                        }
                        if (fiber != null)
                        {
                            result.Add(fiber);
                        }
                    }

                    spliceRow = GetNextSplice(fiberSpliceTable, nextCableId, nextFiberNumber, !isNextFromEnd, out nextCableId, out nextFiberNumber, out spliceClosureIpid, out isNextFromEnd);
                }

                // See if there is a port for this one
                ESRI.ArcGIS.Geodatabase.IRow     portRow  = null;
                ESRI.ArcGIS.Geodatabase.IFeature deviceFt = null;
                if (GetConnectedPort(cableFtClass, nextCableId, nextFiberNumber, isNextFromEnd, out portRow, out deviceFt))
                {
                    if (isStartingAtFromEnd)
                    {
                        result.Add(portRow);
                        result.Add(deviceFt);
                    }
                    else
                    {
                        result.Add(deviceFt);
                        result.Add(portRow);
                    }
                }

                return(result);
            }
        }