Jump to content

LISP for : If layer is frozen : thaw it and vice versa


Troispistols

Recommended Posts

Hi, I found and modify a lisp routine, 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 "a11"

-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 "a22" and "Arch - Niveau 3" command "a33". Is it clear?

 

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

 

(defun c:a11 (/ 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" (!!!!)

 

Can you help me complete my lisp please?

Thanks a lot ! :)

Edited by Troispistols
Link to comment
Share on other sites

Hi Troispistols, welcome to CADTutor :)

 

For your task, I would delegate the operation of creating, freezing or thawing a layer to a subfunction, and pass the subfunction the layer name, for example:

 

(defun c:a11 nil (FreezeThawLayer "Arch - Niveau 1"))
(defun c:a22 nil (FreezeThawLayer "Arch - Niveau 2"))
(defun c:a33 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)
)

Link to comment
Share on other sites

Wow! I'm very happy for your extremly quick and efficient answer! Thank you for your time it is a pleasure!

 

In a new drawing with 2 rectangles it works good but in my existing drawing sometimes it work sometimes not.

 

Now I'v closed Acad and reopen my drawing, reload the lisp, and nothing happen when I hit "a1" (Yess I change it to "a1" instead of "a11")

 

(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)
)

 

 

 

Thanks, I hope you can help me

Edited by Troispistols
Link to comment
Share on other sites

In a new drawing with 2 rectangles it works good but in my existing drawing sometimes it work sometimes not.

 

Now I'v closed Acad and reopen my drawing, reload the lisp, and nothing happen when I hit "a1" (Yess I change it to "a1" instead of "a11")

 

The program works everytime for me, it's a simple code so I can't what could cause it to fail - are the Layers being frozen/thawed but the objects maybe not being updated?

 

Maybe this will solve it:

 

(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 in ss )
   (if (null (setq en (tblobjname "LAYER" layer)))
       (entmake
           (list
              '(0 . "LAYER")
              '(100 . "AcDbSymbolTableRecord")
              '(100 . "AcDbLayerTableRecord")
               (cons 2 layer)
              '(70 . 0)
           )
       )
       (if
           (and
               (setq en (entget en)
                     dx (assoc 70 en)
                     en (entmod (subst (cons 70 (boole 6 1 (cdr dx))) dx en))
               )
               (setq ss (ssget "_X" (list (cons 8 layer))))
           )
           (repeat (setq in (sslength ss))
               (entupd (ssname ss (setq in (1- in))))
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

Ok, thanks it seems to have solve the problem. It is 1 block of an "architecture floor plan" with many layers in it for each of the 3 layers (1 block for 1 layer) if it could help you understand what happened.

 

Thanks, seems to work fine!

 

Impressive! :D

Link to comment
Share on other sites

Oh!

 

Now for the first time I can't open the layer manager (set to old layer manager variable) by clicking the button on the tool bar it says:

26a87bf13af9cb44c89f5660b7867395.jpg

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
  at Autodesk.AutoCAD.DatabaseServices.Database.set_Clayer(ObjectId id)
  at Autodesk.AutoCAD.LayerManager.LayerViewManager.set_CurrentLayer(LayerRecord value)
  at Autodesk.AutoCAD.LayerManager.LayerViewManager.UpdateCurrentLayerAndView()
  at Autodesk.AutoCAD.LayerManager.LayerManagerControl.LoadConfiguration(Boolean bCheckExcessFilters)
  at Autodesk.AutoCAD.LayerManager.LayerManagerControl.InitializeLayerManager(Boolean bCheckExcessFilters)
  at Autodesk.AutoCAD.LayerManager.LayerManagerControl.Initialize(Boolean bCheckExcessFilters)
  at Autodesk.AutoCAD.LayerManager.MainForm.Initialize()
  at Autodesk.AutoCAD.LayerManager.MainForm.RunForm(IntPtr owner, LayerFilter filter)
  at Autodesk.AutoCAD.LayerManager.Commands.Layer()
  at Autodesk.AutoCAD.LayerManager.Commands.RunLayer()
  at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
  at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
  at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
  at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()


************** Loaded Assemblies **************
mscorlib
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.225 (RTMGDR.030319-2200)
   CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
AdApplicationFrame
   Assembly Version: 0.0.0.0
   Win32 Version: 3.2.23.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AdApplicationFrame.DLL
----------------------------------------
Acdbmgd
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcdbMgd.DLL
----------------------------------------
System
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
msvcm90
   Assembly Version: 9.0.30729.4940
   Win32 Version: 9.00.30729.4940
   CodeBase: file:///C:/Windows/WinSxS/amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4940_none_08e4299fa83d7e3c/msvcm90.dll
----------------------------------------
AdWindows
   Assembly Version: 3.2.25.0
   Win32 Version: 3.2.25.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AdWindows.DLL
----------------------------------------
PresentationFramework
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll
----------------------------------------
WindowsBase
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
PresentationCore
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll
----------------------------------------
System.Xaml
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
----------------------------------------
System.Core
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
PresentationFramework.Aero
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.Aero/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.Aero.dll
----------------------------------------
System.Drawing
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Acmgd
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.107.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/Acmgd.DLL
----------------------------------------
System.Configuration
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
AcWindows
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.107.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcWindows.DLL
----------------------------------------
AcCui
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcCui.DLL
----------------------------------------
WindowsFormsIntegration
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsFormsIntegration/v4.0_4.0.0.0__31bf3856ad364e35/WindowsFormsIntegration.dll
----------------------------------------
AcWindows.resources
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/en-US/AcWindows.resources.DLL
----------------------------------------
ManagedMC3
   Assembly Version: 5.8.0.0
   Win32 Version: 5.8.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/ManagedMC3.DLL
----------------------------------------
AcLayer
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcLayer.DLL
----------------------------------------
System.Windows.Forms
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
AcLayer.resources
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/en-US/AcLayer.resources.DLL
----------------------------------------
AcButterflyExt
   Assembly Version: 1.0.0.23
   Win32 Version: 1.0.0.23
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcButterflyExt.DLL
----------------------------------------
AcButterflyExt.resources
   Assembly Version: 1.0.0.23
   Win32 Version: 1.0.0.23
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/en-US/AcButterflyExt.resources.DLL
----------------------------------------
mscorlib.resources
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_fr_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.resources
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.resources/v4.0_4.0.0.0_fr_b77a5c561934e089/System.resources.dll
----------------------------------------
UIAutomationProvider
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
----------------------------------------
Accessibility
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
UIAutomationTypes
   Assembly Version: 4.0.0.0
   Win32 Version: 4.0.30319.1 built by: RTMRel
   CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
----------------------------------------
AcDialogToolTips
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcDialogToolTips.DLL
----------------------------------------
AcDialogToolTips.resources
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/en-US/AcDialogToolTips.resources.DLL
----------------------------------------
AcLayerTools
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/AcLayerTools.DLL
----------------------------------------
AcLayerTools.resources
   Assembly Version: 18.2.0.0
   Win32 Version: 18.2.51.0.0
   CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202012%20-%20English/en-US/AcLayerTools.resources.DLL
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
   <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT de****** registered on the computer
rather than be handled by this dialog box.

 

Do you have an idea what cause that and how do get it back?

Link to comment
Share on other sites

Hi,

 

Is it possible that when I load autocad, load my DWG, that if the layer is already frozen the command does nothing ? I have to unfreeze each of the layer at first for the command to work... any ideas ?

 

-edit-

Or... as i'm trying it, when I run the command ( "a1" ) it does nothing even if I thaw the layer... what seems to be in conflict with the lisp is that my layer is locked. The layer thawed as I unlocked it... again, any idea?

 

Thanks

Link to comment
Share on other sites

The program appears to function as intended in my environment; locked layers won't affect its operation, and similarly, if the layer is already frozen, it will simply be thawed. The only thing that would affect the freeze operation is if the layer is current when frozen, though, this could also be accounted for using a simple conditional to check the layer against the setting of the CLAYER System Variable:

 

(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 in ss )
   (if (null (setq en (tblobjname "LAYER" layer)))
       (entmake
           (list
              '(0 . "LAYER")
              '(100 . "AcDbSymbolTableRecord")
              '(100 . "AcDbLayerTableRecord")
               (cons 2 layer)
              '(70 . 0)
           )
       )
       (if
           (and
               (not (eq (strcase (getvar 'CLAYER)) (strcase layer)))
               (setq en (entget en)
                     dx (assoc 70 en)
                     en (entmod (subst (cons 70 (boole 6 1 (cdr dx))) dx en))
               )
               (setq ss (ssget "_X" (list (cons 8 layer))))
           )
           (repeat (setq in (sslength ss))
               (entupd (ssname ss (setq in (1- in))))
           )
       )
   )
   (princ)
)

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