.NET

Reply
Contributor
kemp77
Posts: 20
Registered: ‎11-06-2008
Message 1 of 6 (723 Views)
Accepted Solution

Block insertion has no name in VB.net

723 Views, 5 Replies
01-11-2012 03:05 PM

As stated, when I use VB.NET to insert a block it has no name, thus making it behave oddly. Is there a way to define the block name properly since it apparently doesn't use the name of the file I am importing? I have tried defining the block manually and have the same result so my code must be missing something:

 

    Public Function InsertBlock(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal expl As Boolean, ByVal rotation As Double, ByVal schaal As Integer) As ObjectId

        If Not File.Exists(BlockName) Then
            MsgBox("File does not exist?")
            Return ObjectId.Null
        End If

        Dim myDB As Database
        Dim myDwg As Document
        myDwg = Application.DocumentManager.MdiActiveDocument
        Using docloc As DocumentLock = myDwg.LockDocument
            myDB = myDwg.Database
            Dim ed As Editor = myDwg.Editor
            Using tr As Transaction = myDwg.TransactionManager.StartTransaction
                Try
                    Using db As Database = New Database(False, True)
                        db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "")
                        Dim BlkId As ObjectId
                        BlkId = myDB.Insert(BlockName, db, True)
                        Dim bt As BlockTable = tr.GetObject(myDB.BlockTableId, OpenMode.ForRead, True)
                        Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
                        Dim bref As BlockReference = New BlockReference(InsPt, BlkId)
                        bref.Rotation = rotation
                        bref.ScaleFactors = New Scale3d(schaal, schaal, schaal)
                        btr.AppendEntity(bref)
                        tr.AddNewlyCreatedDBObject(bref, True)
                        If expl Then
                            bref.ExplodeToOwnerSpace()
                            bref.Erase()
                        End If
                        tr.Commit()
                    End Using
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    MsgBox(ex.ToString)
                End Try
            End Using
        End Using
    End Function

 Thanks for any tips that may be offered!

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (707 Views)

Re: Block insertion has no name in VB.net

01-11-2012 10:32 PM in reply to: kemp77

Just at the quick glance,

Exploded objects does not appends in the database automatically,

so you need to do it within the code after exploding, iow:

for each exploded obj

btr.AppendEntity(obj)

tr.AddNewlyCreatedDBObject(obj, True)

etc...

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
bobbydehero
Posts: 15
Registered: ‎01-12-2009
Message 3 of 6 (690 Views)

Re: Block insertion has no name in VB.net

01-12-2012 02:00 AM in reply to: Hallex

Hallex -

 

That's not needed when using ExplodeToOwnerSpace().

 

 

Kemp77 -

 

Looking at your code I suppose the boolean parameter 'expl' is true when calling 'InsertBlock'?

I'd suggest you do a regular 'INSERT' after you ran your command, if 'BlockName' is among the block definitions you only need to set the 'expl' parameter false in your call.

Contributor
kemp77
Posts: 20
Registered: ‎11-06-2008
Message 4 of 6 (678 Views)

Re: Block insertion has no name in VB.net

01-12-2012 06:55 AM in reply to: bobbydehero

Thanks guys.

 

This is mostly code I copied from sources online, so the expl function is never really used to explode. I typically set the value to false whenever I insert a block. 

 

Do you think elimination of this expl function entirely would help solve my problem? The result I end up with is an attributed block, but one which can not be edited due to what looks like is the fact that the block has no name when listed.

*Expert Elite*
norman.yuan
Posts: 984
Registered: ‎04-27-2009
Message 5 of 6 (664 Views)

Re: Block insertion has no name in VB.net

01-12-2012 09:07 AM in reply to: kemp77

Your problem (block inserted without name, or should I say, with a "blank name") is due to the fact that you use a fully pathed file name as block name.

 

In your code, the parameter BlockName you passed in is obviously a block file name with full path (since you use File.Exists() to verify the block file's existance), it must looks like something "C:\.....\myblock.dwg". Such a string value contains invalid characters that are not allowed to be in block name, such as ":", "\"...

 

At this line of your code:

 

BlkId=myDB.Insert(BlockName...)

 

you passed a fully pathed file name as a block name for the inserted DB (block). Somehow, instead of raising an exception due to invalid character, the API method still inserts the block and give it a blank name.

 

If you manually create a block in Acad, and try to give it name like "C:\Folder\TheBlk.dwg", Acad would prompt you the invalid characters in block name.

 

So, in your case, you need to supply a valid block name like this:

 

BlkId=myDB,Insert(IO.Path.GetFileNameWithoutExtension(BlockName),...)

 

That is, it is your responsibility to make sure the block name you supplied in the Database.Insert() method contains only valid characters, or Acad would make a blank-named block.

 

BTW, sometime, before inserting a block froom a block drawing file, it may be better to check if a block with the same name has already existed in the targeting database. inserting block drawing file and name it as the same name of an existing block will redefine the existing block definition, which may or may not be the intented thing to do.

Contributor
kemp77
Posts: 20
Registered: ‎11-06-2008
Message 6 of 6 (661 Views)

Re: Block insertion has no name in VB.net

01-12-2012 10:16 AM in reply to: norman.yuan

Thank you for your reccomendations Norman, you have described my situation exactly. Now off to fix it!

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.