public ZEdgeConnectivityRule(ZGeometricNetwork geometricNetwork, IEdgeConnectivityRule edgeConnectivityRule)
            : base(edgeConnectivityRule as IRule)
        {
            // Initialize collection
            this.Junctions = new ObservableCollection<ZSubtype>();

            // Get From Edge
            ZSubtype zSubtypeEdge1 = geometricNetwork.FindSubtype(edgeConnectivityRule.FromEdgeClassID, edgeConnectivityRule.FromEdgeSubtypeCode);
            this.FromEdge = zSubtypeEdge1;

            // Get To Edge
            ZSubtype zSubtypeEdge2 = geometricNetwork.FindSubtype(edgeConnectivityRule.ToEdgeClassID, edgeConnectivityRule.ToEdgeSubtypeCode);
            this.ToEdge = zSubtypeEdge2;

            // Loop for each junction
            for (int i = 0; i < edgeConnectivityRule.JunctionCount; i++) {
                // Get junction feature class
                ZSubtype zSubtypeJunction = geometricNetwork.FindSubtype(edgeConnectivityRule.get_JunctionClassID(i), edgeConnectivityRule.get_JunctionSubtypeCode(i));

                // Add junction
                this.Junctions.Add(zSubtypeJunction);
            }

            // Store default
            this.DefaultJunction = geometricNetwork.FindSubtype(edgeConnectivityRule.DefaultJunctionClassID, edgeConnectivityRule.DefaultJunctionSubtypeCode);
        }
        public ZJunctionConnectivityRule(ZGeometricNetwork geometricNetwork, IJunctionConnectivityRule junctionConnectivityRule)
            : base(junctionConnectivityRule as IRule)
        {
            // Get Edge
            ZSubtype zSubtypeEdge = geometricNetwork.FindSubtype(junctionConnectivityRule.EdgeClassID, junctionConnectivityRule.EdgeSubtypeCode);
            this.Edge = zSubtypeEdge;

            // Get Junction
            ZSubtype zSubtypeJunction = geometricNetwork.FindSubtype(junctionConnectivityRule.JunctionClassID, junctionConnectivityRule.JunctionSubtypeCode);
            this.Junction = zSubtypeJunction;

            // Get Cardinality
            this.EdgeMinimum = junctionConnectivityRule.EdgeMinimumCardinality;
            this.EdgeMaximum = junctionConnectivityRule.EdgeMaximumCardinality;
            this.JunctionMinimum = junctionConnectivityRule.JunctionMinimumCardinality;
            this.JunctionMaximum = junctionConnectivityRule.JunctionMaximumCardinality;

            // Get
            IJunctionConnectivityRule2 junctionConnectivityRule2 = (IJunctionConnectivityRule2)junctionConnectivityRule;
            this.IsDefault = junctionConnectivityRule2.DefaultJunction;
        }
        public ZNetWeight(ZGeometricNetwork geometricNetwork, INetWeight netWeight, IEnumNetWeightAssociation enumNetWeightAssociation)
        {
            // Initialize collections
            this.NetWeightAssocations = new ObservableCollection<ZNetWeightAssocation>();

            // Weight name
            this.Name = netWeight.WeightName;

            // Weight type
            this.WeightType = netWeight.WeightType.ToZWeightType();
            if (this.WeightType == ZWeightType.BitGate) {
                this.BitGateSize = netWeight.BitGateSize;
            }

            // Add network assocations
            INetWeightAssociation netWeightAssocation = enumNetWeightAssociation.Next();
            while (netWeightAssocation != null) {
                this.NetWeightAssocations.Add(new ZNetWeightAssocation(geometricNetwork, netWeightAssocation));
                netWeightAssocation = enumNetWeightAssociation.Next();
            }
        }
        //
        // METHODS
        //
        public void Load()
        {
            try {
                // Check parsed named object
                if (this._name == null) {
                    throw new Exception("Invalid object");
                }

                // Open geometric network
                IGeometricNetwork geometricNework = null;
                try {
                    geometricNework = this._name.Open() as IGeometricNetwork;
                }
                catch {
                    throw new Exception("Cannot open geometric network");
                }
                if (geometricNework == null) {
                    throw new Exception("Not a geometric network");
                }

                // Create geometric network
                ZGeometricNetwork zgn = new ZGeometricNetwork(geometricNework);
                GeometricNetworkViewModel.Default.Dataset = zgn;
                GeometricNetworkViewModel.Default.CreateRuleDataSource();
                GeometricNetworkViewModel.Default.AddMessage("Load completed successfully", MessageType.Information);
            }
            catch (Exception ex) {
                // Construct error message
                string message = ex.Message;
            #if DEBUG
                message += ex.StackTrace;
            #endif
                // Add error message
                if (!string.IsNullOrEmpty(message)) {
                    GeometricNetworkViewModel.Default.AddMessage(message, MessageType.Error);
                }
            }
        }
 public ZNetWeightAssocation(ZGeometricNetwork geometricNetwork, INetWeightAssociation netWeightAssociation)
 {
     this.NetworkClass = geometricNetwork.NetworkClasses.FirstOrDefault(n => n.Path.Table == netWeightAssociation.TableName);
     if (this.NetworkClass == null) { return; }
     this.Field = this.NetworkClass.Fields.FirstOrDefault(f => f.Name == netWeightAssociation.FieldName);
 }
        public static IList ToEdgeRuleMatrix(ZGeometricNetwork gn)
        {
            // Create temporary assembly
            AssemblyName an = new AssemblyName("TempAssembly" + gn.GetHashCode());
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
            TypeBuilder tb = moduleBuilder.DefineType(
                "TempType" + gn.GetHashCode(),
                TypeAttributes.Public |
                TypeAttributes.Class |
                TypeAttributes.AutoClass |
                TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit |
                TypeAttributes.AutoLayout,
                typeof(object)
            );

            // Add edge featureclass (first column)
            RuleMatrix.CreateProperty(tb, RuleMatrix.EDGE_FEATURECLASS, typeof(ZNetworkClass));

            // Add edge subtype (second column)
            RuleMatrix.CreateProperty(tb, RuleMatrix.EDGE_SUBTYPE, typeof(ZSubtype));

            // Add duplicate featureclass/subtype fields (columns)
            foreach (ZNetworkClass nc in gn.NetworkClasses.Where(n => n.IsEdge).OrderBy(n => n.Path.Table)) {
                foreach (ZSubtype st in nc.Subtypes.OrderBy(s => s.Name)) {
                    RuleMatrix.CreateProperty(tb, st.Zid, typeof(ZRule));
                }
            }

            // Add edge subtypes (rows)
            Type objectType = tb.CreateType();
            Type listType = typeof(List<>).MakeGenericType(new[] { objectType });
            IList list = Activator.CreateInstance(listType) as IList;
            foreach (ZNetworkClass nc in gn.NetworkClasses.Where(n => n.IsEdge).OrderBy(n => n.Path.Table)) {
                foreach (ZSubtype st in nc.Subtypes.OrderBy(s => s.Name)) {
                    // Create row
                    var row = Activator.CreateInstance(objectType);

                    // Edge feature class
                    PropertyInfo property = objectType.GetProperty(RuleMatrix.EDGE_FEATURECLASS);
                    property.SetValue(
                        row,
                        nc,
                        null);

                    // Edge subtype
                    PropertyInfo property2 = objectType.GetProperty(RuleMatrix.EDGE_SUBTYPE);
                    property2.SetValue(
                        row,
                        st,
                        null);

                    // Add row to list
                    list.Add(row);
                }
            }

            //
            foreach (var row in list) {
                ZSubtype subtype = objectType.GetProperty(RuleMatrix.EDGE_SUBTYPE).GetValue(row, null) as ZSubtype;
                var rules = gn.EdgeRules
                    .Select(r => { return r as ZEdgeConnectivityRule; })
                    .Where(r => r.FromEdge.Zid == subtype.Zid);

                foreach (ZEdgeConnectivityRule rule in rules) {
                    PropertyInfo property2 = objectType.GetProperty(rule.ToEdge.Zid);
                    property2.SetValue(
                        row,
                        rule,
                        null);
                };
            }

            return list;
        }