Author Topic: Get ObjectId from string value  (Read 2998 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Get ObjectId from string value
« on: August 30, 2011, 06:15:03 AM »
Code: [Select]
ObjectId id = per.ObjectId;
string sid = id.ToString();//string value: (2123218520)

How to fulfill retroactive effect: to receive ObjectId on the basis of a string received by means of method ToString?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Get ObjectId from string value
« Reply #1 on: August 30, 2011, 07:09:13 AM »
Code: [Select]
Convert.ToInt32(sid.Trim(new char[] { '(', ')' }))
or

Code: [Select]
Int32.Parse(sid.Trim(new char[] { '(', ')' }))
Speaking English as a French Frog

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Get ObjectId from string value
« Reply #2 on: August 30, 2011, 07:11:39 AM »
Code: [Select]
Convert.ToInt32(sid.Trim(new char[] { '(', ')' }))
or

Code: [Select]
Int32.Parse(sid.Trim(new char[] { '(', ')' }))

ObjectId != Int32

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8704
  • AKA Daniel
Re: Get ObjectId from string value
« Reply #3 on: August 30, 2011, 07:12:07 AM »
what if its 64bit?

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Get ObjectId from string value
« Reply #4 on: August 30, 2011, 07:18:27 AM »
I need get ObjectId from string. I dont need get Int32 from string.

kaefer

  • Guest
Re: Get ObjectId from string value
« Reply #5 on: August 30, 2011, 07:28:23 AM »
I need get ObjectId from string. I dont need get Int32 from string.

I don't think that you need to get ObjectId from string. Why would anybody?

Anyway, look out  for public static explicit operator IntPtr(long value).

 F# to the rescue:
Code: [Select]
    let str = "(2123218520)"
    let (res, long) = str.Trim[|'('; ')'|] |> System.Int64.TryParse
    if res then
        new ObjectId(nativeint long)
    else
        failwith "Boom"

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Get ObjectId from string value
« Reply #6 on: August 30, 2011, 07:43:51 AM »
All thanks. I have decided to use Handle instead of ObjectId:
Code: [Select]
public string GetFullPath(Transaction t, DBObject obj) {
    string path = string.Format("{0}|{1}", obj.ObjectId.ObjectClass.Name, obj.ObjectId.Handle.Value);
    if (obj.OwnerId != ObjectId.Null && obj.OwnerId.IsValid)
        path = string.Format("{0}\\{1}", GetFullPath(t, t.GetObject(obj.OwnerId, OpenMode.ForRead)), path);
    return path;
}
Result (sample):
Quote
Full path: AcDbBlockTable|1\AcDbBlockTableRecord|31\AcDbArc|1148147
With Split method I can get from it's string:

Quote
AcDbBlockTable|1
AcDbBlockTableRecord|31
AcDbArc|1148147

And with Split again I can get class name (AcDbArc) or object handle (1148147). From handle I can get ObjectId.

BillZndl

  • Guest
Re: Get ObjectId from string value
« Reply #7 on: August 30, 2011, 11:29:15 AM »


BillZndl

  • Guest
Re: Get ObjectId from string value
« Reply #9 on: August 30, 2011, 03:12:22 PM »
You're welcome but don't thank me, thank Tony T.  :wink:



Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Get ObjectId from string value
« Reply #10 on: August 30, 2011, 11:55:53 PM »
You're welcome but don't thank me, thank Tony T.  :wink:
I have thanked for the link :)