/// <summary>
        /// Sets the buffer tube and strand counts based on the given configuration. If IPID and/or CABLEID are null, it also
        /// takes care of them
        /// </summary>
        /// <param name="feature">The FiberCable feature to configure</param>
        /// <param name="configuration">The tube/strand counts</param>
        /// <param name="isExistingOperation">Flag to control whether this method is being called from within an existing
        /// edit operation</param>
        /// <returns>Success</returns>
        protected bool ConfigureCable(ESRI.ArcGIS.Geodatabase.IFeature feature, FiberCableConfiguration configuration, bool isExistingOperation)
        {
            bool isComplete         = false;
            bool isOurOperationOpen = false;

            // The following assignments are defaults for the case where they are not already populated on the feature
            string fiberCableIpid = Guid.NewGuid().ToString("B").ToUpper();

            // The following will be set during Validation
            ESRI.ArcGIS.Geodatabase.IObjectClass ftClass = null;
            ESRI.ArcGIS.Geodatabase.IFields      fields  = null;
            int ipidIdx        = -1;
            int bufferCountIdx = -1;
            int strandCountIdx = -1;

            #region Validation

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

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

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

            ftClass = feature.Class;
            fields  = ftClass.Fields;

            string missingFieldFormat = "Field {0} is missing.";

            ipidIdx = fields.FindField(ConfigUtil.IpidFieldName);
            if (-1 == ipidIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.IpidFieldName));
            }

            bufferCountIdx = fields.FindField(ConfigUtil.NumberOfBuffersFieldName);
            if (-1 == bufferCountIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.NumberOfBuffersFieldName));
            }

            strandCountIdx = fields.FindField(ConfigUtil.NumberOfFibersFieldName);
            if (-1 == strandCountIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.NumberOfFibersFieldName));
            }

            #endregion


            ESRI.ArcGIS.esriSystem.ITrackCancel    trackCancel    = new ESRI.ArcGIS.Display.CancelTrackerClass();
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = _hookHelper.CreateProgressDialog(trackCancel, "Preparing to configure cable...", 1, configuration.TotalFiberCount, 1, "Starting edit operation...", "Fiber Configuration");
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;

            progressDialog.ShowDialog();
            stepProgressor.Step();

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

            try
            {
                if (DBNull.Value == feature.get_Value(ipidIdx))
                {
                    feature.set_Value(ipidIdx, fiberCableIpid);
                }
                else
                {
                    fiberCableIpid = feature.get_Value(ipidIdx).ToString();
                }

                feature.set_Value(bufferCountIdx, configuration.BufferCount);
                feature.set_Value(strandCountIdx, configuration.FibersPerTube);

                isComplete = GenerateUnits(feature, configuration, progressDialog, trackCancel);

                progressDialog.Description = "Completing configuration...";
                stepProgressor.Step();

                if (isOurOperationOpen)
                {
                    if (isComplete)
                    {
                        feature.Store();
                        _editor.StopOperation("Configure Fiber");
                    }
                    else
                    {
                        _editor.AbortOperation();
                    }
                }
            }
            catch (Exception e)
            {
                if (isOurOperationOpen)
                {
                    _editor.AbortOperation();
                }
            }

            progressDialog.HideDialog();
            return(isComplete);
        }
Exemple #2
0
        private bool RemoveFiberCableConfigDomains(IWorkspace wksp)
        {
            bool result = true;

            if (wksp == null || ((wksp as IWorkspaceDomains3) == null))
            {
                return(false);
            }

            try
            {
                // get Handles to everything we need first or bail.
                IFeatureClass cableFc = FindFeatureClass(ConfigUtil.FiberCableFtClassName);
                if (cableFc == null)
                {
                    return(false);
                }
                ISubtypes subs = cableFc as ISubtypes;
                if (subs == null)
                {
                    return(false);
                }
                IClassSchemaEdit4 schEdit = cableFc as IClassSchemaEdit4;
                if (schEdit == null)
                {
                    return(false);
                }

                // ----------------------------------------------------
                // First we have to unassign the domains from the FC fields
                // ----------------------------------------------------
                schEdit.AlterDomain(ConfigUtil.NumberOfFibersFieldName, null);
                schEdit.AlterDomain(ConfigUtil.NumberOfBuffersFieldName, null);
                _logHelper.addLogEntry(DateTime.Now.ToString(), "INFO", "Unassigned FiberCable domains", "NumberOfFibers + NumberOfBuffers");

                // ----------------------------------------------------
                // Also have to remove domain from the subtypes (arghh)
                // 1 & 2 are overhead and underground.
                // ----------------------------------------------------
                IEnumSubtype types = subs.Subtypes;
                subs.set_Domain(1, ConfigUtil.NumberOfFibersFieldName, null);
                subs.set_Domain(2, ConfigUtil.NumberOfFibersFieldName, null);
                subs.set_Domain(1, ConfigUtil.NumberOfBuffersFieldName, null);
                subs.set_Domain(2, ConfigUtil.NumberOfBuffersFieldName, null);
                _logHelper.addLogEntry(DateTime.Now.ToString(), "INFO", "Removed FiberCable subtype domains", "NumberOfFibers + NumberOfBuffers");

                // ----------------------------------------------------
                // Now we can remove the domains
                // ----------------------------------------------------
                IWorkspaceDomains3 wkspDomains = wksp as IWorkspaceDomains3;
                if (wkspDomains.get_CanDeleteDomain("NumberOfFibers") &&
                    wkspDomains.get_CanDeleteDomain("NumberOfBuffers"))
                {
                    wkspDomains.DeleteDomain("NumberOfFibers");
                    wkspDomains.DeleteDomain("NumberOfBuffers");
                    MessageBox.Show("Domains successfully deleted");
                    _logHelper.addLogEntry(DateTime.Now.ToString(), "INFO", "Domains successfully deleted", "NumberOfFibers + NumberOfBuffers");
                }
                else
                {
                    MessageBox.Show("Upgrade failed. \nCould not get exclusive access to this database. \nPLEASE RESTORE FROM YOUR BACKUP");
                    _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "Cannot delete domains.", "Non-Exclusive access?");
                    return(false);
                }

                // ----------------------------------------------------
                // Recalculate the fields based on the count of actual
                // related objects found. We'll use this code later
                // for integrity checking.
                // ----------------------------------------------------
                IRelationshipClass bufferRelationship = GdbUtils.GetRelationshipClass(cableFc, ConfigUtil.FiberCableToBufferRelClassName);
                IRelationshipClass strandRelationship = GdbUtils.GetRelationshipClass(cableFc, ConfigUtil.FiberCableToFiberRelClassName);
                IFeature           ft;
                int bufferIdx = cableFc.Fields.FindField(ConfigUtil.NumberOfBuffersFieldName);
                int strandIdx = cableFc.Fields.FindField(ConfigUtil.NumberOfFibersFieldName);
                if (bufferIdx == -1 || strandIdx == -1)
                {
                    _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "Cannot find buffer or strand fields.",
                                           ConfigUtil.NumberOfBuffersFieldName + " " + ConfigUtil.NumberOfFibersFieldName);
                    MessageBox.Show("Upgrade failed. \nCould not find appropriate fields based on current config settings. \nPLEASE RESTORE FROM YOUR BACKUP.");
                    return(false);
                }
                _logHelper.addLogEntry(DateTime.Now.ToString(), "INFO", "Recalculating buffer and strand counts...");
                // Start edit session do the updates....
                ArcMap.Editor.StartEditing(wksp);
                IFeatureCursor cables = cableFc.Update(null, false);
                int            count  = cableFc.FeatureCount(null);

                //ProgressBar
                ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass();

                ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass();

                // Set the properties of the Step Progressor
                ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = progressDialogFactory.Create(trackCancel, ArcMap.Application.hWnd);
                stepProgressor.MinRange  = 1;
                stepProgressor.MaxRange  = count;
                stepProgressor.StepValue = 1;
                stepProgressor.Message   = "Updating cable config for " + count + " cables";

                // Create the ProgressDialog. This automatically displays the dialog
                ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast

                // Set the properties of the ProgressDialog
                progressDialog.CancelEnabled = false;
                progressDialog.Description   = "";
                progressDialog.Title         = "Workspace Upgrade";
                progressDialog.Animation     = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;
                progressDialog.ShowDialog();

                int i = 1;
                while ((ft = cables.NextFeature()) != null)
                {
                    progressDialog.Description = string.Format("Updating cable {0} of {1}", i, count);
                    stepProgressor.Step();

                    ISet buffers = bufferRelationship.GetObjectsRelatedToObject(ft);
                    ft.set_Value(bufferIdx, buffers.Count);
                    ISet strands = strandRelationship.GetObjectsRelatedToObject(ft);
                    ft.set_Value(strandIdx, strands.Count);
                    ft.Store();
                    i++;
                }
                progressDialog.HideDialog();
                ArcMap.Editor.StopEditing(true);
                _logHelper.addLogEntry(DateTime.Now.ToString(), "INFO", "Upgrade Completed Successfully.");
                MessageBox.Show("Upgrade Completed Successfully.");
            }
            catch (Exception e)
            {
                _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "RemoveFiberCableConfigDomains", e.Message);
                result = false;
            }
            return(result);
        }
        /// <summary>
        /// Sets the input and output ports based on the given configuration. If IPID and/or CABLEID are null, it also
        /// takes care of them
        /// </summary>
        /// <param name="feature">The device feature to configure</param>
        /// <param name="inputPorts">Number of input ports</param>
        /// <param name="outputPorts">Number of output ports</param>
        /// <param name="isExistingOperation">Flag to control whether this method is being called from within an existing
        /// edit operation -- the default behavior is "false" in which case it adds an operation of its own</param>
        /// <returns>True if the configuration is complete</returns>
        public bool ConfigureDevice(ESRI.ArcGIS.Geodatabase.IFeature feature, int inputPorts, int outputPorts, bool isExistingOperation)
        {
            bool   isComplete         = false;
            bool   isOurOperationOpen = false;
            string deviceIpid         = string.Empty;

            #region Validation

            string missingFieldFormat = "Field {0} is missing.";

            // The following will be set during Validation
            ESRI.ArcGIS.Geodatabase.IObjectClass ftClass = null;
            ESRI.ArcGIS.Geodatabase.IFields      fields  = null;

            ftClass = feature.Class;
            fields  = ftClass.Fields;

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

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

            if (0 > inputPorts)
            {
                throw new ArgumentOutOfRangeException("inputPorts");
            }

            if (0 > outputPorts)
            {
                throw new ArgumentOutOfRangeException("outputPorts");
            }

            int ipidIdx = fields.FindField(ConfigUtil.IpidFieldName);
            if (-1 == ipidIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.IpidFieldName));
            }

            int inPortsIdx = fields.FindField(ConfigUtil.InputPortsFieldName);
            if (-1 == inPortsIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.InputPortsFieldName));
            }

            int outPortsIdx = fields.FindField(ConfigUtil.OutputPortsFieldName);
            if (-1 == outPortsIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.OutputPortsFieldName));
            }

            #endregion

            // Are we RE-configuring?
            //            int? oldInputPorts = GdbUtils.GetDomainedIntName(feature, ConfigUtil.InputPortsFieldName);
            //            int? oldOutputPorts = GdbUtils.GetDomainedIntName(feature, ConfigUtil.OutputPortsFieldName);

            //            int inputPortDifference = oldInputPorts.HasValue ? Math.Abs(inputPorts - oldInputPorts.Value) : inputPorts;
            //            int outputPortDifference = oldOutputPorts.HasValue ? Math.Abs(outputPorts - oldOutputPorts.Value) : outputPorts;

            ESRI.ArcGIS.esriSystem.ITrackCancel    trackCancel    = new ESRI.ArcGIS.Display.CancelTrackerClass();
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = _hookHelper.CreateProgressDialog(trackCancel, "Configuring device...", 1, inputPorts + outputPorts, 1, "Starting edit operation...", "Device Configuration");
            //            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = CreateProgressDialog(trackCancel, "Configuring device...", 1, inputPortDifference + outputPortDifference + 2, 1, "Starting edit operation...", "Device Configuration");
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;

            progressDialog.ShowDialog();
            stepProgressor.Step();

            if (!isExistingOperation)
            {
                try
                {
                    _editor.StartOperation();
                    isOurOperationOpen = true;
                }
                catch (Exception ex)
                {
                    throw new Exception("Failed to start edit operation.", ex);
                }
            }

            try
            {
                feature.set_Value(inPortsIdx, inputPorts);
                feature.set_Value(outPortsIdx, outputPorts);

                if (DBNull.Value == feature.get_Value(ipidIdx))
                {
                    Guid g = Guid.NewGuid();
                    deviceIpid = g.ToString("B").ToUpper();
                    feature.set_Value(ipidIdx, deviceIpid);
                }
                else
                {
                    deviceIpid = feature.get_Value(ipidIdx).ToString();
                }

                //                if (!oldOutputPorts.HasValue && !oldInputPorts.HasValue)
                //                {
                isComplete = GeneratePorts(feature, 1, inputPorts, 1, outputPorts, progressDialog, trackCancel);
                //                }
                //                else
                //                {
                //                    bool additionsComplete = false;
                //                    bool deletionsComplete = false;
                //
                //                    additionsComplete = GeneratePorts(feature, oldInputPorts.Value + 1, oldInputPorts.Value + inputPortDifference, oldOutputPorts.Value + 1, oldOutputPorts.Value + outputPortDifference, progressDialog, trackCancel);
                //                    deletionsComplete = DeletePorts(feature, inputPorts, outputPorts, progressDialog, trackCancel);
                //
                //                    isComplete = additionsComplete && deletionsComplete;
                //                }

                if (isComplete)
                {
                    stepProgressor.Message = "Finishing configuration...";
                    stepProgressor.Step();

                    if (isOurOperationOpen)
                    {
                        feature.Store();
                        _editor.StopOperation("Configure Device");
                    }
                }
                else
                {
                    stepProgressor.Message = "Cancelling configuration...";
                    stepProgressor.Step();

                    if (isOurOperationOpen)
                    {
                        _editor.AbortOperation();
                    }
                }
            }
            catch
            {
                if (isOurOperationOpen)
                {
                    _editor.AbortOperation();
                }
            }

            if (null != progressDialog)
            {
                progressDialog.HideDialog();
            }

            return(isComplete);
        }