Author Topic: Vanilla in contention with Visual  (Read 10164 times)

0 Members and 1 Guest are viewing this topic.

Chuck Gabriel

  • Guest
Vanilla in contention with Visual
« on: February 24, 2006, 09:58:13 AM »
I know this has been discussed before, if not here then on the Autodesk forums, but I'd like to hear about it from some of the folks here.

Right around the time I started using some of the ActiveX stuff provided by Visual LISP, I started having problems with previously solid code, that used the traditional "vanilla" AutoLISP techniques, failing intermittently (i.e. (setvar "clayer" "somelayer") would fail from time to time).The conventional wisdom, at the time, was to convert any problematic code to use ActiveX equivalents for things like entmod and entmake, and that is precisely what I have done.  However, I still occasionally encounter problems related to this phenomenon.

Is wholesale conversion to ActiveX still considered the best way to deal with the issue?  Has anybody ever discovered an actual root cause for the problem?  Can any of you advise me on a better way to handle this?

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #1 on: February 24, 2006, 10:56:57 AM »
Okay this is going to sound weird but... I remember when Stig just roasted me on some code once. I spent hours-days on a proced to "draw a line" and I asked Stig how it was looking/shaping up and he said to me: there is nothing wrong with the use of command if you are careful and set up the environment. I mean we've all seen the type of code:

(defun foo ()
  (defun crap?! (s)
    (setq *error* olderr
          olderr  nil)
    (setvar "cmdecho" 1)
    (princ) )
  (setq olderr *error* *error* crap?!)
  (setvar "cmdecho" 0)
  (setq a (getpoint "\nenter a point dummy: "))
  (setq b (getpoint a "\nenter another point airhead: "))
  (command "_line" a b "")
  (quit)
 (princ)
)

But there is actual validity in that type of code. When done properly you don't really need a hundred lines of VL code with the PROPER use of `command'. (But therein lies the problem with most new users they don't know how to properly set up the situation and they often get/produce crapy code that looks even worse because they used command.)

If you were to take a look at my "general util" file that gets loaded on start up (the one full of a bunch of lisps I use allt he time) you would think that I didnt know the first thing about Autolisp or Visual lisp. ...I mean take a look at this stuff.

;; move command maped to 'vv'.
(defun c:vv () (princ "MOVE") (command "_.move") (princ) )

;;; set the current layer to the one selected.
(defun c:as ( / lay)
 (while (not (setq lay
   (cdr (assoc 8 (entget (car (entsel))))))))
  (command "layer" "s" lay "")
  (princ) )

;Copy & Rotate in one tool
(defun c:cr (/ p1 objects)
  (prompt "Select Objects: ")
  (setq objects (ssget))
  (setq p1 (getpoint "Select base point: "))
  (command "copy" objects "" p1 p1)
  (command "move" objects "" p1 pause)
  (command "rotate" objects "" (getvar "lastpoint") pause)
)

My most precious file is loaded with this type of code. (I should really update that thing.)

So what im trying to say is unless you writing for development purposes there "REALLY" isnt a need for a full on ActiveX assult most times.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #2 on: February 24, 2006, 11:22:43 AM »
That's all well and good, and I've recently begun to relax regarding that particular issue, but command isn't one of the functions that fails intermittently.  It's primarily stuff like setvar, entmod, entmake, and etc.

Here is a prime example.  I use the following functions to store system variable states at the beginning of a function and restore them at the end:

Code: [Select]
(defun CollectVars (varlst / varnam)
  (foreach varnam varlst
    (setq varlst (subst (cons varnam (getvar varnam)) varnam varlst))
  )
)

(defun RestoreVars (varlst / varnam)
  (foreach varnam varlst
    (cond
      ((= (strcase (car varnam)) "CLAYER")
       (vla-put-activelayer
*thisdrawing*
(vla-item (vla-get-layers *thisdrawing*) (cdr varnam))
       )
      )
      (T (setvar (car varnam) (cdr varnam)))
    )
  )
  (princ)
)

See that bit of ActiveX code in RestoreVars?  I had to do that because the function would periodically kak when trying to restore the clayer system variable to its previous value.

That's just a sample of the types of failures that have occurred.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #3 on: February 24, 2006, 11:36:42 AM »
Thats because:
clayer will NOT restore this way. (setvar)
viewres will not resore this way. (setvar)
(Im sure there are others, but these are the two that ive found from 2000 on.)

Entmod and Entmake are two that bring up another issue.

But the point i was trying to make is that command can be used safely to offer you another alternitive to VL methods (Or etc.) for doing some of the "broken" methods.

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #4 on: February 24, 2006, 11:52:20 AM »
Thats because:
clayer will NOT restore this way.
...

Wanna bet?

(setvar "clayer" "0") at the command line works just fine for me (most of the time).

By the way, thanks for jumping in.  I do appreciate your advice even if it doesn't seem that way. :D

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #5 on: February 24, 2006, 12:36:31 PM »
yeah!
Command: (setvar "clayer" "0")
; error: AutoCAD variable setting rejected: "clayer" "0"

No, i enjoy discussions. (Specially with somone of your calibur [sp])

...hold on, ill be right back.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #6 on: February 24, 2006, 12:41:39 PM »
Okay im back...

Ah HA! I found some backing! This is a var resore function from AcadX and 'it' thinks the same way I do. (Okay thats enough being cocky from me.) This must be where I got the clayer and viewres thing. Cause I used to read acadx alot when I was learning. 

Code: [Select]
(defun vlxx-VarRestore ( / $orr #err)
  (defun #err (s)
    (princ (strcat "\nError: " s))
    (setq G$VARS nil)
    (setq *error* $orr)
    (princ)
    )
  (setq $orr *error* *error* #err)
  (cond
    ( (and G$VARS (listp G$VARS))
     (foreach n G$VARS
              (cond
                ( (= (strcase (car n)) "CLAYER")
                 (command "_.layer" "_s" (cadr n) "")
                 )
                ( (= (strcase (car n)) "VIEWRES")
                 (command "_.viewres" "_Y" (cadr n) "")
                 )
                ( T (setvar (car n) (cadr n)) )
                )
              )
     (setq G$VARS nil)
     )
    )
  (setq *error* $orr $orr nil)
  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Vanilla in contention with Visual
« Reply #7 on: February 24, 2006, 12:48:38 PM »
yeah!
Command: (setvar "clayer" "0")
; error: AutoCAD variable setting rejected: "clayer" "0"

No, i enjoy discussions. (Specially with somone of your calibur [sp])

...hold on, ill be right back.

If I'm not mistaken John setvar will only chuck a wobbly on clayer if the layer doesn't exist or is frozen, otherwise it works.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #8 on: February 24, 2006, 01:09:50 PM »
So then why does it not work on lay zero?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Vanilla in contention with Visual
« Reply #9 on: February 24, 2006, 01:11:13 PM »
Worked here with layer 0.
Quote
Command: (SETVAR "CLAYER" "0")
"0"
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #10 on: February 24, 2006, 01:12:03 PM »
So then why does it not work on lay zero?

It does (for me).  Maybe the problem that I experience intermittently is more pronounced on your machine.  Did you try it several times in succession?


JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #11 on: February 24, 2006, 01:12:43 PM »
...holly cow!?! It worked:
Command: (setvar "clayer" "M-HVAC")
"M-HVAC"

*blink*

So in otherwords, if we check to see if the layer is locked we can use setvar.
*Se7en runs off to do some testing*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #12 on: February 24, 2006, 01:14:08 PM »
So then why does it not work on lay zero?

It does (for me).  Maybe the problem that I experience intermittently is more pronounced on your machine.  Did you try it several times in succession?



yeah i did.

Command: (setvar "clayer" "0")
; error: AutoCAD variable setting rejected: "clayer" "0"

Command: (setvar "clayer" '0)
; error: AutoCAD variable setting rejected: "clayer" 0

Command: (setvar "clayer" 0)
; error: AutoCAD variable setting rejected: "clayer" 0

Command: (setvar "clayer" 0)*Cancel*

Command: (set 'zero 0)
0

Command: (setvar "clayer" zero)
; error: AutoCAD variable setting rejected: "clayer" 0

Command: (set 'zero '0)
0

Command: (setvar "clayer" zero)
; error: AutoCAD variable setting rejected: "clayer" 0

Command: (setvar 'clayer zero)
; error: AutoCAD variable setting rejected: CLAYER 0

Command:

*hummmmm!*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #13 on: February 24, 2006, 01:17:09 PM »
You broke it!

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Vanilla in contention with Visual
« Reply #14 on: February 24, 2006, 01:37:25 PM »
FWIW, I have always used the (setvar "clayer" "whatever"). That is until I started using the ActiveX methods of StartUndoMark/EndUndoMark. When these are used, I then make sure to get & set all SysVars via activex as well.
Code: [Select]
(defun CollectVars (varlst / varnam)
  (foreach varnam varlst
    (setq varlst (subst (cons varnam (vlax-invoke
       *thisdrawing*
       'getvariable
       varnam))
      varnam varlst))
  )
)

(defun RestoreVars (varlst / varnam)
  (foreach varnam varlst
    (vlax-invoke
      *thisdrawing*
      'setvariable
      (car varnam)
      (cdr varnam)
      )
    ) 
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #15 on: February 24, 2006, 02:33:29 PM »
..ok, now its working again. I went to lunch and now its working again. *Argh!*

Command: (setvar "clayer" "0")
"0"
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #16 on: February 24, 2006, 02:37:22 PM »
Thanks Jeff.  I hadn't considered doing it that way.  Does that approach work consistently?

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Vanilla in contention with Visual
« Reply #17 on: February 24, 2006, 04:44:21 PM »
As far as I can tell, yes it does Chuck. I have not had any problems surface since I started doing it this way......but, it's just me and one other person at this office and however many folks folks may have used some of my posted routines that require it.

Another workaround, that for some reason I use more than I prefer, is to use the (command "undo" "be")/(command "undo" "e") sequence in place of the ActiveX undo.

Serge J. Gianolla

  • Guest
Re: Vanilla in contention with Visual
« Reply #18 on: February 24, 2006, 05:10:58 PM »
"Should" fix it!
(setvar 'CLAYER 0)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Vanilla in contention with Visual
« Reply #19 on: February 24, 2006, 05:46:11 PM »
uhmmmm ... that would be a string for the layer , yes Serge ?

 (setvar 'CLAYER "0")
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Vanilla in contention with Visual
« Reply #20 on: February 24, 2006, 05:51:09 PM »
"Should" fix it!
(setvar 'CLAYER 0)

Warning: Symbols are not compatible with get/setvar with versions of AutoCAD older than 2005 if I recall correctly (can't confirm, I only have AutoCAD 2006 here).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Vanilla in contention with Visual
« Reply #21 on: February 24, 2006, 05:55:12 PM »
Test in A2k4
Quote
Command: (setvar 'clayer 0)
; error: AutoCAD variable setting rejected: CLAYER 0

Command: (setvar 'clayer "0")
"0"

Command: (setq tmpLay "0")
"0"

Command: (setvar 'clayer tmplay)
"0"

Command: (setq tmpLay 0)
0

Command: (setvar 'clayer tmplay)
; error: AutoCAD variable setting rejected: CLAYER 0
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Vanilla in contention with Visual
« Reply #22 on: February 24, 2006, 06:10:53 PM »
For what its worth, I use (setvar "CLAYER" variableValue) in all my error traps ...

Similar to Jeff, I use this as a Library routine called from the program initialisation to save the current VARS and set the new values ;

Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;; change sysvar value and save its previous value
(defun kb:savesysvar (vars_list)
  (foreach dotpair vars_list
    (setq KG:sysvarlist (cons (list (car dotpair) (getvar (car dotpair)))
                                 KG:sysvarlist
                           )
    )
    (if (cadr dotpair)
      (setvar (car dotpair) (cadr dotpair))
    )
  )
)

Called like this ... with the Values I expect set for the routine
Code: [Select]
;;------- SAVE Current System Variables to global KG:sysvarlist
;;------- and SET as Listed

(kb:savesysvar '(("CMDECHO" 0)
                  ("EXPERT" 5)
                  ("UCSICON" 3)
                  ("BLIPMODE" 0)
                  ("SNAPANG" 0)
                  ("SNAPMODE" 0)
                  ("OSMODE" 0)
                  ("SNAPANG" 0)
                  ("PICKADD" 1)
                  ("PICKBOX" 5)
                  ("SORTENTS" 1)
                  ("CLAYER")
                 )
 )
Quote
;|
example value of current Global Variable KG:sysvarlist saved by by kb:savesysvar

(("CLAYER" "0")
 ("SORTENTS" 127)
 ("PICKBOX" 5)
 ("PICKADD" 1)
 ("SNAPANG" 0.0)
 ("OSMODE" 0)
 ("SNAPMODE" 0)
 ("SNAPANG" 0.0)
 ("BLIPMODE" 0)
 ("UCSICON" 3)
 ("EXPERT" 0)
 ("CMDECHO" 1)
)
|;
Then call this from the clean-up or error trap ..
Code: [Select]
;;----- Reset System Variables from global list ----------------

(foreach dotpair KG:sysvarlist (setvar (car dotpair) (cadr dotpair)))

This has worked without fault for more years than I care to say .. :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Serge J. Gianolla

  • Guest
Re: Vanilla in contention with Visual
« Reply #23 on: February 24, 2006, 06:13:39 PM »
Well spotted Kerry, I copied the line below from some of my code
(setvar 'BLIPMODE 0)
and changed Only blipmode :-) :oops:

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: Vanilla in contention with Visual
« Reply #24 on: February 24, 2006, 06:43:25 PM »
Ok, Ive tested and tested some more. Ive tested till my brains hurt. I do see that it works like you say guys. Then so the question is: why do i / or how did i come to start to use the command method for restoring the layer prop? I will have to try and look arround this wekend if i get time.

BTW, (Funny) I ended up going off on a tangent, like i often do, while i was writing some code to test some ideas with. I actualy think i came up with a somewhat usefull routine if anyone wants it? (Or am i still a bit goofy from all the parens?)
Code: [Select]
(defun my-getpoint ( pt str / a getpt )
    ;; a general getpoint function for a test I am running.
    ;; Se7en
    ;; 02.24.06
    ;;
    ;; Just a getpoint function that uses grread so that I can tell
    ;; if the user right clicks.
    ;; I am going to STOP the right click for now, I want to add the
    ;; ability to do somethign when the user right clicks later.
    (if (eq pt 'nil)
      (set 'getpt (getpoint str))
      (set 'getpt (getpoint pt str)))
    (setq a (grread getpt 2))
    (if (and a (not (eq (car a) 12)))
      (cadr a)
      (my-getpoint pt str)) )

Later, im going home now.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Vanilla in contention with Visual
« Reply #25 on: February 24, 2006, 07:05:16 PM »
Chuck,

< snip >
[li]Is wholesale conversion to ActiveX still considered the best way to deal with the issue?  [/li]
[li]Has anybody ever discovered an actual root cause for the problem?  [/li]
[li]Can any of you advise me on a better way to handle this? [/li]

  • dont know, to be honest I can't recall having the problem.
  • not to my knowledge.
  • sorry, no.

I'll check the database on the weekend though.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: Vanilla in contention with Visual
« Reply #26 on: February 24, 2006, 07:12:07 PM »
Is wholesale conversion to ActiveX still considered the best way to deal with the issue?  Has anybody ever discovered an actual root cause for the problem?  Can any of you advise me on a better way to handle this?

I saw this problem on pre-AutoCAD 2004

Anyways, I have all my routines done with basically pure VisualLISP...

Glenn R

  • Guest
Re: Vanilla in contention with Visual
« Reply #27 on: February 24, 2006, 08:19:42 PM »
Chuck,

It probably has something to do with COM document locking/lisp locking and Document Execution Context versus Application Execution Context...at a guess.
Also, CLAYER is a per document setting.......

Cheers,
Glenn.

Chuck Gabriel

  • Guest
Re: Vanilla in contention with Visual
« Reply #28 on: February 24, 2006, 10:28:38 PM »
Thank you all for your input.  In the absence of the knowledge of the actual cause of the problem, it's great to at least have a couple of different approaches to working around it.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Vanilla in contention with Visual
« Reply #29 on: February 25, 2006, 07:02:26 AM »
Test in A2k4 ...

Thanks Tim. AutoCAD 2002 anyone?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Vanilla in contention with Visual
« Reply #30 on: February 25, 2006, 01:25:30 PM »
Test in A2k4 ...

Thanks Tim. AutoCAD 2002 anyone?

yep, 2002 too ...

Quote
Welcome to Visual LISP for AutoCAD 2000
; LPP kernel: IDE v.T.0* Apr 23 2001
 [ACADVER = 15.06s (LMS Tech)]
; kern VLLib: IDE v.T* Mar 03 1999, build #688 [4/23/01]

; "C:/ACAD2002/VLLIB.DLL" loaded
; desktop restored from "C:/ACAD2002/VLIDE.DSK"



Command: (ver)
"Visual LISP 2000 (en)"

Command: (setvar 'clayer "Layer1")
"Layer1"

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Vanilla in contention with Visual
« Reply #31 on: February 25, 2006, 06:10:42 PM »
Guess I'm brain dead. Apolgies for the pointless detour everyone.

(thank you for checking Kerry).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst