Jump to content

Set variable in acaddoc.lsp??


lfe011969

Recommended Posts

So as I continue to grow as a lisp writer, something got stuck in my head last night concerning putting a variable in the acaddoc.lsp so that it is always loaded. The specific example I'm referring to is the variable you would normally assign to access the document object, i.e.:

 

(setq adoc (vla-get-activedocument (vlax-get-acad-object)))

I had already used this to access Layers in a layer loading lisp I wrote and I had started to go back and add an error handler to all the lisps I've written when it occurred to me, can't I just establish this variable in my acaddoc.lsp so that I don't have to define the entire variable when I establish my start undo mark?

 

Right now the very generic error handling code I'm adding to each of my lisps looks like this:

 

;; Error handler 
;; From VVA on the forums at [url="http://www.cadtutor.net"]www.cadtutor.net[/url]
(defun *error* (msg))
 (vla-endundomark adoc)
)

(vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))

PROGRAM CODE
    |
    |
    |
END PROGRAM CODE

(vla-endundomark adoc)

So in the above code the line:

 

(vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))

would be replaced with:

 

(vla-startundomark adoc)

This is not a matter of being lazy and wanting to avoid typing out a longer piece of code. I was just copying and pasting the code anyways. No it just ocurred to me that this variable could possibly be used hundreds of times in a lisp collection so I was just thinking along the lines of this being more efficient.

 

Of course I could just be talking out of the blue :shock:

Edited by Tiger
removed stars
Link to comment
Share on other sites

Here is what I do: I have a lisp file with all my common custom functions on it that can be called from any of my lisp and I have it loaded when a drawing is opened. So every time you find yourself writing a custom function like retrieving polyline vertices or even error trapping put it in the main file then you can call it from any new lisp that you create.

Link to comment
Share on other sites

RenderMan, this link was really helpful! I really don't know enough about the VLIDE and didn't really know anything about it's project capabilities so this info has opened my eyes to what I can do. Thanks!

 

 

You're welcome; just trying to share the knowledge. :wink:

Link to comment
Share on other sites

Also, consider using something like this for many of your global variables, so that you don't repeatedly call the same vla-objects:

 

;; Autocad application object
(cond
 (*acadApp*)
 ((setq *acadApp* (vlax-get-acad-object))))

;; Active document object
(cond
 (*activeDoc*)
 ((setq *activeDoc* (vla-get-activedocument *acadApp*))))

;; ... etc.

Link to comment
Share on other sites

Also, consider using something like this for many of your global variables, so that you don't repeatedly call the same vla-objects:

 

;; Autocad application object
(cond
 (*acadApp*)
 ((setq *acadApp* (vlax-get-acad-object))))

;; Active document object
(cond
 (*activeDoc*)
 ((setq *activeDoc* (vla-get-activedocument *acadApp*))))

;; ... etc.

Agreed. I actually start our acaddoc.LSP off with:

(VL-LOAD-COM)
(setq *AcadDoc* (vla-get-activedocument (setq *Acad* (vlax-get-acad-object))))

Link to comment
Share on other sites

Agreed. I actually start our acaddoc.LSP off with:

(VL-LOAD-COM)
(setq *AcadDoc* (vla-get-activedocument (setq *Acad* (vlax-get-acad-object))))

 

 

In other threads I've described this, but we have ACADDOC.lsp mapped to our personal network space, so that any office we go to, our settings, and customizations are available. That said, all I have in our ACADDOC.lsp is (vl-load-com), and a load statement for our personal settings, which conditionally loads if the drawing is titled, but can be invoked via (c:LPS) if starting a new drawing. This avoids the "do you want to save Drawing1.dwg" prompt at startup > open.

 

I store my global vars (certain vla-objects, directories conditionally mapped from the active drawing's current location, layer states, etc.) via a routine that loads with the personal settings (i:vars), as part of my "Code Tools" project, which provides the dependencies needed for my production tools (both functions, and vars).

 

Of course, I've put all improvements on hold temporarily, as regionally we're now rolling out all new directory structures, servers, etc... compounding the matter is the fact that I am both having to adopt an FDOT compliant directory structure, but also cross-training on MicroStation V8i/Civil 3D 2011 at the same time (from Land Desktop Companion). lol

Link to comment
Share on other sites

RenderMan,

 

Thanks to your suggestions I've started adding some global variables to my acaddoc.lsp. You've got me thinking "outside the box" now so you're partly to blame if I work on my lisp files today instead of my drawings that are due next week, lol :whistle:

 

Seriously though, I do have one question about this code you posted:

 

(cond
(*acadApp*)
((setq *acadApp* (vlax-get-acad-object))))

 

 

Why do you use a COND in establishment of your variables? Doesn't this code accomplish the same thing:

(setq *acadApp* (vlax-get-acad-object))

Link to comment
Share on other sites

RenderMan,

 

Thanks to your suggestions I've started adding some global variables to my acaddoc.lsp. You've got me thinking "outside the box" now so you're partly to blame if I work on my lisp files today instead of my drawings that are due next week, lol :whistle:

 

 

If I have in any way contributed to expanding your understanding, and/or imagination, then I consider that a compliment. Thanks!

 

Now, as for remaining on task, that is between you and your employer. o:)

 

Seriously though, I do have one question about this code you posted:

 

(cond
 (*acadApp*)
 ((setq *acadApp* (vlax-get-acad-object))))

 

 

Why do you use a COND in establishment of your variables? Doesn't this code accomplish the same thing:

(setq *acadApp* (vlax-get-acad-object))

 

 

The purpose of the cond statement is to mitigate repeated calls for the same object(s). By using the cond in this way, the object's global var is defined the first time through, and in subsequent calls is already define, and simply returns the object.

 

Another option using the cond statement provided, would be to not define all of your objects with ACADDOC.lsp at all, and instead create toolbox functions (sub-functions) which return the desired object:

 

(defun i:acadApp ()
 (cond
   (*acadApp*)
   ((setq *acadApp* (vlax-get-acad-object)))))

 

 

... Where you could write a production tool function, and where this object is required, simply add the toolbox function:

 

(defun c:ZoomCentered  (/ pt)
 (vl-load-com)             [color=seagreen]; <- This I would add to ACADDOC.lsp[/color]
 (if (setq pt (getpoint "\n  >>  Pick Point to Zoom: "))
   (vla-zoomcenter
[color=red]      (i:acadApp)[/color]
     (vlax-3d-point pt)
     (* 4. (getvar 'dimscale))))
 (princ))

;;; This routine is for example purposes only.

 

 

... Ahhh, yes. The many things that are possible! :)

 

But wait... How do you apply this same methodology to dependent objects, like the ActiveDocument, ModelSpace, etc.!?

 

Easy... You've got toolbox functions for that. :geek:

 

For example:

 

(defun i:activeDoc ()
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument [color=red](i:acadApp)[/color])))))

 

 

(defun i:modelSpace ()
 (cond
   (*modelSpace*)
   ((setq *modelSpace* (vla-get-modelspace [color=red](i:activeDoc)[/color])))))

 

 

... Is all this making sense?

 

... Or did I just:

 

AUGI_BlewMyMind.JPG

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...