AutoCAD Architecture Customization

AutoCAD Architecture Customization

Reply
Contributor
BIM_Manager_Orbicon
Posts: 22
Registered: ‎10-27-2010
Message 1 of 3 (319 Views)
Accepted Solution

How to make the rename-method of the Classificationtree work?

319 Views, 2 Replies
08-07-2012 01:11 AM

I need to rename some nodes in a classification tree but I keep on getting the exception.message "eInvalidInput" from the exception.source Acdbmgd.

 

Any suggestions?

 

Here is the code:

<Autodesk.AutoCAD.Runtime.

CommandMethod("RenameClassificationTreeNode")> _

   

PublicSub RenameClassificationTreeNode()

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor()

       

Dim db AsDatabase = HostApplicationServices.WorkingDatabase

       

Dim tr AsTransaction = db.TransactionManager.StartTransaction

       

Try

           

Dim dict AsDictionaryClassificationDefinition = NewDictionaryClassificationDefinition(db)

           

Dim ids AsObjectIdCollection = dict.Records

           

Dim id AsObjectId

           

ForEach id In ids

               

Dim obj AsObject = tr.GetObject(id, OpenMode.ForRead, False, False)

               

Dim classCollDef AsClassificationDefinition = obj

               

If classCollDef.Name = ("AAU_SfBClassification") Then

                   

Dim classTree AsClassificationTree = classCollDef.ClassificationTree()

                    RenameTreeNode(tr, classTree)

               

EndIf

           

Next

       

Catch ex AsApplicationException

            tr.Abort()

       

Finally

            tr.Dispose()

       

EndTry

   

EndSub

 

Private Sub RenameTreeNode(ByRef tr AsTransaction, ByRef node AsClassificationTree)

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor

       

If node IsNothingThenExit Sub

       

'Here is the task: Remove paranthesis if present

       

IfNot (node.Id.IsNull) Then

           

Dim Clsfc AsClassification = CType(tr.GetObject(node.Id, OpenMode.ForRead), Classification)

           

Dim OldName AsString = Clsfc.Name

           

If OldName.Contains("(") AndAlso OldName.Contains(")") Then

               

Dim Newname AsString = Replace(OldName, "(", "")

                Newname = Replace(Newname,

")", "")

               

Try

                    node.Rename(Clsfc.ObjectId, Newname)

' THIS FAILS!!!

               

Catch ex AsException

                    MsgBox(ex.Message)

               

EndTry

           

EndIf

       

EndIf

       

If node.Children.Count > 0 Then

           

ForEach ChildTree In node.Children

                RenameTreeNode(tr, ChildTree)

           

Next

       

EndIf

   

EndSub

Jan Grenov
CAD Manager
Orbicon A/S

Hi Mikako

 

Thanks a lot for your help. I tried it out and made some minor adjustments. Then it worked out fine :-)

 

 

Here's the final code:

 

''  Rename classification definition tree names

    <

CommandMethod("ACANet", "RenameClassificationDefTree", CommandFlags.Modal)> _

   

PublicSub RenameClassificationDefTreeNames()

       

''  Some basic things here

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor()

       

Dim db AsDatabase = HostApplicationServices.WorkingDatabase

       

''  Start a Transaction

       

Dim tr AsTransaction = db.TransactionManager.StartTransaction

       

Try

           

'' get the dictionary for the classification definition,

           

'' and list of records in it

           

''

           

Dim dict AsDictionaryClassificationDefinition = NewDictionaryClassificationDefinition(db)

           

Dim ids AsObjectIdCollection = dict.Records

           

Dim id AsObjectId

           

''  loop through each classification def

           

''

           

ForEach id In ids

               

Dim obj AsObject

                obj = tr.GetObject(id,

OpenMode.ForWrite, False, False)

               

Dim classCollDef AsClassificationDefinition = obj

               

''  print out the name of the classification.

                ed.WriteMessage(classCollDef.Name + vbCrLf)

               

''  Get the tree and print it out

               

Dim classTree AsClassificationTree = classCollDef.ClassificationTree()

                RenameClassificationTree(tr, classTree,

"")

           

Next

            tr.Commit()

       

Catch ex AsApplicationException

            tr.Abort()

            MsgBox(ex.Message)

       

Finally

            tr.Dispose()

       

EndTry

   

EndSub

   

PublicSub RenameClassificationTree(ByRef tr AsTransaction, ByRef node AsClassificationTree, ByVal sIndent AsString)

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor

       

If node IsNothingThen

           

Return

       

EndIf

       

'' let's get the name of the top most node.

       

IfNot (node.Id.IsNull) Then

           

Dim classi AsClassification = CType(tr.GetObject(node.Id, OpenMode.ForWrite), Classification)

           

If classi.Name.StartsWith("(") AndAlso classi.Name.Substring(3, 1) = ")" Then

               

Dim Newname AsString = classi.Name.Substring(1)

               

SelectCase Newname.Length

                   

Case 3

                        Newname = Newname.Substring(0, 2)

                   

Case 4

Newname = Newname.Substring(0, 2) &

"." & Newname.Substring(3)

                   

EndSelect

                node.Rename(node.Id, Newname)

               

Dim s AsString = sIndent + classi.Name + "(" + node.Id.Handle.ToString + ")" + vbCrLf

                ed.WriteMessage(s)

           

EndIf

       

EndIf

       

'' visit childen.

       

If node.Children.Count > 0 Then

           

'' this is a intemidiate node, go through each branch.

           

Dim child AsClassificationTree

            sIndent = sIndent +

"  "

           

ForEach child In node.Children

                RenameClassificationTree(tr, child, sIndent)

           

Next

       

EndIf

   

EndSub

End

Class

ADN Support Specialist
mikako.harada
Posts: 52
Registered: ‎08-26-2007
Message 2 of 3 (300 Views)

Re: How to make the rename-method of the Classificationtree work?

08-12-2012 10:35 PM in reply to: BIM_Manager_Orbicon

Hi Jan,

 

I did not try your code, but looks like your code is written in read only mode.  If you want to modify database objects, you will need to open for write and commit transaction.

 

Here is a sample code that goes through the nodes and add " API" to the name of each node.  I hope this helps.

 

 

''  Rename classification definition tree names

  ''  

  ''

  <CommandMethod("ACANet", "RenameClassificationDefTree", CommandFlags.Modal)> _

  PublicSub RenameClassificationDefTreeNames()

 

    ''  Some basic things here

    Dim ed AsEditor =

      Application.DocumentManager.MdiActiveDocument.Editor

    Dim db AsDatabase = HostApplicationServices.WorkingDatabase

 

    ''  Start a Transaction

    Dim tr AsTransaction = db.TransactionManager.StartTransaction

 

    Try

      '' get the dictionary for the classification definition,

      '' and list of records in it

      ''

      Dim dict AsDictionaryClassificationDefinition =

        NewDictionaryClassificationDefinition(db)

      Dim ids AsObjectIdCollection = dict.Records

      Dim id AsObjectId

 

      ''  loop through each classification def

      ''

      ForEach id In ids

        Dim obj AsObject = tr.GetObject(id, OpenMode.ForWrite, False, False)

        Dim classCollDef AsClassificationDefinition = obj

 

        ''  print out the name of the classification.

        ed.WriteMessage(classCollDef.Name + vbCrLf)

 

        ''  Get the tree and print it out

        Dim classTree AsClassificationTree =

          classCollDef.ClassificationTree

        RenameClassificationTree(tr, classTree, "")

 

        tr.Commit()

 

        'End If

 

      Next

 

    Catch ex AsApplicationException

      tr.Abort()

      System.Windows.Forms.MessageBox.Show(ex.Message)

    Finally

      tr.Dispose()

    EndTry

 

  EndSub

 

  ''  Given a top most node of a tree, traverse the tree

  ''

  PublicSub RenameClassificationTree(

                                   ByRef tr AsTransaction,

                                   ByRef node AsClassificationTree,

                                   ByVal sIndent AsString)

 

    Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor

 

    If node IsNothingThen

      Return

    EndIf

 

    '' let's get the name of the top most node.

    ''

    IfNot (node.Id.IsNull) Then

      Dim classi AsClassification =

        CType(tr.GetObject(node.Id, OpenMode.ForWrite), Classification)

 

      node.Rename(node.Id, classi.Name + " API")

 

      Dim s AsString = sIndent + classi.Name +

        "(" + node.Id.Handle.ToString + ")" + vbCrLf

      ed.WriteMessage(s)

 

    EndIf

 

    '' visit childen.

    ''

    If node.Children.Count > 0 Then

      '' this is a intemidiate node, go through each branch.

      Dim child AsClassificationTree

      sIndent = sIndent + "  "

      ForEach child In node.Children

        RenameClassificationTree(tr, child, sIndent)

      Next

    EndIf

 

  EndSub


Mikako Harada
Developer Technical Services
Autodesk Developer Network

Contributor
BIM_Manager_Orbicon
Posts: 22
Registered: ‎10-27-2010
Message 3 of 3 (296 Views)

Re: How to make the rename-method of the Classificationtree work?

08-13-2012 01:24 AM in reply to: BIM_Manager_Orbicon

Hi Mikako

 

Thanks a lot for your help. I tried it out and made some minor adjustments. Then it worked out fine :-)

 

 

Here's the final code:

 

''  Rename classification definition tree names

    <

CommandMethod("ACANet", "RenameClassificationDefTree", CommandFlags.Modal)> _

   

PublicSub RenameClassificationDefTreeNames()

       

''  Some basic things here

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor()

       

Dim db AsDatabase = HostApplicationServices.WorkingDatabase

       

''  Start a Transaction

       

Dim tr AsTransaction = db.TransactionManager.StartTransaction

       

Try

           

'' get the dictionary for the classification definition,

           

'' and list of records in it

           

''

           

Dim dict AsDictionaryClassificationDefinition = NewDictionaryClassificationDefinition(db)

           

Dim ids AsObjectIdCollection = dict.Records

           

Dim id AsObjectId

           

''  loop through each classification def

           

''

           

ForEach id In ids

               

Dim obj AsObject

                obj = tr.GetObject(id,

OpenMode.ForWrite, False, False)

               

Dim classCollDef AsClassificationDefinition = obj

               

''  print out the name of the classification.

                ed.WriteMessage(classCollDef.Name + vbCrLf)

               

''  Get the tree and print it out

               

Dim classTree AsClassificationTree = classCollDef.ClassificationTree()

                RenameClassificationTree(tr, classTree,

"")

           

Next

            tr.Commit()

       

Catch ex AsApplicationException

            tr.Abort()

            MsgBox(ex.Message)

       

Finally

            tr.Dispose()

       

EndTry

   

EndSub

 

 

 

   

PublicSub RenameClassificationTree(ByRef tr AsTransaction, ByRef node AsClassificationTree, ByVal sIndent AsString)

       

Dim ed AsEditor = Application.DocumentManager.MdiActiveDocument.Editor

       

If node IsNothingThen

           

Return

       

EndIf

       

'' let's get the name of the top most node.

       

IfNot (node.Id.IsNull) Then

           

Dim classi AsClassification = CType(tr.GetObject(node.Id, OpenMode.ForWrite), Classification)

           

If classi.Name.StartsWith("(") AndAlso classi.Name.Substring(3, 1) = ")" Then

               

Dim Newname AsString = classi.Name.Substring(1)

               

SelectCase Newname.Length

                   

Case 3

                        Newname = Newname.Substring(0, 2)

                   

Case 4

Newname = Newname.Substring(0, 2) &

"." & Newname.Substring(3)

                   

EndSelect

                node.Rename(node.Id, Newname)

               

Dim s AsString = sIndent + classi.Name + "(" + node.Id.Handle.ToString + ")" + vbCrLf

                ed.WriteMessage(s)

           

EndIf

       

EndIf

       

'' visit childen.

       

If node.Children.Count > 0 Then

           

'' this is a intemidiate node, go through each branch.

           

Dim child AsClassificationTree

            sIndent = sIndent +

"  "

           

ForEach child In node.Children

                RenameClassificationTree(tr, child, sIndent)

           

Next

       

EndIf

   

EndSub

 

End

Class

Jan Grenov
CAD Manager
Orbicon A/S
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 or visit the Installation and Licensing Forum to get help installing your software.