+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
  1. #1
    Forum Newbie
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    7

    Default Lisp to sort our not existing layer from list and create selection using the existing

    Registered forum members do not see this ad.

    Hello,

    First of all, I'm quiet new to lisp (one week of self study to create my first automation) but on this point I'm stuck

    My usecase:
    I got a drawing from a supplier and want to modify some objects based on their layer. Unfortunately I have only a list of layer which are of interest for me and could be in the drawing, but not all of them are in every drawing from that supplier. Additional there are some objects in the drawing on layers which are not to be modified

    An example for better understanding:
    Layer to be modified are in a list:
    (setq ltbm (list "131" "144" "154" "204" "Furniture" "Water"))

    In the first drawing are the following layer:
    (list "1" "17" "131" "204" "Stone" "Furniture")
    Second drawing has other layer in use:
    (list "5" "76" "144" "154" "204" "Air" "Water")

    The Problem:
    First:
    I need to find out which of the interesting layer are used.
    In the first case:
    (list "131" "204" "Furniture")
    And in second case:
    (list "144" "154" "204" "Water")

    And second:
    I need to select all objects on that layers. If I do the selection command manually.
    For the first case it would be:
    (ssget mod (ssget "_X" '((-4 . <OR)(8 . "131")(8 . "204")(8 . "Furniture")(-4 . OR>))))
    While in second case:
    (ssget mod (ssget "_X" '((-4 . <OR)(8 . "144")(8 . "154")(8 . "204")(8 . "Water")(-4 . OR>))))

    With the selection set mod I can go ahead myself.

    So my issues are:
    How to find out the layers which are used in the drawing. Than how to get the subset of layer which are of interest to me. And than how to get the selection of objects on these layers done.

    I hope someone can help me out there. Would be so much appreciated.

    Best regards,
    Markus

  2. #2
    Senior Member
    Using
    not applicable
    Join Date
    Jun 2016
    Posts
    485

    Default

    The ssget filter can contain layers that do not exist in the drawing. So much of what you are trying to do is not necessary. But checking if ssget returns a selection set instead of nil is generally advisable:
    Code:
    (if (setq mod (ssget "_X" (8 . "131,144,154,204,Furniture,Water")))
      (progn
        ; Do something
      )
    )
    To check if a layer exists you can use this:
    Code:
    (tblobjname "layer" "Water")
    BricsCAD 16

  3. #3
    Forum Newbie
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    7

    Default

    Oh, really. It works without that nasty OR too. When I tried to put some layer not existing in my code before it returned an error. Looks like I'll have to doublecheck once again when I'm back at the company
    If the solution is that easy it would be really great!!
    Thank you in advance!!

  4. #4
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,635

    Default

    You can use your list of layers and add a bit to Roy_043 suggestion at the alert you could do a add layer etc.

    Code:
    (setq ltbm (list "131" "144" "154" "204" "Furniture" "Water"))
    (repeat (setq x (length ltbm))
    (setq chk (tblobjname "layer" (nth (setq x (- x 1)) ltbm)))
    (if (= chk nil)
    (Alert (strcat "your layer " (nth x ltbm) "  " "is missing"))
    (princ (strcat "\n" (nth x ltbm)))
    )
    )
    A man who never made mistakes never made anything

  5. #5
    Forum Newbie
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    7

    Default

    Oh no, If I'm using the code like Roy_043 suggested I get an error while loading the lisp.
    Corrupt Argument Type: consp "131, 144, 154, 204, Furniture, Water"

    Some ideas how to solve it?

  6. #6
    Senior Member
    Discipline
    Civil
    Aftertouch's Discipline Details
    Discipline
    Civil
    Details
    Engineer
    Using
    AutoCAD 2017
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    168

    Default

    This code does something with all objects on the found layers, defined as 'mod'

    Code:
    (defun C:CadTutor ( / )
    (setq layerslist (list "layer1" "layer2" "layer3" "layer4"))
    	(foreach layer layerslist
    		(if (/= (tblsearch "LAYER" layer) nil)
    			(progn
    				(setq mod (ssget "_X" (list (cons 8 layer)))) ; selects all object on the found layers
    					; Code here to do something with the found layers
    			)
    			(progn
    				(princ (strcat "\nLayer: " layer " not found in drawing"))
    			)
    		)
    	)
    (princ)
    )
    Last edited by Aftertouch; 11th Sep 2017 at 08:21 am.

  7. #7
    Senior Member
    Using
    not applicable
    Join Date
    Jun 2016
    Posts
    485

    Default

    Quote Originally Posted by Ament View Post
    Oh no, If I'm using the code like Roy_043 suggested I get an error while loading the lisp.
    Corrupt Argument Type: consp "131, 144, 154, 204, Furniture, Water"

    Some ideas how to solve it?
    Oops, my bad. The code should be:
    Code:
    (if (setq mod (ssget "_X" '((8 . "131,144,154,204,Furniture,Water"))))
      (progn
        ; Do something
      )
    )
    BricsCAD 16

  8. #8
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2015
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,026

    Default

    Quote Originally Posted by Aftertouch View Post
    This code does something with all objects on the found layers, defined as 'mod'
    Good one.

    I just commented a few statements / codes for your reference and hope you don't mind.

    Quote Originally Posted by Aftertouch View Post
    Code:
    (defun C:CadTutor ( / layerslist  mod ) ;; localizing variables is very important unless any is needed to be global.
    (setq layerslist (list "layer1" "layer2" "layer3" "layer4"))
    	(foreach layer layerslist
    		(if (/= (tblsearch "LAYER" layer) nil) ;; if statement is equal to this: (if (tblsearch "LAYER" layer)  ..... )
    			(progn
    				(setq mod (ssget "_X" (list (cons 8 layer)))) ; selects all object on the found layers
    					; Code here to do something with the found layers
    			)
    			(progn ;; There is no need to progn function since you have only one expression.
    				(princ (strcat "\nLayer: " layer " not found in drawing"))
    			)
    		)
    	)
    (princ)
    )

  9. #9
    Forum Newbie
    Discipline
    Architectural
    Using
    AutoCAD 2016
    Join Date
    Sep 2017
    Posts
    7

    Default

    Unfortunately the solution of Roy isn't working as expected. It always return nil. Maybe because it is looking for a layer with the characters in the quotes. But the way Aftertouch proposed is working well for me. Thank you so much!

  10. #10
    Senior Member
    Using
    not applicable
    Join Date
    Jun 2016
    Posts
    485

    Default

    Registered forum members do not see this ad.

    @Ament:
    I don't understand your comment. The layers in the ssget filter match those supplied by you in the OP. Of course you would have to change them if they no longer apply. And of course you have to add code where I have put "Do something". Just copy-pasting the code will return nil.
    Try:
    Code:
    (if (setq mod (ssget "_X" '((8 . "131,144,154,204,Furniture,Water"))))
      (progn
        (princ (strcat "\n" (itoa (sslength mod)) " entities in selection set "))
        (princ)
        ; Do something
      )
    )
    BricsCAD 16

Similar Threads

  1. Replies: 7
    Last Post: 22nd Aug 2017, 12:15 pm
  2. LISP | IF Conditions | Copy to Existing Layer
    By bluebravo in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 12th Jun 2017, 03:29 am
  3. HELP: LISP to create multiple & resized viewport base on an existing viewport
    By vernonlee in forum AutoLISP, Visual LISP & DCL
    Replies: 31
    Last Post: 10th Jun 2015, 01:17 am
  4. create new layer not visible in existing viewports
    By designerstuart in forum AutoCAD Drawing Management & Output
    Replies: 9
    Last Post: 27th Oct 2014, 06:08 pm
  5. Layer Create or Set Current if already existing
    By clint0577 in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 29th Oct 2012, 02:11 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts