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

Convert string to ObjectId

7 REPLIES 7
Reply
Message 1 of 8
mabe2k11
8674 Views, 7 Replies

Convert string to ObjectId

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.
7 REPLIES 7
Message 2 of 8
mabe2k11
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!
Message 3 of 8
tumpliner
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.
Message 4 of 8
chiefbraincloud
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
Message 5 of 8
Anonymous
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.
Message 6 of 8
Anonymous
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.
Message 7 of 8
uzrgm
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

Message 8 of 8
Andrey_Bushman
in reply to: Anonymous

 

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;
}

 

 

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