A4WaterUtilities.GeoNetTools.ToggleOperableStatus C# (CSharp) Method

ToggleOperableStatus() public static method

public static ToggleOperableStatus ( IApplication app, IPoint point, bool showMessage, string ISOvalveFeatureLayerName, string ISOsourceFeatureLayerName, string ISOoperableFieldNameValves, string ISOoperableFieldNameSources, string ISOoperableValues, double SnapTol ) : IEnvelope
app IApplication
point IPoint
showMessage bool
ISOvalveFeatureLayerName string
ISOsourceFeatureLayerName string
ISOoperableFieldNameValves string
ISOoperableFieldNameSources string
ISOoperableValues string
SnapTol double
return IEnvelope
        public static IEnvelope ToggleOperableStatus(IApplication app, IPoint point, bool showMessage, string ISOvalveFeatureLayerName, string ISOsourceFeatureLayerName, string ISOoperableFieldNameValves, string ISOoperableFieldNameSources, string[] ISOoperableValues, double SnapTol)
        {
            IFeatureCursor fcursor = null;
            ICursor ccursor = null;
            IFeature feat = null;
            UID uID = null;
            IEditor editor = null;
            IMxDocument mxdoc = null;
            IMap map = null;
            IFeatureLayer[] valveFLs = null;
            IFeatureClass[] valveFCs = null;
            IFeatureLayer[] sourceFLs = null;
            IFeatureClass[] sourceFCs = null;
            IEnvelope retEnv = null;
            //IGeometry geom = null;
            //IProximityOperator proxOp = null;
            IEnvelope env = null;
            ISpatialFilter sf = null;
            IWorkspace work = null;
            IWorkspaceEdit workEdit = null;
            IField opField = null;
            IPoint pTmpPnt = null;

            IEnvelope ptmpEnv = null;

            IDisplayExpressionProperties pDEP = null;
            IDisplayString pIDS = null;
            try
            {
                //Get editor
                uID = new UID();
                uID.Value = "esriEditor.Editor";
                editor = app.FindExtensionByCLSID(uID) as IEditor;

                mxdoc = app.Document as IMxDocument;
                map = mxdoc.ActiveView.FocusMap;
                SnapTol = Globals.ConvertPixelsToMap(SnapTol, map);

                string[] strValveFLs = ISOvalveFeatureLayerName.Split('|');
                // string[] strOpValues = .Split('|');
                valveFLs = new IFeatureLayer[strValveFLs.Length];//(IFeatureLayer)Globals.FindLayer(map, valveFLName);
                valveFCs = new IFeatureClass[strValveFLs.Length];
                bool lyrFnd = false;

                for (int i = 0; i < valveFLs.Length; i++)
                {
                    bool FCorLayerTemp = true;

                    valveFLs[i] = (IFeatureLayer)Globals.FindLayerNotInMemory(map, strValveFLs[i], ref FCorLayerTemp);
                    if (valveFLs[i] != null)
                    {
                        valveFCs[i] = valveFLs[i].FeatureClass;
                        lyrFnd = true;
                    }

                }
                string[] strSourceFLs = ISOsourceFeatureLayerName.Split('|');
                // string[] strOpValues = .Split('|');
                sourceFLs = new IFeatureLayer[strSourceFLs.Length];//(IFeatureLayer)Globals.FindLayer(map, valveFLName);
                sourceFCs = new IFeatureClass[strSourceFLs.Length];

                for (int i = 0; i < sourceFLs.Length; i++)
                {
                    bool FCorLayerTemp = true;

                    sourceFLs[i] = (IFeatureLayer)Globals.FindLayer(map, strSourceFLs[i], ref FCorLayerTemp);
                    if (sourceFLs[i] != null)
                    {
                        sourceFCs[i] = sourceFLs[i].FeatureClass;
                        lyrFnd = true;
                    }

                }
                if (lyrFnd == false)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12a") + ISOvalveFeatureLayerName + A4LGSharedFunctions.Localizer.GetString("Or") + ISOsourceFeatureLayerName + A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12b"), A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12c"));
                    return null;
                }

                // Globals.ClearSelected(map, false);

                foreach (IFeatureLayer valveFLayer in valveFLs)
                {

                    if (point != null)
                    {

                        env = new EnvelopeClass();
                        env.DefineFromPoints(1, ref point);
                        env.Expand(SnapTol / 2, SnapTol / 2, false);

                        sf = new SpatialFilterClass();
                        sf.Geometry = env;
                        sf.GeometryField = valveFLayer.FeatureClass.ShapeFieldName;
                        sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                        fcursor = valveFLayer.Search(sf, false);

                    }
                    else
                    {

                        ((IFeatureSelection)valveFLayer).SelectionSet.Search(null, false, out ccursor);
                        fcursor = (IFeatureCursor)ccursor;
                    }

                    work = valveFLayer.FeatureClass.FeatureDataset.Workspace;
                    workEdit = work as IWorkspaceEdit;

                    if (!workEdit.IsBeingEdited())
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("StrtEditing"));
                        return null;
                    }

                    //Find closest feature (in this feature layer)
                    double testDistance = SnapTol;
                    double thisDistance;

                    //IFeature targetFeature = null; int targetOID = -1;
                    workEdit.StartEditOperation();

                    while ((feat = fcursor.NextFeature()) != null)
                    {

                        try
                        {

                            int operableFieldPos = valveFLayer.FeatureClass.FindField(ISOoperableFieldNameValves);
                            if (operableFieldPos < 0)
                            {
                                //MessageBox.Show("Operable status cannot be toggled since " + m_ISOoperableFieldName + " field is not found in " + m_ISOvalveFeatureLayerName + ".", messageBoxHeader);
                                continue;
                            }

                            opField = valveFLayer.FeatureClass.Fields.get_Field(operableFieldPos);

                            //      targetFeature = valveFLayer.FeatureClass.GetFeature(targetOID);
                            string disVal;
                            if (opField.Type == esriFieldType.esriFieldTypeString)
                            {
                                if (feat.get_Value(operableFieldPos) == null)
                                {
                                    feat.set_Value(operableFieldPos, ISOoperableValues[1]);
                                    disVal = ISOoperableValues[1];
                                }

                                else if (feat.get_Value(operableFieldPos) is DBNull)
                                {
                                    feat.set_Value(operableFieldPos, ISOoperableValues[1]);
                                    disVal = ISOoperableValues[1];
                                }
                                else if (feat.get_Value(operableFieldPos).ToString() == ISOoperableValues[0])
                                {
                                    feat.set_Value(operableFieldPos, ISOoperableValues[1]);
                                    disVal = ISOoperableValues[1];
                                }
                                else
                                {
                                    feat.set_Value(operableFieldPos, ISOoperableValues[0]);
                                    disVal = ISOoperableValues[0];
                                }
                            }
                            else
                            {
                                if (feat.get_Value(operableFieldPos) == null)
                                {
                                    feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                    disVal = "True";
                                }

                                else if (feat.get_Value(operableFieldPos) is DBNull)
                                {
                                    feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                    disVal = "True";
                                }
                                else if (Convert.ToInt32(feat.get_Value(operableFieldPos)) == Convert.ToInt32(ISOoperableValues[0]))
                                {
                                    feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                    disVal = "True";
                                }
                                else
                                {
                                    feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[0]));
                                    disVal = "False";
                                }
                            }

                            if (retEnv == null)
                            {
                                retEnv = new EnvelopeClass();
                                pTmpPnt = feat.ShapeCopy as IPoint;

                                retEnv.DefineFromPoints(1, ref  pTmpPnt);
                                retEnv.Expand(SnapTol / 2, SnapTol / 2, false);
                                pTmpPnt = null;

                            }
                            else
                            {

                                pTmpPnt = feat.ShapeCopy as IPoint;
                                ptmpEnv = new EnvelopeClass();
                                ptmpEnv.DefineFromPoints(1, ref  pTmpPnt);
                                ptmpEnv.Expand(SnapTol / 2, SnapTol / 2, false);
                                retEnv.Union(ptmpEnv);

                                pTmpPnt = null;
                                ptmpEnv = null;

                            }
                            feat.Store();

                            pIDS = (IDisplayString)valveFLayer;
                            pDEP = pIDS.ExpressionProperties;
                            string disEx = pIDS.FindDisplayString((IObject)feat);

                            // MessageBox.Show("Valve: " + targetFeature.get_Value(targetFeature.Fields.FindField(valveFLayer.DisplayField)).ToString() + A4LGSharedFunctions.Localizer.GetString("IsNowOperable") + disVal);
                            if (showMessage)
                                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ValveFrom") + valveFLayer.Name + ": " + disEx + opField.AliasName +  A4LGSharedFunctions.Localizer.GetString("IsNowOperable") + disVal);

                            Globals.FlashGeometry(feat.Shape, Globals.GetColor(255, 0, 0), mxdoc.ActiveView.ScreenDisplay, 150);

                        }

                        catch (Exception ex)
                        {
                            editor.AbortOperation();

                            MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12c") + "\n" + ex.Message, ex.Source);
                            return null;
                        }
                        finally
                        {

                        }

                        if (feat != null)
                        {
                            Marshal.ReleaseComObject(feat);
                        }

                    }
                    try
                    {
                        workEdit.StopEditOperation();

                    }
                    catch
                    { }

                }

                if (point != null)
                {

                    foreach (IFeatureLayer sourceFLayer in sourceFLs)
                    {
                        fcursor = null;
                        if (sourceFLayer == null)
                        {

                            continue;
                        }
                        if (sourceFLayer.FeatureClass != null)
                        {
                            continue;
                        }
                        if (retEnv != null)
                        {
                            sf = new SpatialFilterClass();
                            sf.Geometry = retEnv;

                            sf.GeometryField = sourceFLayer.FeatureClass.ShapeFieldName;
                            sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                            fcursor = sourceFLayer.Search(sf, false);

                        }
                        else
                        {

                            ((IFeatureSelection)sourceFLayer).SelectionSet.Search(null, false, out ccursor);
                            fcursor = (IFeatureCursor)ccursor;
                        }

                        work = sourceFLayer.FeatureClass.FeatureDataset.Workspace;
                        workEdit = work as IWorkspaceEdit;

                        if (!workEdit.IsBeingEdited())
                        {
                            MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("StrtEditing"));
                            continue;
                        }

                        workEdit.StartEditOperation();

                        while ((feat = fcursor.NextFeature()) != null)
                        {

                            try
                            {

                                int operableFieldPos = sourceFLayer.FeatureClass.FindField(ISOoperableFieldNameSources);
                                if (operableFieldPos < 0)
                                {
                                    //MessageBox.Show("Operable status cannot be toggled since " + m_ISOoperableFieldName + " field is not found in " + m_ISOsourceFeatureLayerName + ".", messageBoxHeader);
                                    continue;
                                }
                                opField = sourceFLayer.FeatureClass.Fields.get_Field(operableFieldPos);

                                //      targetFeature = sourceFLayer.FeatureClass.GetFeature(targetOID);
                                string disVal;

                                if (opField.Type == esriFieldType.esriFieldTypeString)
                                {
                                    if (feat.get_Value(operableFieldPos) == null)
                                    {
                                        feat.set_Value(operableFieldPos, ISOoperableValues[1]);
                                        disVal = "True";
                                    }

                                    else if (feat.get_Value(operableFieldPos) is DBNull)
                                    {
                                        feat.set_Value(operableFieldPos, (ISOoperableValues[1]));
                                        disVal = "True";
                                    }
                                    else if (feat.get_Value(operableFieldPos).ToString() == ISOoperableValues[0])
                                    {
                                        feat.set_Value(operableFieldPos, ISOoperableValues[1]);
                                        disVal = "True";
                                    }
                                    else
                                    {
                                        feat.set_Value(operableFieldPos, ISOoperableValues[0]);
                                        disVal = "False";
                                    }
                                }
                                else
                                {
                                    if (feat.get_Value(operableFieldPos) == null)
                                    {
                                        feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                        disVal = "True";
                                    }

                                    else if (feat.get_Value(operableFieldPos) is DBNull)
                                    {
                                        feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                        disVal = "True";
                                    }

                                    else if (Convert.ToInt32(feat.get_Value(operableFieldPos)) == Convert.ToInt32(ISOoperableValues[0]))
                                    {
                                        feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[1]));
                                        disVal = "True";
                                    }
                                    else
                                    {
                                        feat.set_Value(operableFieldPos, Convert.ToInt32(ISOoperableValues[0]));
                                        disVal = "False";
                                    }
                                }

                                feat.Store();

                                pIDS = (IDisplayString)sourceFLayer;
                                pDEP = pIDS.ExpressionProperties;
                                string disEx = pIDS.FindDisplayString((IObject)feat);

                                // MessageBox.Show("source: " + targetFeature.get_Value(targetFeature.Fields.FindField(sourceFLayer.DisplayField)).ToString() + A4LGSharedFunctions.Localizer.GetString("IsNowOperable") + disVal);
                                if (showMessage)
                                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("SourceFrom") + sourceFLayer.Name + ": " + disEx + opField.AliasName + A4LGSharedFunctions.Localizer.GetString("IsNowOperable") + disVal);
                                //  MessageBox.Show("source: " + disEx + A4LGSharedFunctions.Localizer.GetString("IsNowOperable") + disVal);

                                Globals.FlashGeometry(feat.Shape, Globals.GetColor(255, 0, 0), mxdoc.ActiveView.ScreenDisplay, 150);

                            }

                            catch (Exception ex)
                            {
                                editor.AbortOperation();
                                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12c") + "\n" + ex.Message, ex.Source);
                                return null;
                            }
                            finally
                            {

                            }

                            if (feat != null)
                            {
                                Marshal.ReleaseComObject(feat);
                            }
                            // feat = fcursor.NextFeature();
                        }
                        try
                        {
                            workEdit.StopEditOperation();
                        }
                        catch
                        { }

                        // if (targetOID < 0) continue;

                    }
                }
                if (retEnv == null)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsMess_13a"));
                }

                return retEnv;

            }
            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("GeoNetToolsError_12c") + "\n" + ex.Message, ex.Source);
                return null;
            }
            finally
            {

                if (fcursor != null)
                {
                    Marshal.ReleaseComObject(fcursor);

                }

                if (ccursor != null)
                {
                    Marshal.ReleaseComObject(ccursor);

                }
                ccursor = null;
                fcursor = null;

                feat = null;
                uID = null;
                editor = null;
                mxdoc = null;
                map = null;
                valveFLs = null;
                valveFCs = null;
                sourceFLs = null;
                sourceFCs = null;
                retEnv = null;
                //geom = null;
                //proxOp = null;
                env = null;
                sf = null;
                work = null;
                workEdit = null;
                opField = null;
                pTmpPnt = null;
                ptmpEnv = null;
                pDEP = null;
                pIDS = null;
            }
        }