Results 1 to 4 of 4

Thread: Block, attributes and Selection sets

  1. #1
    Member
    Join Date
    2012-02
    Posts
    4

    Default Block, attributes and Selection sets

    How can i select (on screen) blocks and get their attribute values with VB.NET?
    Thanks,
    Jean

  2. #2
    100 Club
    Join Date
    2007-08
    Posts
    172

    Default Re: Block, attributes and Selection sets

    Hi,

    Here's a basic C# sample (you cant convert it to VB here).
    Code:
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
    using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
    
    namespace GetAttributesSample
    {
        public class Commands
        {
            [CommandMethod("Test")]
            public void Test()
            {
                Document doc = AcAp.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                // Build a selection filter
                TypedValue[] filter = { new TypedValue(0, "INSERT"), new TypedValue(66, 1) };
                // Prompt the user for a selection
                PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
                if (psr.Status != PromptStatus.OK) return;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // Iterate the selection set
                    foreach (ObjectId brId in psr.Value.GetObjectIds())
                    {
                        // Open the block reference
                        BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
                        ed.WriteMessage("\n\nBlock: {0}", br.Name);
                        // Iterate the attribute collection
                        foreach (ObjectId attId in br.AttributeCollection)
                        {
                            // Open the attribute reference
                            AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
                            ed.WriteMessage("\n\t{0} = {1} ", att.Tag, att.TextString);
                        }
                    }
                    tr.Commit();
                }
            }
        }
    }
    Last edited by 'gile'; 2012-03-03 at 05:49 PM.

  3. #3
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,215

    Default Re: Block, attributes and Selection sets

    Here is my very basic example,
    just change a block name within the code

    Code:
           <CommandMethod("DisplayAttributes", "diat", CommandFlags.Modal + CommandFlags.UsePickSet)> _
            Public Sub GetAttributesTest()
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
                Dim attref As AttributeReference = Nothing
                Dim bref As BlockReference = Nothing
                Dim blkname As String = "MYBLOCK" ''   <--- change block name
                Dim attTag As String = String.Empty
    
                Try
    
                    Dim tr As Transaction = db.TransactionManager.StartTransaction()
                    Using tr
    
                        Dim selres As PromptSelectionResult = Nothing
                        Dim tvs() As TypedValue = New TypedValue() {New TypedValue(0, "insert"), _
                                                                     New TypedValue(66, 1), _
                                                                     New TypedValue(2, "`U*," + blkname)}
                        Dim filt As SelectionFilter = New SelectionFilter(tvs)
    
                        selres = ed.GetSelection(filt)
    
                        If selres.Status <> PromptStatus.OK Then
                            Return
                        End If
                        Dim selset As SelectionSet = selres.Value
                        Dim bname As String = String.Empty
                        For Each selobj As SelectedObject In selset
                            Dim sent As Entity = DirectCast(tr.GetObject(selobj.ObjectId, OpenMode.ForRead), Entity)
                            If TypeOf sent Is BlockReference Then
                                Dim obr As BlockReference = DirectCast(sent, BlockReference)
                                If obr.IsDynamicBlock Then
                                    Dim dynId As ObjectId = obr.DynamicBlockTableRecord
                                    If dynId.IsNull Or dynId.IsErased Then
                                        Continue For
                                    End If
                                    Dim dynbtr As BlockTableRecord = TryCast(tr.GetObject(dynId, OpenMode.ForRead), BlockTableRecord)
                                    If dynbtr IsNot Nothing Then
                                        bname = dynbtr.Name
                                    End If
                                Else
                                    bname = obr.Name
                                End If
                                If bname = blkname Then
    
    
                                    For Each attId As ObjectId In obr.AttributeCollection
    
                                        attref = DirectCast(tr.GetObject(attId, OpenMode.ForRead, False), AttributeReference)
    
                                        ed.WriteMessage(vbLf + "Block Name:   {0}" + vbLf + "Tag:   {1}" + vbLf + "Value:   {2}", bname, attref.Tag, attref.TextString)
                                      
                                    Next
    
                                End If
                            End If
    
                        Next
                        tr.Commit()
                    End Using
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    MsgBox(ex.ToString)
                End Try
            End Sub
    ~'J'~
    "The whole problem with the world is that fools and fanatics are always
    so certain of themselves, and wiser people so full of doubts."
    Bertrand Russell

  4. #4
    Member
    Join Date
    2012-02
    Posts
    4

    Default Re: Block, attributes and Selection sets

    Thank you very much,
    that's exactly what I needed,
    Jean

Similar Threads

  1. Selection Sets and Blocks with Attributes
    By Coolmo in forum Dot Net API
    Replies: 8
    Last Post: 2009-10-09, 04:01 PM
  2. Selection Sets
    By beamer in forum AutoCAD General
    Replies: 7
    Last Post: 2008-08-11, 12:04 PM
  3. ...Selection sets...
    By davidmatyas in forum VBA/COM Interop
    Replies: 15
    Last Post: 2008-04-02, 07:14 PM
  4. Other (selection sets)
    By gketter in forum AutoCAD LT - Wish List
    Replies: 5
    Last Post: 2005-10-26, 07:11 AM
  5. Help relating to selection and selection sets
    By csgohjmj in forum AutoLISP
    Replies: 3
    Last Post: 2004-09-17, 03:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •