Jump to content

Can't remove 'user interaction' part of my code [seeking help]


Recommended Posts

Posted

Hi all,

 

I have got this code by KEAN WALMSLEY.

 

His code will ask user to select a block and then list all of selected block's attributes. However, I need to tweak his code a little bit and instead of user interaction, the block has to be selected by its name and through my code.

 

Here is his code (with slight changes to suit my purpose):

 

Private Sub ListAttributes()
           Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
           Dim db As Database = HostApplicationServices.WorkingDatabase
           Dim tr As Transaction = db.TransactionManager.StartTransaction()
           Try
               Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "INSERT")}
               Dim filter As New SelectionFilter(filList)
               Dim opts As New PromptSelectionOptions()
               opts.MessageForAdding = "Select block references: "
               Dim res As PromptSelectionResult = ed.GetSelection(opts, filter)
               If res.Status <> PromptStatus.OK Then
                   Return
               End If
               Dim selSet As SelectionSet = res.Value
               Dim idArray As ObjectId() = selSet.GetObjectIds()
               For Each blkId As ObjectId In idArray
                   Dim blkRef As BlockReference = DirectCast(tr.GetObject(blkId, OpenMode.ForRead), BlockReference)

                   Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                   ed.WriteMessage(vbLf & "Block: " + btr.Name)
                   btr.Dispose()
                   Dim attCol As AttributeCollection = blkRef.AttributeCollection
                   For Each attId As ObjectId In attCol
                       Dim attRef As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForRead), AttributeReference)
                       Dim str As String = (vbLf & attRef.Tag + "    " & attRef.TextString)
                       ed.WriteMessage(str)
                   Next
               Next
               tr.Commit()
           Catch ex As Autodesk.AutoCAD.Runtime.Exception
               ed.WriteMessage(("Exception: " + ex.Message))
           Finally
               tr.Dispose()
           End Try
       End Sub

 

Now, I need to change this Sub to a Function as below:

 

Private Function ListAttributes (blockName As String) As Boolean

 

The goal is to list all block attributes if the 'blockName' exists in drawing and return True afterwards. Oppositely it should return me False and print nothing to Editor if blockName does not exist.

 

Can anyone help me please?

 

Cheers

Posted

Please, stop double posting

I've answered on Discussion groups

Posted

My apology. Didn't know that pro users monitor couple of CAD forums simultaneously , :roll:

Thanks anyway. :oops:

Posted

No problem, it's just a common rule for Acad forums

Cheers :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...