<?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 Re: Change .NET Selection code to specific block in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164379#M43231</link>
    <description>&lt;P&gt;ok you can try with following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the below routine, you can get object ids of all block.&lt;/P&gt;&lt;PRE&gt;public static ObjectIdCollection BlockReferenceInModelSpace(Editor acEd)
{
	//get block reference 
	TypedValue[] tvs = new TypedValue[1];
	tvs.SetValue(new TypedValue(DxfCode.Start, "INSERT"), 0);
	SelectionFilter sf = new SelectionFilter(tvs);
	PromptSelectionResult psr = acEd.SelectAll(sf);
	if (psr.Status == PromptStatus.OK) {
		return new ObjectIdCollection(psr.Value.GetObjectIds());
	} else {
		return new ObjectIdCollection();
	}
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;main sub:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public void TEST_Attribute()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;
	Editor ed = doc.Editor;
	//get object id's of all block in the active drawing
	ObjectIdCollection objIdColl = BlockReferenceInModelSpace(ed);
	//lock document
	using (DocumentLock docLock = doc.LockDocument()) {
		//start transaction
		using (Transaction tx = db.TransactionManager.StartTransaction()) {
			//iterate object id
			foreach (ObjectId objId in objIdColl) {
				//get the obeject
				Entity ent = tx.GetObject(objId, OpenMode.ForRead);
				if (ent is BlockReference) {
					//casting block reference
					BlockReference br = ent as BlockReference;
					//'now check the block name here
					if (br.Name == "MyBlock") {
						//' if block name  satify then check atttrubute
						foreach (ObjectId attId in br.AttributeCollection) {
							//do as you like
						}
					}

				}
			}
		}

	}

}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; you can try with this code. hopefully would help to serve your purpose&lt;/P&gt;</description>
    <pubDate>Mon, 21 Jul 2014 14:55:20 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2014-07-21T14:55:20Z</dc:date>
    <item>
      <title>Change .NET Selection code to specific block</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164083#M43228</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found this code at Kean Walmsley's blog &lt;A href="http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html" target="_self"&gt;[here]&lt;/A&gt;&lt;/P&gt;&lt;P&gt;It works just fine, however I'm having some trouble adapting the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not want to pick a block, I want to use a specific named block.&lt;BR /&gt;e.g &lt;STRONG&gt;(blk.Name = "MyBlock" or Blk.Name = "MyOtherBlock")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I started commenting some of it, but when I get &lt;STRONG&gt;Dim selSet As SelectionSet = res.Value&lt;/STRONG&gt; I get lost.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can this be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

Namespace MyApplication
    
    Public Class DumpAttributes
        
        &amp;lt;CommandMethod("LISTATT")&amp;gt;  _
        Public Sub ListAttributes()
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim tr As Transaction = db.TransactionManager.StartTransaction
            ' Start the transaction
            Try 
                ' Build a filt&lt;/PRE&gt;&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;er list so that only
                ' block references are selected
                Dim filList() As TypedValue = New TypedValue() {New TypedValue(CType(DxfCode.Start,Integer), "INSERT")}
                Dim filter As SelectionFilter = New SelectionFilter(filList)
                Dim opts As PromptSelectionOptions = New PromptSelectionOptions
                opts.MessageForAdding = "Select block references: "
                Dim res As PromptSelectionResult = ed.GetSelection(opts, filter)
                ' Do nothing if selection is unsuccessful
                If (res.Status &amp;lt;&amp;gt; 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 = CType(tr.GetObject(blkId, OpenMode.ForRead),BlockReference)
                    Dim btr As BlockTableRecord = CType(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead),BlockTableRecord)
                    ed.WriteMessage((""&amp;amp; vbLf&amp;amp;"Block: " + btr.Name))
                    btr.Dispose
                    Dim attCol As AttributeCollection = blkRef.AttributeCollection
                    For Each attId As ObjectId In attCol
                        Dim attRef As AttributeReference = CType(tr.GetObject(attId, OpenMode.ForRead),AttributeReference)
                        Dim str As String = (""&amp;amp; vbLf&amp;amp;"  Attribute Tag: "  _
                                    + (attRef.Tag + (""&amp;amp; vbLf&amp;amp;"    Attribute String: " + 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
    End Class
End Namespace&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jul 2014 13:08:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164083#M43228</guid>
      <dc:creator>TTIII</dc:creator>
      <dc:date>2014-07-21T13:08:33Z</dc:date>
    </item>
    <item>
      <title>Re: Change .NET Selection code to specific block</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164203#M43229</link>
      <description>Can you explain your Problem more clearly? What do you want from your drawing? you can provide a example as well</description>
      <pubDate>Mon, 21 Jul 2014 14:00:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164203#M43229</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-07-21T14:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Change .NET Selection code to specific block</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164211#M43230</link>
      <description>&lt;P&gt;I'm trying to automate it to list the attribues from a list of specific blocks.&lt;/P&gt;&lt;P&gt;I dont want it to prompt the user to select a block.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jul 2014 14:03:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164211#M43230</guid>
      <dc:creator>TTIII</dc:creator>
      <dc:date>2014-07-21T14:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Change .NET Selection code to specific block</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164379#M43231</link>
      <description>&lt;P&gt;ok you can try with following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the below routine, you can get object ids of all block.&lt;/P&gt;&lt;PRE&gt;public static ObjectIdCollection BlockReferenceInModelSpace(Editor acEd)
{
	//get block reference 
	TypedValue[] tvs = new TypedValue[1];
	tvs.SetValue(new TypedValue(DxfCode.Start, "INSERT"), 0);
	SelectionFilter sf = new SelectionFilter(tvs);
	PromptSelectionResult psr = acEd.SelectAll(sf);
	if (psr.Status == PromptStatus.OK) {
		return new ObjectIdCollection(psr.Value.GetObjectIds());
	} else {
		return new ObjectIdCollection();
	}
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;main sub:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public void TEST_Attribute()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;
	Editor ed = doc.Editor;
	//get object id's of all block in the active drawing
	ObjectIdCollection objIdColl = BlockReferenceInModelSpace(ed);
	//lock document
	using (DocumentLock docLock = doc.LockDocument()) {
		//start transaction
		using (Transaction tx = db.TransactionManager.StartTransaction()) {
			//iterate object id
			foreach (ObjectId objId in objIdColl) {
				//get the obeject
				Entity ent = tx.GetObject(objId, OpenMode.ForRead);
				if (ent is BlockReference) {
					//casting block reference
					BlockReference br = ent as BlockReference;
					//'now check the block name here
					if (br.Name == "MyBlock") {
						//' if block name  satify then check atttrubute
						foreach (ObjectId attId in br.AttributeCollection) {
							//do as you like
						}
					}

				}
			}
		}

	}

}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; you can try with this code. hopefully would help to serve your purpose&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jul 2014 14:55:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164379#M43231</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-07-21T14:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change .NET Selection code to specific block</title>
      <link>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164589#M43232</link>
      <description>Thanks! This works perfectly.</description>
      <pubDate>Mon, 21 Jul 2014 16:04:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/change-net-selection-code-to-specific-block/m-p/5164589#M43232</guid>
      <dc:creator>TTIII</dc:creator>
      <dc:date>2014-07-21T16:04:15Z</dc:date>
    </item>
  </channel>
</rss>

