.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit DatabaseServices.DBText using VB.NET

5 REPLIES 5
Reply
Message 1 of 6
MRHooker2u
1175 Views, 5 Replies

Edit DatabaseServices.DBText using VB.NET

I am trying to update some text in a group of drawings outside of AutoCAD's drawing editor using VB.NET. The code runs without error but the text is not updated in the drawing file. With a few minor tweeks, most of this code is from Jerry Winter's VB.NET programming book. Where I think I am having the problem is myText = myEnt, doing the edit to myText, then saying myEnt = myText. I have tried the statement: myText.UpgradeOpen() and leaving out the myEnt = myText statement but I get the same results.

Any insight would be helpful. Thanks!


Added Note: If I insert a myDB.SaveAs after the myTrans.Commit and save the drawings under a different name the text edits have been made.





Code:



Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager = Nothing

Dim myText As New DBText



' Reads a log file and builds list of files to update

getISOGenFiles(ProjectID)



Try

For Each FileName As String In colIsoNumber

' Now open the .dwg file and extract the line data

Dim myDB As New DatabaseServices.Database

myDB.ReadDwgFile(FileName, IO.FileShare.Read, True, "")

Dim myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction

myTransMan = myDB.TransactionManager

myTrans = myTransMan.StartTransaction

myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)

mySTE = myBT.GetEnumerator

While mySTE.MoveNext

myBTR = mySTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)

If myBTR.IsLayout = True Then

myBTE = myBTR.GetEnumerator

Dim myEnt As DatabaseServices.Entity

While myBTE.MoveNext

myEnt = myBTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)

If TypeOf myEnt Is DatabaseServices.DBText Then

myText = myEnt

If myText.TextString.Contains("NOINS") Or _

myText.TextString.Contains("_") Then

myEnt.UpgradeOpen()

' This function updates the text string

myText.TextString = ExtractText(myText.TextString)

myEnt = myText

End If

End If

End While

End If

End While



myTrans.Commit()

myTrans.Dispose()

Next

Catch ex As System.Exception

MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.StackTrace, ex.GetType().ToString())

Finally

myTransMan.Dispose()

End Try


Edited by: MRHooker2u on Sep 15, 2009 2:23 PM Edited by: MRHooker2u on Sep 15, 2009 2:27 PM
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: MRHooker2u

Sorry to learn you've been suckered in by that book.

Were it not for the fact that just looking at at that code hurts,
I would offer some suggestions, but I honestly do not have
time to decyper it.

The only advice I will offer, is to beware that most of the code
in that book is broken because it uses IEnumerator directly,
rather than For Each/Next. That can result in fatal errors in
cases where Dispose() is not called on the object returned
by GetEnumerator() (which the subject code from that book
never does).

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6254527@discussion.autodesk.com...
I am trying to update some text in a group of drawings outside of AutoCAD's
drawing editor using VB.NET. The code runs without error but the text is not
updated in the drawing file. With a few minor tweeks, most of this code is
from Jerry Winter's VB.NET programming book. Where I think I am having the
problem is myText = myEnt, doing the edit to myText, then saying myEnt =
myText. I have tried the statement: myText.UpgradeOpen() and leaving out the
myEnt = myText statement but I get the same results.
Any insight would be helpful. Thanks!
Added Note: If I insert a myDB.SaveAs after the myTrans.Commit and save the
drawings under a different name the text edits have been made.
Code:

Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager =
Nothing
Dim myText As New DBText

' Reads a log file and builds list of files to update
getISOGenFiles(ProjectID)

Try
For Each FileName As String In colIsoNumber
' Now open the .dwg file and extract the line data
Dim myDB As New DatabaseServices.Database
myDB.ReadDwgFile(FileName, IO.FileShare.Read, True, "")
Dim myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction
myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
mySTE = myBT.GetEnumerator
While mySTE.MoveNext
myBTR = mySTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout = True Then
myBTE = myBTR.GetEnumerator
Dim myEnt As DatabaseServices.Entity
While myBTE.MoveNext
myEnt = myBTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If TypeOf myEnt Is DatabaseServices.DBText Then
myText = myEnt
If myText.TextString.Contains("NOINS") Or _
myText.TextString.Contains("_") Then
myEnt.UpgradeOpen()
' This function updates the text string
myText.TextString = ExtractText(myText.TextString)
myEnt = myText
End If
End If
End While
End If
End While

myTrans.Commit()
myTrans.Dispose()
Next
Catch ex As System.Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.StackTrace,
ex.GetType().ToString())
Finally
myTransMan.Dispose()
End Try Edited by: MRHooker2u on Sep 15, 2009 2:23 PM Edited by: MRHooker2u
on Sep 15, 2009 2:27 PM
Message 3 of 6
Anonymous
in reply to: MRHooker2u


Not my area of expertise at all, but I would not
copy the object to a temp object and then back again. Do everything on
myEnt.

 


style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">
I
am trying to update some text in a group of drawings outside of AutoCAD's
drawing editor using VB.NET. The code runs without error but the text is not
updated in the drawing file. With a few minor tweeks, most of this code is
from Jerry Winter's VB.NET programming book. Where I think I am having the
problem is myText = myEnt, doing the edit to myText, then saying myEnt =
myText. I have tried the statement: myText.UpgradeOpen() and leaving out the
myEnt = myText statement but I get the same results.
Any insight would be
helpful. Thanks!
Added Note: If I insert a myDB.SaveAs after the
myTrans.Commit and save the drawings under a different name the text edits
have been made.
Code:

Dim myTransMan As
Autodesk.AutoCAD.DatabaseServices.TransactionManager = Nothing
Dim myText
As New DBText

' Reads a log file and builds list of files to update

getISOGenFiles(ProjectID)

Try
For Each FileName As String In
colIsoNumber
' Now open the .dwg file and extract the line data
Dim myDB
As New DatabaseServices.Database
myDB.ReadDwgFile(FileName,
IO.FileShare.Read, True, "")
Dim myTrans As
Autodesk.AutoCAD.DatabaseServices.Transaction
myTransMan =
myDB.TransactionManager
myTrans = myTransMan.StartTransaction
myBT =
myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
mySTE =
myBT.GetEnumerator
While mySTE.MoveNext
myBTR =
mySTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If
myBTR.IsLayout = True Then
myBTE = myBTR.GetEnumerator
Dim myEnt As
DatabaseServices.Entity
While myBTE.MoveNext
myEnt =
myBTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If TypeOf myEnt
Is DatabaseServices.DBText Then
myText = myEnt
If
myText.TextString.Contains("NOINS") Or _
myText.TextString.Contains("_")
Then
myEnt.UpgradeOpen()
' This function updates the text
string
myText.TextString = ExtractText(myText.TextString)
myEnt =
myText
End If
End If
End While
End If
End
While

myTrans.Commit()
myTrans.Dispose()
Next
Catch ex As
System.Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf &
ex.StackTrace,
ex.GetType().ToString())
Finally
myTransMan.Dispose()
End Try Edited
by: MRHooker2u on Sep 15, 2009 2:23 PM Edited by: MRHooker2u on Sep 15, 2009
2:27 PM
Message 4 of 6
MRHooker2u
in reply to: MRHooker2u

Thanks for the replies! A solution was given to me that resolves the issue. I appreciate the response.
Message 5 of 6
Anonymous
in reply to: MRHooker2u

{quote}

"David Bartliff" <> wrote:

Not my area of expertise at all, but I would not copy the object to a temp
object and then back again. Do everything on myEnt.

{quote}

That code isn't copying anything.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
Message 6 of 6
Anonymous
in reply to: MRHooker2u

{quote}

Added Note: If I insert a myDB.SaveAs after the myTrans.Commit and save the
drawings under a different name the text edits have been made.

{quote}

I didn't read your entire post, but now that I looked again, you save the
modified database. Were you expecting it to be saved automatically?

You have to use Database.SaveAs() to save changes to a database.

My advice is to use SaveAs() to save the database to a temporary file in the
same folder as the original database. Then, assuming the SaveAs() succeeded,
erase the original database, and rename the new database to the filename of
the erased original database.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6254527@discussion.autodesk.com...
I am trying to update some text in a group of drawings outside of AutoCAD's
drawing editor using VB.NET. The code runs without error but the text is not
updated in the drawing file. With a few minor tweeks, most of this code is
from Jerry Winter's VB.NET programming book. Where I think I am having the
problem is myText = myEnt, doing the edit to myText, then saying myEnt =
myText. I have tried the statement: myText.UpgradeOpen() and leaving out the
myEnt = myText statement but I get the same results.
Any insight would be helpful. Thanks!
Added Note: If I insert a myDB.SaveAs after the myTrans.Commit and save the
drawings under a different name the text edits have been made.
Code:

Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager =
Nothing
Dim myText As New DBText

' Reads a log file and builds list of files to update
getISOGenFiles(ProjectID)

Try
For Each FileName As String In colIsoNumber
' Now open the .dwg file and extract the line data
Dim myDB As New DatabaseServices.Database
myDB.ReadDwgFile(FileName, IO.FileShare.Read, True, "")
Dim myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction
myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
mySTE = myBT.GetEnumerator
While mySTE.MoveNext
myBTR = mySTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout = True Then
myBTE = myBTR.GetEnumerator
Dim myEnt As DatabaseServices.Entity
While myBTE.MoveNext
myEnt = myBTE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If TypeOf myEnt Is DatabaseServices.DBText Then
myText = myEnt
If myText.TextString.Contains("NOINS") Or _
myText.TextString.Contains("_") Then
myEnt.UpgradeOpen()
' This function updates the text string
myText.TextString = ExtractText(myText.TextString)
myEnt = myText
End If
End If
End While
End If
End While

myTrans.Commit()
myTrans.Dispose()
Next
Catch ex As System.Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.StackTrace,
ex.GetType().ToString())
Finally
myTransMan.Dispose()
End Try Edited by: MRHooker2u on Sep 15, 2009 2:23 PM Edited by: MRHooker2u
on Sep 15, 2009 2:27 PM

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost