LISP code to prevent any layers from being added to a DWG file

LISP code to prevent any layers from being added to a DWG file

Anonymous
Not applicable
2,294 Views
23 Replies
Message 1 of 24

LISP code to prevent any layers from being added to a DWG file

Anonymous
Not applicable

Hello,

I am trying to set up a 'standard template' with all the layers, LW, LType, colors predefined for my company.

My end goal is to have all the drawings created using only the layers from the template.

The issue is, if anyone tries to copy and paste an object (or anything for that matter) from an old file, it brings the layers along with it. 

Does anyone know a way that can prevent any additional layers from being added to my drawing, using LISP Code.

Maybe a dialog box can appear while pasting an object (with different layers), which will ask the user to reassign all layers from the selected object to any of the existing ones in the file.

Doing so, will ensure that no additional layers other than the predefined ones are populated in the drawings, making it more streamlined.

 

Any ideas would be greatly appreciated.

Thanks!

0 Likes
Accepted solutions (1)
2,295 Views
23 Replies
Replies (23)
Message 2 of 24

pbejse
Mentor
Mentor

@Anonymous wrote:

....

My end goal is to have all the drawings created using only the layers from the template.

The issue is, if anyone tries to copy and paste an object (or anything for that matter) from an old file, it brings the layers along with it. .......


 

Cant go wrong with CAD Standards _Laytrans

 

Sure you can use a reactor for _pasteclip / insert or whatever, that will eat up some of the precious memory we all users need, Its too troublesome to prevent or control what is happening with the Layers/linetpyes and what-not in real-time.  The next best thing can do is control whats happening at startup, ensuring all essentials are loaded, and all standard thingys are avaible for quick loading.

 

Flag the consistent offender and give them a warning/reprimand.. you know how it goes. 🙂

 

0 Likes
Message 3 of 24

hak_vz
Advisor
Advisor

Here is a code I've mocked for you, it's not tested for all possible situations but will work ok. 

In case that some of standard layers is locked error "On locked layer" will show.

 

Load it whenever  standard layer names have to be used.

Code assigns reactor on paste command and after it finishes it collects all Autocad object that are outside defined layers, and place them in a layer "wrong placed". At startup it will create all layers that are inside std_layers list if they don't exist in a layer. It is a good option to have template drawing with all standard layers created with defined properties like color, line type..... In that case existing layer wont be redefined or overwritten.

 

 

 

 

(vl-load-com)
(setvar 'cmdecho 0)
(setq o_layer (getvar 'clayer))
(setq std_layers  '("layer_1""layer_2" "layer_3" "wrong_placed")) ;put names of your layers here
(setq chkstr "")
(foreach str std_layers (setq chkstr (strcat chkstr str ",")))
(setq chkstr (substr chkstr 1 (-(strlen chkstr)1)))
(foreach lay std_layers(command "_.layer" "_thaw" lay "_make" lay "_color" 7 "" ""))
(setvar 'clayer o_layer)
(setvar 'cmdecho 1)


(defun c:check_layers ( / ss cnt item )
    (setq ss (ssget "X" (list (cons -4 "<not")(cons 8 chkstr)(cons -4 "not>"))))
    (setq cnt 0)
	(repeat (sslength ss)
		(setq item (ssname ss cnt))
		(setq item (vlax-ename->vla-object item))
        (vlax-put-property item 'layer "wrong_placed")
        (vlax-release-object item)
        (setq cnt(+ cnt 1))
    )
    (setq ss (ssget "X" '((8 . "wrong_placed"))))
    (if (and ss) (alert "Some objects were placed in a layer - wrong placed!"))
(princ)
)

(defun endCommand (calling-reactor endcommandInfo / thecommandend)
    (setq thecommandend (nth 0 endcommandInfo))
    (if (wcmatch thecommandend "PASTECLIP")(c:check_layers))
    (princ)
)

(vlr-command-reactor "test_layers" '((:vlr-commandEnded . endCommand)))

 

 

You have to replace this line with your standard layer names but leave layer "wrong_placed".

 

 

 

(setq std_layers  '("layer_1""layer_2" "layer_3" "wrong_placed")) 

 

 

 

After any paste operation, if there is a object placed in layer "wrong_placed" it will show an alert. You can approach to this in different ways, but I would immediately thereof replace those objects in appropriate layers so that layer "wrong placed" wont become overpopulated. To paste object correctly, open a temporary drawing, paste inside it and replace layer names before pasting it into a drawing.

 

I hope this will work for you. If it does, or if it needs just slight changes, select this as a SOLUTION.

Lisp file is in attachment, so just place it inside Acad trusted location.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 24

Anonymous
Not applicable

@pbejse . Thanks for the reply. I did try the DWS file but it does't ensure that the user will not be able to bring in any new layers. 

If the user has already created a detail (Drawing) previously with old standards, He/She will want to just copy and paste it in the new drawing rather than recreating it. Having a DWS file will surely prompt the user to reassign the layers but as we know it still is not the best practice to follow. 

 

Really appreciate your response though.

Thanks a lot!

0 Likes
Message 5 of 24

hak_vz
Advisor
Advisor
Accepted solution

Error corrected. File is in attachment.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 6 of 24

Anonymous
Not applicable

@hak_vz I think this is what I am looking for. I wil have to test it out once I get back to work on monday. 

Thank you so much!! 😁

0 Likes
Message 7 of 24

Anonymous
Not applicable

@hak_vz 

I tried to run your LISP routine on my DWG file. It works fine. Iv'e got one issue though.

Whenever the LISP is loaded, it defaults all the layer's Linetype to continuous and color to white. Is there a way to assign the layer's Linetype and Color in the LISP code for each layer?

 

Thanks once again!

0 Likes
Message 8 of 24

hak_vz
Advisor
Advisor

Yes it can be done. You either use template with predefined layers (a dwg file as I've told before) , or layer are created with lisp.

In my code I've used, let say a standard defaults, so that we have all layers predefined before lisp code starts checking paste operations. I would need your template file to update lisp provided or I' can create a sample script that extract layer info from an existing file, so can use it on your template. What info about layers would you need?

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 9 of 24

hak_vz
Advisor
Advisor

Also you can look this post and edit it to serve you.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 10 of 24

Anonymous
Not applicable

@hak_vz 

Sure, I will add somewhat like this to the end of your code

 

(COMMAND "LAYER""SET""CONSTRUCTION""COLOR" "11" "" "Ltype" "Continuous" "" "lweight" ".05" "" "")

 

for all the standard layers and have it set as per my requirements. 

Many Thanks! 

0 Likes
Message 11 of 24

hak_vz
Advisor
Advisor
(foreach lay std_layers(command "_.layer" "_thaw" lay "_make" lay "_color" 7 "" ""))

Replace this line with command statements to create standard layer.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 12 of 24

Anonymous
Not applicable

That's the plan! Thanks

0 Likes
Message 13 of 24

Anonymous
Not applicable

@hak_vz Sorry to bug you but if you can take a look at the lisp file attached. 

I have modified your Lisp routine to add new layers and assign Ltype, Color and LWeight to those layers.

Everything works fine till I copy and paste any blocks/objects from an old file with different layers.

 

My issue is, all the layers that the objects belong to (in an old file), show up in this current file when copied over. it happens even though the objects are being dumped into the 'wrong_layer'. If I apply the purge command, it removes most of the unwanted layers since ther's nothing in it anymore but the layers associated to blocks still remain as a part of the drawing. 

 

What I want the routine to do is not allow any new layers to be added to the drawing.

Is it not possible to just have all the objects (along with blocks) to be dumped into the 'Wrong_Layer' and not allow any new layers to be added? We can then manually assign the copied objects in the respective 'standard layers'

 

Thanks!

0 Likes
Message 14 of 24

hak_vz
Advisor
Advisor

When you have objects in a block when copied they recreate layers in new drawing. It is a good practice to have all elements that are added into a block placed to layer 0. There is a lot of scripts that do that. Try to google for "autolisp reset block to layer 0". Before copying from other file user would have to run such script to reset all blocks.

Also you must have layer "0" inside a list of standard since this is acad standard or basic layer.

Try to find a script. I'll look inside my archive, I guess I have it saved somewhere.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 15 of 24

hak_vz
Advisor
Advisor

Here is a code that puts all blocks to layer "0" . Save it to some file and use it on files from where blocks are copied.

 

;this is from Lee Mac
(defun blkto0 ( / idx lst sel )
    (if (setq sel (ssget '((0 . "INSERT"))))
        (repeat (setq idx (sslength sel))
            (block->0 (cdr (assoc 2 (entget (ssname sel (setq idx (1- idx)))))))
        )
    )
    (command "_.regen")
    (princ)
)
(defun block->0 ( blk / ent enx )
    (cond
        (   (member blk lst))
        (   (setq ent (tblobjname "block" blk))
            (while (setq ent (entnext ent))
                (entmod (subst-append 8 "0" (subst-append 62 256 (setq enx (entget ent)))))
                (if (= "INSERT" (cdr (assoc 0 enx)))
                    (block->0 (cdr (assoc 2 enx)))
                )
            )
            (setq lst (cons blk lst))
        )
    )
)

To run it from console rename blkto0 to c:blkto0

I hope this will help

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 16 of 24

Anonymous
Not applicable

@hak_vz 

I tried saving your code (Lee Mac Code) as a .lsp file and loaded that on to the drawing where the blocks are being copied to. 

It does not seem to work.

So I loaded the .lsp file (with the Lee Mac code) to the .dwg from which I am copying the objects. Typed in BLKTO0 and selected the objects but gives me an error saying 'No function definition - SUBST APPEND'

 

Clearly I'm doing something wrong.

 

Thanks in advance!

0 Likes
Message 17 of 24

hak_vz
Advisor
Advisor
(defun subst-append ( key val lst / itm )
    (if (setq itm (assoc key lst))
        (subst (cons key val) itm lst)
        (append lst (list (cons key val)))
    )
)

I have forgot it. Sorry

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 18 of 24

Anonymous
Not applicable

@hak_vz Perfect, it does work.

 

But not with dynamic blocks having visual parameters.

Is that a limitation of this routine?

0 Likes
Message 19 of 24

hak_vz
Advisor
Advisor

This routine has been created for classic blocks. Now, it would have to be updated for dynamic block, and it is not an easy task. I haven't been working a lot with dynamic block so have no clue how much work has to be done to reconfigure dynamic block. 

Here is a post about converting dynamic blocks to static. Maybe this is an option,

 

Also interesting story starts in a post just under this on a list of posts.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 20 of 24

Anonymous
Not applicable

@hak_vz 

Really appreciate your help, but I would rather not change the dynamic block library of my company to a static one. It really is handy to have dynamic blocks in the drawings for any quick changes that might be needed later on. 

 

Thank You so much for all your help!! Cheers! 😄

0 Likes