Author Topic: Getting first attribute value in a block  (Read 4078 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Getting first attribute value in a block
« on: February 22, 2013, 05:03:50 AM »
I see lots of examples online to get ALL the values in a block, but is there a way to get just the first one without using a "foreach" and "break"? Seems sufficient enough but feels brutal to the process... what happens if "break" causes an actual break (crash at same time or something)?

Guess I'm used to lisp, where one can specify the attribute value one wants to collect.

Just a noobs curiosity.

Thanks!
« Last Edit: February 22, 2013, 06:03:28 AM by Alien »

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Getting first attribute value in a block
« Reply #1 on: February 22, 2013, 06:03:10 AM »
You still have to check if there are attributes, so you just can't get the first attribute as GetMyAtrtibute(0).
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Getting first attribute value in a block
« Reply #2 on: February 22, 2013, 06:07:24 AM »
You still have to check if there are attributes, so you just can't get the first attribute as GetMyAtrtibute(0).

Didn't think about it that way. Thank you!

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Getting first attribute value in a block
« Reply #3 on: February 22, 2013, 07:12:57 AM »
Don't worry about performance, if I extract attributes and insertion point location from 5000 blocks and put them in a ListView, it takes less then half a second.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Getting first attribute value in a block
« Reply #4 on: February 22, 2013, 07:23:57 AM »
Don't worry about performance, if I extract attributes and insertion point location from 5000 blocks and put them in a ListView, it takes less then half a second.


  :-)

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Getting first attribute value in a block
« Reply #5 on: February 22, 2013, 07:54:14 AM »
IF you are using Tony T's Runtime Extension Methods you can simplify getting one attribute.
Code: [Select]
var db = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    var bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
    var btr = (BlockTableRecord)bt["MYBLOCK"].GetObject(OpenMode.ForRead);
    var att = btr.Cast<ObjectId>()
        .Where(id=> id.IsTypeOf<AttributeDefinition>())  <-- Need Tony T's extension method here
        .Select(id => (AttibuteDefinition)id.GetObject(OpenMode.ForRead))
        .Where(att => att.Tag == "MY_TAG")
        .FirstOrDefault();
    if (att != null)
    {
        //Do what you want with the single attribute
    }
}

If you know that its a specific block and it always has specific attributes
Code: [Select]
var db = Application.DocumentManager.MdiActiveDocument.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    var bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
    var btr = (BlockTableRecord)bt["MYBLOCK"].GetObject(OpenMode.ForRead);
    var att = btr.GetObjects<AttributeDefinition>().FirstOrDefault(a=>a.Tag == "MY_TAG);
    if (att != null)
    {
        //Do what you want with the single attribute
    }
}
« Last Edit: February 22, 2013, 08:23:21 AM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

bargool

  • Guest
Re: Getting first attribute value in a block
« Reply #6 on: February 22, 2013, 01:15:52 PM »
IF you are using Tony T's Runtime Extension Methods
Could you post link to this methods, please?

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Getting first attribute value in a block
« Reply #7 on: February 22, 2013, 01:20:53 PM »
IF you are using Tony T's Runtime Extension Methods
Could you post link to this methods, please?

2nd this

MexicanCustard

  • Swamp Rat
  • Posts: 705
Revit 2019, AMEP 2019 64bit Win 10

bargool

  • Guest
Re: Getting first attribute value in a block
« Reply #9 on: February 22, 2013, 01:44:34 PM »
Oh ye! Thank you!

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Getting first attribute value in a block
« Reply #11 on: February 22, 2013, 04:07:05 PM »
Hi Alien,

If you're looking for the first attribute of a block reference (BlockReference) instead of a block definition (BlockTableRecord), you can use some LINQ extension methods:
  • Cast<> to convert the AttributeCollection into an IEnumerable<ObjectId> usable with Linq methods.
  • Any<> to check if the collection isn't empty.
  • First<> to get the iENumerable first item.

Here's a little snippet (assuming br is a BlockReference)
Code - C#: [Select]
  1.                 IEnumerable<ObjectId> atts = br.AttributeCollection.Cast<ObjectId>();
  2.                 if (atts.Any())
  3.                 {
  4.                     AttributeReference attRef =
  5.                         (AttributeReference)atts.First().GetObject(OpenMode.ForRead);
  6.                     // ...
  7.                 }
  8.                 else
  9.                 {
  10.                     // ...
  11.                 }
Speaking English as a French Frog

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Getting first attribute value in a block
« Reply #12 on: February 22, 2013, 06:38:27 PM »
Fantastic!!! Thank you!

TheMaster

  • Guest
Re: Getting first attribute value in a block
« Reply #13 on: February 22, 2013, 08:26:04 PM »
Don't worry about performance, if I extract attributes and insertion point location from 5000 blocks and put them in a ListView, it takes less then half a second.

With WinForms or WPF ?

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Getting first attribute value in a block
« Reply #14 on: February 23, 2013, 04:43:24 AM »
...
With WinForms or WPF ?

Winforms. Actually it wasn't a listview but a datagrid. I've timed the operation from filtering blocks, extracting attributes and locations of 5250 entities, and in 489 milliseconds the data was placed in the datagrid.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.