Revit API

Revit API

Reply
Contributor
12 Posts
0 Kudos
Registered: ‎09-15-2011
Post 1 of 4

objCategoryNameMapIterator Erase Collection is Read only Purge line styles

205 Views, 3 Replies
09-15-2011 07:41 AM

Hello,

 

I'm trying to write a program to erase imported line styles, however I get Collection is Read only from the following code.

 

Any help appretiated or any work arounds?

 

<Autodesk.Revit.Attributes.

Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _

<Autodesk.Revit.Attributes.

Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)> _

<Autodesk.Revit.Attributes.

Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)> _


Public

Class PurgeLineSubCategories


   

Implements Autodesk.Revit.UI.IExternalCommand


   

PublicFunction Execute(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _

ByRef message AsString, ByVal elements As Autodesk.Revit.DB.ElementSet) _

                            

As Autodesk.Revit.UI.ResultImplements Autodesk.Revit.UI.IExternalCommand.Execute

       

 

       

Dim lineCat As Autodesk.Revit.DB.Categories

     

Dim docActiveDoc AsDocument = commandData.Application.ActiveUIDocument.Document

       

       

Dim trans As New Autodesk.Revit.DB.Transaction(docActiveDoc)

       

If trans.Start("PurgeLineSubCategories") = Autodesk.Revit.DB.TransactionStatus.Started Then


           

If docActiveDoc.IsModifiable = FalseThen


                MsgBox(

"Active document '" & docActiveDoc.PathName.ToString() & "' is not modifiable...")

           

Else



                lineCat = commandData.Application.ActiveUIDocument.Document.Settings.Categories

               

ForEach objCategory AsCategoryIn lineCat

                   

If objCategory.CanAddSubcategory Then

 

                       

Dim objCategoryNameMap As Autodesk.Revit.DB.CategoryNameMap = objCategory.SubCategories

                       

   

Dim objCategoryNameMapIterator As Autodesk.Revit.DB.CategoryNameMapIterator = objCategoryNameMap.ForwardIterator

                        objCategoryNameMapIterator.Reset()

                       

Dim strKey AsString = ""


 

                       

Do While objCategoryNameMapIterator.MoveNext

                                                 

Try

                               

Dim objSubCategory AsCategory = objCategoryNameMapIterator.Current

                               

                               

If objSubCategory.Name = "Enter new name here"Then


                                    strKey = objCategoryNameMapIterator.Key

                                    MsgBox(objSubCategory.Name)

 

                                    objCategoryNameMap.Erase(objCategoryNameMapIterator.Key)

                               

EndIf


    

Catch ex AsException


                                MsgBox(ex.Message)

                           

EndTry


                       

Loop


                       

Try


                            objCategoryNameMap.Erase(strKey)

                       

Catch ex AsException


                            MsgBox(ex.Message)

                       

EndTry


EndIf


      

Next

 

           

EndIf


       

EndIf


   

EndFunction



End

Class

ADN Support Specialist
701 Posts
92 Kudos
Registered: ‎08-20-2007
Post 2 of 4

Re: objCategoryNameMapIterator Erase Collection is Read only Purge line styles

09-19-2011 02:24 AM in reply to: david.rock

Dear David,

You are being a bad boy. Erasing during iteration. Tsk, tsk.

I would suggest that you just make a note of the elements you would like to delete within the loop 'Do While objCategoryNameMapIterator.MoveNext', for instance by adding them to some collection of your own.

Once the iteration has terminated, you can delete them all in a separate step.

I hope this helps.

Cheers, Jeremy.
--
Jeremy Tammik
Autodesk Developer Network -- http://www.autodesk.com/joinadn
The Building Coder -- http://thebuildingcoder.typepad.com



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Contributor
12 Posts
0 Kudos
Registered: ‎09-15-2011
Post 3 of 4

Re: objCategoryNameMapIterator Erase Collection is Read only Purge line styles

09-19-2011 06:50 AM in reply to: jeremytammik

Hello Jeremy,

 

Thank you for your reply.

 

I did thin of that and tried it woithout any luck, I also tried outside the For Each, but with no luck :-(..

 

Any other ideas?

 

Regards

David

 

I now have.

 

<Autodesk.Revit.Attributes.

Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _

<Autodesk.Revit.Attributes.

Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)> _

<Autodesk.Revit.Attributes.

Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)> _


Public

ClassPurgeLineSubCategories


   

Implements Autodesk.Revit.UI.IExternalCommand


   

PublicFunction Execute(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _

ByRef message AsString, ByVal elements As Autodesk.Revit.DB.ElementSet) _

                            

As Autodesk.Revit.UI.ResultImplements Autodesk.Revit.UI.IExternalCommand.Execute

 

Dim docActiveDoc AsDocument = commandData.Application.ActiveUIDocument.Document

       

Dim trans AsNewTransaction(docActiveDoc)

       

If trans.Start("PurgeLineSubCategories") = TransactionStatus.Started Then


           

If docActiveDoc.IsModifiable = FalseThen


                MsgBox(

"Active document '" & docActiveDoc.PathName.ToString() & "' is not modifiable...")

           

Else


               

Dim lineCat AsCategories = docActiveDoc.Settings.Categories

               

Dim strKey AsString = ""


               

ForEach objCategory AsCategoryIn lineCat

                   

If objCategory.Name = "Lines"Then


                       

Dim objCategoryNameMap AsCategoryNameMap = objCategory.SubCategories

                       

Dim objCategoryNameMapIterator AsCategoryNameMapIterator = objCategoryNameMap.ForwardIterator

                        objCategoryNameMapIterator.Reset()

                       

DoWhile objCategoryNameMapIterator.MoveNext

                           

If objCategoryNameMapIterator.Key = "Enter new name here"Then


                                strKey = objCategoryNameMapIterator.Key

                           

EndIf


                       

Loop


                        objCategoryNameMapIterator.Dispose()

                       

If (Not strKey = "") Then


                           

Try


                                MsgBox(

"Erasing " & objCategory.Name & " " & strKey)

                                objCategoryNameMap.Erase(strKey)

                           

Catch ex AsException


                                MsgBox(ex.Message)

                           

EndTry


                       

EndIf


                   

EndIf


               

Next


               

Try


                    lineCat.Erase(strKey)

               

Catch ex AsException


                    MsgBox(ex.Message)

               

EndTry


                trans.Commit()

           

EndIf


       

EndIf


   

EndFunction



End

Class

ADN Support Specialist
701 Posts
92 Kudos
Registered: ‎08-20-2007
Post 4 of 4

Re: objCategoryNameMapIterator Erase Collection is Read only Purge line styles

09-19-2011 07:30 AM in reply to: david.rock

Hi David,

 

Good for you. Nope, sorry, no oither ideas, off-hand, I'm afraid.

 

Cheers, Jeremy.



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!