public static string AddLaterals(IApplication app, List<AddLateralDetails> addLateralsDetails, IFeature inFeatures, bool logOperation, bool suppressDialog, bool store, bool ForceSourcePointConnection, IFeatureLayer pEditLayer)
        {
            string resetFlow = "";
            bool useDefaultTemplate;
            List<IFeature> ComplFeat = new List<IFeature>();
            IMap map = null;
            IEditor editor = null;
            IMouseCursor appCursor = null;
            IMxDocument mxdoc = null;
            IFeatureLayer pointFLayer = null;
            IFeatureLayer matchLineFLayer = null;
            IFeatureLayer targetLineFLayer = null;
            IEditLayers eLayers = null;
            ISelectionSet2 pointSelSet = null;
            IFeatureSelection pointFeatureSelection = null;
            IEditTemplate pLateralLineEditTemp = null;
            List<pointAlongSettings> pointAlongLayers = null;
            pointAlongSettings pointAlongLayer = null;
            ICursor pointCursor = null;
            IFeature pointFeature = null;

            //ProgressBar
            ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = null;
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = null;
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = null;
            // Create a CancelTracker
            ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = null;

            try
            {

                if (Control.ModifierKeys == Keys.Control)
                {
                    useDefaultTemplate = false;
                }
                else
                {

                    useDefaultTemplate = true;
                }
                bool boolSelectedEdges = false;
                if (Control.ModifierKeys == Keys.Shift)
                {
                    boolSelectedEdges = true;
                }
                else
                {

                    boolSelectedEdges = false;
                }
                //Get edit session
                bool LatCreated = false;

                editor = Globals.getEditor(app);
                if (editor.EditState != esriEditState.esriStateEditing)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("MustBEditg"), _caption);
                    editor = null;

                    return "";
                }

                //Change mouse cursor to wait - automatically changes back (ArcGIS Desktop only)
                appCursor = new MouseCursorClass();
                appCursor.SetCursor(2);

                mxdoc = (IMxDocument)app.Document;
                map = editor.Map;

                for (int k = 0; k < addLateralsDetails.Count; k++)
                {
                    bool FCorLayerPoint = true;
                    if (pEditLayer != null)
                    {
                        if (pEditLayer.Name == addLateralsDetails[k].Point_LayerName)
                        {
                            pointFLayer = pEditLayer;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else {
                        pointFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].Point_LayerName, ref FCorLayerPoint);
                    }
                    if (inFeatures != null)
                    {
                        if (pointFLayer == null)
                            continue;
                        if (pointFLayer.FeatureClass == null)
                            continue;

                        if (inFeatures.Class.CLSID.ToString() != pointFLayer.FeatureClass.CLSID.ToString())
                            continue;
                        if (inFeatures.Class.ObjectClassID.ToString() != pointFLayer.FeatureClass.ObjectClassID.ToString())
                            continue;
                        if (inFeatures.Class.AliasName.ToString() != pointFLayer.FeatureClass.AliasName.ToString())
                            continue;

                    }
                    //Report any problems before exiting
                    if (pointFLayer == null)
                    {
                        continue;
                    }

                    bool FCorLayerMatch = true;
                    bool FCorLayerTarget = true;

                    matchLineFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].MainLine_LayerName, ref FCorLayerMatch);
                    targetLineFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].LateralLine_LayerName, ref FCorLayerTarget);

                    // IFeatureLayerDefinition2 pFeatLayerdef = matchLineFLayer as IFeatureLayerDefinition2;

                    if (matchLineFLayer == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_1") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (matchLineFLayer.FeatureClass == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_1") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (matchLineFLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_2") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_3") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer.FeatureClass == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_3") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_4") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }

                    //Confirm the other layers are the correct shape type
                    if (pointFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint || matchLineFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                        return "";

                    //Confirm that target layer is editable and is a line layer
                    eLayers = (IEditLayers)editor;
                    if (!(eLayers.IsEditable(targetLineFLayer)) || (targetLineFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline))
                        return "";

                    //Confirm that the two line layers are different Feature classes
                    if ((matchLineFLayer.FeatureClass.CLSID == targetLineFLayer.FeatureClass.CLSID) && (matchLineFLayer.FeatureClass.AliasName == targetLineFLayer.FeatureClass.AliasName))
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_1") , A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_2") );
                        return "";
                    }

                    //Verify that some points are selected
                    pointFeatureSelection = (IFeatureSelection)pointFLayer;

                    if (pointFeatureSelection.SelectionSet.Count == 0)
                        continue;

                    pointSelSet = pointFeatureSelection.SelectionSet as ISelectionSet2;

                    if (useDefaultTemplate)
                    {
                        //pLateralLineEditTemp = Globals.PromptAndGetEditTemplate(app, targetLineFLayer, addLateralsDetails[k].LateralLine_EditTemplate);
                        pLateralLineEditTemp = Globals.PromptAndGetEditTemplateGraphic(targetLineFLayer, addLateralsDetails[k].LateralLine_EditTemplate);
                    }
                    else
                    {
                        pLateralLineEditTemp = Globals.PromptAndGetEditTemplateGraphic(targetLineFLayer, "");
                        //pLateralLineEditTemp = Globals.PromptAndGetEditTemplate(app, targetLineFLayer, "");
                    }

                    if (addLateralsDetails[k].PointAlong != null)
                    {

                        if (addLateralsDetails[k].PointAlong.Length > 0)
                        {
                            pointAlongLayers = new List<pointAlongSettings>();

                            // IEditTemplate pPointAlongEditTemp;
                            for (int j = 0; j < addLateralsDetails[k].PointAlong.Length; j++)
                            {
                                pointAlongLayer = new pointAlongSettings();
                                bool FCorLayerPointsAlong = true;
                                pointAlongLayer.PointAlongLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].PointAlong[j].LayerName, ref FCorLayerPointsAlong);
                                if (pointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PointAlongLayer.FeatureClass == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_2") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";
                                }
                                else if (pointAlongLayer.PointAlongLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
                                {
                                    MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_3") , A4LGSharedFunctions.Localizer.GetString("Warning") );

                                    return "";
                                }

                                pointAlongLayer.PolygonIntersectSide = addLateralsDetails[k].PointAlong[j].PolygonOffsetSide;
                                if (pointAlongLayer.PolygonIntersectSide == null)
                                {
                                    pointAlongLayer.PolygonIntersectSide = "TO";

                                }
                                bool FCorLayerTemp = true;
                                pointAlongLayer.PolygonIntersectLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].PointAlong[j].PolygonOffsetLayerName, ref FCorLayerTemp);
                                pointAlongLayer.FoundAsLayer = FCorLayerTemp;
                                if (pointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PolygonIntersectLayer != null)
                                {

                                    if (pointAlongLayer.PolygonIntersectLayer.FeatureClass != null)
                                    {

                                        //Confirm that target layer is editable and is a line layer
                                        if (pointAlongLayer.PolygonIntersectLayer != null)
                                        {
                                            if (pointAlongLayer.PolygonIntersectLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                                            {
                                                MessageBox.Show(addLateralsDetails[k].PointAlong[j].PolygonOffsetLayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_3"));

                                                return "";
                                            }

                                        }
                                    }
                                }
                                //Confirm that target layer is editable and is a line layer
                                if (pointAlongLayer.PointAlongLayer != null)
                                {
                                    if (!(eLayers.IsEditable(pointAlongLayer.PointAlongLayer)) || (pointAlongLayer.PointAlongLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint))
                                    {
                                        MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_4"));

                                        return "";
                                    }
                                    if (useDefaultTemplate)
                                    {
                                        pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplateGraphic(pointAlongLayer.PointAlongLayer, addLateralsDetails[k].PointAlong[j].EditTemplate);
                                        //pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplate(app, pointAlongLayer.PointAlongLayer, addLateralsDetails[k].PointAlong[j].EditTemplate);
                                    }
                                    else
                                    {
                                        pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplateGraphic(pointAlongLayer.PointAlongLayer, "");
                                        //pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplate(app, pointAlongLayer.PointAlongLayer, "");
                                    }

                                }

                                //if (addLateralsDetails[k].PointAlong[j].Distance < 0)
                                //    pointAlongLayer.PointAlongDistance = 0;
                                //else
                                pointAlongLayer.PointAlongDistance = (double)addLateralsDetails[k].PointAlong[j].Distance;

                                //if (addLateralsDetails[k].PointAlong[j].DistanceIsPercent != null)
                                pointAlongLayer.DistanceIsPercent = (bool)addLateralsDetails[k].PointAlong[j].DistanceIsPercent;
                                //else
                                //  pointAlongLayer.DistanceIsPercent =false;

                                pointAlongLayers.Add(pointAlongLayer);

                                //Verify subtype is valid for target point
                                //if (targetPointFLayer != null)
                                //{
                                //    ISubtypes targetPointSubtypes = targetPointFLayer[j].FeatureClass as ISubtypes;
                                //    //string targetPointSubtypeName = targetPointSubtypes.get_SubtypeName(_targetPointSubtype);
                                //    if ((targetPointSubtypes == null) || (!targetPointSubtypes.HasSubtype))// || (String.IsNullOrEmpty(targetPointSubtypeName)))
                                //        addLateralsDetails[k].PointAlong[j].Subtype = -1;
                                //    else
                                //    {
                                //        try
                                //        {
                                //            string SubVal = targetPointSubtypes.get_SubtypeName(addLateralsDetails[k].PointAlong[j].Subtype);
                                //            //  addLateralsDetails[k].PointAlong[j].Subtype = SubVal
                                //            //targetPointSubtype[k] = addLateralsDetails[k].PointAlong[j].Subtype;

                                //        }
                                //        catch
                                //        {
                                //            addLateralsDetails[k].PointAlong[j].Subtype = targetPointSubtypes.DefaultSubtypeCode;
                                //        }
                                //    }
                                //}
                                //else
                                //{
                                //    addLateralsDetails[k].PointAlong[j].Subtype = -1;
                                //}
                                //addLateralsDetails[k].PointAlong[j].Distance
                                //    addLateralsDetails[k].PointAlong[j].DistanceIsPercent
                                //        addLateralsDetails[k].PointAlong[j].FieldToPopulate
                                //        addLateralsDetails[k].PointAlong[j].ValueToPopulate
                            }
                        }
                    }
                    //****************************************

                    int total;

                    total = pointSelSet.Count;

                    int i = 0;

                    // Create a CancelTracker
                    trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass();

                    // Set the properties of the Step Progressor
                    System.Int32 int32_hWnd = app.hWnd;
                    if (suppressDialog == false)
                    {
                        progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass();
                        stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);

                        stepProgressor.MinRange = 0;
                        stepProgressor.MaxRange = total;
                        stepProgressor.StepValue = 1;
                        stepProgressor.Message = _caption;
                        // Create the ProgressDialog. This automatically displays the dialog
                        progressDialog = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast

                        // Set the properties of the ProgressDialog
                        progressDialog.CancelEnabled = true;
                        progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("AddLine") + i.ToString() + A4LGSharedFunctions.Localizer.GetString("Of") + total.ToString() + ".";
                        progressDialog.Title = _caption;
                        progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;
                        progressDialog.ShowDialog();

                    }
                    // Create an edit operation enabling undo/redo

                    if (logOperation)
                    {
                        try
                        {
                            editor.StartOperation();
                        }
                        catch
                        {
                            logOperation = false;
                        }

                    }

                    IPoint fromPoint = null;
                    IPoint selPt1 = null;
                    IPoint selPt2 = null;
                    ILine distanceLine = null;
                    object Missing = null;
                    IEnvelope env = null;
                    IEnumIDs selIds = null;
                    IFeature pointFeature2 = null;
                    List<int> completedOIDArrayList = null;
                    ITopologicalOperator topoOp = null;
                    IPolygon poly = null;
                    ISpatialFilter sFilter = null;
                    IFeatureCursor lineCursor = null;
                    INetworkFeature pNF = null;
                    IFeature testPointFeature = null;

                    int featOID1, featOID2, nearbyCount;

                    try
                    {

                        // ISelectionSet2 sel = pointSelSet as ISelectionSet2;
                        pointSelSet.Update(null, false, out pointCursor);
                        completedOIDArrayList = new List<int>();

                        while ((pointFeature = (IFeature)pointCursor.NextRow()) != null)
                        {
                            try
                            {
                                //if (inFeatures != null)
                                //{
                                //    if (pointFeature.OID != inFeatures.OID)
                                //    {
                                //        continue;

                                //    }
                                //}
                                i += 1;
                                if (suppressDialog == false)
                                {
                                    //Update progress bar
                                    progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("AddLine") + i.ToString() + A4LGSharedFunctions.Localizer.GetString("Of") + total.ToString() + "." + Environment.NewLine +
                                      A4LGSharedFunctions.Localizer.GetString("CurrentOID") + pointFeature.OID;
                                    stepProgressor.Step();
                                }
                                ESRI.ArcGIS.esriSystem.IStatusBar statusBar = app.StatusBar;
                                statusBar.set_Message(0, i.ToString());

                                //Check if the cancel button was pressed. If so, stop process
                                bool boolean_Continue = trackCancel.Continue();
                                if (!boolean_Continue)
                                {
                                    break;
                                }

                                if (!ComplFeat.Contains(pointFeature))
                                {
                                    //Get the "from" point for new line (start from selected point)
                                    fromPoint = pointFeature.ShapeCopy as IPoint;

                                    //Create new feature(s)

                                    env = new EnvelopeClass();

                                    //Dual Laterals When Two Selected
                                    if (total == 2 && addLateralsDetails[k].Dual_When_Two_Selected)
                                    {
                                        if (suppressDialog == false)
                                        {
                                            //Update progress bar
                                            progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("AddLine") + i.ToString() + A4LGSharedFunctions.Localizer.GetString("Of") + total.ToString() + "." + Environment.NewLine +
                                              A4LGSharedFunctions.Localizer.GetString("CurrentOID") + pointFeature.OID;
                                            stepProgressor.Step();
                                        }
                                        //Obtain both starting points
                                        selIds = pointSelSet.IDs;
                                        selIds.Reset();
                                        featOID1 = selIds.Next();
                                        featOID2 = selIds.Next();
                                        pointFeature2 = pointFLayer.FeatureClass.GetFeature(featOID2);
                                        selPt1 = pointFeature.ShapeCopy as IPoint;
                                        selPt2 = pointFeature2.ShapeCopy as IPoint;

                                        //Measure distance
                                        distanceLine = new LineClass();
                                        distanceLine.PutCoords(selPt1, selPt2);
                                        if (distanceLine.Length <= addLateralsDetails[k].Dual_Max_Distance_When_Two_Selected)
                                        {
                                            LatCreated = CreateDual(ref app, ref editor, pointFeature, pointFeature2, distanceLine, matchLineFLayer,
                                                              targetLineFLayer, pLateralLineEditTemp, pointAlongLayers,
                                                              addLateralsDetails[k].DeleteExistingLines,
                                                              addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].Dual_Option_Make_Square,
                                                              addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                              addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature2);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature);
                                            //CreateDualOld(pointFeature, pointFeature2, distanceLine, matchLineFLayer,
                                            //                lineFeature, targetLineFLayer, targetPointFLayer, addLateralsDetails[k].DeleteExistingLines,
                                            //                 addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].Dual_Option_Make_Square,
                                            //                 addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                            //                 addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                            //                 addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                            //                 addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                            //                 addLateralsDetails[k].PointAlong);

                                            //_targetPointDistance, _targetPointDistanceIsPercent, _targetPointSubtype, _targetPointValue, _targetPointFieldName);
                                            break;
                                        }
                                        //Create two single laterals if the duals are not created
                                        else
                                        {
                                            LatCreated = CreateSingle(ref app, ref editor, pointFeature, matchLineFLayer, targetLineFLayer, pLateralLineEditTemp, pointAlongLayers,
                                                             addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                                             addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                             addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature);

                                            LatCreated = CreateSingle(ref app, ref editor, pointFeature2, matchLineFLayer, targetLineFLayer, pLateralLineEditTemp, pointAlongLayers,
                                                            addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                                            addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                            addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature2);

                                            //CreateSingleOld(pointFeature, matchLineFLayer, lineFeature, targetLineFLayer, targetPointFLayer,
                                            //                addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                            //                addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                            //                addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                            //                addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                            //                addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                            //                addLateralsDetails[k].PointAlong);

                                            //CreateSingleOld(pointFeature2, matchLineFLayer, lineFeature, targetLineFLayer, targetPointFLayer,
                                            //                addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                            //                addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                            //                addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                            //                addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                            //                addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                            //                addLateralsDetails[k].PointAlong);
                                            break;
                                        }
                                    }

                                    //Dual Laterals when Nearby
                                    else if ((total != 1) & addLateralsDetails[k].Dual_When_Nearby)
                                    {

                                        //Check that this feature has not already been completed
                                        if (completedOIDArrayList.Contains(pointFeature.OID))
                                            continue;

                                        selPt1 = pointFeature.ShapeCopy as IPoint;
                                        nearbyCount = 0;
                                        pointFeature2 = null;

                                        //Determine if extactly one other point is within the specified max distance
                                        topoOp = selPt1 as ITopologicalOperator;
                                        poly = topoOp.Buffer(addLateralsDetails[k].Dual_Max_Distance_When_Nearby / 2) as IPolygon;
                                        sFilter = new SpatialFilterClass();
                                        sFilter.Geometry = poly;
                                        sFilter.GeometryField = pointFLayer.FeatureClass.ShapeFieldName;
                                        sFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                                        if (addLateralsDetails[k].SearchOnLayer)
                                            lineCursor = pointFLayer.Search(sFilter, false);
                                        else
                                            lineCursor = pointFLayer.FeatureClass.Search(sFilter, false);

                                        while ((testPointFeature = lineCursor.NextFeature()) != null)
                                        {
                                            if (testPointFeature.OID != pointFeature.OID)
                                            {
                                                //Check that this nearby feature has not already been completed
                                                if (!completedOIDArrayList.Contains(pointFeature.OID))
                                                {
                                                    pointFeature2 = testPointFeature;
                                                    nearbyCount += 1;
                                                }
                                            }
                                            if (nearbyCount > 1)
                                                break;
                                        }

                                        if (nearbyCount == 1)
                                        {
                                            selPt2 = pointFeature2.ShapeCopy as IPoint;

                                            //Measure distance
                                            distanceLine = new LineClass();
                                            distanceLine.PutCoords(selPt1, selPt2);
                                            LatCreated = CreateDual(ref app, ref editor, pointFeature, pointFeature2, distanceLine, matchLineFLayer,
                                                              targetLineFLayer, pLateralLineEditTemp, pointAlongLayers, addLateralsDetails[k].DeleteExistingLines,
                                                              addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].Dual_Option_Make_Square,
                                                              addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                              addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature2);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature);
                                            //CreateDualOld(pointFeature, pointFeature2, distanceLine, matchLineFLayer,
                                            //               lineFeature, targetLineFLayer, targetPointFLayer, addLateralsDetails[k].DeleteExistingLines,
                                            //                addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].Dual_Option_Make_Square,
                                            //                addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                            //                addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                            //                addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                            //                addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                            //                addLateralsDetails[k].PointAlong);
                                            //Add 2nd OID to completed list
                                            completedOIDArrayList.Add(pointFeature2.OID);
                                        }

                                        //Create a single lateral if 1 nearby not found
                                        else
                                        {
                                            LatCreated = CreateSingle(ref app, ref editor, pointFeature, matchLineFLayer, targetLineFLayer, pLateralLineEditTemp, pointAlongLayers,
                                                addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                                            addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                            addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                            if (LatCreated)
                                                ComplFeat.Add(pointFeature);
                                            //CreateSingleOld(pointFeature, matchLineFLayer, lineFeature, targetLineFLayer, targetPointFLayer,
                                            //    addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                            //                addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                            //                addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                            //                addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                            //                addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                            //                addLateralsDetails[k].PointAlong);
                                        }
                                    }
                                    //Single Laterals
                                    else
                                    {
                                        LatCreated = CreateSingle(ref app, ref editor, pointFeature, matchLineFLayer, targetLineFLayer, pLateralLineEditTemp, pointAlongLayers,
                                              addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                                             addLateralsDetails[k].FromToFields, addLateralsDetails[k].Hook_DoglegDistance,
                                                            addLateralsDetails[k].Hook_DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete, store, addLateralsDetails[k].SearchOnLayer, addLateralsDetails[k].SearchDistance, addLateralsDetails[k].Hook_Angle, boolSelectedEdges);
                                        if (LatCreated)
                                            ComplFeat.Add(pointFeature);
                                        //CreateSingleOld(pointFeature, matchLineFLayer, lineFeature, targetLineFLayer, targetPointFLayer,
                                        //     addLateralsDetails[k].LateralLine_StartAtMain, addLateralsDetails[k].DeleteExistingLines,
                                        //                   addLateralsDetails[k].Point_FieldToCalcFromMain, addLateralsDetails[k].Main_FieldToCalcForPoint,
                                        //                   addLateralsDetails[k].Point_PrefixForMainValue, addLateralsDetails[k].LateralLine_ValueToPopulate,
                                        //                   addLateralsDetails[k].LateralLine_FieldToPopulate, targetLineSubValue,
                                        //                   addLateralsDetails[k].Hook_DoglegDistance, addLateralsDetails[k].DistanceIsPercent, addLateralsDetails[k].TolerenceForDelete,
                                        //                   addLateralsDetails[k].PointAlong);
                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);

                            }
                            finally
                            {

                            }
                            //   addLateralsDetails[k].InitDefaults();
                            if (addLateralsDetails[k].Reset_Flow != null)
                            {
                                resetFlow = addLateralsDetails[k].Reset_Flow;

                                if (resetFlow.ToUpper() == "DIGITIZED")
                                {
                                    Globals.GetCommand("A4WaterUtilities_EstablishFlowDigitized", app).Execute();

                                }
                                else if (resetFlow.ToUpper() == "ROLE")
                                {
                                    Globals.GetCommand("A4WaterUtilities_EstablishFlowAncillary", app).Execute();
                                }
                                else if (resetFlow.ToUpper() == "Ancillary".ToUpper())
                                {
                                    Globals.GetCommand("A4WaterUtilities_EstablishFlowAncillary", app).Execute();
                                }
                                else
                                {
                                }
                            }

                        }

                        if (ForceSourcePointConnection)
                        {
                            foreach (IFeature sourcePnt in ComplFeat)
                            {
                                if (sourcePnt != null)
                                {
                                    if (sourcePnt.Shape.IsEmpty != true)
                                    {
                                        if (sourcePnt is INetworkFeature)
                                        {
                                            pNF = (INetworkFeature)sourcePnt;
                                            try
                                            {
                                                pNF.Connect();
                                            }
                                            catch
                                            { }
                                        }
                                    }
                                }
                            }

                        }

                    }
                    catch (Exception ex)
                    {
                        editor.AbortOperation();
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);

                    }
                    finally
                    {
                        // Cleanup
                        if (progressDialog != null)
                            progressDialog.HideDialog();
                        if (lineCursor != null)
                            Marshal.ReleaseComObject(lineCursor);

                        pNF = null;
                        fromPoint = null;
                        selPt1 = null;
                        selPt2 = null;
                        distanceLine = null;
                        Missing = null;
                        env = null;
                        selIds = null;
                        pointFeature2 = null;
                        completedOIDArrayList = null;
                        topoOp = null;
                        poly = null;
                        sFilter = null;
                        lineCursor = null;
                        testPointFeature = null;
                    }

                    if (logOperation)
                    {
                        try
                        {
                            // Stop the edit operation
                            editor.StopOperation(_caption);

                        }
                        catch
                        {
                            logOperation = false;
                        }

                    }

                    //88
                }

                return resetFlow;

            }

            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);
                return "";
            }
            finally
            {
                ComplFeat.Clear();
                if (map != null)
                {
                    (map as IActiveView).Refresh();
                }
                if (progressDialog != null)
                    progressDialog.HideDialog();

                ComplFeat = null;
                map = null;
                editor = null;
                appCursor = null;
                mxdoc = null;
                pointFLayer = null;
                matchLineFLayer = null;
                targetLineFLayer = null;
                eLayers = null;
                pointSelSet = null;
                pointFeatureSelection = null;
                pLateralLineEditTemp = null;
                pointAlongLayers = null;
                pointAlongLayer = null;
                pointCursor = null;
                pointFeature = null;

                //ProgressBar
                progressDialogFactory = null;
                stepProgressor = null;
                progressDialog = null;
                // Create a CancelTracker
                trackCancel = null;

            }
        }
        public static string AddLateralsFromMainPoint(IApplication app, List<AddLateralFromMainPointDetails> addLateralsDetails, IFeature inFeatures, bool logOperation, bool suppressDialog, bool store)
        {
            string resetFlow = "";
            bool useDefaultTemplate;
            List<IFeature> ComplFeat = new List<IFeature>();
            IMap map = null;
            IEditor editor = null;
            IMouseCursor appCursor = null;
            IMxDocument mxdoc = null;
            IFeatureLayer pointFLayer = null;
            IFeatureLayer matchLineFLayer = null;
            IFeatureLayer targetLineFLayer = null;
            IEditLayers eLayers = null;
            ISelectionSet2 pointSelSet = null;
            IFeatureSelection pointFeatureSelection = null;
            IEditTemplate pLateralLineEditTemp = null;
            List<pointAlongSettings> pointAlongLayers = null;
            pointAlongSettings pointAlongLayer = null;
            ICursor pointCursor = null;
            IFeature pointFeature = null;

            //ProgressBar
            ESRI.ArcGIS.Framework.IProgressDialogFactory progressDialogFactory = null;
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = null;
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = null;
            // Create a CancelTracker
            ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel = null;

            try
            {

                if (Control.ModifierKeys == Keys.Control)
                {
                    useDefaultTemplate = false;
                }
                else
                {

                    useDefaultTemplate = true;
                }
                bool boolSelectedEdges = false;
                if (Control.ModifierKeys == Keys.Shift)
                {
                    boolSelectedEdges = true;
                }
                else
                {

                    boolSelectedEdges = false;
                }
                //Get edit session
                bool LatCreated = false;

                editor = Globals.getEditor(app);
                if (editor.EditState != esriEditState.esriStateEditing)
                {
                    MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("MustBEditg"), _caption);
                    editor = null;

                    return "";
                }

                //Change mouse cursor to wait - automatically changes back (ArcGIS Desktop only)
                appCursor = new MouseCursorClass();
                appCursor.SetCursor(2);

                mxdoc = (IMxDocument)app.Document;
                map = editor.Map;

                for (int k = 0; k < addLateralsDetails.Count; k++)
                {
                    bool FCorLayerPoint = true;
                    pointFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].Point_LayerName, ref FCorLayerPoint);
                    if (inFeatures != null)
                    {
                        if (pointFLayer == null)
                            continue;
                        if (pointFLayer.FeatureClass == null)
                            continue;

                        if (inFeatures.Class.CLSID.ToString() != pointFLayer.FeatureClass.CLSID.ToString())
                            continue;

                    }
                    //Report any problems before exiting
                    if (pointFLayer == null)
                    {
                        continue;
                    }

                    bool FCorLayerMatch = true;
                    bool FCorLayerTarget = true;

                    matchLineFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].MainLine_LayerName, ref FCorLayerMatch);
                    targetLineFLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].LateralLine_LayerName, ref FCorLayerTarget);

                    // IFeatureLayerDefinition2 pFeatLayerdef = matchLineFLayer as IFeatureLayerDefinition2;

                    if (matchLineFLayer == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_1") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (matchLineFLayer.FeatureClass == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_1") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (matchLineFLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_2") + "'" + addLateralsDetails[k].MainLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_3") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer.FeatureClass == null)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_3") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }
                    if (targetLineFLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsMess_4") + "'" + addLateralsDetails[k].LateralLine_LayerName + "'.", _caption);
                        return "";
                    }

                    //Confirm the other layers are the correct shape type
                    if (pointFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint || matchLineFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                        return "";

                    //Confirm that target layer is editable and is a line layer
                    eLayers = (IEditLayers)editor;
                    if (!(eLayers.IsEditable(targetLineFLayer)) || (targetLineFLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline))
                        return "";

                    //Confirm that the two line layers are different Feature classes
                    if ((matchLineFLayer.FeatureClass.CLSID == targetLineFLayer.FeatureClass.CLSID) && (matchLineFLayer.FeatureClass.AliasName == targetLineFLayer.FeatureClass.AliasName))
                    {
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_1") , A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_2") );
                        return "";
                    }

                    //Verify that some points are selected
                    pointFeatureSelection = (IFeatureSelection)pointFLayer;

                    if (pointFeatureSelection.SelectionSet.Count == 0)
                        continue;

                    pointSelSet = pointFeatureSelection.SelectionSet as ISelectionSet2;

                    if (useDefaultTemplate)
                    {
                        //pLateralLineEditTemp = Globals.PromptAndGetEditTemplate(app, targetLineFLayer, addLateralsDetails[k].LateralLine_EditTemplate);
                        pLateralLineEditTemp = Globals.PromptAndGetEditTemplateGraphic(targetLineFLayer, addLateralsDetails[k].LateralLine_EditTemplate);
                    }
                    else
                    {
                        pLateralLineEditTemp = Globals.PromptAndGetEditTemplateGraphic(targetLineFLayer, "");
                        //pLateralLineEditTemp = Globals.PromptAndGetEditTemplate(app, targetLineFLayer, "");
                    }

                    if (addLateralsDetails[k].PointAlong != null)
                    {

                        if (addLateralsDetails[k].PointAlong.Length > 0)
                        {
                            pointAlongLayers = new List<pointAlongSettings>();

                            // IEditTemplate pPointAlongEditTemp;
                            for (int j = 0; j < addLateralsDetails[k].PointAlong.Length; j++)
                            {
                                pointAlongLayer = new pointAlongSettings();
                                bool FCorLayerPointsAlong = true;
                                pointAlongLayer.PointAlongLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].PointAlong[j].LayerName, ref FCorLayerPointsAlong);
                                if (pointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PointAlongLayer.FeatureClass == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_2") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";
                                }
                                else if (pointAlongLayer.PointAlongLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
                                {
                                    MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_3") , A4LGSharedFunctions.Localizer.GetString("Warning") );

                                    return "";
                                }

                                pointAlongLayer.PolygonIntersectSide = addLateralsDetails[k].PointAlong[j].PolygonOffsetSide;
                                if (pointAlongLayer.PolygonIntersectSide == null)
                                {
                                    pointAlongLayer.PolygonIntersectSide = "TO";

                                }
                                bool FCorLayerTemp = true;
                                pointAlongLayer.PolygonIntersectLayer = (IFeatureLayer)Globals.FindLayer(map, addLateralsDetails[k].PointAlong[j].PolygonOffsetLayerName, ref FCorLayerTemp);
                                pointAlongLayer.FoundAsLayer = FCorLayerTemp;
                                if (pointAlongLayer == null)
                                {
                                    if (MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsAsk_1") , A4LGSharedFunctions.Localizer.GetString("Warning") , MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)

                                        return "";

                                }
                                else if (pointAlongLayer.PolygonIntersectLayer != null)
                                {

                                    if (pointAlongLayer.PolygonIntersectLayer.FeatureClass != null)
                                    {

                                        //Confirm that target layer is editable and is a line layer
                                        if (pointAlongLayer.PolygonIntersectLayer != null)
                                        {
                                            if (pointAlongLayer.PolygonIntersectLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                                            {
                                                MessageBox.Show(addLateralsDetails[k].PointAlong[j].PolygonOffsetLayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_3"));

                                                return "";
                                            }

                                        }
                                    }
                                }
                                //Confirm that target layer is editable and is a line layer
                                if (pointAlongLayer.PointAlongLayer != null)
                                {
                                    if (!(eLayers.IsEditable(pointAlongLayer.PointAlongLayer)) || (pointAlongLayer.PointAlongLayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint))
                                    {
                                        MessageBox.Show(addLateralsDetails[k].PointAlong[j].LayerName + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsError_4"));

                                        return "";
                                    }
                                    if (useDefaultTemplate)
                                    {
                                        pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplateGraphic(pointAlongLayer.PointAlongLayer, addLateralsDetails[k].PointAlong[j].EditTemplate);
                                        //pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplate(app, pointAlongLayer.PointAlongLayer, addLateralsDetails[k].PointAlong[j].EditTemplate);
                                    }
                                    else
                                    {
                                        pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplateGraphic(pointAlongLayer.PointAlongLayer, "");
                                        //pointAlongLayer.PointAlongEditTemplate = Globals.PromptAndGetEditTemplate(app, pointAlongLayer.PointAlongLayer, "");
                                    }

                                }

                                //if (addLateralsDetails[k].PointAlong[j].Distance < 0)
                                //    pointAlongLayer.PointAlongDistance = 0;
                                //else
                                pointAlongLayer.PointAlongDistance = (double)addLateralsDetails[k].PointAlong[j].Distance;

                                //if (addLateralsDetails[k].PointAlong[j].DistanceIsPercent != null)
                                pointAlongLayer.DistanceIsPercent = (bool)addLateralsDetails[k].PointAlong[j].DistanceIsPercent;
                                //else
                                //  pointAlongLayer.DistanceIsPercent =false;

                                pointAlongLayers.Add(pointAlongLayer);

                            }
                        }
                    }
                    //****************************************

                    int total;

                    total = pointSelSet.Count;

                    int i = 0;

                    // Create a CancelTracker
                    trackCancel = new ESRI.ArcGIS.Display.CancelTrackerClass();

                    // Set the properties of the Step Progressor
                    System.Int32 int32_hWnd = app.hWnd;
                    if (suppressDialog == false)
                    {
                        progressDialogFactory = new ESRI.ArcGIS.Framework.ProgressDialogFactoryClass();
                        stepProgressor = progressDialogFactory.Create(trackCancel, int32_hWnd);

                        stepProgressor.MinRange = 0;
                        stepProgressor.MaxRange = total;
                        stepProgressor.StepValue = 1;
                        stepProgressor.Message = _caption;
                        // Create the ProgressDialog. This automatically displays the dialog
                        progressDialog = (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast

                        // Set the properties of the ProgressDialog
                        progressDialog.CancelEnabled = true;
                        progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("AddLine") + i.ToString() + A4LGSharedFunctions.Localizer.GetString("Of") + total.ToString() + ".";
                        progressDialog.Title = _caption;
                        progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressGlobe;
                        progressDialog.ShowDialog();

                    }
                    // Create an edit operation enabling undo/redo

                    if (logOperation)
                    {
                        try
                        {
                            editor.StartOperation();
                        }
                        catch
                        {
                            logOperation = false;
                        }

                    }

                    //IPoint fromPoint = null;
                    //IPoint selPt1 = null;
                    //IPoint selPt2 = null;
                    //ILine distanceLine = null;
                    //object Missing = null;
                    //IEnvelope env = null;
                    //IEnumIDs selIds = null;
                    //IFeature pointFeature2 = null;
                    List<int> completedOIDArrayList = null;
                    //ITopologicalOperator topoOp = null;
                    //IPolygon poly = null;
                    //ISpatialFilter sFilter = null;
                    IFeatureCursor lineCursor = null;
                    INetworkFeature pNF = null;
                    IFeature testPointFeature = null;

                    int featOID1, featOID2, nearbyCount;

                    try
                    {

                        // ISelectionSet2 sel = pointSelSet as ISelectionSet2;
                        pointSelSet.Update(null, false, out pointCursor);
                        completedOIDArrayList = new List<int>();

                        while ((pointFeature = (IFeature)pointCursor.NextRow()) != null)
                        {
                            try
                            {

                                i += 1;
                                if (suppressDialog == false)
                                {
                                    //Update progress bar
                                    progressDialog.Description = A4LGSharedFunctions.Localizer.GetString("AddLine") + i.ToString() + A4LGSharedFunctions.Localizer.GetString("Of") + total.ToString() + "." + Environment.NewLine +
                                      A4LGSharedFunctions.Localizer.GetString("CurrentOID") + pointFeature.OID;
                                    stepProgressor.Step();
                                }
                                ESRI.ArcGIS.esriSystem.IStatusBar statusBar = app.StatusBar;
                                statusBar.set_Message(0, i.ToString());

                                //Check if the cancel button was pressed. If so, stop process
                                bool boolean_Continue = trackCancel.Continue();
                                if (!boolean_Continue)
                                {
                                    break;
                                }

                                CreateLateralFromMainPoint(ref app, ref editor, pointFeature, matchLineFLayer, targetLineFLayer, pLateralLineEditTemp, pointAlongLayers, addLateralsDetails[k].LateralLine_StartAtMain,
                                    addLateralsDetails[k].FromToFields, addLateralsDetails[k].LateralLine_AngleDetails, addLateralsDetails[k].SearchOnLayer, boolSelectedEdges);

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);

                            }
                            finally
                            {

                            }
                            //   addLateralsDetails[k].InitDefaults();
                            if (addLateralsDetails[k].Reset_Flow != null)
                            {
                                resetFlow = addLateralsDetails[k].Reset_Flow;

                            }
                        }

                        //if (ForceSourcePointConnection)
                        //{
                        //    foreach (IFeature sourcePnt in ComplFeat)
                        //    {
                        //        if (sourcePnt != null)
                        //        {
                        //            if (sourcePnt.Shape.IsEmpty != true)
                        //            {
                        //                if (sourcePnt is INetworkFeature)
                        //                {
                        //                    pNF = (INetworkFeature)sourcePnt;
                        //                    try
                        //                    {
                        //                        pNF.Connect();
                        //                    }
                        //                    catch
                        //                    { }
                        //                }
                        //            }
                        //        }
                        //    }

                        // }

                    }
                    catch (Exception ex)
                    {
                        editor.AbortOperation();
                        MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);

                    }
                    finally
                    {
                        // Cleanup
                        if (progressDialog != null)
                            progressDialog.HideDialog();
                        if (lineCursor != null)
                            Marshal.ReleaseComObject(lineCursor);

                        pNF = null;
                        //fromPoint = null;
                        //selPt1 = null;
                        //selPt2 = null;
                        //distanceLine = null;
                        //Missing = null;
                        //env = null;
                        //selIds = null;
                        //pointFeature2 = null;
                        completedOIDArrayList = null;
                        //topoOp = null;
                        //poly = null;
                        //sFilter = null;
                        lineCursor = null;
                        testPointFeature = null;
                    }

                    if (logOperation)
                    {
                        try
                        {
                            // Stop the edit operation
                            editor.StopOperation(_caption);

                        }
                        catch
                        {
                            logOperation = false;
                        }

                    }

                    //88
                }

                return resetFlow;

            }

            catch (Exception ex)
            {
                MessageBox.Show(A4LGSharedFunctions.Localizer.GetString("ErrorInThe") + A4LGSharedFunctions.Localizer.GetString("ConstructionToolsLbl_2") + "\n" + ex.Message, ex.Source);
                return "";
            }
            finally
            {
                ComplFeat.Clear();
                if (map != null)
                {
                    (map as IActiveView).Refresh();
                }
                if (progressDialog != null)
                    progressDialog.HideDialog();

                ComplFeat = null;
                map = null;
                editor = null;
                appCursor = null;
                mxdoc = null;
                pointFLayer = null;
                matchLineFLayer = null;
                targetLineFLayer = null;
                eLayers = null;
                pointSelSet = null;
                pointFeatureSelection = null;
                pLateralLineEditTemp = null;
                pointAlongLayers = null;
                pointAlongLayer = null;
                pointCursor = null;
                pointFeature = null;

                //ProgressBar
                progressDialogFactory = null;
                stepProgressor = null;
                progressDialog = null;
                // Create a CancelTracker
                trackCancel = null;

            }
        }