.NET

Reply
Valued Mentor
KerryBrown
Posts: 269
Registered: ‎11-29-2008
Message 31 of 36 (310 Views)

Re: Repeatedly Insert Block

01-30-2012 11:59 AM in reply to: vince1327

>> The first one is that in the sample code there are references to "CreateLayer", "InsertBlockReference" and "RadiansToDegrees" which don't exist. Are these placeholders for code or are these documented fuctions in the API i can't seem to find? <<

 

Have a look at Reply #28

http://www.theswamp.org/index.php?topic=37686.msg427833#msg427833

 

OR

make the .NET forum current

and do a search for the keyword you want

 

Regards

 

//-------------------------------------------------------

class keyThumper<T> : Lazy<T>;      another  Swamper

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 32 of 36 (307 Views)

Re: Repeatedly Insert Block

01-30-2012 12:56 PM in reply to: KerryBrown

When i try to put the code together from reply 28, i get the errors i posted above. When i try the compiled dll that was attached to the post and run BI_20, i get an eFilerError which halts everything. It's making me crazy that what i'm trying to do should be so simple yet is so annoyingly difficult. I've also gone through the .NET forums with no luck on this. I'll keep tacking at it and post any progress.

 

Cheers

Vince

Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 33 of 36 (292 Views)

Re: Repeatedly Insert Block

01-31-2012 05:35 AM in reply to: vince1327

Maybe try one thing at a time.

 

1. Can you manually or through the UI insert a block into a drawing?(I guess you are inserting a drawing as block vs a block from another drawing)

2. Can you code just the part to insert another blockreference with attributes?

You can also find your answers @ TheSwamp
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 34 of 36 (286 Views)

Re: Repeatedly Insert Block

01-31-2012 05:51 AM in reply to: jeff

Hey Jeff,

 

1.) I can insert the block with no problems manually by going to "Insert"-->"Block" from the AutoCAD 2012 GUI.

2.) I'm not sure what you mean here....i need it to be able to insert the block reference with the original attributes and then do that "x' times over. I've tried pre-exploding/bursting the original block in the original dwg but to no avail.

Valued Mentor
gasty1001
Posts: 478
Registered: ‎04-11-2010
Message 35 of 36 (262 Views)

Re: Repeatedly Insert Block

02-02-2012 12:35 PM in reply to: vince1327

Try this:

 

Private Sub InsertAttibuteInBlockRef(ByVal blkRef As BlockReference, ByVal attributeTag As String, ByVal attributeText As String, ByVal tr As Transaction)
        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
        For Each attId As ObjectId In btr
            Dim ent As Entity = DirectCast(tr.GetObject(attId, OpenMode.ForRead), Entity)
            If TypeOf ent Is AttributeDefinition Then
                Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
                Dim attRef As New AttributeReference()
                attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform)
                If attRef.Tag = attributeTag Then
                    attRef.TextString = attributeText
                    Dim id As ObjectId = blkRef.AttributeCollection.AppendAttribute(attRef)
                    tr.AddNewlyCreatedDBObject(attRef, True)
                    Exit For
                End If
            End If
        Next
    End Sub
 
Public Sub BlkInsert(ByVal basePath As String, ByVal blkName As String, ByVal px As Point3d, ByVal theta As Double, ByVal sx As Double, ByVal Layer As String, ByVal side As String, ByVal AttValList As ArrayList, ByVal AttTagList As ArrayList)
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database

        acCurDb = HostApplicationServices.WorkingDatabase()

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            Dim acBlkTbl As BlockTable
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

            If acBlkTbl.Has(blkName) Then
                Dim blkObjId = acBlkTbl(blkName)
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim blkRef As BlockReference = New BlockReference(px, blkObjId)

                blkRef.Rotation = theta
                blkRef.ScaleFactors = New Scale3d(sx, sx, 1)
                blkRef.Layer = Layer

                acBlkTblRec.AppendEntity(blkRef)
                acTrans.AddNewlyCreatedDBObject(blkRef, True)


                Dim dPropsCollection As DynamicBlockReferencePropertyCollection
                dPropsCollection = blkRef.DynamicBlockReferencePropertyCollection

                For Each dprop As DynamicBlockReferenceProperty In dPropsCollection
                    If dprop.PropertyName = "Visibility1" Then
                        dprop.Value = side
                    End If
                Next

                Try
                    Dim i As Integer = 0
                    For Each tag As String In AttTagList
                        InsertAttibuteInBlockRef(blkRef, tag, AttValList(i).ToString, acTrans)
                        i = i + 1
                    Next
                    acBlkTblRec.UpdateAnonymousBlocks()
                    blkRef.RecordGraphicsModified(True)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Else
                Dim blkFile As String = basePath + "\\" + blkName + ".dwg"
                Dim blkObjId As ObjectId
                Dim dbDwg As New Database(False, True)
                dbDwg.ReadDwgFile(blkFile, IO.FileShare.Read, True, "")
                blkObjId = acCurDb.Insert(blkName, dbDwg, True)
                dbDwg.Dispose()
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim blkRef As BlockReference = New BlockReference(px, blkObjId)

                blkRef.Rotation = theta
                blkRef.ScaleFactors = New Scale3d(sx, sx, 1)
                blkRef.Layer = Layer

                acBlkTblRec.AppendEntity(blkRef)
                acTrans.AddNewlyCreatedDBObject(blkRef, True)


                Dim dPropsCollection As DynamicBlockReferencePropertyCollection
                dPropsCollection = blkRef.DynamicBlockReferencePropertyCollection

                For Each dprop As DynamicBlockReferenceProperty In dPropsCollection
                    If dprop.PropertyName = "Visibility1" Then
                        dprop.Value = side
                    End If
                Next
                Dim i As Integer = 0
                For Each tag As String In AttTagList
                    InsertAttibuteInBlockRef(blkRef, tag, AttValList(i).ToString, acTrans)
                    i = i + 1
                Next
                acBlkTblRec.UpdateAnonymousBlocks()
                blkRef.RecordGraphicsModified(True)
            End If
            acTrans.Commit()
        End Using
    End Sub
<CommandMethod("MBLKTEST")> _
    Public Sub Blktest()
        Dim TagList As New ArrayList
        Dim ValList As New ArrayList
        Dim side As String = "Top"

        TagList.Add("TAG1")
        TagList.Add("TAG2")
        

        ValList.Add("VAL1")
        ValList.Add("VAL2")
        

        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Dim pr As PromptPointResult = ed.GetPoint("Pick a point:")

        If pr.Status = PromptStatus.OK Then
            Dim i As Integer = 0
            Dim n As Integer = 10
            Dim basePath As String = "C:\\MyBasePath"
            Dim px As Point3d = pr.Value

            For i = 0 To n
                AlInsert(basePath, "MyBlockName", px, 0, 1, "0", side, ValList, TagList)
                px = Polar(px, 3500, 0)
            Next

        End If


    End Sub
Private Function Polar(ByVal bpoint As Point3d, ByVal r As Double, ByVal theta As Double) As Point3d
        Dim px As New Point3d(bpoint.X + r * Math.Cos(theta), bpoint.Y + r * Math.Sin(theta), bpoint.Z)
        Return px
    End Function

 

You will need a dynamic block with 2 visibility states (the "side" parameter) and 2 attributes , or you can just get rid off that part of the code, and use a simple block with attributes.

 

Gaston Nunez

 

 

 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 36 of 36 (235 Views)

Re: Repeatedly Insert Block

02-07-2012 07:24 AM in reply to: gasty1001

Thanks gasty1001,

 

I actually got it running with help from jeff, sorry for the delayed response. Thanks for the help though!

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.