.NET

Reply
Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 1 of 7 (438 Views)
Accepted Solution

Insert Block - Workflow?

438 Views, 6 Replies
04-27-2012 11:13 AM

Am I along the correct workflow path here? 

I want to insert a drawing as a block into my current drawing. 

 

Before I start writing code I want to make sure I understand the steps.

 

Open Transaction

Wblock Drawing to Temp Database

Now have my New BlockTableRecord Object 

Copy BlockTableRecord to ActiveDwg.Database

Create New BlockReference

Add attributes 

Set Scale Properties

Set Insertion Point

Set Rotation 

Append BlockReference to Database

Commit Transaction

 

Am I missing any of the steps? 

Is this how I would imidate the old VBA modelspace.insertblock(myPoint, "C:\path\path\path\block.dwg",1,1,1,0)

 

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 2 of 7 (429 Views)

Re: Insert Block - Workflow?

04-27-2012 12:05 PM in reply to: VB_Autocad_guy

What about Database.Insert method?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 3 of 7 (419 Views)

Re: Insert Block - Workflow?

04-27-2012 01:05 PM in reply to: Alexander.Rivilis

Yes!

 

I think that will work. 

 

AutoCAD help says: 

This function mimics the AutoCAD INSERT command. First a new block table record is created in the database executing this function. This new block table record is given the name pointed to by blockName. Then, all the Model Space entities in the database pointed to by dataBase are copied into the new block table record.

 

Thank you. 

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 4 of 7 (417 Views)

Re: Insert Block - Workflow?

04-27-2012 01:29 PM in reply to: VB_Autocad_guy

Here's some code for people after me. 

 

Public Shared Sub InsertInstrumentBlock()
            Dim gstrc_InstrumentBubbleBlock_FilePath As String = "C:\myblock.dwg"
            Dim BlockName As String = "Field Instrument"

            Dim myDwg As Document = Application.DocumentManager.MdiActiveDocument
            Using myTrans As Transaction = myDwg.TransactionManager.StartTransaction

                '<<Get Block From Drawing>>
                'Read Block Drawing's Database
                Dim BlkDwgDB As New Database(False, False)
                BlkDwgDB.ReadDwgFile(gstrc_InstrumentBubbleBlock_FilePath, System.IO.FileShare.Read, True, "")

                'Insert Block, Get ObjectID
                Dim id As ObjectId = mydwg.Database.Insert(BlockName, BlkDwgDB, False)

                '<<Insert BlockDefinition Into Specific Place on ModelSpace/PaperSpace>>
                'Open the database for Write
                Dim myBT As BlockTable = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
                Dim myModelSpace As BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

                'Block Table Record (Block Definitions)
                Dim myBlockDef As BlockTableRecord = id.GetObject(OpenMode.ForWrite)

                Dim InsPt As New Point3d(0,0,0)
                

                'Block References (Insertions of Blocks into Drawing Space)
                Dim myBlockRef As New DatabaseServices.BlockReference(InsPt, myBT(BlockName))
                myBlockRef.ScaleFactors = New Geometry.Scale3d(1, 1, 1)

                myModelSpace.AppendEntity(myBlockRef)
                myTrans.AddNewlyCreatedDBObject(myBlockRef, True)

                'Append Attribute References to the BlockReference
                Dim myAttColl As DatabaseServices.AttributeCollection
                Dim myEntID As ObjectId
                Dim myEnt As DatabaseServices.Entity

                myAttColl = myBlockRef.AttributeCollection
                For Each myEntID In myBlockDef
                    myEnt = myEntID.GetObject(OpenMode.ForWrite)
                    If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
                        Dim myAttDef As DatabaseServices.AttributeDefinition = _
                       CType(myEnt, AttributeDefinition)
                        Dim myAttRef As New DatabaseServices.AttributeReference
                        myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
                        myAttColl.AppendAttribute(myAttRef)
                        myTrans.AddNewlyCreatedDBObject(myAttRef, True)
                    End If
                Next

                'Commit the Transaction
                myTrans.Commit()
            End Using




        End Sub

 

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 5 of 7 (411 Views)

Re: Insert Block - Workflow?

04-27-2012 01:53 PM in reply to: VB_Autocad_guy

You forgot to skip the constant attributes


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 6 of 7 (406 Views)

Re: Insert Block - Workflow?

04-27-2012 02:00 PM in reply to: Alexander.Rivilis

Constant Attributes? 

 

What's that? Can you tell me more please. 

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 7 of 7 (402 Views)

Re: Insert Block - Workflow?

04-27-2012 02:05 PM in reply to: VB_Autocad_guy

VB_Autocad_guy wrote:

Constant Attributes? 

 

What's that? Can you tell me more please. 


If AttributeDefinition.Constant = True then you have not add this attribute to attribute collection.

More detailes about constant attribute definition: http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%202010%20User%20Documentation/index.html?url=WS1a91938...

Also read: http://forums.autodesk.com/t5/NET/How-to-Preserve-Constat-Attributes-on-BlockReference/m-p/2634989/h...


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

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.