Author Topic: Block from Layout  (Read 1721 times)

0 Members and 1 Guest are viewing this topic.

djee

  • Newt
  • Posts: 49
Block from Layout
« on: June 30, 2016, 11:42:22 AM »
Sorry for my basic question in advance... I would like the user to pick the drawing titleblock & display all the attributes in my WPF windows (maybe in a Listbox...???). I can't get to the attributecollection... I'm still very new to XAML & WPF, should I bind anything to my listbox? At this point i'm using a modal window... Any suggestion would be greatly appreciated...
Code: [Select]
  Private Sub SelectTargetedBlock_Click(sender As Object, e As Windows.RoutedEventArgs) Handles SelectTargetedBlock.Click
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        'this - is the modal dialog box.
        Using UI As EditorUserInteraction = ed.StartUserInteraction(Me)
            Dim entResult As PromptEntityResult = ed.GetEntity(vbLf & "Select entity")
            If entResult.Status <> PromptStatus.OK Then
                Return
            End If

            Using Tx As Transaction = db.TransactionManager.StartTransaction()
                ' Dim obj As DBObject = Tx.GetObject(entResult.ObjectId, OpenMode.ForRead)
                Dim BlkRef As BlockReference = Tx.GetObject(entResult.ObjectId, OpenMode.ForRead)
                Dim attCol As AttributeCollection = BlkRef.AttributeCollection
                For Each attId As ObjectId In attCol
                    Dim attRef As AttributeReference = TryCast(Tx.GetObject(attId, OpenMode.ForRead), AttributeReference)
                Next
                Tx.Commit()
            End Using
        End Using
    End Sub
End Class
« Last Edit: June 30, 2016, 12:03:06 PM by djee »

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: Block from Layout
« Reply #1 on: June 30, 2016, 01:41:43 PM »
Djee, I'd imagine you'd need to add each attref to a list, then bind your listbox to the list.

I don't speak VB, but it might look something like this:
Code - vb.net: [Select]
  1. Dim attList as List<AttributeRef> = new List<AttributeRef>
  2. ...
  3. For Each attId As ObjectId In attCol
  4.         Dim attRef As AttributeReference = TryCast(Tx.GetObject(attId, OpenMode.ForRead), AttributeReference)
  5.         attList.Add(attRef)
  6. Next
  7. ...
  8. myListbox.DataSource=attList

Though I'm looking forward to some answers from others, MC is always busting out some cool WPF/databinding stuff.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Block from Layout
« Reply #2 on: June 30, 2016, 01:53:06 PM »
Hi,

I am not a VB.net guy and here is my attempt to build a list of attribute strings with the following method in C# :
Code - C#: [Select]
  1.     public static List<string> PrintAttributesToCommandLine(Database db,Document doc, Editor ed)
  2.         {
  3.             var lst = new List<string>();
  4.             try
  5.             {                
  6.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  7.                 {
  8.                     TypedValue[] tv = { new TypedValue(0, "INSERT"), new TypedValue(66, 1) };
  9.                     PromptSelectionResult sel = ed.GetSelection(new SelectionFilter(tv));
  10.                     if (sel.Status == PromptStatus.OK)
  11.                     {
  12.                         foreach (ObjectId obj in sel.Value.GetObjectIds())
  13.                         {
  14.                             BlockReference rf = (BlockReference)tr.GetObject(obj, OpenMode.ForRead);
  15.                             if (rf.AttributeCollection != null)
  16.                             {
  17.                                 foreach (ObjectId id in rf.AttributeCollection)
  18.                                 {
  19.                                     AttributeReference att = (AttributeReference)tr.GetObject(id, OpenMode.ForRead);
  20.                                     if (att.TextString != "")
  21.                                        lst.Add(att.TextString);
  22.                                 }
  23.                             }
  24.                         }
  25.                     }
  26.                     tr.Commit();
  27.                 }                
  28.             }
  29.             catch (System.Exception ex)
  30.             {
  31.                 ed.WriteMessage("\nError : {0}", ex.Message);
  32.             }
  33.             return lst;
  34.         }

djee

  • Newt
  • Posts: 49
Re: Block from Layout
« Reply #3 on: June 30, 2016, 03:34:19 PM »
Thanks to all for getting me started! So I ended up creating an ObservableCollection class & binded my listbox to a property of that class (atttribute tag)... Everything seems to be working (so far...). That WPF/XAML mountain is hard to climb...
Code: [Select]
  Private Sub SelectTargetedBlock_Click(sender As Object, e As Windows.RoutedEventArgs) Handles SelectTargetedBlock.Click

        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        'this - is the modal dialog box.
        Using UI As EditorUserInteraction = ed.StartUserInteraction(Me)
            Dim entResult As PromptEntityResult = ed.GetEntity(vbLf & "Select entity")
            If entResult.Status <> PromptStatus.OK Then
                Return
            End If

            Using Tx As Transaction = db.TransactionManager.StartTransaction()
                ' Dim obj As DBObject = Tx.GetObject(entResult.ObjectId, OpenMode.ForRead)
                Dim BlkRef As BlockReference = Tx.GetObject(entResult.ObjectId, OpenMode.ForRead)

                If BlkRef.AttributeCollection IsNot Nothing Then
                    Dim attCol As AttributeCollection = BlkRef.AttributeCollection
                    For Each attId As ObjectId In attCol
                        Dim attRef As AttributeReference = TryCast(Tx.GetObject(attId, OpenMode.ForRead), AttributeReference)
                        MyObjectSource.Add(New MyAttribute With {
                    .BlkTagToLookAt = attRef.Tag,
                    .DestinationBlkName = attRef.BlockName})

                    Next
                End If
                Tx.Commit()
            End Using
        End Using

        Dim b As New Binding("BlkTagToLookAt")
        b.Mode = BindingMode.OneWay
        b.Source = MyObjectSource
        BindingOperations.SetBinding(MyListBox, TextBox.TextProperty, b)
        MyListBox.DataContext = MyObjectSource