Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Load Lisp File and Run Program Inside a Condition Does Not Work

12 REPLIES 12
Reply
Message 1 of 13
kpennell
1787 Views, 12 Replies

Load Lisp File and Run Program Inside a Condition Does Not Work

Hey all,

 

I sincerly expected this to work.

 

(setq ListOfCommands (dos_multilist "List of Commands" "Select Commands" '("EP" "UB")))

 

(cond
((= "UB" (car (member "UB" ListOfCommands)))        (Alert "UB")    (load "W:\\Lisp\\UB.vlx")        (C:UB)        (princ))

)

 

The condition is met, becasue i get the proper "Alert", but the file does not load and the command for the file does not get performed.  I guess I'm not conforming to a rule.  It prompts me that the "LISP command is not available."

 

Any ideas?

 

KP

 

 

12 REPLIES 12
Message 2 of 13
kpennell
in reply to: kpennell

Writing the condition like this solves the problem, but I'm sure there is a more proper way.

 

(cond
((= "UB" (car (member "UB" ListOfCommands)))        (Alert "UB")    (load "W:\\Lisp\\Assemblies\\GDI-Production-UB.vlx")(load "W:\\Lisp\\Assemblies\\GDI-Production-UB.vlx")        (C:UB)        (princ))
)

 

Message 3 of 13
Lee_Mac
in reply to: kpennell

If the VLX has been compiled to run in its own separate namespace, then the functions defined within the VLX will not be available outside of that namespace.

 

If the command defined by the VLX has been registered to the AcEdCommandStack object (using vlax-add-cmd - and in fact, this is recommended for VLX files compiled to a separate namespace), then you may be able to evaluate the command as a standard AutoCAD command using either the command or vl-cmdf functions, e.g. (command "UB")

 

Alternatively, specific functions may be exported from a separate namespace VLX using the vl-doc-export function, which makes the function available to be evaluated from the document namespace. When exposed to the document namespace, the function may then be evaluated from an AutoLISP program running in the document namespace using (c:UB)

 

I hope this helps!

 

Lee

Message 4 of 13
kpennell
in reply to: Lee_Mac

this is way over my head.  I'll have to dig into this though.

 

Thanks LM

KP

Message 5 of 13
kpennell
in reply to: Lee_Mac

okay, so already done some digging.

 

Where might one build the code for this way of adding commands?  acaddocXX.lsp file?

 

Message 6 of 13
Lee_Mac
in reply to: kpennell


@kpennell wrote:

okay, so already done some digging.

 

Where might one build the code for this way of adding commands?  acaddocXX.lsp file?


It would need to be performed from within the VLX namespace, hence, if you do not have access to the code comprising the VLX, you may be out of luck.

 

Lee

Message 7 of 13
mid-awe
in reply to: kpennell

Isn't this the same as?

(autoload "W:\\Lisp\\UB.vlx" '("EP" "UB"))
Message 8 of 13
kpennell
in reply to: Lee_Mac

I've made a VLX out of every command (which is it's own file), so I do have access to it.

 

An example I drummed up from what I understand is the following:

 

(defun 180 ()
    (setq ss (ssget))
    (prompt "\nSelect Base Point..." )
    (setq gp (getpoint))
    (command "rotate" ss "" gp "180")
    (prin1)
)

_$ (vlax-add-cmd "180" '180 "180" 1)

 

But when I try to load it, it prompts "; error: syntax error"

 

I had difficulty with the word "namespace" (dumbed down, it's a drawing) but I understand that now too, or so I think.

Message 9 of 13
Lee_Mac
in reply to: kpennell


@kpennell wrote:

I've made a VLX out of every command (which is it's own file), so I do have access to it.


 

Oh right - in that case, you should be able to simply compile the LISP program to a VLX without choosing the 'Separate Namespace' option, and the functions should be available to be called from the document namespace when the VLX is loaded.

 


@kpennell wrote:

I had difficulty with the word "namespace" (dumbed down, it's a drawing) but I understand that now too, or so I think.


 

Essentially a namespace is an allocation of memory in which symbols can be defined (as functions, variables, constants etc.), a 'container' if you like. In this way, the scope of defined symbols is limited to the namespace in which they are defined - i.e. symbols defined in one namespace cannot interact or interfere with those defined in another namespace. As a result, the same symbol name can hold different values in different namespaces, without clashing.

 

Since AutoLISP runs in the document or drawing namespace, the set of functions & symbols defined when a drawing is open are lost when the drawing is closed, and are also not available in other open drawings since each drawing has its own namespace. Visual LISP also provides the functions vl-load-all & vl-propagate for defining functions & symbols across multiple document namespaces.

 

There is also the blackboard namespace which is a namespace existing within the application but separate from any open drawings. Symbols can be defined in the blackboard namespace can be accessed using the vl-bb-set & vl-bb-ref functions.

 

Furthermore, as I have noted above, you can compile a VLX to a separate namespace - that is, rather than the functions contained in the VLX being defined in the document namespace along with any other AutoLISP functions or symbols which happen to be defined, the VLX file can inhabit its own separate namespace, isolated from the document namespace.

 

However, the VLX can still interact with the document namespace and define symbols in this namespace using the vl-doc-set & vl-doc-ref functions -  this is analogous to using the vl-bb-set & vl-bb-ref functions to interact with the blackboard namespace from the document namespace.

 

And, as mentioned above, functions defined in the VLX namespace can be made available to the document namespace using the vl-doc-export function, and similarly imported from the document namespace to the VLX namespace using the vl-doc-import function.

 

I hope this helps with your understanding of the concept of a namespace.

 

Lee

Message 10 of 13
kpennell
in reply to: Lee_Mac

I certainly have a better understanding.

 

I'm using vl-bb-set and vl-bb-ref now in some codes, to prompt the user what commands they want to use in certain drawings.  I tried the scriptpro process, and it just wasn't intuitive for the regular users.  People are familiar with the command prompt.  Its rare to find people dig into other ways of doing things, like scrippro or even the publish command.  When you got CAD users that just use CAD,

 

So when I use the vl-bb-ref, and try to call the commands, that's where it fails, until for some reason I asked to load the file twice, then it worked (as per my previous post).  But I am intrigued and think it's worth my while to put all of my commands to "vlax-add-cmd".  It will be easier to call commands, and I guess, a lot more reliable.

 

So my question now is, what about my "vlax" string causes a syntax error?

 

Message 11 of 13
Lee_Mac
in reply to: kpennell


@kpennell wrote:

But I am intrigued and think it's worth my while to put all of my commands to "vlax-add-cmd". It will be easier to call commands, and I guess, a lot more reliable.

 

So my question now is, what about my "vlax" string causes a syntax error?


 Personally, I tend to use vlax-add-cmd reservedly, and only when necessary as it can lead to subtle differences in the behaviour of the command when evaluated from LISP, for example, as noted in the documentation:

 

Warning: You cannot use the command function call in a transparently defined vlax-add-cmd function. Doing so can cause AutoCAD to close unexpectedly.

 

However, to answer your question: you are attempting to redefine the number 180 as a function - this will fail regardless of the vlax-add-cmd expression.

 

Here is an example to demonstrate how vlax-add-cmd may be used:

 

 

(defun myfun ( / ss gp )
    (if
        (and
            (setq ss (ssget))
            (setq gp (getpoint "\nBase point: "))
        )
        (command "_.rotate" ss "" "_non" gp "180")
    )
    (prin1)
)
(vlax-add-cmd "myfun" 'myfun "myfun" acrx_cmd_modal)

 

Message 12 of 13
kpennell
in reply to: Lee_Mac

It worked when I did this... very interesting.

 

(defun r180 ()
    (setq ss (ssget))
    (prompt "\nSelect Base Point..." )
    (setq gp (getpoint))
    (command "rotate" ss "" gp "180")
    (prin1)
)

_$ (vlax-add-cmd "180" 'r180 "180" 1)

Message 13 of 13
Lee_Mac
in reply to: kpennell


@kpennell wrote:

It worked when I did this... very interesting.

 

(defun r180 ()


Well yes, because now you are defining the symbol 'r180' and not trying to redefine the number 180 Smiley Wink

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Autodesk Design & Make Report