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

Trying to Insert Block - Error on ReadDWG

9 REPLIES 9
Reply
Message 1 of 10
acedmond
831 Views, 9 Replies

Trying to Insert Block - Error on ReadDWG

Hello,
I'm trying to insert a dynamic block w/attributes in to a drawing where a reference doesn't already exist. I have got some code off of here (Thanks who ever wrote it.)

Here is what I'm running:

Public Sub InsertBlock(ByVal ActiveDoc As Document, ByVal blockname As String)
Dim CompleteFilePath As String
CompleteFilePath = HostApplicationServices.Current.FindFile(blockname & ".dwg", ActiveDoc.Database, FindFileHint.Default)
'Using
Dim tempdb As Database = New Database(False, False)
Try
tempdb.ReadDwgFile(CompleteFilePath, System.IO.FileShare.ReadWrite, True, Nothing)
'Using
Dim t As Transaction = ActiveDoc.TransactionManager.StartTransaction
Try
Dim idBTR As ObjectId = ThisDrawing.Database.Insert(blockname, tempdb, False)
Dim bt As BlockTable = CType(t.GetObject(ActiveDoc.Database.BlockTableId, DatabaseServices.OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), DatabaseServices.OpenMode.ForWrite), BlockTableRecord)
Dim origin As acad.Geometry.Point3d = New acad.Geometry.Point3d(0, 0, 0)
'Using
Dim bref As BlockReference = New BlockReference(origin, idBTR)
Try
btr.AppendEntity(bref)
t.TransactionManager.AddNewlyCreatedDBObject(bref, True)
Finally
CType(bref, IDisposable).Dispose()
End Try
t.Commit()
t.Dispose()
Finally
CType(t, IDisposable).Dispose()
End Try
Finally
CType(tempdb, IDisposable).Dispose()
End Try
End Sub

There error always happens on the db.readdwg sub.

I get Error Number: -2147467261
Error Description: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

The block I'm trying to insert was a block that had been wblocked, then had dynamic properties added. I can also open the block with or without blockeditor with no errors, and I'm able to insert the block with no errors either.

Thanks,
Adam Edmonds
9 REPLIES 9
Message 2 of 10
acedmond
in reply to: acedmond

Ok I think I have narrowed down the issue. I kept getting the error on :
tempdb.ReadDwgFile(CompleteFilePath, System.IO.FileShare.ReadWrite, True, Nothing

Well, I replaced that line with:
Dim BlockDrawing As acad.ApplicationServices.Document = Application.DocumentManager.Open(CompleteFilePath, False)

But, Since this block is a dynamic block, It prompted me to open in Block Editor. If I say no the routine continues with no issues. My question is, Does database.ReadDWGFile work for Dynamic Blocks, or only for regular blocks?

Thanks,
Adam Edmonds
Message 3 of 10
smcclure
in reply to: acedmond

Here is the code I used to insert an entity.

- Scott

[code]
Public Shared Sub InsertSymbol(ByVal doc As Document, ByVal dwgPath As String, ByVal insertPoint As Point3d, Optional ByRef attribValues As StringDictionary = Nothing, Optional ByVal prompt As Boolean = False)
If Not File.Exists(dwgPath) Then Throw New FileNotFoundException("Could not find symbol file file to insert.", dwgPath)
If doc Is Nothing Then Throw New ArgumentNullException("The document cannot be null.", "doc")

Using t As Transaction = doc.TransactionManager.StartTransaction(), db As Database = New Database(False, False)
'read drawing
db.ReadDwgFile(dwgPath, FileShare.Read, True, Nothing)

'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert("Watermark", db, True)
'create a ref to the block

Using bt As BlockTable = t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), _
btr As BlockTableRecord = t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), _
bref As BlockReference = New BlockReference(insertPoint, idBTR)

btr.AppendEntity(bref)
doc.TransactionManager.AddNewlyCreatedDBObject(bref, True)

Using btAttRec As BlockTableRecord = t.GetObject(bref.BlockTableRecord, OpenMode.ForRead)
For Each idEnt As ObjectId In btAttRec
Dim ent As Entity = t.GetObject(idEnt, OpenMode.ForRead)
If TypeOf (ent) Is AttributeDefinition Then
'MsgBox("test2")
Dim attDef As AttributeDefinition = ent
Dim attRef As AttributeReference = New AttributeReference()
With attRef
'.SetAttributeFromBlock(attDef, bref.BlockTransform)
.HorizontalMode = attDef.HorizontalMode
If attDef.VerticalMode <> TextVerticalMode.TextBase Or attDef.HorizontalMode <> TextHorizontalMode.TextLeft Then
.AlignmentPoint = New Point3d(attDef.AlignmentPoint.X + bref.Position.X, _
attDef.AlignmentPoint.Y + bref.Position.Y, _
attDef.AlignmentPoint.Z + bref.Position.Z)
End If
.VerticalMode = attDef.VerticalMode
.WidthFactor = attDef.WidthFactor
.FieldLength = attDef.FieldLength
.Height = attDef.Height
.Rotation = attDef.Rotation
.TextStyle = attDef.TextStyle
.Position = New Point3d(attDef.Position.X + bref.Position.X, _
attDef.Position.Y + bref.Position.Y, _
attDef.Position.Z + bref.Position.Z)
.Tag = attDef.Tag
If attribValues IsNot Nothing AndAlso attribValues.ContainsKey(attDef.Tag) Then
.TextString = attribValues(attDef.Tag)
ElseIf prompt Then
Dim value As String = InputBox(attDef.Prompt, "Set Attribute Value", "")
If attribValues IsNot Nothing Then attribValues.Add(attDef.Tag, value)
.TextString = value
End If
End With
bref.AppendAttribute(attRef)
doc.TransactionManager.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End Using
End Using
t.Commit()
End Using
End Sub
[/code] Message was edited by: smcclure
Message 4 of 10
acedmond
in reply to: acedmond

Thank you Scott for you reply.

Can you tell me what namespaces you have imported for this code?

Thank you again,
Adam
Message 5 of 10
smcclure
in reply to: acedmond

Oops! Here are the imports at the top of the file (this is my utility file, though, so there may be A LOT of extras... My guess is most of them are not needed...)

[code]
Imports System.IO
Imports System.Collections.Specialized
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
[/code]
Message 6 of 10
acedmond
in reply to: acedmond

Ok after trying that routine on multiple drawings, trying to insert a number of different blocks, dynamic and not. I still get the same error.

Error Number: -2147467261
Error Description: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Anyone have any ideas?

Thanks,
Adam
Message 7 of 10
smcclure
in reply to: acedmond

Hmmm... If it is producing an error with both routines, I think that the surroujnding code may be at fault. Is it possible you can post the full file as an attachment?

- Scott
Message 8 of 10
acedmond
in reply to: acedmond

Here is code file.

I had to change the extension of the file in order for it to allow me to post. You can change back to .vb, if you wish.

Thank you,
Adam
Message 9 of 10
smcclure
in reply to: acedmond

Make sure you notify the transaction of your newly created objects (I think you are using 2006+, so that is under the Transaction.AddNewlyCreatedDBObject() or something) and maybe try using ReadDWG under the Database object instead of Open().

- Scott
Message 10 of 10
acedmond
in reply to: acedmond

Are you referring to InsertBlock or InsertSymbol routine. I Commented my InsertBlock method out and tried using your InsertSymbol. I still got the error when I tried db.ReadDWGFile. I can't get past this line in order to notify the transaction manager.


I used Open in Insert Block in order to get around the error. I'm able to insert the block by using the open method. The are two backs to using the Open method. 1). On the drawing shows up as open on the screen, and 2). If the target block is a dynamic block the user is prompted if they want to open it in the block editor.

Thank you,
Adam

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