.NET
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
983 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.
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.
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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!
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!
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
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. 

*Tony Tanzillo
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.ht m
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.
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.ht
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
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
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.ht m
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.ht m
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.
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.ht
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
"Tony Tanzillo"
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.ht
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");
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.
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
Re: Convert string to ObjectId
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
AutoCAD .Net Laboratory
