Revit API

Reply
Active Member
david.rock
Posts: 7
Registered: ‎09-15-2011
Message 1 of 4 (199 Views)

objCategoryNameMapIterator Erase Collection is Read only Purge line styles

199 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
jeremytammik
Posts: 434
Registered: ‎08-20-2007
Message 2 of 4 (180 Views)

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

Active Member
david.rock
Posts: 7
Registered: ‎09-15-2011
Message 3 of 4 (176 Views)

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
jeremytammik
Posts: 434
Registered: ‎08-20-2007
Message 4 of 4 (174 Views)

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Revit Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Revit helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Revit

Twitter

Facebook

Blogs

Youtube