<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Inserting block reference which stored in the same drawing / db in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3486682#M55221</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am trying to write a function to insert a block reference of a block definition which is already stored in the same drawing.&lt;/P&gt;&lt;P&gt;All threads are leads me to the way of inserting another drawing to current drawing with use of database.readdwgfile &amp;amp; database.insert.&amp;nbsp;&lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://forums.autodesk.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/P&gt;&lt;P&gt;Kindly share how to use those methods for a block definition stored in the same drawing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Jun 2012 00:30:49 GMT</pubDate>
    <dc:creator>ProfWolfMan</dc:creator>
    <dc:date>2012-06-06T00:30:49Z</dc:date>
    <item>
      <title>Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3486682#M55221</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am trying to write a function to insert a block reference of a block definition which is already stored in the same drawing.&lt;/P&gt;&lt;P&gt;All threads are leads me to the way of inserting another drawing to current drawing with use of database.readdwgfile &amp;amp; database.insert.&amp;nbsp;&lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://forums.autodesk.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/P&gt;&lt;P&gt;Kindly share how to use those methods for a block definition stored in the same drawing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2012 00:30:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3486682#M55221</guid>
      <dc:creator>ProfWolfMan</dc:creator>
      <dc:date>2012-06-06T00:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3486924#M55222</link>
      <description>&lt;PRE&gt;Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.LayerManager.LayerFilter
Imports Autodesk.AutoCAD.EditorInput
Imports System.IO
Imports Autodesk.AutoCAD.Geometry

Public Class Class1

    ' Define command 'Asdkcmd1'
    &amp;lt;CommandMethod("ImpBloc")&amp;gt; _
    Public Sub ImpBloc()
        'implanter un bloc Bp
        'imp the bloc
        Dim DB As Database = HostApplicationServices.WorkingDatabase
        Dim ed As Editor
        ed = Application.DocumentManager.MdiActiveDocument.Editor

        Dim trans As Transaction
        trans = DB.TransactionManager.StartTransaction

        'récupération de la table des block
        'rep the block def.
        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("bp") Then
            'found in the  base
            Dim btrSrc As BlockTableRecord
            btrsrc=trans.GetObject(bt.Item("BP"), OpenMode.ForRead)
            id = btrSrc.Id
        Else
            'not found --&amp;gt; load the dwg

            Dim Dbdwg As New Database(False, True)
            Dbdwg.ReadDwgFile("d:\autocad\bloc.dwg\bp.dwg", FileOpenMode.OpenTryForReadShare, True, "")
            id = DB.Insert("bp", Dbdwg, False)
            Dbdwg.Dispose()
        End If
        '

        'insertion :
        Dim opPt As New PromptPointOptions("Sélect a point")
        Dim resPt As PromptPointResult
        resPt = ed.GetPoint(opPt)

        Dim ptInsert As Point3d = Nothing
        Dim blkRef As BlockReference

        If resPt.Status = PromptStatus.OK Then
            ptInsert = resPt.Value

            blkRef = New BlockReference(ptInsert, id)

            btr.AppendEntity(blkRef)
            trans.AddNewlyCreatedDBObject(blkRef, True)

            ' modify the attribute of the block reference
            Dim btAttRec As BlockTableRecord
            btAttRec = trans.GetObject(id, OpenMode.ForRead)
            Dim idAtt As ObjectId
            For Each idAtt In btAttRec
                Dim ent As Entity
                ent = trans.GetObject(idAtt, OpenMode.ForRead)
                If TypeOf ent Is AttributeDefinition Then
                    Dim attDef As AttributeDefinition
                    attDef = CType(ent, AttributeDefinition)
                    Dim attRef As New AttributeReference()
                    attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform)
                    'Dim ptBase As New Point3d(blkRef.Position.X + attDef.Position.X, blkRef.Position.Y + attDef.Position.Y, blkRef.Position.Z + attDef.Position.Z)
                    'attRef.Position = ptBase
                    'attRef.Rotation = attDef.Rotation
                    'attRef.Tag = attDef.Tag
                    attRef.TextString = "input your data here"
                    'attRef.Height = attDef.Height
                    'attRef.FieldLength = attDef.FieldLength
                    Dim idTmp As ObjectId
                    idTmp = blkRef.AttributeCollection.AppendAttribute(attRef)
                    trans.AddNewlyCreatedDBObject(attRef, True)

                End If
            Next
            trans.Commit()

        Else
            MsgBox("Point introuvable !")
            trans.Abort()
        End If


       
    End Sub
end class&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;here a example for insert a block and fill the attribut&lt;/P&gt;&lt;P&gt;if the block is know in the base --&amp;gt; insert but if not the vb.net load a file bp.dwg&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2012 05:33:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3486924#M55222</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-06-06T05:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3488104#M55223</link>
      <description>&lt;P&gt;Try this code for the same drawing&lt;/P&gt;&lt;PRE&gt;    Public Sub ApplyAttributes(db As Database, tr As Transaction, bref As BlockReference)

        Dim btrec As BlockTableRecord = TryCast(tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)

        If btrec.HasAttributeDefinitions Then

            Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection

            For Each subid As ObjectId In btrec

                Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)

                Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)

                If attDef IsNot Nothing Then

                    Dim attRef As New AttributeReference()

                    attRef.SetDatabaseDefaults()

                    attRef.SetAttributeFromBlock(attDef, bref.BlockTransform)

                    attRef.Position = attDef.Position.TransformBy(bref.BlockTransform)

                    attRef.Tag = attDef.Tag

                    attRef.TextString = attDef.TextString

                    attRef.AdjustAlignment(db)

                    atcoll.AppendAttribute(attRef)

                    tr.AddNewlyCreatedDBObject(attRef, True)

                End If

            Next
        End If
    End Sub

    Public Sub TestInsert()
        Dim blkname As String = "MYBLOCK" ''&amp;lt;-- change to your needs
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Try
            Using docloc As DocumentLock = doc.LockDocument

                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

                    If Not bt.Has(blkname) Then
                        MsgBox("Block does not exists")
                        Return
                    End If

                    Dim pto As PromptPointOptions = New PromptPointOptions(vbLf + "Pick a block insertion point: ")
                    Dim ptres As PromptPointResult = ed.GetPoint(pto)
                    Dim ipt As Point3d

                    If ptres.Status &amp;lt;&amp;gt; PromptStatus.Cancel Then
                        ipt = ptres.Value
                    End If

                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, False), BlockTableRecord)
                    Dim blk As BlockTableRecord = DirectCast(tr.GetObject(bt(blkname), OpenMode.ForRead, False), BlockTableRecord)
                    Dim bref As New BlockReference(ipt, blk.ObjectId)
                    bref.BlockUnit = UnitsValue.Millimeters''&amp;lt;-- change to your needs
                    bref.Rotation = 0
                    bref.ScaleFactors = New Scale3d(1.0) ''&amp;lt;-- change to your needs
                    btr.AppendEntity(bref)
                    tr.AddNewlyCreatedDBObject(bref, True)
                    ApplyAttributes(db, tr, bref)
                    ed.Regen()
                    tr.Commit()
                End Using
            End Using
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800000" face="arial,helvetica,sans-serif"&gt;~'J'~&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2012 18:38:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3488104#M55223</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-06-06T18:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3488784#M55224</link>
      <description>&lt;P&gt;Both answers my question. Thank you very much for your valuable time spend on this.&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now slowly i am learning dotnet .While i need one clarification on Hallex code.&lt;/P&gt;&lt;P&gt;Why you use "Directcast" on below lines?. If the question make any sense kindly answer.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, False), BlockTableRecord)
                    Dim blk As BlockTableRecord = DirectCast(tr.GetObject(bt(blkname), OpenMode.ForRead, False), BlockTableRecord)&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jun 2012 01:13:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3488784#M55224</guid>
      <dc:creator>ProfWolfMan</dc:creator>
      <dc:date>2012-06-07T01:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489082#M55225</link>
      <description>&lt;P&gt;Here is an excerpt from MSDN docs:&lt;A target="_blank" href="#"&gt;Remarks&lt;/A&gt;&lt;/P&gt;&lt;DIV class="section expand"&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;DirectCast&lt;/SPAN&gt;&lt;/SPAN&gt; does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than &lt;SPAN&gt;&lt;SPAN&gt;CType&lt;/SPAN&gt;&lt;/SPAN&gt; when converting to and from data type &lt;SPAN&gt;&lt;SPAN&gt;Object&lt;/SPAN&gt;&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;You use the &lt;SPAN&gt;&lt;SPAN&gt;DirectCast&lt;/SPAN&gt;&lt;/SPAN&gt; keyword similar to the way you use the &lt;SPAN&gt;&lt;A target="_blank" href="http://msdn.microsoft.com/en-us/library/4x2877xb"&gt;CType Function (Visual Basic)&lt;/A&gt;&lt;/SPAN&gt; and the &lt;SPAN&gt;&lt;A target="_blank" href="http://msdn.microsoft.com/en-us/library/zyy863x8"&gt;TryCast Operator (Visual Basic)&lt;/A&gt;&lt;/SPAN&gt; keyword. You supply an expression as the first argument and a type to convert it to as the second argument. &lt;SPAN&gt;&lt;SPAN&gt;DirectCast&lt;/SPAN&gt;&lt;/SPAN&gt; requires an inheritance or implementation relationship between the data types of the two arguments. This means that one type must inherit from or implement the other.&lt;/P&gt;Errors and Failures&lt;DIV class="subsection"&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;DirectCast&lt;/SPAN&gt;&lt;/SPAN&gt; generates a compiler error if it detects that no inheritance or implementation relationship exists. But the lack of a compiler error does not guarantee a successful conversion. If the desired conversion is narrowing, it could fail at run time. If this happens, the runtime throws an &lt;SPAN&gt;&lt;A target="_blank" href="http://msdn.microsoft.com/en-us/library/system.invalidcastexception"&gt;InvalidCastException&lt;/A&gt;&lt;/SPAN&gt; error.&lt;/P&gt;&lt;P&gt;Hope it make a sense,&lt;/P&gt;&lt;P&gt;Cheers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="arial,helvetica,sans-serif"&gt;~'J'~&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 07 Jun 2012 08:33:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489082#M55225</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-06-07T08:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489126#M55226</link>
      <description>&lt;P&gt;Today i learned some more things.&lt;/P&gt;&lt;P&gt;Thank you very much for your reply.&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2012 09:04:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489126#M55226</guid>
      <dc:creator>ProfWolfMan</dc:creator>
      <dc:date>2012-06-07T09:04:36Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting block reference which stored in the same drawing / db</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489132#M55227</link>
      <description>&lt;P&gt;Glad I could help,&lt;/P&gt;&lt;P&gt;Happy coding &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;~'J'~&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2012 09:13:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-block-reference-which-stored-in-the-same-drawing-db/m-p/3489132#M55227</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-06-07T09:13:13Z</dc:date>
    </item>
  </channel>
</rss>

