public void AddNode(EditObject eObj)
 {
     Node snappednd = null;
     Node ndNew = null;
     if (eObj.Connectivity.snappedNodes.Count > 1)
     {
         snappednd = eObj.Connectivity.snappedNodes.First(nd => StatusExtensions.IsNetJunction(nd.status));
     }
     snappednd = eObj.Connectivity.snappedNodes != null ? eObj.Connectivity.snappedNodes[0] : null;
     if (StatusExtensions.IsNetJunction(snappednd.status) && ClassIDRules.IsAdjJunction(snappednd) && ClassIDRules.IsAdjGroupNode(eObj.CLSID))
     {
         ndNew = new Node();
         ndNew.status = eObj.Connectivity.status;
         ndNew.uid = eObj.UID;
         snappednd.adjacentNode = ndNew;
         ndNew.classID = eObj.CLSID;
         ndNew.x = snappednd.x;
         ndNew.y = snappednd.y;
         ndNew.phaseCode = eObj.Connectivity.phaseCode;
         ndNew.parentList = snappednd.parentList;
         AddSecondaryToAdjNode(snappednd, ndNew);
     }
     else if (StatusExtensions.IsNetJunction(snappednd.status))
     {
         ndNew = new Node();
         ndNew.status = eObj.Connectivity.status;
         ndNew.uid = eObj.UID;
         ndNew.x = snappednd.x;
         ndNew.y = snappednd.y;
         ndNew.classID = eObj.CLSID;
         ndNew.phaseCode = eObj.Connectivity.phaseCode;
         ndNew.parentList = snappednd.parentList;
         ndNew.childList = snappednd.childList;
         AdjustParentChild(snappednd, ndNew);
         //now delete net junction *****
         if (StaticStuff._nodeList.ContainsKey(snappednd.uid)) StaticStuff._nodeList.Remove(snappednd.uid);
     }
     if (ndNew != null)
     {
         StaticStuff._nodeList.Add(ndNew.uid, ndNew);
         ExtensionInfo.checkFlow.AddPointsToPointList(ndNew);
     }
 }
        public void AddSection(EditObject eObj)
        {
            Section newSect = new Section();
            newSect.uid = eObj.UID;
            if (eObj.Connectivity.parentNode == null)
            {
                IPolyline ln = (IPolyline)eObj.shape;
                Node FromNetJn = CreateNetJn(eObj);
                FromNetJn.x = ln.FromPoint.X;
                FromNetJn.y = ln.FromPoint.Y;
                eObj.Connectivity.parentNode = FromNetJn;
                StaticStuff._nodeList.Add(FromNetJn.uid, FromNetJn);
                ExtensionInfo.checkFlow.AddPointsToPointList(FromNetJn);
                Node toNetJn = CreateNetJn(eObj);
                toNetJn.x = ln.ToPoint.X;
                toNetJn.y = ln.ToPoint.Y;
                eObj.Connectivity.childNode = toNetJn;
                StaticStuff._nodeList.Add(toNetJn.uid, toNetJn);
                ExtensionInfo.checkFlow.AddPointsToPointList(toNetJn);
            }
            else if (eObj.Connectivity.childNode == null)
            {
                Node netJn = CreateNetJn(eObj);
                IPolyline ln = (IPolyline)eObj.shape;
                if (StatusExtensions.IsWithFlow(eObj.Connectivity.status))
                {
                    netJn.x = ln.ToPoint.X;
                    netJn.y = ln.ToPoint.Y;
                }
                else if (StatusExtensions.IsAgainstFlow(eObj.Connectivity.status))
                {
                    netJn.x = ln.FromPoint.X;
                    netJn.y = ln.FromPoint.Y; ;
                }
                eObj.Connectivity.childNode = netJn;
                StaticStuff._nodeList.Add(netJn.uid, netJn);
                ExtensionInfo.checkFlow.AddPointsToPointList(netJn);
            }
            newSect.phaseCode = eObj.Connectivity.phaseCode;
            newSect.classID = eObj.CLSID;
            newSect.status = eObj.Connectivity.status;
            if (StatusExtensions.IsWithFlow(eObj.Connectivity.status))
            {
                newSect.x = eObj.Connectivity.parentNode.x;
                newSect.y = eObj.Connectivity.parentNode.y;
                newSect.tox = eObj.Connectivity.childNode.x;
                newSect.toy = eObj.Connectivity.childNode.y;
            }
            else if (StatusExtensions.IsAgainstFlow(eObj.Connectivity.status))
            {
                newSect.x = eObj.Connectivity.childNode.x;
                newSect.y = eObj.Connectivity.childNode.y;
                newSect.tox = eObj.Connectivity.parentNode.x;
                newSect.toy = eObj.Connectivity.parentNode.y;
            }

            newSect.classID = eObj.CLSID;
            newSect.phaseCode = eObj.Connectivity.phaseCode;
            newSect.parentNode = eObj.Connectivity.parentNode;
            newSect.childNode = eObj.Connectivity.childNode;
            if (newSect.parentNode.childList == null)
                newSect.parentNode.childList = new Dictionary<string, Section>();
            newSect.parentNode.childList.Add(newSect.uid, newSect);
            if (newSect.childNode.parentList == null)
                newSect.childNode.parentList = new Dictionary<string, Section>();
            newSect.childNode.parentList.Add(newSect.uid, newSect);

            StaticStuff._sectionList.Add(newSect.uid, newSect);
            ExtensionInfo.checkFlow.AddPointsToLineList(newSect);
        }
 public Node CreateNetJn(EditObject eObj)
 {
     Node netJn = new Node();
     netJn.status = Constants.Energized + Constants.NetJunction;
     if (StatusExtensions.IsUnenergized(eObj.Connectivity.status))
     {
         netJn.status = Constants.Unenergized + Constants.NetJunction;
     }
     else if (StatusExtensions.IsEnergized(eObj.Connectivity.status))
     {
         netJn.status = Constants.Energized + Constants.NetJunction;
     }
     else if (StatusExtensions.IsLoop(eObj.Connectivity.status))
     {
         netJn.status = Constants.Loop + Constants.NetJunction;
     }
     if (StatusExtensions.IsDisconnected(eObj.Connectivity.status))
     {
         netJn.status = Constants.Disconnected + Constants.NetJunction;
     }
     netJn.uid = "{" + Guid.NewGuid().ToString() + "}";
     netJn.phaseCode = eObj.Connectivity.phaseCode;
     netJn.classID = -1;//ntInfo.networkClassIds[ClassIDRules.NetJunction];
     netJn.oid = -1;
     return netJn;
 }
        private void CreateFeature(IGeometry geometry)
        {
            if (geometry == null)
                return;

            if (_currentTargetLayer == null)
                return;

            _operationStarted = false;
            bool alreadyInEditOperation = GISApplication.IsInEditOperation(_workspace);
            if (alreadyInEditOperation)
                GISApplication.StopEditOperation(_workspace);

            //SRIRAM:calc connectivity for new feature; if invalid feature, return not creating feature. We are not sure what is invalid yet.

            ConnectivityInfo eoConn = null; //some of the info is for nodes only and some for sections only.//TODO

            if (geometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                IPoint pt = (IPoint)geometry;
                eoConn = ExtensionInfo.checkFlow.GetParent(pt.X, pt.Y);
            }
            if (geometry.GeometryType == esriGeometryType.esriGeometryPolyline)
            {
                IPolyline ln = (IPolyline)geometry;
                eoConn = ExtensionInfo.checkFlow.GetParent(ln.FromPoint.X, ln.FromPoint.Y, ln.ToPoint.X, ln.ToPoint.Y);
            }

            _operationStarted = GISApplication.StartEditOperation(_workspace);
            if (!_operationStarted)
                return;

            //if 2 lines below are inside of the EditOperation, storing Network Edges throws a COM Exception.
            _newFeature = _currentTargetLayer.FeatureClass.CreateFeature();
            if (_newFeature == null)
                return;

            try { _newFeature.Shape = geometry; }
            catch (Exception e)
            {
                DeactivateCurrentTool();
                string msg = e.Message;
                if (((fdoError)Marshal.GetHRForException(e)) == fdoError.FDO_E_CLOSED_POLYLINE) msg = "Closed polylines not allowed.";
                Exception newEx = new Exception(msg);
            }
            try
            {

            ExtensionInfo.InternalEdit = true;

            DatabaseUtil.SetDefaultValues(_newFeature); //set default values
            string guid = Utilities.GenerateGUID(true);
            DatabaseUtil.SetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.guidName, guid);

            if (ExtensionInfo.netUtil.ntInfo.networkClassIds.ContainsValue(_currentTargetLayer.FeatureClass.ObjectClassID))
            {
                EditObject eObj = new EditObject()
                {
                    ClassName = Associated_FeatureClassName,
                    CLSID = _currentTargetLayer.FeatureClass.ObjectClassID,
                    Connectivity = eoConn,
                    ElementType = (geometry.GeometryType == esriGeometryType.esriGeometryPoint) ? ElementType.Node : ElementType.Section,
                    OID = _newFeature.OID,
                    Partner = null, //Partner is only for split
                    Type = EditType.Add,
                    UID = guid,
                    shape = geometry
                };//TODO

                ExtensionInfo.EditQueue.Add(eObj);
                //inherit from parent
                int engPh = 128;
                string constPhase = "UnKnown";
                if (eoConn != null && eoConn.parentFeature != null)
                {
                    engPh = eoConn.phaseCode;
                    constPhase = Utilities.BitgatePhaseToStringPhase(eoConn.constructedPhaseCode);
                    IFeature parent = DatabaseUtil.GetReadOnlyFeature(ExtensionInfo.Editor.EditWorkspace, eoConn.parentFeature.classID, eoConn.parentFeature.oid);
                    if (parent != null)
                    {
                        InheritElectricAttributesFromParent(_newFeature, parent);
                        if(eObj.ElementType == ElementType.Section && eoConn.constructedPhaseCode != eoConn.parentFeature.phaseCode)
                        {
                            //TODO
                        }
                    }
                }

                //update PhaseCode, ConstPhase
                DatabaseUtil.SetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.phaseCodeField, engPh);

                DatabaseUtil.SetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.constructedPhaseField, constPhase);

                //check if it is source
                if (ExtensionInfo.netUtil.ntInfo.Source != null && ExtensionInfo.netUtil.ntInfo.Source.ClassID == _currentTargetLayer.FeatureClass.ObjectClassID)
                {
                    if (string.IsNullOrEmpty(ExtensionInfo.netUtil.ntInfo.Source.FieldName) ||
                        string.Compare(ExtensionInfo.netUtil.ntInfo.Source.FieldValue, DatabaseUtil.ToText(DatabaseUtil.GetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.Source.FieldName)), true) == 0)
                        eObj.IsSource = true;
                }

                //check if it is network protector
                if (ExtensionInfo.netUtil.ntInfo.NTProtector != null && ExtensionInfo.netUtil.ntInfo.NTProtector.ClassID == _currentTargetLayer.FeatureClass.ObjectClassID)
                {
                    if (string.IsNullOrEmpty(ExtensionInfo.netUtil.ntInfo.NTProtector.FieldName) ||
                        string.Compare(ExtensionInfo.netUtil.ntInfo.NTProtector.FieldValue, DatabaseUtil.ToText(DatabaseUtil.GetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.NTProtector.FieldName)), true) == 0)
                        eObj.IsNetworkProtector = true;
                }

                //check if it is YD transformer
                if (ExtensionInfo.netUtil.ntInfo.YDTransformer != null && ExtensionInfo.netUtil.ntInfo.YDTransformer.ClassID == _currentTargetLayer.FeatureClass.ObjectClassID)
                {
                    if (string.IsNullOrEmpty(ExtensionInfo.netUtil.ntInfo.YDTransformer.FieldName) ||
                        string.Compare(ExtensionInfo.netUtil.ntInfo.YDTransformer.FieldValue, DatabaseUtil.ToText(DatabaseUtil.GetFieldValue(_newFeature, ExtensionInfo.netUtil.ntInfo.YDTransformer.FieldName)), true) == 0)
                        eObj.IsYDTransformer = true;
                }
            }

            _newFeature.Store(); //this will fire OnCreate event

            ExtensionInfo.InternalEdit = false;

                //if (DataElement.DoesFieldExist(_newFeature, GISFieldNames.Structure_Guid_FieldName))
                //{
                //    ElectricDataElement.AssociateOrCreateStructure(_newFeature as IFeature, false);
                //    _newFeature.Store();
                //}

                GISApplication.StopEditOperation(_workspace);

                ArcMap.Document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, _newFeature, ArcMap.Document.ActiveView.Extent);
            }
            catch(Exception ex)
            {
                if (_operationStarted)
                {
                    GISApplication.AbortEditOperation(_workspace);
                    _operationStarted = false;
                }
                DeactivateCurrentTool();
            }
            finally
            {
                _operationStarted = false;
            }

            if (alreadyInEditOperation)
                GISApplication.StartEditOperation(_workspace);
        }