Author Topic: Determine if Variable is ename or vla-object  (Read 4183 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Determine if Variable is ename or vla-object
« on: August 04, 2023, 02:02:24 PM »
I can't find where I put a piece of code I know I had, it would determine if something is an ename and if it is, it would convert it to a VLA-Object, otherwise it returns the object, something like:
Code: [Select]
(defun BDG_GetObject (Ent /)
(if (= Ent (Vla-IsObject Ent)
(set Obj Ent)
(setq Obj (vlax-ename->vla-object Ent))
)
Obj
)

I jsut don't remember how I handled the VLA-IsObject part.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Determine if Variable is ename or vla-object
« Reply #1 on: August 04, 2023, 02:03:50 PM »
I have tried the following without success:
Code: [Select]
(defun BDG_GetObj (Ent /) 
  (if (= (vlax-variant-type Ent) 9)   
    (setq Obj Ent)
    (setq Obj (vlax-ename->vla-object Ent))
  )
  Obj
)

ribarm

  • Gator
  • Posts: 3307
  • Marko Ribar, architect
Re: Determine if Variable is ename or vla-object
« Reply #2 on: August 04, 2023, 02:23:56 PM »
Code: [Select]
(defun BDG_GetObj (Ent / Obj)
  (if (= (type Ent) 'VLA-OBJECT)
    (setq Obj Ent)
    (if (= (type Ent) 'ENAME)
      (setq Obj (vlax-ename->vla-object Ent))
      (prompt "\nProvided argument Ent, neither ENAME, nor VLA-OBJECT")
    )
  )
  Obj
)

This seemed to me more logical...
« Last Edit: August 04, 2023, 03:47:15 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10659
Re: Determine if Variable is ename or vla-object
« Reply #3 on: August 04, 2023, 03:31:14 PM »
Localize your variables, guys.

I would do something like this (returning a thing is easier with COND than with IF):

Code - Auto/Visual Lisp: [Select]
  1. (defun BDG_GetObj (x)
  2.   (cond
  3.     ;; If thing is a VLA-Object, return it.
  4.     ((= (type x) 'VLA-OBJECT) x)
  5.     ;; If thing is a ENAME, convert it.
  6.     ((= (type x) 'ENAME) (vlax-ename->vla-object x))
  7.     ;; Anything else, just return it.
  8.     ;; Remove this condition to "error fast".
  9.     (T x)) )
(BDG_GetObj (car (entsel)))
> vla-object
(BDG_GetObj "LINE")
> "LINE"
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3307
  • Marko Ribar, architect
Re: Determine if Variable is ename or vla-object
« Reply #4 on: August 04, 2023, 03:48:38 PM »
Localize your variables, guys.

Good catch...
Localized...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Determine if Variable is ename or vla-object
« Reply #5 on: August 07, 2023, 12:35:57 PM »
No need to set "obj" IMO, just return the output.
Code - Auto/Visual Lisp: [Select]
  1. (defun bdg_getobj (ent)
  2.   (if (= 'ename (type ent))
  3.     (vlax-ename->vla-object ent)
  4.     ent
  5.   )
  6. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ribarm

  • Gator
  • Posts: 3307
  • Marko Ribar, architect
Re: Determine if Variable is ename or vla-object
« Reply #6 on: August 07, 2023, 01:24:42 PM »
Based on Ron's observation :

Code - Auto/Visual Lisp: [Select]
  1. (defun bdg_getobj (ent)
  2.   (if (= 'ename (type ent))
  3.     (vlax-ename->vla-object ent)
  4.     (if (= 'vla-object (type ent))
  5.       ent
  6.       (prompt "\nProvided argument \"ent\" neither ENAME, nor VLA-OBJECT")
  7.     )
  8.   )
  9. )
  10.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10659
Re: Determine if Variable is ename or vla-object
« Reply #7 on: August 07, 2023, 01:55:14 PM »
Isn't that what I said?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3307
  • Marko Ribar, architect
Re: Determine if Variable is ename or vla-object
« Reply #8 on: August 07, 2023, 02:29:57 PM »
Isn't that what I said?

Yes John, I just wanted to correct my version...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Determine if Variable is ename or vla-object
« Reply #9 on: August 07, 2023, 02:58:47 PM »
Just for completeness, I based mine off of John's version:
Code: [Select]
(defun BDG_GetObj (x) ;Adapted from JohnK's code at: http://www.theswamp.org/index.php?topic=58465.msg615685#msg615685
  (if (vlax-erased-p x)
    nil
    (progn
      (cond
          ;; If x is nil, return nil
          ((= x nil) nil)
          ;; If x is a VLA-Object, return it.
          ((= (type x) 'VLA-OBJECT)
            x
          )
          ;; If x is a ENAME, convert it.
          ((= (type x) 'ENAME) (vlax-ename->vla-object x))
          ;; Anything else, return nil
          ;; Remove this condition to "error fast".
          (T nil)
        )
    )
  )
)

I added coded that is the object had been erased, it would return nil and in my case, I want to return nil if it is anything besides ename or vla-object.

JohnK

  • Administrator
  • Seagull
  • Posts: 10659
Re: Determine if Variable is ename or vla-object
« Reply #10 on: August 07, 2023, 03:31:24 PM »
Simplified a bit for you:
1. You do not need the PROGN because COND is a single statement.
2. I also moved the checking to see if the object was erased inside the check to see if we've been given an object.
3. I've also removed the default condition; the only reason you would want the final condition--"(t x)"--is if you were to ever get something else besides the two choices and you didn't want to error out here but since you do want a NIL, this should return nil if neither condition is met (either an object or an ent).

Code - Auto/Visual Lisp: [Select]
  1. (defun BDG_GetObj (x)
  2.   (cond
  3.     ((= (type x) 'VLA-OBJECT)
  4.      (if (vlax-erased-p x) nil)                         ; -If x was erased, then return nil.
  5.      x                                                  ; -otherwise return x.
  6.      )
  7.     ((= (type x) 'ENAME) (vlax-ename->vla-object x))    ; -If x is an EName, convert it.
  8.     )
  9.   )

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Determine if Variable is ename or vla-object
« Reply #11 on: August 07, 2023, 04:07:04 PM »
@CMWADE .. why even have this function?
IMO the programmer should be responsible for passing the correct data type initially rather than creating a sub that checks on each iteration. Inefficient.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Determine if Variable is ename or vla-object
« Reply #12 on: August 07, 2023, 04:31:05 PM »
@CMWADE .. why even have this function?
IMO the programmer should be responsible for passing the correct data type initially rather than creating a sub that checks on each iteration. Inefficient.

In a perfect world that would be ideal; however, this is part of a much, much more complex routine that actually keeps track of the last object it drew, even after closing and reopening the drawing. Part of the issue becomes handling this if someone erased the last object drawn.

Additionally, this is part of an extremely complex routine that all totaled is about 1,100 lines of code, some of which are supporting functions done by others and some of those routines return ENAME and some return VLA-OBJECT, now I could sit here and code each one to convert ENAME to VLA-OBJECT when necessary or I can use a simple function to figure it out automatically.

And yes, I could go and rework the whole thing, but I have 70+ AutoCAD users to support and this issue was causing some major headaches, so I had to take the quickest path to the solution, even if it isn't the most efficient, in terms of this routine it causes 2-3milliseconds in delay at most, I am all for speed, but there has to be a balance, especially when writing the routines is not the only part of my job.
« Last Edit: August 07, 2023, 04:35:39 PM by cmwade77 »

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Determine if Variable is ename or vla-object
« Reply #13 on: August 07, 2023, 04:32:33 PM »
Simplified a bit for you:
1. You do not need the PROGN because COND is a single statement.
2. I also moved the checking to see if the object was erased inside the check to see if we've been given an object.
3. I've also removed the default condition; the only reason you would want the final condition--"(t x)"--is if you were to ever get something else besides the two choices and you didn't want to error out here but since you do want a NIL, this should return nil if neither condition is met (either an object or an ent).

Code - Auto/Visual Lisp: [Select]
  1. (defun BDG_GetObj (x)
  2.   (cond
  3.     ((= (type x) 'VLA-OBJECT)
  4.      (if (vlax-erased-p x) nil)                         ; -If x was erased, then return nil.
  5.      x                                                  ; -otherwise return x.
  6.      )
  7.     ((= (type x) 'ENAME) (vlax-ename->vla-object x))    ; -If x is an EName, convert it.
  8.     )
  9.   )

I did try this approach (although still with the progn, which you are right is not needed) and if the object had been erased, it would error out in AutoCAD 2024, but not in 2023 for some reason.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Determine if Variable is ename or vla-object
« Reply #14 on: August 07, 2023, 04:44:32 PM »
@CMWADE .. why even have this function?
IMO the programmer should be responsible for passing the correct data type initially rather than creating a sub that checks on each iteration. Inefficient.

In a perfect world that would be ideal; however, this is part of a much, much more complex routine that actually keeps track of the last object it drew, even after closing and reopening the drawing. Part of the issue becomes handling this if someone erased the last object drawn.

Additionally, this is part of an extremely complex routine that all totaled is about 1,100 lines of code, some of which are supporting functions done by others and some of those routines return ENAME and some return VLA-OBJECT, now I could sit here and code each one to convert ENAME to VLA-OBJECT when necessary or I can use a simple function to figure it out automatically.

And yes, I could go and rework the whole thing, but I have 70+ AutoCAD users to support and this issue was causing some major headaches, so I had to take the quickest path to the solution, even if it isn't the most efficient, in terms of this routine it causes 2-3milliseconds in delay at most, I am all for speed, but there has to be a balance, especially when writing the routines is not the only part of my job.
Fair enough .. assuming you only pass enames and vla-objects to this sub, then this should work:
Code - Auto/Visual Lisp: [Select]
  1. (defun bdg_getobj (oe)
  2.   (if oe
  3.     (cond ((and (= (type oe) 'vla-object) (null (vlax-erased-p oe))) oe)
  4.           ((vlax-ename->vla-object oe))
  5.     )
  6.   )
  7. )
« Last Edit: August 07, 2023, 04:52:32 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC