Author Topic: Need help with Object id  (Read 5579 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Need help with Object id
« on: June 11, 2011, 04:15:50 PM »
Hello everyone .

Is it normal when I am trying to get the object id with the following codes and it returns the number with two digits ?

Code: [Select]
(setq ss (vlax-ename->vla-object (car (entsel "\n Select a circle :"))))
(setq id (vla-get-Objectid ss))

Returns :
Quote
#<VLA-OBJECT IAcadCircle 0000000026dee198>
47

Thanks

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Need help with Object id
« Reply #1 on: June 11, 2011, 04:29:45 PM »
Sure.
What happens when you reverse the process?
Code: [Select]
(setq newobj (vla-objectidtoobject (vla-get-activedocument (vlax-get-acad-object)) id))
This should return the original vla-object.

Coder

  • Swamp Rat
  • Posts: 827
Re: Need help with Object id
« Reply #2 on: June 11, 2011, 04:34:49 PM »
Thanks Jeff .

This is what return after implementing your codes .. :-o

Quote
; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on unknown exception

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Need help with Object id
« Reply #3 on: June 11, 2011, 04:43:36 PM »
From the length of the object code it looks like you are using a 64-bit system.

For 64-bit systems use either the ObjectID32 property of the object, or the GetObjectIDString method of the Utility object.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Need help with Object id
« Reply #4 on: June 11, 2011, 04:49:49 PM »
Oops, forgot about that, Lee.

Coder, have a look at THIS thread for some talk about the 32/64 bit implementations of this.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Need help with Object id
« Reply #5 on: June 11, 2011, 05:13:04 PM »
I doubt I'll ever forget it again after the troubles it caused me with that thread ^^ :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: Need help with Object id
« Reply #6 on: June 11, 2011, 05:26:14 PM »
A very interesting thread Jeff that you brought . Thank you so much .

I got the correct id with the following codes .

Code: [Select]
(setq id (vlax-invoke-method (vla-get-Utility  (vla-get-ActiveDocument (vlax-get-acad-object)))
  'GetObjectIdString   ss  :vlax-false )
       )
Am I on the right track ?

Many thanks.  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: Need help with Object id
« Reply #7 on: June 12, 2011, 02:26:39 PM »
I am still facing a problem which cause the routine not to do the trick . :-(

This code does not work and returns two digits as the function vla-get-objectid .
Code: [Select]
(vla-get-objectid32 vla-obj)
Finally this routine returns error with Field code and something wrong that I could not deal with , Any help with it Please ?

Code: [Select]
(vl-load-com)
(defun c:test (/ ss i sselection vla-obj id pt)
  (if (setq ss (ssget "_:L" '((0 . "CIRCLE"))))
    (repeat
      (setq i (sslength ss))
       (setq sselection (ssname ss 0))
       (setq vla-obj (vlax-ename->vla-object sselection))
       (setq id (vlax-invoke-method
                  (vla-get-Utility
                    (vla-get-ActiveDocument (vlax-get-acad-object))
                  )
                  'GetObjectIdString
                  vla-obj
                  :vlax-false
                )
       )
       (setq pt (cdr (assoc 10 (entget sselection))))
       (vla-addMText
         (vla-get-ModelSpace
           (vla-get-ActiveDocument (vlax-get-acad-object))
         )
         (vlax-3d-point pt)
         2.
         (strcat ("%<\\AcObjProp Object(%<\\_ObjId"
                   (atoi id)
                   ">%).Area \\f \"%lu2\">%"
                 )
         )
       )
    )
    (princ)
  )
  (princ)
)


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Need help with Object id
« Reply #8 on: June 12, 2011, 02:38:54 PM »
Hi,

The GetObjectIdString method returns a string and you use it with atoi which requires an integer: (atoi id)

By my side I use a function which return the objectId as string and works whatever the AutoCAD version (32 ou 64 bits):

Code: [Select]
(defun gc:GetObjectIdString (obj)
  (or *util*
      (setq *util* (vla-get-Utility (vla-get-ActiveDocument (vlax-get-acad-object))))
  )
  (if  (vlax-method-applicable-p *util* 'GetObjectIdString)
    (vla-GetObjectIdString *util* obj :vlax-false)
    (itoa (vla-get-ObjectId obj))
  )
)

Using example in a field code:

Code: [Select]
         (strcat ("%<\\AcObjProp Object(%<\\_ObjId"
                   (gc:GetObjectIdString vla-obj)
                   ">%).Area \\f \"%lu2\">%"
                 )
         )
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Need help with Object id
« Reply #9 on: June 13, 2011, 03:12:27 PM »
Thanks gile .

But I am still suffering of this thread because I could not pass the error that occur form the field .

The is the return error ...
Quote
; error: bad function: "%<\\AcObjProp Object(%<\\_ObjId"

Code: [Select]
(vl-load-com)
(defun gc:GetObjectIdString (obj)
  (or *util*
      (setq *util* (vla-get-Utility  (vla-get-ActiveDocument (vlax-get-acad-object)))))
  (if (vlax-method-applicable-p *util* 'GetObjectIdString)
    (vla-GetObjectIdString *util* obj :vlax-false)
    (itoa (vla-get-ObjectId obj))))
(defun c:test (/ ss i sselection vla-obj id pt)
  (if (setq ss (ssget "_:L" '((0 . "CIRCLE"))))
    (repeat
      (setq i (sslength ss))
       (setq sselection (ssname ss (setq i (1- i))))
       (setq vla-obj (vlax-ename->vla-object sselection))
       (setq pt (cdr (assoc 10 (entget sselection))))
       (vla-addMText (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object)))
         (vlax-3d-point pt)
         2.
         (strcat ("%<\\AcObjProp Object(%<\\_ObjId"
                   (gc:GetObjectIdString vla-obj)
                   ">%).Area \\f \"%lu2\">%"
                 ))))
    (princ))
  (princ))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Need help with Object id
« Reply #10 on: June 13, 2011, 03:29:18 PM »
Maybe something like this:

Code: [Select]
(vl-load-com)

(defun gc:GetObjectIdString ( obj )
  (or *util* (setq *util* (vla-get-Utility  (vla-get-ActiveDocument (vlax-get-acad-object)))))
  (if (vlax-method-applicable-p *util* 'GetObjectIdString)
    (vla-GetObjectIdString *util* obj :vlax-false)
    (itoa (vla-get-ObjectId obj))
  )
)

(defun c:test ( / spc ss i en )
  (setq spc (vla-get-modelspace (vla-get-ActiveDocument (vlax-get-acad-object))))
 
  (if (setq ss (ssget "_:L" '((0 . "CIRCLE"))))
    (repeat (setq i (sslength ss))
      (setq en (ssname ss (setq i (1- i))))
      (vla-addMText spc (vlax-3d-point (cdr (assoc 10 (entget en)))) 2.
        (strcat "%<\\AcObjProp Object(%<\\_ObjId " (gc:GetObjectIdString (vlax-ename->vla-object en)) ">%).Area \\f \"%lu2\">%")
      )
    )
  )
  (princ)
)

A few pointers:

  • Don't use (vlax-get-acad-object) inside a loop, its slow and is bad practice.
  • Your Field string was missing a space after the ObjId

Coder

  • Swamp Rat
  • Posts: 827
Re: Need help with Object id
« Reply #11 on: June 13, 2011, 03:36:16 PM »
Great .  8-)

Thank you so much for your time Lee , and the others also as well .

Appreciated a lot. :-)


Maybe something like this:

Code: [Select]
(vl-load-com)

(defun gc:GetObjectIdString ( obj )
  (or *util* (setq *util* (vla-get-Utility  (vla-get-ActiveDocument (vlax-get-acad-object)))))
  (if (vlax-method-applicable-p *util* 'GetObjectIdString)
    (vla-GetObjectIdString *util* obj :vlax-false)
    (itoa (vla-get-ObjectId obj))
  )
)

(defun c:test ( / spc ss i en )
  (setq spc (vla-get-modelspace (vla-get-ActiveDocument (vlax-get-acad-object))))
 
  (if (setq ss (ssget "_:L" '((0 . "CIRCLE"))))
    (repeat (setq i (sslength ss))
      (setq en (ssname ss (setq i (1- i))))
      (vla-addMText spc (vlax-3d-point (cdr (assoc 10 (entget en)))) 2.
        (strcat "%<\\AcObjProp Object(%<\\_ObjId " (gc:GetObjectIdString (vlax-ename->vla-object en)) ">%).Area \\f \"%lu2\">%")
      )
    )
  )
  (princ)
)

A few pointers:

  • Don't use (vlax-get-acad-object) inside a loop, its slow and is bad practice.
  • Your Field string was missing a space after the ObjId