Author Topic: Testing if a symbol is an atom  (Read 2673 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
Testing if a symbol is an atom
« on: February 25, 2005, 12:43:15 PM »
In lesson 3, exercise 3.2, we are asked to write a function that appends a symbol to a list. That's all very good, but what does the author mean with a symbol and, in case it means anything than a list (i.e. an atom), how do we write the function so that it ensures that only an atom will be appended?

I'll try to answer it with an example here. But first a quest: Find a function that returns T if the value of a symbol is an atom and nil if it is a list. Here are some choices:

SYMBOLP
ATOMP
ATOM
SYMB
VL-NOT-LIST
.. or none of the above?

jorgen

  • Guest
Testing if a symbol is an atom
« Reply #1 on: February 26, 2005, 11:55:53 AM »
I will abstain since I've already caused Stig to write an additional 'chapter' regarding this very issue (and he has so kindly pointed out the correct test for such a circumstance).

jorgen

  • Guest
Testing if a symbol is an atom
« Reply #2 on: February 26, 2005, 01:02:05 PM »
Well OK, since this is not the answer Stig is looking for, I'll post an alternative resulting from what  Stig finally beat into my head in the last lesson:
Code: [Select]

(defun isatom (v)
   (/= 'list (type v))
)

(defun islist (v)
   (= 'list (type v))
)


At least so far, these appear to differentiate between 'list' and 'atom' in the few tests I've tried. Of course aLisp being the quirky sub-set script-language it is, these may not work for every case (but all things being equal, they should...).

Again, this assumes that the definition of 'atom' remains as was presented, i.e. '...anything that is not a list...'.
I won't post the code these tests are used in since it was not at issue.

(and thanks again, Stig!)

jorgen

  • Guest
Testing if a symbol is an atom
« Reply #3 on: February 28, 2005, 11:39:18 PM »
I have another question...nothing to do with the above (although I actually use the 'type' test that you taught us  in the code).

How do I initialize rotate3d?

I've built a wrapper around this function (just for fun) that rotates 3D objects in real-time (like a trackball) but I find that it will crash if I call it without having used rotate3d at the command-line first, in the same session (whereupon I get the prompt *Initializing...*).

The alisp Help says it uses the saget library but I don't find any arx, vlx or lsp routines with that name in my directories so I don't know what code to load-ahead before calling the main routine.

I also looked at acad.mns to see if there were any clues there but it just calls the function the same as I would, although it may have loaded a lib prior to the call, haven't looked that close.

Any advice for doing this?

Thanks,

Tony

SMadsen

  • Guest
Testing if a symbol is an atom
« Reply #4 on: March 01, 2005, 07:13:30 AM »
I can only test this in A2K5 so it may not apply to all versions. You can run the ARX command and get a list of all modules and their commands:

Code: [Select]
Command: ARX
Enter an option [?/Load/Unload/Commands/Options]: c

Commands Registered by Extension Programs:

Command Group '*DOC(0x01185a50)'

    acad-pop-dbmod, acad-pop-dbmod
    acad-push-dbmod, acad-push-dbmod
    ACAD_COLORDLG, ACAD_COLORDLG
    ... blabla.. etc...

Command Group 'ACAD_GEOM'

    ALIGN, ALIGN
    MIRROR3D, MIRROR3D
    ROTATE3D, ROTATE3D


^There it is. I only know one way to get from the command group to the name of the ARX and that goes through the registry. Others will know better, I'm sure.
But you can get lucky by running the ARX command again, get a list of loaded ARX's (the ?-option) and associate the names with the command group. In this case, it's the geom3d.arx.

In fact, I just went into HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R16.1\ACAD-301:409\Applications\AcadGeom and checked the commands. It fit nicely.

To get a first notion of where such a command is defined, you can try check it as a symbol:

Command: !C:ROTATE3D
#<SUBR @05b5244c <EXRXSUBR>>

The EXRXSUBR type will tell you it's an ARX-defined command. Unloading the geom3d.arx and doing the above will return nil. Running the command as (C:ROTATE3D) on the command line while it is unloaded will result in an error. Depending on your demand load settings, running ROTATE3D will reload it (try unload geom3d.arx, set DEMANDLOAD to 0 and run the command. No luck. Set DEMANDLOAD to 3 and run the command. Success).

jorgen

  • Guest
Testing if a symbol is an atom
« Reply #5 on: March 01, 2005, 11:13:05 AM »
Your resourcefulness never ceases to amaze! Just the information I needed to track down erant routines to their parent libs, I do thank you for your (most) illuminating reply.

It seems it might be a good project to investigate writting a routine that                     worked something like: (while (not rotate3d) (lxload (lxfind rotate3d)) ), 'lxfind' being the call to a routine that actually did the 'digging' (or called an external routine that did).

'while' might insure that we don't just make the call and move on prematurely, in which case the subsequent call to the required function would still fail.

In fact, one could probably implement it in such a way that it simply 'cataloged' all the routines in a lookup table (after having been initialized once), in which case nothing more complicated than a simple hashed index would provide reasonably fast access to any routine that needed a lookup.

'lxload' could be a wrapper that implemented (load) or (arxload) depending on the extension of the matching routine that was found.
(I suppose one could also check the 'magic number' in the actual binary, like we do with image formats, just to be sure the extension wasn't bogus, although this being autocad, it probably wouldn't be necessary).

I can think of at least two ways to go about this after having read your reply.

But then, I'm just on a high-speed runaway, acad probably already has something like this in which case this would only be re-inventing the wheel.

But Thanks, Stig! I'll play around with it.

Tony

SMadsen

  • Guest
Testing if a symbol is an atom
« Reply #6 on: March 01, 2005, 02:27:42 PM »
You could maybe take a look at the ARX function:

(member "geom3d.arx" (arx))

jorgen

  • Guest
Testing if a symbol is an atom
« Reply #7 on: March 01, 2005, 02:49:44 PM »
>(member "geom3d.arx" (arx))
>("geom3d.arx" "oleaprot.arx" "vl.arx")

That's what I get, next step the SDK? Look for methods?
(Could also just grep for the regex in *.arx,  *.vlx and *.lsp, no SDK required, do it stand-alone regardless of version...no API dependencies)

Wait a minute, Yeah, I see...only took me about four hours for the 'light' to come on...
That would indeed test to see whether it was loaded or not...but it still doesn't locate the arx that the routine is in to begin with.

For future circumstances like this, it would be nice to just be able to 'dial it up'.

However I think I have the beginnings of a fix for that, we'll see how it goes.