.NET

Reply
Contributor
mabe2k11
Posts: 24
Registered: ‎03-14-2006
Message 1 of 8 (1,862 Views)

Convert string to ObjectId

1862 Views, 7 Replies
05-02-2006 04:32 AM
Hello!

It is possible to convert an ObjectId into a string. But is it also possible to convert a string/int into an ObjectId?
I want to save some ObjectIDs to a text-file to use them later.
But I haven't found something to convert them back.
Contributor
mabe2k11
Posts: 24
Registered: ‎03-14-2006
Message 2 of 8 (1,862 Views)

Re: Convert string to ObjectId

05-04-2006 03:33 AM in reply to: mabe2k11
Hi!

I found a solution for this problem. I use the Handle instead of the ObjectID-string:

string oldHandle = ent.ObjectId.Handle.Value.ToString();
ObjectId newObjectId = db.GetObjectId(false, new Handle(Convert.ToInt64(oldHandle)), 0);

It works perfect!
Valued Contributor
tumpliner
Posts: 50
Registered: ‎02-07-2007
Message 3 of 8 (1,862 Views)

Re: Convert string to ObjectId

06-17-2009 04:13 AM in reply to: mabe2k11
This will only work "perfect" as long as there are no alpha characters in the string. It is really a hex number string, but Convert.ToInt64 is treating it as though it were a decimal number string. That will work until you get into the "A,B,C,D,E of F" character(s) of a hex handle string. I could not find a .NET function that would convert a hex string to decimal integer, so I had to write my own. It is not too difficult.
*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 4 of 8 (1,862 Views)

Re: Convert string to ObjectId

06-17-2009 11:34 AM in reply to: mabe2k11
That is not quite true. There is no need to 'write your own' hex translator, just to use the proper overload of the ToInt64 function. There is a version which accepts an argument to specify the base that you are translating from, which works fine with alphanumerics.

ToInt64(ByVal value As String, ByVal fromBase As Integer) As Long
Member of: System.Convert

ObjectId newObjectId = db.GetObjectId(false, new Handle(Convert.ToInt64(oldHandle, 16)), 0);

The only way this fails is if the DBObject referred to by the Handle is no longer in the drawing.
Dave O.                                                                  Sig-Logos32.png
*Tony Tanzillo
Message 5 of 8 (1,862 Views)

Re: Convert string to ObjectId

06-17-2009 11:53 AM in reply to: mabe2k11
Convert a string representation of a number in hex notation to an Int64:

Int64 result = Int64.Parse( "12AFE", NumberStyles.AllowHexSpecifier );

--
http://www.caddzone.com

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

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

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


wrote in message news:6203162@discussion.autodesk.com...
This will only work "perfect" as long as there are no alpha characters in
the string. It is really a hex number string, but Convert.ToInt64 is
treating it as though it were a decimal number string. That will work until
you get into the "A,B,C,D,E of F" character(s) of a hex handle string. I
could not find a .NET function that would convert a hex string to decimal
integer, so I had to write my own. It is not too difficult.
*Tony Tanzillo
Message 6 of 8 (1,862 Views)

Re: Convert string to ObjectId

06-17-2009 12:09 PM in reply to: mabe2k11
And for those using VS2008:

public static class DatabaseExtensionMethods
{
public static ObjectId GetObjectId( this Database db, string handle )
{
return db.GetObjectId( false,
new Handle(
Int64.Parse(handle, NumberStyles.AllowHexSpecifier )));
}
}


--
http://www.caddzone.com

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

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

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


"Tony Tanzillo" wrote in message
news:6203664@discussion.autodesk.com...
Convert a string representation of a number in hex notation to an Int64:

Int64 result = Int64.Parse( "12AFE", NumberStyles.AllowHexSpecifier );

--
http://www.caddzone.com

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

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

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


wrote in message news:6203162@discussion.autodesk.com...
This will only work "perfect" as long as there are no alpha characters in
the string. It is really a hex number string, but Convert.ToInt64 is
treating it as though it were a decimal number string. That will work until
you get into the "A,B,C,D,E of F" character(s) of a hex handle string. I
could not find a .NET function that would convert a hex string to decimal
integer, so I had to write my own. It is not too difficult.
New Member
uzrgm
Posts: 1
Registered: ‎08-15-2011
Message 7 of 8 (1,605 Views)

Re: Convert string to ObjectId

08-15-2011 11:53 PM in reply to: mabe2k11

string objIdStr = someObj.ObjectId.Tostring();//after this code objIdStr = "(1234)", where  1234 - id of the object.

 

string pureObjId = objectId.Trim(new char []{ '(', ')'});//removes '(' and ')'. it's need to convert this string to int
 ObjectId objId = new ObjectId(Convert.ToInt32(pureObjId));//create ObjectId with defined id

Active Contributor
Andrey_Bushman
Posts: 33
Registered: ‎01-18-2009
Message 8 of 8 (1,539 Views)

Re: Convert string to ObjectId

08-30-2011 11:07 AM in reply to: *Tony Tanzillo

 

public static ObjectId GetObjectId(this Database db, string handle) {   
Handle h = new Handle(Int64.Parse(handle, NumberStyles.AllowHexSpecifier));   
ObjectId id = ObjectId.Null;   
db.TryGetObjectId(h, out id);//TryGetObjectId method
return id;
}

 

 

_________________________________________
AutoCAD .Net Laboratory
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.