Author Topic: Get Dynamic Block Name  (Read 1526 times)

0 Members and 1 Guest are viewing this topic.

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Get Dynamic Block Name
« on: December 25, 2015, 01:34:41 PM »
Hello guys,

I successfully wrote the following codes on my won  8-) but I faced a problem with Dynamic Block Name , so how to get the anonymous block name ?

Feel free to comment the codes cause I am on the way learning codes in C#  :-)

Code - C#: [Select]
  1.         static public void JustAtest()
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.             Dictionary<string, int> dic = new Dictionary<string, int>();
  7.             TypedValue[] val = { new TypedValue((int)DxfCode.Start, "INSERT") };
  8.             SelectionFilter ftr = new SelectionFilter(val);
  9.             PromptSelectionResult ss = ed.GetSelection(ftr);
  10.             if (ss.Status == PromptStatus.OK)
  11.             {
  12.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  13.                 {
  14.                     try
  15.                     {
  16.                         BlockReference ent;
  17.                         foreach (ObjectId id in ss.Value.GetObjectIds())
  18.                         {
  19.                             ent = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
  20.                             string BlockName = ent.Name;
  21.                             if (dic.ContainsKey(BlockName))
  22.                             {
  23.                                 int n = 1 + dic[BlockName];
  24.                                 dic.Remove(BlockName);
  25.                                 dic.Add(BlockName, n);
  26.                             }
  27.                             else
  28.                             {
  29.                                 dic.Add(BlockName, 1);
  30.                             }
  31.                         }
  32.                     }
  33.                     catch (System.Exception ex)
  34.                     {
  35.                         ed.WriteMessage(ex.Message);
  36.                         return;
  37.                     }
  38.                 }
  39.                 if (dic.Count > 0)
  40.                 {
  41.                     ed.WriteMessage("\nBlock Name: [Quantity]");
  42.                     foreach (KeyValuePair<string, int> item in dic)
  43.                     {
  44.                         ed.WriteMessage("\n" + item.Key + " ----- [ " + item.Value + " ]");
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.  


Thank you.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Get Dynamic Block Name
« Reply #1 on: December 25, 2015, 03:02:48 PM »
Hi,

Code - C#: [Select]
  1. public static string GetEffectiveName(BlockReference source)
  2. {
  3.         if (source.IsDynamicBlock)
  4.                 return ((BlockTableRecord)source.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
  5.         return source.Name;
  6. }
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 712
  • Hypersensitive
Re: Get Dynamic Block Name
« Reply #2 on: December 25, 2015, 03:13:53 PM »
Nice ,

Thank you gile so much.  :-)