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

Problem Converting Handle to ObjectId

4 REPLIES 4
Reply
Message 1 of 5
LialAtArnold
1298 Views, 4 Replies

Problem Converting Handle to ObjectId

I have been trying to get the objectId for a block when given the Handle of the object as a string.
The code is as follows:

//Use this to get the Handle from a BlockReference
String strHand = myBR.ObjectId.Handle.Value.ToString();

// strHand now = "11007"

//Convert the string to long Int and allow for Hex chars
long ln = Int64.Parse(strHand, System.Globalization.NumberStyles.AllowHexSpecifier);

//At this point, the value of ln is now: 69639

//Also tried this, but get the same 69639 value
long ln = Convert.ToInt64(strHand, 16);

//When I pass this value to create the handle it generates a Handle which does not exist in the dB!!
Handle hand = new Handle(ln);

//The GetObjectId then fails
ObjectId id = db.GetObjectId(false, hand, 0);


If I do the following it works perfectly:

long ln = 11007;
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);

How do I set this up so the correct Handle gets created
AND the potential Hex characters in the string are parsed out??

Edited by: lial.williams@arnold.af.mil on Sep 30, 2009 4:32 PM Edited by: lial.williams@arnold.af.mil on Sep 30, 2009 4:37 PM
4 REPLIES 4
Message 2 of 5

my code looks a lot like yours, here:
{code}
Public Function HandletoObjectID(ByVal Hndl As String) As ObjectId
Dim db As Database = acdocs.MdiActiveDocument.Database
Try
Dim ln As Long = Convert.ToInt64(Hndl, 16)
Dim hn As Handle = New Handle(ln)
Dim id As ObjectId = db.GetObjectId(False, hn, 0)
Return id
Catch ex As System.Exception
EdWrite("Error Converting Handle:" & Hndl & vbLf)
Return Nothing
End Try
End Function
{code}
After looking in the object browser, the Handle.Value property is already returning a long, which then you sent .ToString, and re-converted it into long.
Also, you can get the handle from the myBR.Handle, no need to do myBR.ObjectID.Handle.
if you tried one of these:
{code}
String strHand = myBR.Handle.ToString();
long ln = Convert.ToInt64(strHand, 16);
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);
---OR---
long ln = myBR.Handle.Value
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);
{code}
you'd probably get the desired results.

FYI, my code is used to get ObjectID's from handles that have been stored as strings in XData, and the only time it ever fails is if the object with that handle has been deleted from the database since the handle was stored.
Dave O.                                                                  Sig-Logos32.png
Message 3 of 5
Anonymous
in reply to: LialAtArnold

Don't use

Handle.Value.ToString()

Use

Handle.ToString()

The result of the latter should be convertible back to a handle
using your call to Int64.Parse()

--
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:6263760@discussion.autodesk.com...
I have been trying to get the objectId for a block when given the Handle of
the object as a string.
The code is as follows:

//Use this to get the Handle from a BlockReference
String strHand = myBR.ObjectId.Handle.Value.ToString();

// strHand now = "11007"

//Convert the string to long Int and allow for Hex chars
long ln = Int64.Parse(strHand,
System.Globalization.NumberStyles.AllowHexSpecifier);

//At this point, the value of ln is now: 69639

//Also tried this, but get the same 69639 value
long ln = Convert.ToInt64(strHand, 16);

//When I pass this value to create the handle it generates a Handle which
does not exist in the dB!!
Handle hand = new Handle(ln);

//The GetObjectId then fails
ObjectId id = db.GetObjectId(false, hand, 0);


If I do the following it works perfectly:

long ln = 11007;
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);

How do I set this up so the correct Handle gets created
AND the potential Hex characters in the string are parsed out??

Edited by: lial.williams@arnold.af.mil on Sep 30, 2009 4:32 PM

Edited by: lial.williams@arnold.af.mil on Sep 30, 2009 4:37 PM
Message 4 of 5
LialAtArnold
in reply to: LialAtArnold

Chief / Tony,
I ended up with this:

String strHand = myBR.Handle.ToString();
long ln = Convert.ToInt64(strHand, 16);
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);

It works great!

Thanks for the help!
Message 5 of 5
Anonymous
in reply to: LialAtArnold

The only thing wrong with your original code was that
it was calling Handle.Value.ToString(), which returns
the numeric value (base10) of the handle, rather than
the value in hex.

--
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:6264159@discussion.autodesk.com...
Chief / Tony,
I ended up with this:

String strHand = myBR.Handle.ToString();
long ln = Convert.ToInt64(strHand, 16);
Handle hand = new Handle(ln);
ObjectId id = db.GetObjectId(false, hand, 0);

It works great!

Thanks for the help!

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