Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 1 of 10 (907 Views)
Accepted Solution

Command | If layer is frozen : thaw it and vice versa

907 Views, 9 Replies
03-29-2012 10:17 AM

Hi, I found and modify a lisp routine that I've found, I would like to know if it is on the right track. Also to mention that I don't  really understand all the mechanics of it but first, here's what I want to achieve :

 

I have a  Layer named "Arch - Niveau 1"

I would like to enter in the command prompt  "a1"

It would read if the layer is frozen or thawed and if it is frozen, it will thaw it (? sorry bad english)

And if the layer is thawed, it would freeze it

 

That's it!

 

What could be added is:

To check up if the layer exists and if not, create it.

Would do the same for 2 other layer named "Arch - Niveau 2" command "a2"   and  "Arch - Niveau 3" command "a3". Is it clear?

 

Now the lisp I've found and modified is this:


(defun c:a1 (/ lay ldef flag)
  (setq layn "Arch - Niveau 1")

  (command "_.LAYER")
  (if (not (tblsearch "LAYER" layn))
      (command "_Make" layn)
      (progn
        (setq ldef (tblsearch "LAYER" layn)
              flag (cdr (assoc 70 ldef)))
        (and (= (logand flag  1)  1)
             (command "_Thaw" layn))
        ))
  (command "")
)

 This lisp looks if it exists, if not, create it and only thaw the layer.

 

I'm not sure I want to understand all the mechanics because I'm not enough familiar with the "not", the "progn" the "flag", "and" and "logand"  (!!!!)

 

Thanks a lot !

 

Finally, elsewhere someone made me this lisp, credit to cadtutor forum

 

(defun c:a1 nil (FreezeThawLayer "Arch - Niveau 1"))
(defun c:a2 nil (FreezeThawLayer "Arch - Niveau 2"))
(defun c:a3 nil (FreezeThawLayer "Arch - Niveau 3"))
(defun FreezeThawLayer ( layer / dx en )
(if (null (setq en (tblobjname "LAYER" layer)))
(entmake
(list
'(0 . "LAYER")
'(100 . "AcDbSymbolTableRecord")
'(100 . "AcDbLayerTableRecord")
(cons 2 layer)
'(70 . 0)
)
)
(setq en (entget en)
dx (assoc 70 en)
en (entmod (subst (cons 70 (boole 6 1 (cdr dx))) dx en))
)
)
(princ)
)

 

 

Works fine, thanks again for taking the time

 

Have a good week end

*Expert Elite*
Kent1Cooper
Posts: 5,500
Registered: ‎09-13-2004
Message 2 of 10 (897 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-29-2012 11:03 AM in reply to: Troispistols

Troispistols wrote:

.... 

I have a  Layer named "Arch - Niveau 1"

I would like to enter in the command prompt  "a1"

It would read if the layer is frozen or thawed and if it is frozen, it will thaw it ... And if the layer is thawed, it would freeze it

.... 

What could be added is:

To check up if the layer exists and if not, create it.

Would do the same for 2 other layer named "Arch - Niveau 2" command "a2"   and  "Arch - Niveau 3" command "a3". .... 


Here's a pretty efficient way to thaw a Layer [with its name in the 'layn' variable] if it's frozen or Freeze it if it's thawed:

 

(setq layobj (vlax-ename->vla-object (tblobjname "Layer" layn)))

(vlax-put layobj 'Freeze (1- (abs (vlax-get layobj 'Freeze))))

 

It finds the Freeze property assigned to that Layer as converted to a VLA object, and if it's 0 [thawed], makes it -1 [frozen], or if it's -1, makes it 0.

 

So, you could define all three commands together, by putting this into a .lsp file:

 

(defun LFTTM (layn / layobj); = Layer Freeze-Thaw Toggle or Make [sub-routine]

  (if (tblsearch "Layer" layn)

    (progn ; then -- freeze/thaw toggle it

      (setq layobj (vlax-ename->vla-object (tblobjname "Layer" layn)))

      (vlax-put layobj 'Freeze (1- (abs (vlax-get layobj 'Freeze))))

    ); progn

    (command "_.layer" "_make" layn ""); else - make it

); defun

(defun C:a1 ()

  (LFTTM "Arch - Niveau 1")

)

(defun C:a2 ()

  (LFTTM "Arch - Niveau 2")

)

(defun C:a3 ()

  (LFTTM "Arch - Niveau 3")

)

Kent Cooper
Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 3 of 10 (892 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-29-2012 11:58 AM in reply to: Kent1Cooper

OK thanks, but I can't make it work the lisp I create :

(defun LFTTM (layn / layobj); = Layer Freeze-Thaw Toggle or Make [sub-routine]
  (if (tblsearch "Layer" layn)
    (progn ; then -- freeze/thaw toggle it
      (setq layobj (vlax-ename->vla-object (tblobjname "Layer" layn)))
      (vlax-put layobj 'Freeze (1- (abs (vlax-get layobj 'Freeze))))
    ); progn
    (command "_.layer" "_make" layn ""); else - make it
); defun

(defun C:a11 ()
  (LFTTM "Arch - Niveau 1")
)
(defun C:a22 ()
  (LFTTM "Arch - Niveau 2")
)
(defun C:a33 ()
  (LFTTM "Arch - Niveau 3")
)
)

 

 

When I hit "a11" and the layer is already frozen, it answer me:

 

Command: *Cancel*

Command: *Cancel*

Command: a11 -layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: new
Enter name list for new layer(s): Arch - Niveau 1
Layer "Arch - Niveau 1" already exists.
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command:
Frozen"\nFrozen"

 

(Nothing Happen)

 

If the layer is already thawed

 

Command: *Cancel*

Command: *Cancel*

Command: a11 -layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: new
Enter name list for new layer(s): Arch - Niveau 1
Layer "Arch - Niveau 1" already exists.
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command:
Thawednil

 

So at this point, it does not work

 

Thanks

*Expert Elite*
Kent1Cooper
Posts: 5,500
Registered: ‎09-13-2004
Message 4 of 10 (879 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-29-2012 02:21 PM in reply to: Troispistols

Troispistols wrote:

OK thanks, but I can't make it work.... 

.... 

Command: a11 -layer
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: new
....


I can only imagine there's some older definition of the same command name still in effect, which probably means the file you put that in did not load properly.  There's nothing in your posted code that would be feeding in the layer command name with the hyphen in front of it [not needed when inside a (command) function], nor the new option, nor should it be calling up the Layer command at all if the Layer already exists.  And the other oddball things happening later must also be coming from something else.

 

[And it looks like you have an extra right parenthesis at the very end.  That might prevent it from loading, but I think you should get some kind of message about it.]

Kent Cooper
Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 5 of 10 (856 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 06:53 AM in reply to: Kent1Cooper

Hi, thanks for taking the time. But I found this more complicated that it is...

 

If I copy/paste what you gave to me and drop it in autocad in a fresh new dwg it doesn't load properly :

 

Regenerating model.

AutoCAD menu utilities loaded.
*** Acaddoc.lsp successfully loaded ***
Command:
Command:
Command:
Command: (LOAD "C:/Users/jpetitclerc/Desktop/a11.lsp") ; error: malformed list on input

 

I had to add the right parenthisis at the end do make it load properly :


Command:
Command: (LOAD "C:/Users/jpetitclerc/Desktop/a11.lsp") LFTTM

 

When I hit a11. It doesn't work :

 

Command:
Command: (LOAD "C:/Users/jpetitclerc/Desktop/a11.lsp") LFTTM

Command: a11
Unknown command "A11". Press F1 for help.

 

Also english isn't my first language and i didn't understand everything you answer me... Specificaly, what should I do to make it run? I think the principle is simple? 

 

"If layer is frozen : thaw it and vice versa"

 

Does someone can help me whit that please?

*Expert Elite*
Kent1Cooper
Posts: 5,500
Registered: ‎09-13-2004
Message 6 of 10 (850 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 07:13 AM in reply to: Troispistols

My fault -- I left out a closing parenthesis for an (if) function.  Try it this way:

 

(defun LFTTM (layn / layobj); = Layer Freeze-Thaw Toggle or Make [sub-routine]

  (if (tblsearch "Layer" layn)

    (progn ; then -- freeze/thaw toggle it

      (setq layobj (vlax-ename->vla-object (tblobjname "Layer" layn)))

      (vlax-put layobj 'Freeze (1- (abs (vlax-get layobj 'Freeze))))

    ); progn

    (command "_.layer" "_make" layn ""); else - make it

  ); if

); defun

(defun C:a11 ()

  (LFTTM "Arch - Niveau 1")

)

(defun C:a22 ()

  (LFTTM "Arch - Niveau 2")

)

(defun C:a33 ()

  (LFTTM "Arch - Niveau 3")

)

Kent Cooper
Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 7 of 10 (845 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 07:36 AM in reply to: Kent1Cooper

OK, thanks,

 

but again,

 

I get this:

 

Command: (LOAD "C:/Users/jpetitclerc/Desktop/a11.lsp") C:A33

Command: A11 ; error: no function definition: VLAX-ENAME->VLA-OBJECT

 

Sorry

:smileyindifferent:

Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 8 of 10 (841 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 07:47 AM in reply to: Troispistols

Finally, elsewhere someone made me this lisp, credit to cadtutor forum

 

(defun c:a1 nil (FreezeThawLayer "Arch - Niveau 1"))
(defun c:a2 nil (FreezeThawLayer "Arch - Niveau 2"))
(defun c:a3 nil (FreezeThawLayer "Arch - Niveau 3"))

(defun FreezeThawLayer ( layer / dx en )
    (if (null (setq en (tblobjname "LAYER" layer)))
        (entmake
            (list
               '(0 . "LAYER")
               '(100 . "AcDbSymbolTableRecord")
               '(100 . "AcDbLayerTableRecord")
                (cons 2 layer)
               '(70 . 0)
            )
        )
        (setq en (entget en)
              dx (assoc 70 en)
              en (entmod (subst (cons 70 (boole 6 1 (cdr dx))) dx en))
        )
    )
    (princ)
)

 

 

Works fine, thanks again for taking the time

 

Have a good week end

*Expert Elite*
Kent1Cooper
Posts: 5,500
Registered: ‎09-13-2004
Message 9 of 10 (840 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 07:49 AM in reply to: Troispistols

One other thing I often forget -- add this line to the code somewhere [most people put it at the top, but I don't think it matters in this case]:

 

(vl-load-com)

 

[I often forget to include that, because we use Architectural Desktop which somehow loads that for us, so things always work here without our including that in our routines.  Sometimes I remember in things I post, but obviously not always.]

Kent Cooper
Active Contributor
Troispistols
Posts: 38
Registered: ‎08-29-2008
Message 10 of 10 (832 Views)

Re: Command | If layer is frozen : thaw it and vice versa

03-30-2012 08:07 AM in reply to: Kent1Cooper

Haha ok ! :smileyvery-happy:

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.