C# .net how do I convert "object ID string" to objectID?

C# .net how do I convert "object ID string" to objectID?

jeff.wangD95HG
Collaborator Collaborator
748 Views
6 Replies
Message 1 of 7

C# .net how do I convert "object ID string" to objectID?

jeff.wangD95HG
Collaborator
Collaborator

i used another command to grab an object's BlockID and outputted it on a table in autocad.

blocks[i].BlockId.ToString();

 

now I want to be able to take that string and convert it back to objectID that I can use on another command. How would I do that? I already wrote the part that parses the "objectID string" out of the table but I don't know how to convert that string back to objectID

 

I tried reading the documentation about objectID but I am not sure if any of those does what I wanted to do.

AutoCAD 2025 Developer and ObjectARX Help | ObjectId Properties | Autodesk

 

0 Likes
Accepted solutions (1)
749 Views
6 Replies
Replies (6)
Message 2 of 7

ActivistInvestor
Mentor
Mentor

See if this works:

 

string IdString = // assign to the string representation of the ObjectId

ObjectId id = new ObjectId(IntPtr.Parse(idString));
0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor

By saying "... outputted it on a table in autocad...", you do mean a table in the drawing, don't you? Does the operation of outputting ObjectId string to table in the drawing and then reading back as ObjectId ONLY IN THE SAME AutoCAD session (that is, you do not save the ObjectID string, and then the drawing is closed, then it is opened again and the code tries to convert the string as a valid ObjectId)? If the data has to be dealt with crossing drawing openning session, you should use Handle instead of ObjectId, because handle is persisted with the drawing, while ObjectId is not, meaning the same entity may have different ObjectId in different AutoCAD/drawing session, but the Handle is the same.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

jeff.wangD95HG
Collaborator
Collaborator

@norman.yuan 
yea you are right. I read that up somewhere before and forgot about it. this is the current output I have generated

jeffwangD95HG_0-1744397125789.png

so what would handle look like? something like 8E48?

 

and the same question stands then. If I were to generate the handle into a string then how would I turn it back into a handle?

0 Likes
Message 5 of 7

ActivistInvestor
Mentor
Mentor
Accepted solution

Unless the ObjectId strings are embedded into a field expression (e.g., <AcObjProp>...), you can't use ObjectIds as a persistent reference to an objects. IOW, if your table is simply text that contains the string representation of an ObjectId, that does not work. ObjectIds are not persistent. You have to use Handles in that case. You can generate a string representation of a Handle using the Handle's ToString() method.

 

Given a string representing a handle in hex notation, this will produce an ObjectId:

static ObjectId TryGetObjectId(this Database db, string handle)
{
   if(db is null)
      throw new ArgumentNullException(nameof(db));
   if(string.IsNullOrWhiteSpace(handle))
      return ObjectId.Null;
   try
   {
      return db.GetObjectId(false, new Handle(Convert.ToInt64(handle, 16)), 0);
   }
   catch(Autodesk.AutoCAD.Runtime.Exception ex) 
   {
      return ObjectId.Null;
   }
}

 

0 Likes
Message 6 of 7

jeff.wangD95HG
Collaborator
Collaborator

I was wondering about that too. Since I did some played around with some fields expressions referencing tables and found that it uses objectID and it remains persistent across sessions.

0 Likes
Message 7 of 7

ActivistInvestor
Mentor
Mentor

Yes, as I mentioned above, when storing Ids in field expressions, AutoCAD takes care of the bookkeeping (id translation and conversion to/from handles for saving), but that doesn't apply to simple text content that happens to contain a number, because AutoCAD doesn't recognize that as a reference to an Object in the drawing.

0 Likes