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

What's the best way to "insert" a Block to a new DWG file at exact location?

2 REPLIES 2
Reply
Message 1 of 3
lukehuang
2351 Views, 2 Replies

What's the best way to "insert" a Block to a new DWG file at exact location?

It's the part of project: I need read some named Blocks from a sample DWG and copy (clone) them (one at a time) to a new drawing.

There are 2 possible way to do it:

1. Database.Insert  - it does allow you pass in location point but it takes whole DWG file not blocks inside the whole DWG;

2. Database.WblockCloneObjects - it takes blocks as parameter but it doesn't pass in with location information you want them there

 

Anyone have similar implementation can share your code/solution with me? Thanks

2 REPLIES 2
Message 2 of 3
StephenPreston
in reply to: lukehuang

Hi Luke,

 

>>1. Database.Insert  - it does allow you pass in location point but it takes whole DWG file not blocks inside the whole DWG;

 

Database.Insert has an override that allows you to specify a specific block to copy from the source data base:
 
>>Database.Insert Method (string, string, Database, [MarshalAs(UnmanagedType.U1)] bool)
 

>>This function creates a new block table record in the database executing this function. This new block table record is given the name pointed to by destinationBlockName. Then, each entity in the block table record whose name is sourceBlockName and which resides in the database pointed to by dataBase is copied into the new block table record. 

 

I'd recommend you use Insert for copying blocks from one database to another unless there is a very good reason why its unsuitable.

 

Once you've instered the BlockTableRecord, you have to add a BlockReference to the drawing to display it. Here's a copy of a DevNote from the ADN website showing how to insert a BTR+BR in the drawing (albeit using a different version of Database.Insert, and omitting code to add AttributeReferences):

 

How to insert external dwg file as a block in current drawing using VB.NET?

 

Published date: 2009-06-04
ID: TS87078

Applies to:
AutoCAD® 2010
AutoCAD® 2009
AutoCAD® 2008
AutoCAD® 2007
AutoCAD® 2006

Issue

How to insert external dwg file as a block in current drawing using VB.NET?

Solution
If you want insert the external dwg file into current database, first you can use dbDwg.readDwgFile(...) and use id = db.Insert("test", dbDwg, False) ,test is the block name, then you can use  Dim blkRef As New BlockReference(ptInsert, id) to create a new block reference and append it to the currrent database. The following is sample code snippet.
 
 
<CommandMethod("InsertBlk")> _
   Public Sub InsertBlk()
        Dim db As Database
        db = HostApplicationServices.WorkingDatabase()
        Dim ed As Editor
        ed = Application.DocumentManager.MdiActiveDocument.Editor
        Dim trans As Transaction
        trans = db.TransactionManager.StartTransaction
        Try
            Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord
            btr = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            Dim id As ObjectId
            If bt.Has("test") Then
                Dim btrSrc As BlockTableRecord
                btrsrc=trans.GetObject(bt.Item("test"), OpenMode.ForRead)
                id = btrSrc.Id
            Else
                Dim dbDwg As New Database(False, True)
                dbDwg.ReadDwgFile("C:\\TEST.dwg", IO.FileShare.Read, True, "")
                id = db.Insert("test", dbDwg, True)
                dbDwg.Dispose()
            End If
            Dim opPt As New PromptPointOptions("pick the insert point of the blk")
            Dim resPt As PromptPointResult
            resPt = ed.GetPoint(opPt)
            If resPt.Status <> PromptStatus.OK Then
                MsgBox("fail to get the insert point")
                Exit Sub
            End If
            Dim ptInsert As Point3d
            ptInsert = resPt.Value
            Dim blkRef As New BlockReference(ptInsert, id)
            btr.AppendEntity(blkRef)
            trans.AddNewlyCreatedDBObject(blkRef, True)
            trans.Commit()
        Catch
            MsgBox("fail to read dwg file and insert in current database ")
        Finally
            trans.Dispose()
        End Try
    End Sub
Cheers,

Stephen Preston
Autodesk Developer Network
Message 3 of 3
lukehuang
in reply to: StephenPreston

Thanks for the answer.

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