Author Topic: Get objectid from handle string  (Read 5745 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Get objectid from handle string
« on: May 03, 2009, 07:14:54 pm »
Hi All,

I'm having a bit a brain fart trying to get an object id from a handle string that I pull in from a text file the string is fine, just want to convert to an object id so I can do some work on the entities, any tips?
here's what I'm using so far but I'm getting rubbish.

Code: [Select]
               // convert the string to a valid handle:
ads_name name;
acdbHandEnt(wchandle, name);
// get the object id from the name entity:
AcDbObjectId id;
acdbGetObjectId(id, name);
acutPrintf(_T("id: %ld\n"), name[0]);

thanks,
Mick.
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

Spike Wilbury

  • Guest
Re: Get objectid from handle string
« Reply #1 on: May 03, 2009, 08:37:03 pm »
Hi All,

I'm having a bit a brain fart trying to get an object id from a handle string that I pull in from a text file the string is fine, just want to convert to an object id so I can do some work on the entities, any tips?
here's what I'm using so far but I'm getting rubbish.

Code: [Select]
               // convert the string to a valid handle:
ads_name name;
acdbHandEnt(wchandle, name);
// get the object id from the name entity:
AcDbObjectId id;
acdbGetObjectId(id, name);
acutPrintf(_T("id: %ld\n"), name[0]);

thanks,
Mick.

Mick,

It is just a minor stuff:

acutPrintf(_T("id: %ld\n"), id);

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #2 on: May 03, 2009, 08:48:17 pm »
you can also use  AcDbHandle and AcDbDatabase::getAcDbObjectId()

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #3 on: May 03, 2009, 08:49:50 pm »
Thanks Luis, I did see that, was just trying to see what was in the ads_name as I think it's supposed to be the objectid??

I'm not sure that acdbHandEnt is quite what I need but I can't spot anything else at the moment.
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #4 on: May 03, 2009, 08:52:35 pm »
acutPrintf(_T("id: %ld\n"), name[0]);
is the same as
acutPrintf(_T("id: %ld\n"), (long)(AcDbStub*)id);

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #5 on: May 03, 2009, 08:55:09 pm »
Thanks Daniel, I'll take a look at that now.
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

Spike Wilbury

  • Guest
Re: Get objectid from handle string
« Reply #6 on: May 03, 2009, 09:01:09 pm »
Thanks Luis, I did see that, was just trying to see what was in the ads_name as I think it's supposed to be the objectid??

Yes.

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #7 on: May 03, 2009, 09:21:03 pm »
Here's the code I'm using if someone wants to have a play.

Code: [Select]
void test()
{
AcDbObjectIdArray ids;
char handle[80];
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
FILE* fp = fopen("C:\\test\\handles.txt","r");
while(fgets(handle,80,fp))
{
ACHAR uStr[80]; // storage for our unicode converted string
size_t origsize = strlen(handle) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wchandle[newsize];
mbstowcs_s(&convertedChars, wchandle, origsize, handle, _TRUNCATE);
acutPrintf(_T("%s"), wchandle); // <---------works fine.
// get the object id from the name entity:
AcDbObjectId id;
AcDbHandle h = wchandle;
pDb->getAcDbObjectId(id, false, h);
acutPrintf(_T("id: %ld\n"), (long)(AcDbStub*)id);
}
fclose(fp);
}

and I write the handles out to a text file in vba using this -
Code: [Select]
Public Sub testhandles()
Dim SS1 As AcadSelectionSet
Dim Ent As AcadEntity
Dim i As Integer

Dim FilterType1(0 To 0) As Integer
Dim FilterData1(0 To 0) As Variant

FilterType1(0) = 0
FilterData1(0) = "3DSOLID"
Set SS1 = vbdPowerSet("SS1") 'let me know if you need this.
SS1.SelectOnScreen FilterType1, FilterData1
Dim file As String
Dim fileno As Integer

fileno = FreeFile
Open "C:\test\handles.txt" For Output As #fileno

If SS1.Count <> 0 Then
       For Each Ent In SS1
       Print #fileno, Ent.Handle
       Debug.Print Ent.ObjectID
       Next
End If
End Sub

just create the directory or change the paths to suit as required.
thanks.
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #8 on: May 03, 2009, 09:49:50 pm »
try this

Code: [Select]
  static void ArxDbg_doit(void)
  {
    AcDbObjectIdArray ids;
    TCHAR handle[80];
    AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
    FILE* fp = _tfopen(_T("C:/test/handles.txt"),_T("r"));
    while(_fgetts(handle,80,fp))
    {
      CString cstrHandle = handle;
      cstrHandle.Trim();
      AcDbObjectId id;
      AcDbHandle acDbHandle(cstrHandle);
      pDb->getAcDbObjectId(id, false, acDbHandle);

      if(!id.isNull())
      {
        ids.append(id);
      }
    }
    fclose(fp);

    for (int i = 0 ; i < ids.length() ; i++)
    {
      acutPrintf(_T("id: %ld\n"), (long)(AcDbStub*)ids[i]);
    }
  }
« Last Edit: May 03, 2009, 09:58:56 pm by Daniel »

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #9 on: May 03, 2009, 10:22:04 pm »
Thanks Daniel,
I'll give it a go, I was trying to avoid MFC if I could and I didn't create the project with MFC support and I can't get CString to work, what project settings do I need to bring MFC back in??
I've had a bit of a look but nothing popped out at me.
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #10 on: May 03, 2009, 11:46:28 pm »
or this

Code: [Select]
#define stringlen 80
  static void ArxDbg_doit(void)
  {
    AcDbObjectIdArray ids;
    TCHAR inhandle[stringlen];
    AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
    FILE* fp = _tfopen(_T("C:/test/handles.txt"),_T("r"));
    while(_fgetts(inhandle,80,fp))
    {
      TCHAR outhandle[stringlen];
      for (int i = 0, j = 0 ; i < stringlen  ; i++)
      {
        if(47 < inhandle[i] && inhandle[i] < 58 ||
           64 < inhandle[i] && inhandle[i] < 71 ||
           96 < inhandle[i] && inhandle[i] < 103 )
        {
          outhandle[j++] = inhandle[i];
        }
        outhandle[j] = '\0';
      }
      AcDbObjectId id;
      AcDbHandle acDbHandle(outhandle);
      pDb->getAcDbObjectId(id, false, acDbHandle);
      if(!id.isNull())
      {
        ids.append(id);
      }
    }
    fclose(fp);

    for (int i = 0 ; i < ids.length() ; i++)
    {
      acutPrintf(_T("id: %ld\n"), (long)(AcDbStub*)ids[i]);
    }
  }

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #11 on: May 04, 2009, 12:17:01 am »
You da man Dan!   :kewl:

A bit of gynastics there but the result worked perfect, thanks a bunch. :-)
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #12 on: May 04, 2009, 12:31:40 am »
My pleasure, I needed a little bit of exercise this morning  :laugh:

MickD

  • Gator
  • Posts: 2899
  • I don't need a job, I need Money!!
Re: Get objectid from handle string
« Reply #13 on: August 10, 2016, 11:14:53 pm »
I've just fixed a subtle bug that has been a sporadic problem over the years. It seems that creating a handle from the string value is not reliable, to fix it I convert the string to an int (which is really what a hex value is anyway) and all is good in the world again :)

Here's the change:

Code - C++: [Select]
  1. #define stringlen 80
  2. static void ArxDbg_doit(void)
  3. {
  4.        AcDbObjectIdArray ids;
  5. TCHAR inhandle[stringlen];
  6. AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
  7. FILE* fp = _tfopen(_T("C:/test/handles.txt"), _T("r"));
  8.  
  9. while (_fgetts(inhandle, 80, fp))
  10. {
  11. Adesk::UInt64 hexnum = std::stoi(inhandle, 0, 16);//<<-------ADDED
  12. AcDbHandle acDbHandle(hexnum);
  13.  
  14. AcDbObjectId id;
  15. ErrorStatus es = pDb->getAcDbObjectId(id, false, acDbHandle);
  16. if (es == eOk)
  17. {
  18. ids.append(id);
  19. }
  20. if(es == eUnknownHandle){
  21. acutPrintf(_T("Error: eUnkownHandle"));
  22. }
  23. }
  24. fclose(fp);
  25.  
  26.    for (int i = 0 ; i < ids.length() ; i++)
  27.    {
  28.      acutPrintf(_T("id: %ld\n"), (long)(AcDbStub*)ids[i]);
  29.    }
  30.  }
  31.  
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

nullptr

  • BricsCAD
  • Needs a day job
  • Posts: 6729
  • AKA Daniel
Re: Get objectid from handle string
« Reply #14 on: August 15, 2016, 08:11:24 am »
***runs off to review all posted code***