.NET

Reply
Contributor
jamkhp
Posts: 24
Registered: ‎05-19-2011
Message 1 of 5 (264 Views)

Insert Block Not Working

264 Views, 4 Replies
01-17-2012 02:53 AM

Hi all,

Here is my code to insert the Block in the Currently opened Drawing, it used to work well, when I was using Express version of VB, now i am using VB studio professional, and I am facing this error  !dbinsert834@ eNotdatabase,

 i get the error in line in red

please help me.

 

 

Public Sub InsertDrawing(ByVal dwgName As String, ByVal insPt As AcGe1.Point3d, ByVal s3d As AcGe1.Scale3d, ByVal rot As Double)
        Dim blkName As String = Path.GetFileNameWithoutExtension(dwgName)
        Using db As Database = AcDb1.HostApplicationServices.WorkingDatabase()
            Using tr As Transaction = db.TransactionManager.StartTransaction
                Try
                    Dim id As ObjectId = Nothing
                    Using bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead, True)
                        If Not bt.Has(blkName) Then
                            Using sourceDb As Database = New Autodesk.AutoCAD.DatabaseServices.Database(False, True)
                                sourceDb.ReadDwgFile(dwgName, FileOpenMode.OpenForReadAndAllShare, True, "")
                                id = db.Insert(dwgName, sourceDb, False)
                                Dim blk As BlockTableRecord = tr.GetObject(id, OpenMode.ForWrite, False, True)
                                blk.Name = blkName
                            End Using
                        Else
                            id = bt.Item(blkName)
                        End If
                    End Using
                    Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, True)
                    Dim bref As BlockReference = New BlockReference(insPt, id)
                    bref.ScaleFactors = s3d
                    bref.Rotation = rot
                    Dim ObjColl As DBObjectCollection = New DBObjectCollection()
                    bref.Explode(ObjColl)
                    For Each acEnt As Entity In ObjColl
                        btr.AppendEntity(acEnt)
                        tr.AddNewlyCreatedDBObject(acEnt, True)
                    Next

                    PurgeBlockMethod(blkName)
                    bref.Dispose()
                    btr.Dispose()
                    tr.Commit()
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    MessageBox.Show(ex.StackTrace)
                Finally
                    'PlineExplode()
                End Try
            End Using
        End Using
    End Sub

Valued Mentor
gasty1001
Posts: 450
Registered: ‎04-11-2010
Message 2 of 5 (257 Views)

Re: Insert Block Not Working

01-17-2012 06:36 AM in reply to: jamkhp

Hi,

 

My guess is that the blkname it's not a valid block name, because it has "\" in the name. try getting just the blockname from dwgname without the file path in it.

 

Gaston Nunez

Contributor
jamkhp
Posts: 24
Registered: ‎05-19-2011
Message 3 of 5 (235 Views)

Re: Insert Block Not Working

01-18-2012 10:33 PM in reply to: jamkhp

Everything looks fine.,  but still I cant figure this out,

please help me

 

ADN Support Specialist
augusto.goncalves
Posts: 312
Registered: ‎04-30-2009
Message 4 of 5 (222 Views)

Re: Insert Block Not Working

01-19-2012 05:59 AM in reply to: jamkhp

This should not change because the VB version, maybe between AutoCAD versions or the file permission.

 

Can you quick try the last parameter as TRUE? Use as FALSE is not a easy way, check out the help file.

Regards,



Augusto Goncalves
Autodesk Developer Network
Active Contributor
cincir
Posts: 32
Registered: ‎08-12-2011
Message 5 of 5 (209 Views)

Re: Insert Block Not Working

01-19-2012 02:58 PM in reply to: jamkhp

Dim id As ObjectId = Nothing is it true in VB.NET. because in C# ObjectId is a struct thus a ValueType descendant so must be initialized like

ObjectId id = ObjectId.Null;

 i am just asking because i am not very good at VB.NET.

 

and did you try the overload of ReadDwgFile like this

dbSource.ReadDwgFile(szFileName,FileShare.Read,true,string.Empty);

 


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