Jump to content

LISP:Obtain Coordinates of a point & the zoom magnification and save to script file


vernonlee

Recommended Posts

Could some help me with a LISP.

 

Basically a LISP to

 

1) obtain the Coordinates of a user input point &

2) the zoom magnification of the same point and

3) save the values to an existing file & override it: ZoomToView.scr

 

This is the individual command I run to obtain the values for item 1 & 2

Command: [b]ID[/b]
Specify point:  X = [color=red][b]540.4345[/b][/color]     Y = [b][color=red]166.2423[/color][/b]     Z = [b][color=red]0.0000[/color][/b]

Command: [b]Z[/b]
ZOOM
Specify corner of window, enter a scale factor (nX or nXP), or
[All/Center/Dynamic/Extents/Previous/Scale/Window/Object] <real time>: C

Specify center point:
Enter magnification or height <[color=blue][b]112.1034[/b][/color]>: *Cancel*

Then transfer the values to an existing script file, save & override the existing file: ZoomToView.scr

 

This is what is inside ZoomToView.scr

Zoom
C
[color=red][b]540.4345[/b][/color],[b][color=red]166.2423[/color][/b],[b][color=red]0.0000[/color][/b]
[color=blue][b]112.1034[/b][/color]

The location of the script where i save to is as follows:-

D:\Office\AutoCAD\Scripts\ZoomToView.scr

 

Thanks

Edited by vernonlee
Link to comment
Share on other sites

Use getpoint to obtain the point and query the VIEWSIZE system variable for the height.

 

Thanks Lee for the advise.

 

Alas, I do not understand LISP enough to write it out from scratch. Do appreciate if someone can help me out on this. :sweat:

Link to comment
Share on other sites

This one is pretty simple so you should at least be able to give it a try with some guidance - The basic functions you will need are as follows:

 

defun

if

getpoint

getvar

open

progn

write-line

strcat

rtos

close

princ

 

I would first suggest reading the developer documentation for each of the above functions if you don't already know what they do.

 

The pseudo-code for the program (that is, the basic operations it needs to perform) would be something like:

[color=blue]def[/color]i[color=blue]n[/color]e function     
   [color=blue]if[/color] user [color=blue]get[/color]s [color=blue]point     [/color]
   then         
       [color=blue]if [/color]the target .scr file can be [color=blue]open[/color]ed for [color=darkred]w[/color]riting         
       then
           [color=blue]progn[/color]
           [color=blue]write-line[/color] [color=darkred]"_.zoom"[/color] to file
           [color=blue]write-line[/color][color=darkred] "_C"[/color] to file
           [color=blue]write-line[/color] [[color=blue]strcat [/color][[color=blue]rtos [/color]x-coord] [[color=blue]rtos [/color]y-coord] [[color=blue]rtos [/color]z-coord]] to file
           [color=blue]write-line[/color] [[color=blue]rtos [/color][[color=blue]getvar [/color][color=darkred]viewsize[/color]]] to file
           [color=blue]close [/color]file      
       else             
           [color=blue]princ [/color]file could not be opened for writing 
[color=blue]princ[/color]

Link to comment
Share on other sites

Thanks Lee. Will give it a shot. Hope you can continue to guide me along the way.

 

For a start, any recommended link for the developer documentation I can read up on those functions you mention.

 

Thanks

Link to comment
Share on other sites

My suggestion is that there isn't really a need to create a script file. Instead, it would be easier to just create two separate lisp routines, one that stores the zoom information and one that zooms to the stored information. It is fairly easy using the commands Lee gave, you'll just need to make sure that your variables you store the zoom information in are global, not local. This way you won't have to mess with an extra file, but it will function the same way, it will just be quicker and more efficient. Let me know if you need any more help/suggestions because I'm glad to help.

Link to comment
Share on other sites

boncos15 I agree with Lee with no disrespect to Vernonlee he has 494 posts its about time to start learning rather than just take the freebies. Lots like me are here to help not just write code.

Link to comment
Share on other sites

My suggestion is that there isn't really a need to create a script file. Instead, it would be easier to just create two separate lisp routines, one that stores the zoom information and one that zooms to the stored information. It is fairly easy using the commands Lee gave, you'll just need to make sure that your variables you store the zoom information in are global, not local. This way you won't have to mess with an extra file, but it will function the same way, it will just be quicker and more efficient. Let me know if you need any more help/suggestions because I'm glad to help.

 

Hi. The reason why i needed to "export" this values is I need to zoom to the same location for multiple drawings.

Link to comment
Share on other sites

Hi. The reason why i needed to "export" this values is I need to zoom to the same location for multiple drawings.
If your variables are global, they will be remembered between drawings. That way, you can zoom to the same location in as many drawings as you want.
Link to comment
Share on other sites

If your variables are global, they will be remembered between drawings

 

Having a problem with this do you know a secret ?

 

Global variables as per help is about running within one dwg not across multiples outside of say multiple defuns running in a session, the code by Lee hints at this by writing to file. Another alternative is to write say a string to the registery. vl-registry-write vl-registry-read

Link to comment
Share on other sites

If your variables are global, they will be remembered between drawings. That way, you can zoom to the same location in as many drawings as you want.

 

:o

 

Could you elobrate?

Link to comment
Share on other sites

If your variables are global, they will be remembered between drawings. That way, you can zoom to the same location in as many drawings as you want.

 

Not true I'm afraid: all symbols in AutoLISP are defined within the document namespace by default and therefore cannot be accessed outside of this namespace (unless you use vl-bb-set/vl-bb-ref to define symbols within the blackboard namespace which is a document-independent namespace and may therefore be accessed from any drawing).

 

Another alternative is to use vl-propagate to propagate a variable across all document namespaces.

Link to comment
Share on other sites

Lee, I thought setenv and getenv could be used to store variables in the registry (just need to be careful to not use the same variables and to not overuse it because it can bloat it). I haven't used them yet (haven't seen the need) but I remember reading about it when I was first learning lisp, did I misunderstand it? Also can't vl-propagate be used to "store" the variable so that it can be used for all open drawings in the current session?

Link to comment
Share on other sites

Lee, I thought setenv and getenv could be used to store variables in the registry (just need to be careful to not use the same variables and to not overuse it because it can bloat it). I haven't used them yet (haven't seen the need) but I remember reading about it when I was first learning lisp, did I misunderstand it? Also can't vl-propagate be used to "store" the variable so that it can be used for all open drawings in the current session?

 

Indeed, getenv & setenv can be used to read & write a string value (REG_SZ / REG_EXPAND_SZ) to a specific branch of the registry [specifically the location given by: (strcat "HKEY_CURRENT_USER\\" (vlax-product-key) "\\FixedProfile\\General") ], however, these are not global variables as you stated in your post.

 

Writing such values to the registry is essentially the same as reading & writing data to a text file (which may in fact be a preferred option due to the ease of removal - this is the route I follow with the majority of my larger applications).

 

As noted in my post above, vl-propagate is also an option, however, this will cause a global variable to persist only during the current instance of the application.

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