Author Topic: Count blocks by attribute tag  (Read 770 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Count blocks by attribute tag
« on: August 16, 2012, 09:34:43 PM »
Code: [Select]
       // using System.Linq;
        #region "Selection block by attribute tag"

        //      get the real block name.        //     
        public static string EffectiveName(Transaction tr, BlockReference bref)
        {
            BlockTableRecord btr = null;
            if ((bref.IsDynamicBlock)
                | (bref.Name.StartsWith("`*U", StringComparison.InvariantCultureIgnoreCase)))
            {
                btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
            }
            else
            {
                btr = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
            }
            return btr.Name;
        }
        [CommandMethod("SelectByAttribute","byatt", CommandFlags.Modal)]
        public void GetSelectionByAtt()
        {
   
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            string m_str = string.Empty;

            PromptStringOptions psto= new PromptStringOptions("\nEnter the tag to find (case-nonsensitive): ");
            // to disable the blank space in user input
            psto.AllowSpaces=false;

            PromptResult pstr= ed.GetString(psto);

            if (pstr.Status!= PromptStatus.OK) return;

            m_str= pstr.StringResult.ToUpper();

            List<object[]> blockdata = new List<object[]>();

            PromptSelectionOptions pso = new PromptSelectionOptions();

            pso.MessageForRemoval = "\nFailed to select a blocks!";

            pso.MessageForAdding = "\nPlease select a blocks: ";

            // build filter to select all attributed blocks
            SelectionFilter filter =
                new SelectionFilter(
                new TypedValue[] { new TypedValue(0, "insert"),
                    new TypedValue(66, 1) }
                    );

            // perform block selection
            PromptSelectionResult sres = ed.GetSelection(pso, filter);// might be SelectAll instead

            if (sres.Status != PromptStatus.OK) return;
            try
            {

            SelectionSet sset = sres.Value;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

            foreach (SelectedObject sobj in sset)
            {
                Entity ent = tr.GetObject(sobj.ObjectId,OpenMode.ForRead, false) as Entity;

                BlockReference bref = ent as BlockReference;

                AttributeCollection attribs = bref.AttributeCollection;

                foreach (  ObjectId attid in attribs)
                {
                    AttributeReference attref= tr.GetObject(attid, OpenMode.ForRead,false) as AttributeReference;

                if (attref.Tag==m_str)
                {
                   
                    object[] datum = new object[]{EffectiveName(tr, bref), attref.Tag,attref.TextString};
                            // add block  info to the list
                            blockdata.Add(datum);
           
                }
                    }
                }
             // display total result
            //foreach (object[] item in blockdata)
            //    ed.WriteMessage("\n{0}\t{1}\t{2}", item[0], item[1],item[2]);

                // group items by name and attribute tag
            var grps = from s in blockdata
                       group s by s[0]  into grp
                       select new
                       {
                           BlockName = grp.Key,
                           Tag = grp.First().ElementAt(1),
                           AttValue = grp.First().ElementAt(2),
                           Count= grp.Count()
                       };
            // display subtotals
            foreach (var grp in grps)
            {
                ed.WriteMessage("\n[ Block Name: {0} |Tag: {1} | Value: {2} | Count: {3} ]\n",
                    grp.BlockName,
                    grp.Tag,
                    grp.AttValue,
                    grp.Count
);
            }
            tr.Commit();

            }           
            }       
            catch (System.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
            }
        }
        #endregion

~'J'~
« Last Edit: June 06, 2013, 02:46:22 PM by fixo »