• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 24
    Registered: ‎03-14-2006

    Convert string to ObjectId

    1013 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.
    Please use plain text.
    Contributor
    Posts: 24
    Registered: ‎03-14-2006

    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!
    Please use plain text.
    Valued Contributor
    Posts: 50
    Registered: ‎02-07-2007

    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.
    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    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
    Please use plain text.
    *Tony Tanzillo

    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.
    Please use plain text.
    *Tony Tanzillo

    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.
    Please use plain text.
    New Member
    uzrgm
    Posts: 1
    Registered: ‎08-15-2011

    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

    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎01-18-2009

    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
    Please use plain text.