/// <summary>
        /// Check if a rep rule for the selected SymbolIDCode exists and if so returns it
        /// Ignore Case in check for Rule Name (since Rep Rules ignore case)
        /// </summary>
        /// <returns>-1 if not found</returns>
        private int getRepRuleIdForSidc(IRepresentationClass repClass, string sidc)
        {
            int returnRepRuleId = -1;

            if ((symbolCreator == null) || (repClass == null))
            {
                return(-1);
            }

            string symboName = symbolCreator.GetRuleNameFromSidc(sidc).ToUpper();

            if (string.IsNullOrEmpty(symboName))
            {
                Console.WriteLine("Empty Name returned for SIDC: " + sidc);
                return(-1);
            }

            IRepresentationRules repRules = repClass.RepresentationRules;

            repRules.Reset();

            int ruleID = 0;
            IRepresentationRule rule;

            repRules.Next(out ruleID, out rule);

            while (rule != null)
            {
                if (rule != null)
                {
                    string ruleName = repRules.get_Name(ruleID).ToUpper();

                    if (ruleName == symboName)
                    {
                        returnRepRuleId = ruleID;
                        break;
                    }
                }

                repRules.Next(out ruleID, out rule);
            }

            if (returnRepRuleId == -1)
            {
                System.Diagnostics.Debug.WriteLine("Existing Rule not found for " + symboName);
            }

            return(returnRepRuleId);
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            switch (this.int_0)
            {
            case 0:
                if (!this.reprensationGeneralPage_0.Apply())
                {
                    return;
                }
                this.reprensationGeneralPage_0.Visible = false;
                this.representationRulesPage_0.Visible = true;
                this.btnLast.Enabled = true;
                this.btnNext.Text    = "完成";
                break;

            case 1:
            {
                IDataset   dataset   = this.ifeatureClass_0 as IDataset;
                IWorkspace workspace = dataset.Workspace;
                try
                {
                    IWorkspaceExtensionManager manager = workspace as IWorkspaceExtensionManager;
                    UID gUID = new UIDClass
                    {
                        Value = "{FD05270A-8E0B-4823-9DEE-F149347C32B6}"
                    };
                    IRepresentationWorkspaceExtension extension =
                        manager.FindExtension(gUID) as IRepresentationWorkspaceExtension;
                    if (extension != null)
                    {
                        new RepresentationRulesClass();
                        this.irepresentationClass_0 = extension.CreateRepresentationClass(this.ifeatureClass_0,
                                                                                          this.reprensationGeneralPage_0.RepresentationName,
                                                                                          this.reprensationGeneralPage_0.RuleIDFieldName,
                                                                                          this.reprensationGeneralPage_0.OverrideFieldName,
                                                                                          this.reprensationGeneralPage_0.RequireShapeOverride, this.irepresentationRules_0, null);
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                    return;
                }
                base.DialogResult = DialogResult.OK;
                return;
            }
            }
            this.int_0++;
        }
        /// <summary>
        /// Check if a rep rule for the selected SymbolIDCode exists and if so returns it
        /// </summary>
        /// <returns>-1 if symbol not found/could not be added</returns>
        private int addRepRuleIdForSidc(IRepresentationClass repClass, string sidc)
        {
            if ((symbolCreator == null) || (repClass == null))
            {
                return(-1);
            }

            int repRuleId = -1;

            IRepresentationRules repRules = repClass.RepresentationRules;

            IRepresentationRule newRule = new RepresentationRuleClass();

            ISymbol symbol = symbolCreator.GetMarkerSymbolFromSIC(sidc) as ISymbol;

            if (symbol == null)
            {
                Console.WriteLine("ERROR: Null Symbol returned for SIDC: " + sidc);
                return(-1);
            }

            IMarkerSymbol markerSymbol = symbol as IMarkerSymbol;

            const double DEFAULT_MARKER_SIZE = 32.0;

            if (markerSymbol != null)
            {
                markerSymbol.Size = DEFAULT_MARKER_SIZE;
            }

            (newRule as IRepresentationRuleInit).InitWithSymbol(symbol);

            string symboName = symbolCreator.GetRuleNameFromSidc(sidc);

            if (string.IsNullOrEmpty(symboName))
            {
                return(-1);
            }

            repRuleId = repRules.Add(newRule);
            repRules.set_Name(repRuleId, symboName);

            repRulesWereAdded = true;

            Console.WriteLine("Adding new RepRule for Name: {0}, SIDC:{1}", symboName, sidc);

            return(repRuleId);
        }
        private void processSidc(IRepresentationClass repClass, IFeatureBuffer targetFeatureBuffer, string sidc)
        {
            if ((symbolCreator == null) || (repClass == null) || (targetFeatureBuffer == null))
            {
                Console.WriteLine("Failed to initialize - could not create RepRule for SIDC: " + sidc);
                return;
            }

            if (!symbolCreator.Initialized)
            {
                Console.WriteLine("SymbolCreator is not initialized - can't look up SIDCs/symbols");
                return;
            }

            int ruleIdIndex = targetFeatureBuffer.Fields.FindField(MilitaryFeatureClassHelper.RULE_FIELD_NAME1);

            if (ruleIdIndex < 0) // *2* different rule field names, need to check for both
            {
                ruleIdIndex = targetFeatureBuffer.Fields.FindField(MilitaryFeatureClassHelper.RULE_FIELD_NAME2);
            }

            if (ruleIdIndex < 0)
            {
                Console.WriteLine("Could not find field:{0}/{1} in output",
                                  MilitaryFeatureClassHelper.RULE_FIELD_NAME1,
                                  MilitaryFeatureClassHelper.RULE_FIELD_NAME2);
                return;
            }

            int repRuleId = getRepRuleIdForSidc(repClass, sidc);

            if (repRuleId < 0)
            {
                repRuleId = addRepRuleIdForSidc(repClass, sidc);
            }

            if (repRuleId < 0)
            {
                Console.WriteLine("Could not create RepRule for SIDC: " + sidc);
                return;
            }

            object o = repRuleId;

            targetFeatureBuffer.set_Value(ruleIdIndex, o);
        }
Exemple #5
0
        internal static IRepresentationClass GetRepresentationClassByFeature(ICompositeLayer compLayer,
                                                                             IFeature pFeature)
        {
            IRepresentationClass representationClass;
            IRepresentationClass representationClassByFeature = null;
            int num = 0;

            while (true)
            {
                if (num < compLayer.Count)
                {
                    ILayer layer = compLayer.Layer[num];
                    if (layer is IGroupLayer)
                    {
                        representationClassByFeature =
                            MapHelper.GetRepresentationClassByFeature(layer as ICompositeLayer, pFeature);
                        if (representationClassByFeature != null)
                        {
                            representationClass = representationClassByFeature;
                            break;
                        }
                    }
                    else if (layer is IFeatureLayer && (layer as IFeatureLayer).FeatureClass == pFeature.Class &&
                             (layer as IGeoFeatureLayer).Renderer is IRepresentationRenderer)
                    {
                        representationClass =
                            ((layer as IGeoFeatureLayer).Renderer as IRepresentationRenderer).RepresentationClass;
                        break;
                    }
                    num++;
                }
                else
                {
                    representationClass = null;
                    break;
                }
            }
            return(representationClass);
        }
Exemple #6
0
        public static IRepresentationClass GetRepresentationClassByFeature(IBasicMap ibasicMap_0, IFeature ifeature_0)
        {
            IRepresentationClass representationClassByFeature = null;

            for (int i = 0; i < ibasicMap_0.LayerCount; i++)
            {
                ILayer layer = ibasicMap_0.get_Layer(i);
                if (layer is IGroupLayer)
                {
                    representationClassByFeature = GetRepresentationClassByFeature(layer as ICompositeLayer, ifeature_0);
                    if (representationClassByFeature != null)
                    {
                        return(representationClassByFeature);
                    }
                }
                else if (((layer is IFeatureLayer) && ((layer as IFeatureLayer).FeatureClass == ifeature_0.Class)) &&
                         ((layer as IGeoFeatureLayer).Renderer is IRepresentationRenderer))
                {
                    return(((layer as IGeoFeatureLayer).Renderer as IRepresentationRenderer).RepresentationClass);
                }
            }
            return(null);
        }
        public IRepresentationClass GetRepresentationClassForFeatureClass(IFeatureClass featureClass)
        {
            if (RepresentationWorkspaceExtension == null)
            {
                return(null);
            }

            IRepresentationClass repClass = null;

            IEnumDatasetName datasetNames = RepresentationWorkspaceExtension.get_FeatureClassRepresentationNames(featureClass);

            datasetNames.Reset();
            IDatasetName dsName;

            while ((dsName = datasetNames.Next()) != null)
            {
                string repName = dsName.Name;
                repClass = RepresentationWorkspaceExtension.OpenRepresentationClass(repName);
                // TODO: only gets first Rep Class set / assumes only one name/set
                break;
            }

            return(repClass);
        }
Exemple #8
0
        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (this.txtRepresentationName.Text.Trim().Length == 0)
            {
                MessageBox.Show("名字不能为空!");
            }
            else if (this.txtruleIDFldName.Text.Trim().Length == 0)
            {
                MessageBox.Show("RuleID字段不能为空!");
            }
            else if (this.txtoverrideFldName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Override字段不能为空!");
            }
            else
            {
                try
                {
                    IFeatureClass featureClass = this.ifeatureLayer_0.FeatureClass;
                    IRepresentationWorkspaceExtension repWSExtFromFClass =
                        RepresentationAssist.GetRepWSExtFromFClass(featureClass);
                    if (repWSExtFromFClass != null)
                    {
                        IRepresentationRule  repRule = RepresentationAssist.CreateRepresentationRule(featureClass);
                        IRepresentationRules rules   = new RepresentationRulesClass();
                        rules.Add(repRule);
                        IRepresentationClass class3 = repWSExtFromFClass.CreateRepresentationClass(featureClass,
                                                                                                   this.txtRepresentationName.Text, this.txtruleIDFldName.Text, this.txtoverrideFldName.Text,
                                                                                                   this.rdoRequireShapeOverride.Checked, rules, null);
                        if ((this.imap_0 != null) && this.chkAddLayer.Checked)
                        {
                            IFeatureLayer layer = new FeatureLayerClass
                            {
                                FeatureClass = featureClass
                            };
                            IFeatureRenderer renderer = new RepresentationRendererClass();
                            (renderer as IRepresentationRenderer).RepresentationClass = class3;
                            (layer as IGeoFeatureLayer).Renderer = renderer;
                            this.imap_0.AddLayer(layer);
                            (this.imap_0 as IActiveView).Refresh();
                        }
                        base.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        base.DialogResult = DialogResult.OK;
                    }
                }
                catch (COMException exception)
                {
                    switch (exception.ErrorCode)
                    {
                    case -2147218682:
                        MessageBox.Show(exception.Message);
                        return;

                    case -2147218675:
                        MessageBox.Show("该要素类中已存在同名的制图表现");
                        break;

                    case -2147218674:
                        MessageBox.Show("该要素类中已存在同名RuleID字段或Override字段");
                        break;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        private bool processFeatureClass(string rule, string geometry, IFeatureClass inputFeatureClass, IFeatureClass outputFeatureClass, string sidcFieldName)
        {
            repRulesWereAdded = false;

            // allows testing without writing the output to the feature
            const bool DEBUG_DONT_WRITE_OUTPUT = false; //  true;

            if ((mapper == null) || (inputFeatureClass == null) || (outputFeatureClass == null) ||
                (militaryFeatures == null) || (symbolCreator == null) ||
                (!mapper.Initialized) || (!militaryFeatures.Initialized))
            {
                return(false);
            }

            bool success = false;

            int sicFieldIndex = inputFeatureClass.Fields.FindField(sidcFieldName);

            if (sicFieldIndex < 0)
            {
                Console.WriteLine("SIDC field not found: {0} - ABORTING", sidcFieldName);
                return(false);
            }

            doFieldMapping(inputFeatureClass, outputFeatureClass, sidcFieldName);

            // Start Editing
            IWorkspaceEdit workspaceEdit = militaryFeatures.Workspace as IWorkspaceEdit;

            if (workspaceEdit == null)
            {
                return(false);
            }
            workspaceEdit.StartEditing(false);
            workspaceEdit.StartEditOperation();

            IRepresentationClass repClass = militaryFeatures.GetRepresentationClassForFeatureClass(outputFeatureClass);

            if (repClass == null)
            {
                Console.WriteLine("RepresentationClass not found in output - ABORTING");
                return(false);
            }

            // setup insert cursor
            IFeatureBuffer targetFeatureBuffer = outputFeatureClass.CreateFeatureBuffer();
            IFeatureCursor targetFeatureCursor = outputFeatureClass.Insert(true);

            IFeatureCursor featureCursor  = inputFeatureClass.Search(null, true);
            IFeature       currentFeature = featureCursor.NextFeature();

            int matchingFeatureCount = 0;

            while (currentFeature != null)
            {
                string sidc = currentFeature.get_Value(sicFieldIndex) as string;

                string matchingRule = mapper.RuleNameFromSymbolIdAndGeometry(sidc, geometry);

                if (matchingRule != rule)
                {
                    currentFeature = featureCursor.NextFeature();
                    continue;
                }

                matchingFeatureCount++;

                Console.WriteLine("Processing Matching Feature: #:{0}, SIDC:{1}, Rule:{2}", matchingFeatureCount, sidc, rule);

                try
                {
                    targetFeatureBuffer.Shape = currentFeature.Shape;
                }
                catch (System.Runtime.InteropServices.COMException ce)
                {
                    Console.WriteLine("-->Could not copy geometry - you may need to add Z-values or run Fix Geometry Tool, error code=" + ce.ErrorCode);
                }

                processFieldMapping(currentFeature, targetFeatureBuffer);

                processSidc(repClass, targetFeatureBuffer, sidc);

                processMiscellaneousFields(targetFeatureBuffer, sidc);

                if (!DEBUG_DONT_WRITE_OUTPUT)
                {
                    // insert new feature
                    targetFeatureCursor.InsertFeature(targetFeatureBuffer);
                }

                currentFeature = featureCursor.NextFeature();
            }

            if (!DEBUG_DONT_WRITE_OUTPUT)
            {
                targetFeatureCursor.Flush();
            }

            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);

            // Release the cursors to remove the lock on the data.
            System.Runtime.InteropServices.Marshal.ReleaseComObject(targetFeatureCursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
            targetFeatureCursor = null;
            featureCursor       = null;

            if (!DEBUG_DONT_WRITE_OUTPUT)
            {
                // looks totally nonsensical - but this forces any new rules to written
                if (repRulesWereAdded)
                {
                    repClass.RepresentationRules = repClass.RepresentationRules;
                }
            }

            return(success);
        }
        /// <summary>
        /// Set the Representation Rule ID based on sidc field of a
        /// Military Feature destinationGeodatabase/featurelayer
        /// </summary>
        /// <returns>Success: True/False</returns>
        public bool CalculateRepRulesFromSidc(string outputMilitaryFeatureClassString, string sidcFieldName,
                                              string standard)
        {
            bool success = false;

            MilitaryFeatureClassHelper.SIDC_FIELD_NAME2 = sidcFieldName;

            symbolCreator = new SymbolCreator(standard);
            bool initialized = symbolCreator.Initialize();

            if (!initialized)
            {
                lastErrorCode = 7; detailedErrorMessage = "Could not load required Style Files. Check ArcGIS/Styles folder.";
                Console.WriteLine("Failed to initialize - could not load required Style Files");
                return(false);
            }

            militaryFeatures = new MilitaryFeatureClassHelper();
            IFeatureClass outputFeatureClass = militaryFeatures.GetFeatureClassByName(outputMilitaryFeatureClassString);

            if (!militaryFeatures.Initialized || (outputFeatureClass == null))
            {
                lastErrorCode = 4; detailedErrorMessage = "Output FeatureClass could not be found/opened: " + outputMilitaryFeatureClassString;
                Console.WriteLine(detailedErrorMessage);
                return(false);
            }

            if (!militaryFeatures.IsFeatureClassLockable(outputFeatureClass))
            {
                // if a schema lock can't be obtained for feature class then bail on them all
                lastErrorCode = 5; detailedErrorMessage = string.Format("Exclusive Schema Lock can not be obtained for output feature class found for Rule:{0}", outputMilitaryFeatureClassString);
                Console.WriteLine(detailedErrorMessage);
                return(false);
            }

            ////////////////////////////////////////////////////////////
            // Initialization/Verification complete, now do processing

            repRulesWereAdded = false;

            ///////////////////////////////////////////////////////////////////
            // DEBUG SWITCH: allows testing without writing the output to the feature class
            const bool DEBUG_DONT_WRITE_OUTPUT = false; //  true;
            ///////////////////////////////////////////////////////////////////

            ////////////////////////////////////////////////////////////
            // TRICKY: Handle the 2 different output SIC/SIDC names in Military Features
            int sicFieldIndex = outputFeatureClass.Fields.FindField(MilitaryFeatureClassHelper.SIDC_FIELD_NAME1);

            if (sicFieldIndex < 0)
            {
                sicFieldIndex = outputFeatureClass.Fields.FindField(MilitaryFeatureClassHelper.SIDC_FIELD_NAME2);
                if (sicFieldIndex < 0)
                {
                    lastErrorCode = 6; detailedErrorMessage = string.Format("ABORTING: Could not find SIDC field in output");
                    Console.WriteLine(detailedErrorMessage);
                    return(false);
                }
            }
            ////////////////////////////////////////////////////////////

            // Start Editing
            IWorkspaceEdit workspaceEdit = militaryFeatures.Workspace as IWorkspaceEdit;

            if (workspaceEdit == null)
            {
                lastErrorCode = 5; detailedErrorMessage = string.Format("Exclusive Schema Lock can not be obtained for output feature class found for Rule:{0}", outputMilitaryFeatureClassString);
                Console.WriteLine(detailedErrorMessage);
                return(false);
            }

            workspaceEdit.StartEditing(false);
            workspaceEdit.StartEditOperation();

            IRepresentationClass repClass = militaryFeatures.GetRepresentationClassForFeatureClass(outputFeatureClass);

            if (repClass == null)
            {
                Console.WriteLine("ABORTING: RepresentationClass not found in output");
                return(false);
            }

            // setup insert cursor
            IFeatureCursor featureCursor  = outputFeatureClass.Update(null, true);
            IFeature       currentFeature = featureCursor.NextFeature();

            int featureCount = 0;

            while (currentFeature != null)
            {
                string sidc = currentFeature.get_Value(sicFieldIndex) as string;

                if (!symbolCreator.IsValidSic(sidc))
                {
                    if (string.IsNullOrEmpty(sidc) || (sidc.Length <= 0))
                    {
                        Console.WriteLine("Skipping empty SIDC");
                    }
                    else
                    {
                        Console.WriteLine("Skipping invalid SIDC: " + sidc);
                    }

                    currentFeature = featureCursor.NextFeature();
                    continue;
                }

                featureCount++;

                IFeatureBuffer featureBuffer = currentFeature as IFeatureBuffer;

                processSidc(repClass, featureBuffer, sidc);

                if (!DEBUG_DONT_WRITE_OUTPUT)
                {
                    featureCursor.UpdateFeature(currentFeature);
                }

                currentFeature = featureCursor.NextFeature();
            }

            if (!DEBUG_DONT_WRITE_OUTPUT)
            {
                featureCursor.Flush();
            }

            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);

            // Release the cursors to remove the lock on the data.
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
            featureCursor = null;

            if (!DEBUG_DONT_WRITE_OUTPUT)
            {
                // looks totally nonsensical - but this forces any new rules to written
                if (repRulesWereAdded)
                {
                    repClass.RepresentationRules = repClass.RepresentationRules;
                }
            }

            success = true;

            return(success);
        }
        private void processSidc(IRepresentationClass repClass, IFeatureBuffer targetFeatureBuffer, string sidc)
        {
            if ((symbolCreator == null) || (repClass == null) || (targetFeatureBuffer == null))
            {
                Console.WriteLine("Failed to initialize - could not create RepRule for SIDC: " + sidc);
                return;
            }

            if (!symbolCreator.Initialized)
            {
                Console.WriteLine("SymbolCreator is not initialized - can't look up SIDCs/symbols");
                return;
            }

            int ruleIdIndex = targetFeatureBuffer.Fields.FindField(MilitaryFeatureClassHelper.RULE_FIELD_NAME1);
            if (ruleIdIndex < 0) // *2* different rule field names, need to check for both
                ruleIdIndex = targetFeatureBuffer.Fields.FindField(MilitaryFeatureClassHelper.RULE_FIELD_NAME2);

            if (ruleIdIndex < 0)
            {
                Console.WriteLine("Could not find field:{0}/{1} in output",
                    MilitaryFeatureClassHelper.RULE_FIELD_NAME1,
                    MilitaryFeatureClassHelper.RULE_FIELD_NAME2);
                return;
            }

            int repRuleId = getRepRuleIdForSidc(repClass, sidc);

            if (repRuleId < 0)
                repRuleId = addRepRuleIdForSidc(repClass, sidc);

            if (repRuleId < 0)
            {
                Console.WriteLine("Could not create RepRule for SIDC: " + sidc);
                return;
            }

            object o = repRuleId;
            targetFeatureBuffer.set_Value(ruleIdIndex, o);
        }
        /// <summary>
        /// Check if a rep rule for the selected SymbolIDCode exists and if so returns it
        /// Ignore Case in check for Rule Name (since Rep Rules ignore case)
        /// </summary>
        /// <returns>-1 if not found</returns>
        private int getRepRuleIdForSidc(IRepresentationClass repClass, string sidc)
        {
            int returnRepRuleId = -1;

            if ((symbolCreator == null) || (repClass == null))
                return -1;

            string symboName = symbolCreator.GetRuleNameFromSidc(sidc).ToUpper();
            if (string.IsNullOrEmpty(symboName))
            {
                Console.WriteLine("Empty Name returned for SIDC: " + sidc);
                return -1;
            }

            IRepresentationRules repRules = repClass.RepresentationRules;
            repRules.Reset();

            int ruleID = 0;
            IRepresentationRule rule;
            repRules.Next(out ruleID, out rule);

            while (rule != null)
            {
                if (rule != null)
                {
                    string ruleName = repRules.get_Name(ruleID).ToUpper();

                    if (ruleName == symboName)
                    {
                        returnRepRuleId = ruleID;
                        break;
                    }
                }

                repRules.Next(out ruleID, out rule);
            }

            if (returnRepRuleId == -1)
                System.Diagnostics.Debug.WriteLine("Existing Rule not found for " + symboName);

            return returnRepRuleId;
        }
        /// <summary>
        /// Check if a rep rule for the selected SymbolIDCode exists and if so returns it
        /// </summary>
        /// <returns>-1 if symbol not found/could not be added</returns>
        private int addRepRuleIdForSidc(IRepresentationClass repClass, string sidc)
        {
            if ((symbolCreator == null) || (repClass == null))
                return -1;

            int repRuleId = -1;

            IRepresentationRules repRules = repClass.RepresentationRules;

            IRepresentationRule newRule = new RepresentationRuleClass();

            ISymbol symbol = symbolCreator.GetMarkerSymbolFromSIC(sidc) as ISymbol;
            if (symbol == null)
            {
                Console.WriteLine("ERROR: Null Symbol returned for SIDC: " + sidc);
                return -1;
            }

            IMarkerSymbol markerSymbol = symbol as IMarkerSymbol;

            const double DEFAULT_MARKER_SIZE = 32.0;
            if (markerSymbol != null)
                markerSymbol.Size = DEFAULT_MARKER_SIZE;

            (newRule as IRepresentationRuleInit).InitWithSymbol(symbol);

            string symboName = symbolCreator.GetRuleNameFromSidc(sidc);
            if (string.IsNullOrEmpty(symboName))
                return -1;

            repRuleId = repRules.Add(newRule);
            repRules.set_Name(repRuleId, symboName);

            repRulesWereAdded = true;

            Console.WriteLine("Adding new RepRule for Name: {0}, SIDC:{1}", symboName, sidc);

            return repRuleId;
        }