No Block Names in DXF Files

No Block Names in DXF Files

larry.daubenspeck
Advocate Advocate
916 Views
2 Replies
Message 1 of 3

No Block Names in DXF Files

larry.daubenspeck
Advocate
Advocate

I'm using the following code (most of it borrowed from various sources) to insert blocks, and it works fine.  The resulting DWG file is fine, with the exception that the names are missing from the inserted blocks.  This is not a big problem, until using the DXFOUT.  The resulting DXF file cannot be opened.  I'm told it's due to the blocks having no names.  As you can see, the name is being passed to the function.  How can I get the names to "stick" to the resulting block reference?  I've tried to name the reference after the fact, but the Name property is Read Only.

 

Friend Function InsertBlock(ByRef AcadDbase As Database, BlkName As String, InsPt As Point3d, scale As Double, rotation As Double, ByRef ErrorMsg As String) As Boolean

 

        Using trans As Transaction = AcadDbase.TransactionManager.StartTransaction

 

            'Open the block table for read.

            Dim BlkTbl As BlockTable = trans.GetObject(AcadDbase.BlockTableId, OpenMode.ForRead, True)

 

            'Create a variable for the Object Id.

            Dim BlkRecId As ObjectId = ObjectId.Null

 

            'If block already exists...

            If BlkTbl.Has(BlkName) Then

                'Assign an ID.

                BlkRecId = BlkTbl(BlkName)

 

                'Append it into the current space.

                Using BlkRef As New BlockReference(InsPt, BlkRecId)

                    Dim AcCurSpaceBlkTblRec As BlockTableRecord = trans.GetObject(AcadDbase.CurrentSpaceId, OpenMode.ForWrite)

 

                    AcCurSpaceBlkTblRec.AppendEntity(BlkRef)

 

                    trans.AddNewlyCreatedDBObject(BlkRef, True)

                End Using

            Else

                'Create a new temporary database.

                Using newDb As New Database(False, True)

                    'Into this temp database, read in a drawing that is used as a block.

                    newDb.ReadDwgFile(BlkName, FileOpenMode.OpenForReadAndReadShare, True, "")

 

                    'Insert this dwg into the acad database, and assign a block id.

                    BlkRecId = AcadDbase.Insert(BlkName, newDb, True)

 

                    'Create a block table record.

                    Dim BlkTblRec As BlockTableRecord = trans.GetObject(BlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)

 

                    'Create a block reference.

                    Dim BlkRef As BlockReference = New BlockReference(InsPt, BlkRecId) With {

                        .ScaleFactors = New Scale3d(scale),

                        .Rotation = rotation}

 

                    'Append the record with the block ref.

                    BlkTblRec.AppendEntity(BlkRef)

 

                    'Add the new record to the database.

                    trans.AddNewlyCreatedDBObject(BlkRef, True)

 

                    'Close the temp database.

                End Using

            End If

 

            'Commit the transaction.

            trans.Commit()

 

            'Close the transaction.

        End Using

 

        Return True

 

    End Function

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Accepted solutions (1)
917 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

You did not say, but my guess is that to the BlkName argument in your InsertBlock() function your code most likely passes a value block file name (with path and .dwg extension). That is the course of the issue due to a deficiency of Database.Insert(): when inserting another database into current data as block definition, even the given block name is invalid (in your case, since you used a full file name as block name, it would contains invalid characters, like "\", "."), however, the Insert() method can still succeed to create a new BlockTableRecord and return a valid ObjectId (so, your code continue to create a BlockReference with that BlockTablerecordId. However, AutoCAD gives this BlockTableRecord a blank name (or whatever, but just not be shown in block list. That is, as you described, "No block name").

 

In your code of inserting block definition, you should use block file name with path and extension, something like:

                    

                    BlkRecId = AcadDbase.Insert(System.IO.Path.GetFileNameWithoutExtension(BlkName), newDb, True)

 

Better yet, in your Function's signature, 

 

instead of

 

Friend Function InsertBlock(ByRef AcadDbase As Database, BlkName As String, InsPt As Point3d, scale As Double, rotation As Double, ByRef ErrorMsg As String) As Boolean

 

You might want to:

 

Friend Function InsertBlock(ByRef AcadDbase As Database, FullBlkFilePath As String, InsPt As Point3d, scale As Double, rotation As Double, ByRef ErrorMsg As String) As Boolean

 

So when you call this function later, you would be clearly know what you need to pass it to is a full dwg file path/name. If you only want to pass a valid block name, the if the block name does not existing in blocktable, then you need to find corresponding block dwg file by searching all support paths:

 

Dim blkFile As String = BlockName & ".dwg"

Dim fullPath As String = SearchSupportPaths()

If String.IsNullOrEmpty(fullPath)  Then Throw New System.IO.FileNotFoundException(....)

'' The open the block file database and read the drawing into it with this full file path.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

larry.daubenspeck
Advocate
Advocate

Thanks Norman!  That was the info I needed!

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes