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
Solved! Go to Solution.
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
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
Can't find what you're looking for? Ask the community or share your knowledge.