private ResultType ExportGeometricNetwork() { // Create FeatureDataset filter for GxDialog IGxObjectFilter gxObjectFilter = new GxFilterGeometricNetworksClass(); // Create GxDialog IGxDialog gxDialog = new GxDialogClass() { AllowMultiSelect = false, ButtonCaption = "Export", ObjectFilter = gxObjectFilter, RememberLocation = true, Title = "Save Geometric Network As" }; // Open Dialog if (!gxDialog.DoModalSave(0)) { return ResultType.Cancelled; } // Must specify a name if (string.IsNullOrWhiteSpace(gxDialog.Name)) { GeometricNetworkViewModel.Default.AddMessage("You must specify a name", MessageType.Error); return ResultType.Error; } // Check parent feature dataset IGxObject fd = gxDialog.FinalLocation; if (fd == null || !fd.IsValid) { GeometricNetworkViewModel.Default.AddMessage(string.Format("Invalid feature dataset", gxDialog.Name), MessageType.Error); return ResultType.Error; } // Get feature dataset and workspace IGxDataset dataset = (IGxDataset)fd; IFeatureDataset d = (IFeatureDataset)dataset.Dataset; IWorkspace w = d.Workspace; // Check if geometric network already exists IGeometricNetwork gn = w.FindGeometricNetwork(gxDialog.Name); if (gn != null) { MessageBoxResult r = MessageBox.Show( "The geometric network already exists. Remove?", GeometricNetworkViewModel.Default.WindowTitle, MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation, MessageBoxResult.Yes ); switch (r) { case MessageBoxResult.Yes: IDataset dd = (IDataset)gn; if (!dd.CanDelete()) { GeometricNetworkViewModel.Default.AddMessage("Cannot delete geometric network", MessageType.Error); return ResultType.Error; } try { dd.Delete(); GeometricNetworkViewModel.Default.AddMessage("Deletion successful", MessageType.Information); } catch (Exception ex) { GeometricNetworkViewModel.Default.AddMessage(ex.Message, MessageType.Error); return ResultType.Error; } break; case MessageBoxResult.No: case MessageBoxResult.Cancel: default: return ResultType.Cancelled; } } // Initialize loader INetworkLoader3 networkLoader = new NetworkLoaderClass() { FeatureDatasetName = (IDatasetName)d.FullName, NetworkName = gxDialog.Name, NetworkType = esriNetworkType.esriNTUtilityNetwork, PreserveEnabledValues = PRESERVE_EXISTING_ENABLED_VALUES }; INetworkLoaderProps networkLoadedProps = (INetworkLoaderProps)networkLoader; INetworkLoaderProgress_Event ne = (INetworkLoaderProgress_Event)networkLoader; ne.PutMessage += (a, b) => { GeometricNetworkViewModel.Default.AddMessage(string.Format("Progress: '{0}'", b), MessageType.Information); }; // Assign XY snapping tolerance switch (SNAP_XY) { case SnapTolerance.None: networkLoader.SnapTolerance = 0d; networkLoader.UseXYsForSnapping = false; break; case SnapTolerance.Default: networkLoader.SnapTolerance = networkLoader.DefaultSnapTolerance; networkLoader.UseXYsForSnapping = true; break; case SnapTolerance.Minimum: networkLoader.SnapTolerance = networkLoader.MinSnapTolerance; networkLoader.UseXYsForSnapping = true; break; case SnapTolerance.Maximum: networkLoader.SnapTolerance = networkLoader.MaxSnapTolerance; networkLoader.UseXYsForSnapping = true; break; case SnapTolerance.Custom: if (SNAP_XY_CUSTOM < networkLoader.MinSnapTolerance) { networkLoader.SnapTolerance = networkLoader.MinSnapTolerance; } else if (SNAP_XY_CUSTOM > networkLoader.MaxSnapTolerance) { networkLoader.SnapTolerance = networkLoader.MaxSnapTolerance; } else { networkLoader.SnapTolerance = SNAP_XY_CUSTOM; } networkLoader.UseXYsForSnapping = true; break; } // Assign Z snapping tolerance if (networkLoader.CanUseZs) { switch (SNAP_Z) { case SnapTolerance.None: networkLoader.ZSnapTolerance = 0d; networkLoader.UseZs = false; break; case SnapTolerance.Default: networkLoader.ZSnapTolerance = networkLoader.DefaultZSnapTolerance; networkLoader.UseZs = true; break; case SnapTolerance.Minimum: networkLoader.ZSnapTolerance = networkLoader.MinZSnapTolerance; networkLoader.UseZs = true; break; case SnapTolerance.Maximum: networkLoader.ZSnapTolerance = networkLoader.MaxZSnapTolerance; networkLoader.UseZs = true; break; case SnapTolerance.Custom: if (SNAP_Z_CUSTOM < networkLoader.MinZSnapTolerance) { networkLoader.SnapTolerance = networkLoader.MinZSnapTolerance; } else if (SNAP_XY_CUSTOM > networkLoader.MaxZSnapTolerance) { networkLoader.SnapTolerance = networkLoader.MaxZSnapTolerance; } else { networkLoader.SnapTolerance = SNAP_Z_CUSTOM; } networkLoader.UseZs = true; break; } } else { switch (SNAP_Z) { case SnapTolerance.None: break; case SnapTolerance.Minimum: case SnapTolerance.Maximum: case SnapTolerance.Custom: GeometricNetworkViewModel.Default.AddMessage("Z snapping is unavailable", MessageType.Warning); break; } } // Check each feature class bool badfeatureclass = false; IFeatureClassContainer fcc = (IFeatureClassContainer)d; ZGeometricNetwork zgn = GeometricNetworkViewModel.Default.Dataset as ZGeometricNetwork; foreach (ZNetworkClass znc in zgn.NetworkClasses.Where(f => !f.IsOrphanJunctionFeatureClass)) { // Does the feature class exist? IFeatureClass fc = null; try { fc = fcc.get_ClassByName(znc.Path.Table); } catch { } if (fc == null) { GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' does not exists", znc.Path.Table), MessageType.Error); badfeatureclass = true; continue; } // Is the feature class compatiable? switch (networkLoader.CanUseFeatureClass(znc.Path.Table)) { case esriNetworkLoaderFeatureClassCheck.esriNLFCCCannotOpen: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' cannot be opened", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCInAnotherNetwork: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' belongs to another network", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCInTerrain: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' belongs to a terrain", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCInTopology: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' belongs to a topology", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCInvalidFeatureType: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' has an invalid feature type", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCInvalidShapeType: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' has an invalid shape type", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCIsCompressedReadOnly: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' is compressed readonly", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCRegisteredAsVersioned: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' is registered as versioned", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCUnknownError: GeometricNetworkViewModel.Default.AddMessage(string.Format("Feature class '{0}' has an unknown error", znc.Path.Table), MessageType.Error); badfeatureclass = true; break; case esriNetworkLoaderFeatureClassCheck.esriNLFCCValid: break; } } if (badfeatureclass) { return ResultType.Error; } // Add feature class to network bool snap = SNAP_XY == SnapTolerance.None; foreach (ZNetworkClass znc in zgn.NetworkClasses.Where(f => !f.IsOrphanJunctionFeatureClass)) { string table = znc.Path.Table; if (znc.IsJunction) { // Add simple junction UID uid = new UIDClass() { Value = GUID_SIMPLEJUNCTION_CLSID }; networkLoader.AddFeatureClass(table, esriFeatureType.esriFTSimpleJunction, uid, snap); if (znc.IsSourceSink) { // Check junction already has a role field string field = networkLoadedProps.DefaultAncillaryRoleField; switch (networkLoader.CheckAncillaryRoleField(table, field)) { case esriNetworkLoaderFieldCheck.esriNLFCInvalidDomain: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - The AncillaryRole field has invalid domain", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCInvalidType: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - The AncillaryRole field has invalid type", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCNotFound: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - Adding AncillaryRole field", table, field), MessageType.Information); networkLoader.PutAncillaryRole(table, esriNetworkClassAncillaryRole.esriNCARSourceSink, field); break; case esriNetworkLoaderFieldCheck.esriNLFCUnknownError: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - An unknown error was encountered", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCValid: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - An AncillaryRole field already exists", table, field), MessageType.Warning); networkLoader.PutAncillaryRole(table, esriNetworkClassAncillaryRole.esriNCARSourceSink, field); break; } } } if (znc.IsEdge) { if (znc.IsComplex) { // Add simple junction UID uid = new UIDClass() { Value = GUID_COMPLEXEDGE_CLSID }; networkLoader.AddFeatureClass(table, esriFeatureType.esriFTComplexEdge, uid, snap); } else { // Add simple junction UID uid = new UIDClass() { Value = GUID_SIMPLEEDGE_CLSID }; networkLoader.AddFeatureClass(table, esriFeatureType.esriFTSimpleEdge, uid, snap); } } } // Check if network class has enabled field foreach (ZNetworkClass znc in zgn.NetworkClasses.Where(f =>!f.IsOrphanJunctionFeatureClass)) { string table = znc.Path.Table; string field = networkLoadedProps.DefaultEnabledField; switch (networkLoader.CheckEnabledDisabledField(table, field)) { case esriNetworkLoaderFieldCheck.esriNLFCInvalidDomain: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - The enabled field has invalid domain", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCInvalidType: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - The enabled field has invalid type", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCNotFound: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - Adding enabled field", table, field), MessageType.Information); networkLoader.PutEnabledDisabledFieldName(table, field); break; case esriNetworkLoaderFieldCheck.esriNLFCUnknownError: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - An unknown error was encountered", table, field), MessageType.Error); return ResultType.Error; case esriNetworkLoaderFieldCheck.esriNLFCValid: GeometricNetworkViewModel.Default.AddMessage(string.Format("{0}:{1} - Enabled field already exists", table, field), MessageType.Warning); break; } } // Import network weights foreach (ZNetWeight weight in zgn.Weights) { // Add weight GeometricNetworkViewModel.Default.AddMessage(string.Format("Adding weight {0}", weight.Name), MessageType.Information); networkLoader.AddWeight(weight.Name, weight.WeightType.ToWeightType(), weight.BitGateSize); // Loop for each association foreach (ZNetWeightAssocation association in weight.NetWeightAssocations) { // Add weight association GeometricNetworkViewModel.Default.AddMessage(string.Format("Adding weight association {0}:{1}", association.NetworkClass.Path.Table, association.Field.Name), MessageType.Information); networkLoader.AddWeightAssociation(weight.Name, association.NetworkClass.Path.Table, association.Field.Name); } } // Load network try { networkLoader.LoadNetwork(); } catch (Exception ex) { GeometricNetworkViewModel.Default.AddMessage(ex.Message, MessageType.Error); return ResultType.Error; } // Find new network IGeometricNetwork gn2 = w.FindGeometricNetwork(gxDialog.Name); if (gn2 == null) { GeometricNetworkViewModel.Default.AddMessage("Geometric network creation failed", MessageType.Error); return ResultType.Error; } // Update the name of the orphaned network class ZNetworkClass orphan = zgn.NetworkClasses.FirstOrDefault(n => n.IsOrphanJunctionFeatureClass); if (orphan != null) { orphan.Path.Table = string.Format("{0}_junctions", gxDialog.Name); } // Import junction rules foreach (ZJunctionConnectivityRule rule in zgn.JunctionRules) { GeometricNetworkViewModel.Default.AddMessage(string.Format("Adding junction rule: {0}", rule.Id), MessageType.Information); IJunctionConnectivityRule2 r = new JunctionConnectivityRuleClass { EdgeClassID = rule.Edge.Parent.ToVerfiedId(w).Value, EdgeSubtypeCode = rule.Edge.Code, JunctionClassID = rule.Junction.Parent.ToVerfiedId(w).Value, JunctionSubtypeCode = rule.Junction.Code, EdgeMinimumCardinality = rule.EdgeMinimum, EdgeMaximumCardinality = rule.EdgeMaximum, JunctionMinimumCardinality = rule.JunctionMinimum, JunctionMaximumCardinality = rule.JunctionMaximum, DefaultJunction = rule.IsDefault }; gn2.AddRule(r); } // Import edge rules foreach (ZEdgeConnectivityRule rule in zgn.EdgeRules) { GeometricNetworkViewModel.Default.AddMessage(string.Format("Adding edge rule: {0}", rule.Id), MessageType.Information); IEdgeConnectivityRule r = new EdgeConnectivityRuleClass() { FromEdgeClassID = rule.FromEdge.Parent.ToVerfiedId(w).Value, FromEdgeSubtypeCode = rule.FromEdge.Code, ToEdgeClassID = rule.ToEdge.Parent.ToVerfiedId(w).Value, ToEdgeSubtypeCode = rule.ToEdge.Code }; // Add junctions foreach (ZSubtype j in rule.Junctions) { r.AddJunction(j.Parent.ToVerfiedId(w).Value, j.Code); } // Default junction? if (rule.DefaultJunction != null) { r.DefaultJunctionClassID = rule.DefaultJunction.Parent.ToVerfiedId(w).Value; r.DefaultJunctionSubtypeCode = rule.DefaultJunction.Code; } gn2.AddRule(r); } // GeometricNetworkViewModel.Default.AddMessage("Geometric network creation successful!", MessageType.Information); return ResultType.Successful; }
public void CreateGeometricNetwork(IWorkspace _pWorkspace, IFeatureDatasetName _pFeatureDatasetName,String _pGeometricName) { INetworkLoader2 pNetworkLoader = new NetworkLoaderClass(); // 网络的名称 pNetworkLoader.NetworkName = _pGeometricName; // 网络的类型 pNetworkLoader.NetworkType = esriNetworkType.esriNTUtilityNetwork; // Set the containing feature dataset. pNetworkLoader.FeatureDatasetName = (IDatasetName)_pFeatureDatasetName; // 检查要建立几何网络的数据,每一个要素只能参与一个网络 if (pNetworkLoader.CanUseFeatureClass("PrimaryLine") == esriNetworkLoaderFeatureClassCheck.esriNLFCCValid) { pNetworkLoader.AddFeatureClass("PrimaryLine", esriFeatureType.esriFTComplexEdge, null, false); } if (pNetworkLoader.CanUseFeatureClass("Feeder") == esriNetworkLoaderFeatureClassCheck.esriNLFCCValid) { pNetworkLoader.AddFeatureClass("Feeder", esriFeatureType.esriFTSimpleJunction, null, false); } // 我的数据中没有enable字段,所以,用了false,如果用true的话,就要进行相关的设置 INetworkLoaderProps pNetworkLoaderProps = (INetworkLoaderProps)pNetworkLoader; pNetworkLoader.PreserveEnabledValues = false; // Set the ancillary role field for the Feeder class. String defaultAncillaryRoleFieldName = pNetworkLoaderProps.DefaultAncillaryRoleField; esriNetworkLoaderFieldCheck ancillaryRoleFieldCheck = pNetworkLoader.CheckAncillaryRoleField("Feeder", defaultAncillaryRoleFieldName); switch (ancillaryRoleFieldCheck) { case esriNetworkLoaderFieldCheck.esriNLFCValid: case esriNetworkLoaderFieldCheck.esriNLFCNotFound: pNetworkLoader.PutAncillaryRole("Feeder", esriNetworkClassAncillaryRole.esriNCARSourceSink, defaultAncillaryRoleFieldName); break; default: Console.WriteLine( "The field {0} could not be used as an ancillary role field.", defaultAncillaryRoleFieldName); break; } pNetworkLoader.SnapTolerance = 0.02; // 给几何网络添加权重 pNetworkLoader.AddWeight("Weight", esriWeightType.esriWTDouble, 0); // 将权重和PrimaryLine数据中的SHAPE_Length字段关联 pNetworkLoader.AddWeightAssociation("Weight", "PrimaryLine", "SHAPE_Length"); // 构建网络 pNetworkLoader.LoadNetwork(); }