.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to find the most recently-created Group

16 REPLIES 16
Reply
Message 1 of 17
Anonymous
456 Views, 16 Replies

How to find the most recently-created Group

Not sure where I saw this question asked, but it was how
to find the most-recently created GROUP in a drawing.

Here's a few extension methods that may help;

{code}

public static class MiscExtensionMethods
{
static ObjectId FindNewest( this IEnumerable ids )
{
if( ids.Any() )
return ids.First().Database.GetObjectId( false,
new Handle( ids.Cast()
.Max( id => id.Handle.Value ) ), 0 );
else
return ObjectId.Null;
}

// for ObjectIdCollection
static ObjectId FindNewest( this ObjectIdCollection ids )
{
return FindNewest( ids.Cast() );
}

// for SymbolTable
static ObjectId FindNewest( this SymbolTable table )
{
return FindNewest( table.Cast() );
}

// for BlockTableRecord
static ObjectId FindNewest( this BlockTableRecord btr )
{
return FindNewest( btr.Cast() );
}

// for DBDictionary
static ObjectId FindNewest( this DBDictionary dictionary )
{
return FindNewest( ( (IDictionary)
dictionary ).Values.Cast() );
}
}

{code}


With the above, finding the ObjectId of the most-recently created
group would involve little more than this:

{code}


public class Class1
{
void Example()
{
Database db = HostApplicationServices.WorkingDatabase;
using( Transaction tr = db.TransactionManager.StartTransaction() )
{
DBDictionary groups =
db.GroupDictionaryId.GetObject( OpenMode.ForRead ) as
DBDictionary;

ObjectId newestGroup = groups.FindNewest();

// blah blah blah..

tr.Commit();
}
}
}




{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
16 REPLIES 16
Message 2 of 17
Anonymous
in reply to: Anonymous

Correction to the first method:

static ObjectId FindNewest( this IEnumerable ids )
{
if( ids.Any() )
return ids.First().Database.GetObjectId( false,
new Handle( ids.Max( id => id.Handle.Value ) ), 0 );
else
return ObjectId.Null;
}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Tony Tanzillo" wrote in message
news:6263175@discussion.autodesk.com...
Not sure where I saw this question asked, but it was how
to find the most-recently created GROUP in a drawing.

Here's a few extension methods that may help;

{code}

public static class MiscExtensionMethods
{
static ObjectId FindNewest( this IEnumerable ids )
{
if( ids.Any() )
return ids.First().Database.GetObjectId( false,
new Handle( ids.Cast()
.Max( id => id.Handle.Value ) ), 0 );
else
return ObjectId.Null;
}

// for ObjectIdCollection
static ObjectId FindNewest( this ObjectIdCollection ids )
{
return FindNewest( ids.Cast() );
}

// for SymbolTable
static ObjectId FindNewest( this SymbolTable table )
{
return FindNewest( table.Cast() );
}

// for BlockTableRecord
static ObjectId FindNewest( this BlockTableRecord btr )
{
return FindNewest( btr.Cast() );
}

// for DBDictionary
static ObjectId FindNewest( this DBDictionary dictionary )
{
return FindNewest( ( (IDictionary)
dictionary ).Values.Cast() );
}
}

{code}


With the above, finding the ObjectId of the most-recently created
group would involve little more than this:

{code}


public class Class1
{
void Example()
{
Database db = HostApplicationServices.WorkingDatabase;
using( Transaction tr = db.TransactionManager.StartTransaction() )
{
DBDictionary groups =
db.GroupDictionaryId.GetObject( OpenMode.ForRead ) as
DBDictionary;

ObjectId newestGroup = groups.FindNewest();

// blah blah blah..

tr.Commit();
}
}
}




{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
Message 3 of 17
wesbird
in reply to: Anonymous

Hi Tony:
I got this error message when I compile it:

Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments C:\RS\Training\AutoCAD\TtI\2010\point-in-curve\GroupUtil.cs 49 41 Through-the-Interface

at
static ObjectId FindNewest(this IEnumerable ids)
{
...

great code.


Thank you,
Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 4 of 17
Anonymous
in reply to: Anonymous

As I mentioned in the previous post, I cannot be responsible for the
butt-head incompetence that underlies the way these discussion groups are
run, or the garbage software interface used on the web side.

My code was mangled by the discussion group software (if you look at it
through the web interface, anything that is surrounded by angle braces was
removed).

So, what I would suggest you do, is write Carl Bass a short note, thanking
him for this stinking, rotting pile of garbage that makes his entire
organization look idiotic.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6263315@discussion.autodesk.com...
Hi Tony:
I got this error message when I compile it:

Error 1 Using the generic type 'System.Collections.Generic.IEnumerable'
requires '1' type arguments
C:\RS\Training\AutoCAD\TtI\2010\point-in-curve\GroupUtil.cs 49 41
Through-the-Interface

at
static ObjectId FindNewest(this IEnumerable ids)
{
...

great code.


Thank you,
Wes
Message 5 of 17
wesbird
in reply to: Anonymous

Hi Tony:

I just realize this caused by this forum. But I cannot figure out what are missing in the code. So could you provide a download file, or post one to Last group in drawing on the swamp, which it come from.





Thank you

Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 6 of 17
Anonymous
in reply to: Anonymous

See the attached.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6263364@discussion.autodesk.com...
Hi Tony:
I just realize this caused by this forum. But I cannot figure out what are
missing in the code. So could you provide a download file, or post one to
Last group in drawing on the swamp, which it come from.


Thank you
Wes
Message 7 of 17
Anonymous
in reply to: Anonymous

Thanks Tony.

I was looking for something like this.

Alas, my going back to net 2.0 will keep me from testing this as I'm missing the "using System.Linq;" in my resources.

They may upgrade things here and then again, they may just shut it down.

Anyway, thanks again.
Message 8 of 17
Anonymous
in reply to: Anonymous

No, Wait, I found it!

using System.Linq is in the System core dll.

I'll get back here after I try this.
Message 9 of 17
Anonymous
in reply to: Anonymous

Dunno,

Everything lights up in red. Maybe I'm missing something or this is for a newer version of VS or net.

See attached.
Message 10 of 17
Anonymous
in reply to: Anonymous

Extension methods require Visual Studio 2008 and .NET 3.0.

Regardless of how I wrote it, it shows how to get the
oldest or newest object, and you can just adapt that to
your own code.



--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6263999@discussion.autodesk.com...
Dunno,

Everything lights up in red. Maybe I'm missing something or this is for a
newer version of VS or net.

See attached.
Message 11 of 17
Anonymous
in reply to: Anonymous

Thanks Tony,

You can do it in VS2008 targeting net 2.0,
you just have to add an extension attribute for the compiler.

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}

I'll figure something out, if not, I can live with it. 🙂
Message 12 of 17
Anonymous
in reply to: Anonymous

I noticed your solution to this problem on another forum, along with Dan's
code.

Just a little tip: You don't have to sort a sequence to find a min/max value
in it. Sorting involves *much* more work than simply walking the sequence
and finding the min/max value by hand.

Basically you can just do this:

{code}

public static void GetNewest( DBDictionary dictionary )
{
ICollection ids = ((IDictionary) dictionary).Values;
ObjectId last = ids[0];
long current = last.Handle.Value;
foreach( ObjectId id in ids )
{
long value = id.Handle.Value;
if( value > current )
{
current = value;
last = id;
}
}
return last
}

{code}

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6264045@discussion.autodesk.com...
Thanks Tony,

You can do it in VS2008 targeting net 2.0,
you just have to add an extension attribute for the compiler.

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class |
AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}

I'll figure something out, if not, I can live with it. 🙂
Message 13 of 17
Anonymous
in reply to: Anonymous

Oh just saw this thread,
Thanks for posting your 'way' more elegant solution to the problem. : )
Message 14 of 17
Anonymous
in reply to: Anonymous

Not terribly elegant, just helping out a beginner with a basic programming
concept, (finding a min/max value without the overhead of sorting).


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6272649@discussion.autodesk.com...
Oh just saw this thread,
Thanks for posting your 'way' more elegant solution to the problem. : )
Message 15 of 17
Anonymous
in reply to: Anonymous

Thanks Tony.

Didn't know that about sort.
It's fortunate that this all boils down to the highest handle value.
Should come in handy for most all objects created in the drawing.

I'm having one problem with: ObjectId last = ids[0];
it tells me that I cannot use indexing on an ICollection.
I'll see if I can figure out how to get a single item from the collection or use something different.

Edit:

This seemed a straight forward way to get a single item from the ICollection:

>DBDictionary groups = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
>ICollection ids = ((IDictionary)groups).Values;
>IEnumerator enumerator = ids.GetEnumerator();
>ObjectId last = ObjectId.Null;
>
> if (enumerator.MoveNext())
>{
>last = (ObjectId)enumerator.Current;
>long current = last.Handle.Value;
>
> foreach (ObjectId id in ids)
>{
>long value = id.Handle.Value;
>if (value > current)
> {
>current = value;
>last = id;
>}
>}
>}
>if (!last.IsNull)
>{
>Group grp = (Group)tr.GetObject(last, OpenMode.ForRead, false, true);
>return grp.Name;
>}

Bill Edited by: BillZndl on Oct 20, 2009 8:40 AM
Message 16 of 17
Anonymous
in reply to: Anonymous

Hi Bill.

Sorry, that code was bugged.

You might have noticed one of my rants about using IEnumerator directly in
code, which is not advised except in unusual situations, and if you do use
it, you have to check to see if it implements IDisposable and if it does,
call it's Dispose() method.

Here is a revised version that works. It uses foreach() to get the first
item in the collection which eliminates the need to deal with IEnumerator
directly.

{code}

public static ObjectId GetNewest( DBDictionary dictionary )
{
ICollection ids = ((IDictionary) dictionary).Values;
ObjectId last = ObjectId.Null;
foreach( ObjectId temp in ids )
{
last = temp;
break;
}
if( ! last.IsNull )
{
long current = last.Handle.Value;
foreach( ObjectId id in ids )
{
long value = id.Handle.Value;
if( value > current )
{
current = value;
last = id;
}
}
}
return last;
}

{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6274283@discussion.autodesk.com...
Thanks Tony.

Didn't know that about sort.
It's fortunate that this all boils down to the highest handle value.
Should come in handy for most all objects created in the drawing.

I'm having one problem with: ObjectId last = ids[0];
it tells me that I cannot use indexing on an ICollection.
I'll see if I can figure out how to get a single item from the collection or
use something different.

Edit:

This seemed a straight forward way to get a single item from the
ICollection:

>DBDictionary groups =
(DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
>ICollection ids = ((IDictionary)groups).Values;
>IEnumerator enumerator = ids.GetEnumerator();
>ObjectId last = ObjectId.Null;
>
> if (enumerator.MoveNext())
>{
>last = (ObjectId)enumerator.Current;
>long current = last.Handle.Value;
>
> foreach (ObjectId id in ids)
>{
>long value = id.Handle.Value;
>if (value > current)
> {
>current = value;
>last = id;
>}
>}
>}
>if (!last.IsNull)
>{
>Group grp = (Group)tr.GetObject(last,
OpenMode.ForRead, false, true);
>return grp.Name;
>}

Bill

Edited by: BillZndl on Oct 20, 2009 8:40 AM
Message 17 of 17
Anonymous
in reply to: Anonymous

>You might have noticed one of my rants about using IEnumerator directly in
>code, which is not advised except in unusual situations, and if you do use
>it, you have to check to see if it implements IDisposable and if it does,
>call it's Dispose() method.

Hmm, I remember that.
I wonder if that's why all of a sudden I started getting "Fatal Error" when closing AutoCAD or switching between drawings. :?
I didn't see a dispose method for the "enumerator".

Anyway, thanks for clearing that up.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost