Author Topic: (code) Is object this type  (Read 4791 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(code) Is object this type
« on: November 12, 2003, 03:55:15 PM »
All comments welcome.
Code: [Select]
;;; determines if the argument 'obj' is of the type 'objtype'
;;; returns T or nil
;;; example
;;; (setq obj (vlax-ename->vla-object (car (entsel "Select a LINE: "))))
;;; (if (= (objname obj "AcDbLine"))(Alert "You have done this before"))
;;; arguments
;;; obj = VLA-OBJECT
;;; objtype = string
(defun objname (obj objtype)
  (if (= (type obj) 'VLA-OBJECT)
    (eval (= (vlax-get-property obj 'ObjectName) objtype))
    )
  )
TheSwamp.org  (serving the CAD community since 2003)

Columbia

  • Guest
(code) Is object this type
« Reply #1 on: November 13, 2003, 10:54:06 AM »
The ony modifications I would make are:
1. use a  (strcase) so that when the user passes the objtype argument it doesn't matter how he typed it.
2. make it be able to take DXF data and enames as well.

sorta like this...
Code: [Select]

(defun objname (obj objtype / dxfobj rtn)
  (vl-load-com)
  (if (vl-string-search "ACDB" (strcase objtype))
    (setq dxfobj "VL")
    (setq dxfobj "DXF")
  )
  (cond
    ( (= dxfobj "VL")
      (setq obj (if (= (type obj) 'VLA-OBJECT) obj (vlax-ename->vla-object obj))
            rtn (eval (= (strcase (vlax-get-property obj 'ObjectName)) (strcase objtype)))
      )
    )
    ( (= dxfobj "DXF")
      (setq obj (if (= (type obj) 'VLA-OBJECT) (vlax-vla-object->ename obj) obj)
            rtn (eval (= (strcase (cdr (assoc 0 (entget ename)))) (strcase objtype)))
      )
    )
  )
  rtn
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(code) Is object this type
« Reply #2 on: November 14, 2003, 09:09:47 AM »
Another try:
Code: [Select]

(defun objname (obj objtype)
  (vl-load-com) ;<-- i always forget this :/
  (if (= (type objtype) 'STR)
    (progn
      (setq objtype (strcase objtype))
      (cond ((= (type obj) 'ENAME)
             (eval (= (cdr (assoc 0 (entget obj))) objtype))
             )
            ((= (type obj) 'VLA-OBJECT)
             (setq objtype (strcat "ACDB"objtype))
             (eval (= (strcase (vlax-get-property obj 'ObjectName)) objtype))
             )
            (T nil)
            )
      )
    )
  )

Pehaps it's my lack of formal education in programming, but I don't
understand why you would assign a variable to the 'eval' function and
return it for the output of the program. i.e. as Columbia did in his
previous post.
(setq rtn (eval (= (strcase (......
TheSwamp.org  (serving the CAD community since 2003)

Columbia

  • Guest
(code) Is object this type
« Reply #3 on: November 14, 2003, 09:25:33 AM »
It's just a different way to skin the same cat.  Acutally, in this case you don't necessarily have to run an eval on the test condition either.
Code: [Select]

(eval (= (strcase (vlax-get-property obj 'ObjectName)) objtype))

is equal to...
Code: [Select]

(= (strcase (vlax-get-property obj 'ObjectName)) objtype)

they both return the same value.

And just as a side note, one thing you may or may not want to consider is that the user of this function could supply the wrong type definition to the wrong type of object.  What I mean is that the user could call the function like this...
(objname <ename> "AcDbLine") and this would return an error where
(objname <vla-object> "LINE") would not.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(code) Is object this type
« Reply #4 on: November 14, 2003, 10:03:40 AM »
Quote from: Columbia
It's just a different way to skin the same cat.

So we're talking readability here?
Quote
Acutally, in this case you don't necessarily have to run an eval on the test condition either.
Code: [Select]

(eval (= (strcase (vlax-get-property obj 'ObjectName)) objtype))

is equal to...
Code: [Select]

(= (strcase (vlax-get-property obj 'ObjectName)) objtype)

they both return the same value.

True, I didn't think of that.
Quote

And just as a side note, one thing you may or may not want to consider is that the user of this function could supply the wrong type definition to the wrong type of object.  What I mean is that the user could call the function like this...
(objname <ename> "AcDbLine") and this would return an error where
(objname <vla-object> "LINE") would not.

And that brings brings me to another question. How much error checking do you add to a subroutine like this? Obviously if I'm the only one using it then I know what it can and cannot do. But since I made it public I should add as much error checking as possible as well as comments. That's probably the prudent thing to do, not to mention professional. :D

thanks for the comments.
TheSwamp.org  (serving the CAD community since 2003)