.NET

Reply
Active Member
lukehuang
Posts: 9
Registered: ‎07-14-2011
Message 1 of 3 (981 Views)

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

981 Views, 2 Replies
07-18-2011 04:06 PM

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

Board Manager
StephenPreston
Posts: 359
Registered: ‎05-22-2006
Message 2 of 3 (955 Views)

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

07-19-2011 09:49 AM 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
Active Member
lukehuang
Posts: 9
Registered: ‎07-14-2011
Message 3 of 3 (934 Views)

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

07-20-2011 09:59 AM in reply to: StephenPreston

Thanks for the answer.

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