PDA

View Full Version : Set UCS to world co-ordinates when closing drawing.



cgv
17th Oct 2013, 12:26 am
Hi,

Just wondering if anyone knows if there is some kind of routine that will set the UCS of a drawing back to world when the drawing is closed? Is this possible?

Thanks.

BIGAL
17th Oct 2013, 02:25 am
You can redefine a command so that it runs a series of commands.

Its been a while something like this needs to be saved as part of your Autoload lisps area



(command "Undefine" "CLOSE")
(defun c:CLOSE ()
(command "ucs" "w")
(command "Zoom" "E")
(command "._CLOSE")
)

Also http://www.afralisp.net/autolisp/tutorials/redefining-commands.php

cgv
17th Oct 2013, 02:37 am
Al,

I've loaded that LISP into the startup, but it doesn't seem to be setting my UCS to world co-ordinates on close.

jdiala
17th Oct 2013, 03:14 am
(command "Undefine" "CLOSE")
(defun c:CLOSE ()
(command "ucs" "w")
(command "Zoom" "E")
(command "._qsave")
(command "._CLOSE")
)

cgv
17th Oct 2013, 03:23 am
jdiala,

Is there a way to have the drawing ask whether to save or not? Otherwise, it just saves the drawings automatically.

Thanks!

jdiala
17th Oct 2013, 03:28 am
(command "undefine" "close")
(defun C:close ()
(if (= 1 (getvar 'dwgtitled))
(command "_.ucs" "w" "_.zoom" "e" "_.qsave" "_.close")
(command "_.close")
)
)

cgv
17th Oct 2013, 04:19 am
jdiala,

The routine still doesn't seem to be asking whether or not to save the drawing. It is still saving automatically on close.

Thanks again.

jdiala
17th Oct 2013, 05:07 am
(command "undefine" "close")
(defun C:close ()
(initget 7 "Yes No")
(setq a (getkword "\nDo you want to save the drawing? (Yes / No) "))

(if (= a "Yes")
(if (= 1 (getvar 'dwgtitled))
(command "_.ucs" "w" "_.zoom" "e" "_.qsave" "_.close")
(progn
(command "_.ucs" "w" "_.zoom" "e")
(initdia)
(command "_.save" "_.close")
)
)
(command "_.close" "n")
)
)

cgv
17th Oct 2013, 05:42 am
Thanks jdiala, but it seems to disagree with closing my drawings sometimes. I guess there is no way around that.

I have read something about using a command in a 'reactor', but I don't know how these work.
http://www.cadtutor.net/forum/showthread.php?46761-UCS-WORLD-without-the-UCS-command-call

Lee Mac
17th Oct 2013, 02:11 pm
Here is an example using a Drawing Reactor:



;; UCS Reactor - Lee Mac
;; Sets the active UCS to a UCS equivalent to WCS when the drawing is saved.

(defun c:ucsr-on ( )
(if (= 'vlr-dwg-reactor (type ucsr:reactor))
(if (vlr-added-p ucsr:reactor)
(princ "\nUCS reactor already running.")
(progn
(vlr-add ucsr:reactor)
(princ "\nUCS reactor enabled.")
)
)
(progn
(setq ucsr:reactor
(vlr-dwg-reactor "ucs-reactor"
'(
(:vlr-beginsave . ucsr:beginsave)
(:vlr-savecomplete . ucsr:savecomplete)
)
)
)
(princ "\nUCS reactor enabled.")
)
)
(princ)
)

(defun c:ucsr-off ( / cmd )
(if (= 'vlr-dwg-reactor (type ucsr:reactor))
(progn
(vlr-remove ucsr:reactor)
(setq ucsr:reactor nil)
(setq cmd (getvar 'cmdecho))
(setvar 'cmdecho 0)
(command "_.ucs" "_w")
(setvar 'cmdecho cmd)
(if (tblsearch "ucs" "ucsr-ucs")
(vla-delete (vla-item (vla-get-usercoordinatesystems (ucsr:acdoc)) "ucsr-ucs"))
)
(princ "\nUCS reactor disabled.")
)
(princ "\nUCS reactor not running.")
)
(princ)
)

(defun ucsr:beginsave ( obj arg )
(if (= "" (setq ucsr:prevucs (getvar 'ucsname)))
(setq ucsr:prevucs (mapcar 'getvar '(ucsorg ucsxdir ucsydir)))
)
(vla-put-activeucs (ucsr:acdoc)
(vlax-invoke (vla-get-usercoordinatesystems (ucsr:acdoc)) 'add
'(0.0 0.0 0.0)
'(1.0 0.0 0.0)
'(0.0 1.0 0.0)
"ucsr-ucs"
)
)
(princ)
)

(defun ucsr:savecomplete ( obj arg )
(cond
( (= 'str (type ucsr:prevucs))
(if (tblsearch "ucs" ucsr:prevucs)
(vla-put-activeucs (ucsr:acdoc)
(vla-item (vla-get-usercoordinatesystems (ucsr:acdoc)) ucsr:prevucs)
)
)
)
( (= 'list (type ucsr:prevucs))
(vla-put-activeucs (ucsr:acdoc)
(apply 'vlax-invoke
(append
(list (vla-get-usercoordinatesystems (ucsr:acdoc)) 'add)
ucsr:prevucs
'("ucsr-ucs")
)
)
)
)
)
(setq ucsr:prevucs nil)
(princ)
)

(defun ucsr:acdoc nil
(eval (list 'defun 'ucsr:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
(ucsr:acdoc)
)

(vl-load-com) (princ)


Type ucsr-on to activate the reactor which will then run silently in the background to be triggered when the drawing is saved.

When saved, the reactor callback function will set the UCS to a WCS equivalent, and will then restore the previous UCS when the save operation is complete.

You can disable the reactor at any time using the ucsr-off command.

However, note that since one cannot evaluated command calls from within reactor callback functions, and furthermore it is not possible (to my knowledge) to set the UCS to WCS using Visual LISP, the above program will set the UCS to a Named UCS equivalent to WCS.

Lee

cgv
17th Oct 2013, 11:42 pm
Lee Mac,

That is exactly what I want. What a guy. The only thing is that it doesn't seem to work when your UCS is set to 'view'. Additionally, would there be a way to have this reactor always on when CAD is started up?

Thanks heaps.

Lee Mac
18th Oct 2013, 12:28 pm
Lee Mac,

That is exactly what I want. What a guy. The only thing is that it doesn't seem to work when your UCS is set to 'view'. Additionally, would there be a way to have this reactor always on when CAD is started up?

Thanks heaps.

You're welcome! :thumbsup:

I've tested the application and I see no problem when the UCS is set to View - the drawing is saved with the UCS set to WCS and the previous UCS is successfully restored - what problem do you encounter?

Regarding loading the program on start-up, follow the procedure I describe here (http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Autosave-I-need-it-to-be-a-TRUE-AUTOMATIC-SAVE-BACKUP-DWG/m-p/4368029#M314376) with regards to creating an acaddoc.lsp and add (c:ucsr-on) on a new line at the end of the program.

cgv
20th Oct 2013, 11:13 pm
Lee,

The problem I'm encountering is when I set the UCS to 'view'. I've just realised that the first time I save, while the UCS is at 'view', it does not revert back to WCS. Hitting save a second time though reverts the UCS to world.

Lee Mac
21st Oct 2013, 12:09 pm
The problem I'm encountering is when I set the UCS to 'view'. I've just realised that the first time I save, while the UCS is at 'view', it does not revert back to WCS. Hitting save a second time though reverts the UCS to world.

Please note that the program cannot set the UCS to WCS, but will set the UCS to a Named UCS which is equivalent to the WCS, for the reasons noted in my earlier post:


However, note that since one cannot evaluate command calls from within reactor callback functions, and furthermore it is not possible (to my knowledge) to set the UCS to WCS using Visual LISP, the above program will set the UCS to a Named UCS equivalent to WCS.

This might be the behaviour that you are observing.

cgv
22nd Oct 2013, 11:29 pm
Ah well. Should do me fine. Thanks Lee.

Lee Mac
22nd Oct 2013, 11:52 pm
No worries cgv :)

I'd gladly be proven wrong and for someone to find a method of setting the UCS to WCS via Visual LISP, but in my experience it seems that this is a limitation of ActiveX.