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

Insert Block - Workflow?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
VB_Autocad_guy
898 Views, 6 Replies

Insert Block - Workflow?

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)

 

6 REPLIES 6
Message 2 of 7

What about Database.Insert method?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 7

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. 

Message 4 of 7

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

 

Message 5 of 7

You forgot to skip the constant attributes

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 6 of 7

Constant Attributes? 

 

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

Message 7 of 7


@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...

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

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