Hello everyone,
here I have some code that I need help with, cause sometimes when exiting the command ACAD doesn't close the file and hence I have an empty file which I can't even delete until closing ACAD at which time it also closes the mentioned file and writes data in it, so I would need some optimization of code so cad would close the file whenever one would break the command, thx
(defun c:lp (/ f a numt)
(setvar "cmdecho" 0)
(command "dimzin" 0)
(setvar "cmdecho" 1)
(setq f (open (getfiled "Text File" "c:/Users/tvargek/Desktop/" "txt" 5) "a"))
(while (setq numt (ENTGET (CAR (ENTSEL "\nSelect point number: "))))
(if (or (= (cdr (assoc 0 numt)) "ATTRIB")
(= (cdr (assoc 0 numt)) "TEXT")
(= (cdr (assoc 0 numt)) "MTEXT")
)
(progn
(setq a (getpoint "\nSelect point: "))
(princ (cdr (assoc 1 numt)) f)
(princ "," f)
(princ (rtos (car a) 2 3) f)
(princ "," f)
(princ (rtos (cadr a) 2 3) f)
(princ "," f)
(princ (rtos (caddr a) 2 3) f)
(princ "," f)
(princ "\n" f)
)
(PROGN
(close f)
(quit)
)
);_ end of if
) ;_ end of while
(close f)
) ;_ end of defun
(princ
"\nList points...Copyright © by TOMISLAV VARGEK...Osijek,Croatia...\n...Type LP to start..."
)
(defun *error* (msg)
(close f)
)
Solved! Go to Solution.
Solved by BlackBox_. Go to Solution.
Give this a try:
(defun c:LP (/ *error* oldDimzin path file eName eData Point) (defun *error* (msg) (and oldDimzin (setvar 'dimzin oldDimzin)) (if file (close file) ) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it ) (princ) ) (if (and (setq oldDimzin (getvar 'dimzin)) (setvar 'dimzin 0) (setq path (getfiled "Text File" "c:/Users/tvargek/Desktop/" "txt" 5) ) (setq file (open path "a")) ) (while (and (setq eName (car (entsel "\nSelect point number: "))) (wcmatch (cdr (assoc 0 (setq eData (entget eName)))) "ATTRIB,MTEXT,TEXT" ) (setq point (getpoint "\nSelect point: ")) ) (write-line (strcat (cdr (assoc 1 eData)) "," (rtos (car point) 2 3) "," (rtos (cadr point) 2 3) "," (rtos (last point) 2 3) "," ) file ) ) ) (*error* nil) )
"How we think determines what we do, and what we do determines what we get."
True... I'm always working in World, so I often fail to include this.
"How we think determines what we do, and what we do determines what we get."
Right on. I change my UCS when dealing with rotated views, so MLeaders will come in correctly. That's the only reason I think about it.
that works correctly..thx...
when I look at the code the main difference(besides advanced coding by you) is that error function is declared before main function code so I made that also on my code and got the same result...I didn't know that can make such a difference!
thx again
@tomislav.vargek wrote:that works correctly..thx...
when I look at the code the main difference(besides advanced coding by you) is that error function is declared before main function code so I made that also on my code and got the same result...I didn't know that can make such a difference!
thx again
I'm happy to help, tomislav.vargek.
In your code, the *error* function is redefined globally when the code is loaded... You can do that, but it's always a good idea to first store the original *error* function, and have your *error* function restore it.
A far less error-prone method (no pun intended), is to make your custom *error* function a local variable within your Defun, as by definition a localized variable is only defined while in scope (i.e., while the Defun is being executed)... This means you do not have to store the original *error* function, and worry about how / when to restore, etc..
In any event, if I answered your question with my earlier post, please mark it as solved.
Cheers
"How we think determines what we do, and what we do determines what we get."
Sorry in advance for this being off topic....
@alanjt_ wrote:Right on. I change my UCS when dealing with rotated views, so MLeaders will come in correctly. That's the only reason I think about it.
Written quickly in the last few of my lunch break (I'm certain some refinement is needed😞
(vl-load-com) ;;;--------------------------------------------------------------------; (defun MleaderReactor:Start () (or *MleaderReactor* (setq *MleaderReactor* (vlr-command-reactor "Mleader reactor" '( (:vlr-commandcancelled . MleaderCallback:CommandEnded) (:vlr-commandended . MleaderCallback:CommandEnded) (:vlr-commandfailed . MleaderCallback:CommandEnded) (:vlr-commandwillstart . MleaderCallback:CommandWillStart) ) ) ) ) (princ) ) ;;;--------------------------------------------------------------------; (defun MleaderCallback:CommandEnded (rea cmd) (if (wcmatch (strcase (car cmd)) "*MLEADER") (progn (if *Mleader_oldSnapang* (progn (setvar 'snapang *Mleader_oldSnapang*) (setq *Mleader_oldSnapang* nil) ) ) (if *Mleader_OldUcs* (progn (vla-put-activeucs acDoc *Mleader_OldUcs*) (setq *Mleader_OldUcs* nil) ) ) (if *Mleader_NewUcs* (progn (vla-delete *Mleader_NewUcs*) (setq *Mleader_NewUcs* nil) ) ) ) ) ) ;;;--------------------------------------------------------------------; (defun MleaderCallback:CommandWillStart (rea cmd / oUcss ucsName oldUcs base) (if (wcmatch (strcase (car cmd)) "*MLEADER") (progn ;; save the current ucs (if (not (vl-catch-all-error-p (setq oldUcs (vl-catch-all-apply 'vla-item (list (setq oUcss (vla-get-usercoordinatesystems acDoc)) (setq ucsName (if (= 1 (getvar 'worlducs)) "worldUcs" "oldUcs" ) ) ) ) ) ) ) (vla-delete oldUcs) ) (setq *Mleader_OldUcs* (vla-add oUcss (vlax-3d-point (setq base (getvar 'ucsorg))) (vlax-3d-point (getvar 'ucsxdir)) (vlax-3d-point (getvar 'ucsydir)) ucsName ) ) ;; create the new ucs (setq *Mleader_NewUcs* (vla-add oUcss (vlax-3d-point base) (vlax-3d-point (polar base (setq *Mleader_oldSnapang* (getvar 'snapang)) 1.0 ) ) (vlax-3d-point (polar base (+ (* 0.5 pi) *Mleader_oldSnapang*) 1.0) ) "newUcs" ) ) ;; set the new ucs current (setvar 'snapang 0) (vla-put-activeucs acDoc newUcs) ) ) ) ;;;--------------------------------------------------------------------; (MleaderReactor:Start) (princ)
... Now I just need to find out how to remove the old ucs from the ActiveUcs Property, and I can delete it too. In general though, it seems to work quite well, as I use a lot of DVIEW 'twist' action to orientate myself about an process piping assembly, etc. and copy/paste using World... This just makes it easier for me to now add all of the MLeaders is need.
Thanks for inspiring the idea... Cheers
/OffTopic
"How we think determines what we do, and what we do determines what we get."
Odd, I cannot get it to work. Will have to play around with it.
To clarify, are you trying to set the UCS based on the how you have your view twisted (using DVIEW TWIST) and then reset it when the MLeader command exits?
Neat idea.
@alanjt_ wrote:Odd, I cannot get it to work. Will have to play around with it.
To clarify, are you trying to set the UCS based on the how you have your view twisted (using DVIEW TWIST) and then reset it when the MLeader command exits?
Neat idea.
Strange... But yes, that is exactly what I doing for my work, and the code seems to work for me just fine.
Again, written quickly, so I may not have considered how others work.
Cheers
"How we think determines what we do, and what we do determines what we get."