OpenRoads Designer CONNECT Edition SDK Help

Delete all named boundaries with related sheet and drawing models

The below code snippet shows how to delete all named boundaries from the DGN with their related sheets and drawings .


 public void DeleteAllNamedBoundaries()
        {

            List<string> nbList = new List<string>();

            //Get active DGN
            DgnModelRef dgnModelRef = Session.Instance.GetActiveDgnModelRef();
            DgnFile dgnFile = dgnModelRef.GetDgnFile();

            //Get all models 
            ModelIndexCollection modelIndexCollection = dgnFile.GetModelIndexCollection();
            foreach (ModelIndex modelIndex in modelIndexCollection)
            {
                DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt status, modelIndex.Id);

                if (dgnModel.ModelType == DgnModelType.Normal)
                {
                    //Get Named Boundary Collection using DgnModel
                    NamedBoundaryCollection namedBoundaries = new NamedBoundaryCollection(dgnModel);
                    IEnumerator<NamedBoundary> namedBoundaryIterator = namedBoundaries.GetEnumerator();

                    //Add unique names of Named boundaries into list 
                    while (namedBoundaryIterator.MoveNext())
                    {
                        nbList.Add(namedBoundaryIterator.Current.UniqueName);
                    }
                }
            }

            //Iterate the names to delete named boundaries 
            foreach (string namedBoundaryName in nbList)
            {
                DeleteNamedBoundaryWithSheetAndDrawing(namedBoundaryName);
            }
        }

        public void DeleteNamedBoundaryWithSheetAndDrawing(string namedBoundaryUniqueName)
        {

            try
            {
                if (namedBoundaryUniqueName == null) return;

                //Get active DGN
                DgnModelRef dgnModelRef = Session.Instance.GetActiveDgnModelRef();
                DgnFile dgnFile = dgnModelRef.GetDgnFile();

                //Get all models 
                ModelIndexCollection modelIndexCollection = dgnFile.GetModelIndexCollection();
                foreach (ModelIndex modelIndex in modelIndexCollection)
                {
                    DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt status, modelIndex.Id);

                    if (dgnModel.ModelType == DgnModelType.Normal)
                    {
                        //Get Named Boundary Collection using DgnModel
                        NamedBoundaryCollection namedBoundaries = new NamedBoundaryCollection(dgnModel);
                        if (namedBoundaries.Count() == 0) return;

                        //Get the named boundary from collection
                        var namedBoundaryToDelete = namedBoundaries.FirstOrDefault(m => m.UniqueName == namedBoundaryUniqueName);
                        if (namedBoundaryToDelete == null) return;

                        //Delete Named Boundary
                        namedBoundaryToDelete.Delete();
                        break;
                    }
                }


                //Generate sheet model and drawing model name from Named boundary unique name
                string drawingModelName = namedBoundaryUniqueName.Split(':').First() + " - " + namedBoundaryUniqueName.Split(':').Last();
                string sheetModelName = drawingModelName + " [Sheet]";

                //Iterate for deleting the sheet and drawing model
                foreach (ModelIndex modelIndex in modelIndexCollection)
                {
                    DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt status, modelIndex.Id);

                    if (dgnModel.ModelType == DgnModelType.Sheet || dgnModel.ModelType == DgnModelType.Drawing)
                    {
                        //Delete models related to named boundary               
                        if (dgnModel.ModelName == drawingModelName || dgnModel.ModelName == sheetModelName)
                        {
                            //delete model
                            dgnFile.DeleteModel(dgnModel);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }