Code Red > AutoLISP (Vanilla / Visual)

(code) Is object this type

(1/1)

Mark:
All comments welcome.

--- Code: ---;;; 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))
    )
  )

--- End code ---

Columbia:
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: ---
(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
)

--- End code ---

Mark:
Another try:

--- Code: ---
(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)
            )
      )
    )
  )

--- End code ---

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 (......

Columbia:
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: ---
(eval (= (strcase (vlax-get-property obj 'ObjectName)) objtype))

--- End code ---

is equal to...

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

--- End code ---

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:

--- Quote from: Columbia ---It's just a different way to skin the same cat.
--- End quote ---

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: ---
(eval (= (strcase (vlax-get-property obj 'ObjectName)) objtype))

--- End code ---

is equal to...

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

--- End code ---

they both return the same value.

--- End quote ---

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.
--- End quote ---

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.

Navigation

[0] Message Index

Go to full version