help with Lisp to change line to a layer from its original color

Hi.
I'm importing a drawing from hydraulic software,
and all the lines that represent pipes are in one layer and differentiate by color.
I'm trying to write a lisp that loops through al those line and change them to a different layer based on their color,
for example:
if the line color is 140,then change it to layer P_DN125 and assign color by layer,
if the line color is 112,then change it to layer P_DN110 and assign color by layer,
and so on.
i fount a simple lisp that change the color from a fixed one to color by layer,
but i struggle with converting it in to a loop and to change the layer.

(defun c:byl (/ ss)
(if (setq ss (ssget "x" '((8 . "Main and secondary pipes"))))
(command "
.chprop" "_si" ss "_color" "_bylayer" "")
)
(princ

Thanks,
Ari.

Comments

  • James Maeding
    edited October 2020

    @aridzv
    all, right, some basic lisp in the morning!
    You will loop through the entity names and change each as needed.
    Thereis a trick, in that if a entity is bylayer in color, it has no dxf color property - group 62
    I am assuming index color here, say if you are dealing with trucolor.
    I just updated this compared to original post....
    (defun c:byl (/ SS COLOR ELIST ENAME INDEX LNAME)
    (if (setq ss (ssget "x" '((8 . "Main and secondary pipes"))))
    (progn
    (setq index 0)
    (repeat (sslength ss)
    (setq ename (ssname ss index)
    elist (entget ename)
    color (cdr (assoc 62 elist))
    )
    (if color
    (progn
    (setq lname (itoa color))
    ;only make layer if needed, its slow. TBLSEARCH is fast.
    (if (not (TBLSEARCH "layer" lname))
    (command "-layer" "m" lname "")
    )
    (entmod (subst (cons 62 lname)(assoc 62 elist) elist)) ;entmod-entupd is way faster than chprop
    (entupd ename)
    )
    )
    (setq index (+ 1 index))
    )
    )
    (princ)
    )
    )
    ok, the formatting in this forum kills all indentation, look at attached lisp in following post. Note that I use tabs, so you should set the spacing to 2, or replace with 2 spaces to get looking decent.

    I would recommend you also have this "get" function handy, for listing the dxf pairs of any entity:
    (DEFUN C:GET () (PRINC "\nPrint dxf groups")
    (setq e1 (car (entsel)))(foreach i (entget e1 '("*"))(print i))(textscr)(princ))

    and this is a good one too, for listing "VBA" style properties. You can do exactly the same with them - get and set them - but using vla commands instead of dxf group ones like entmod and entmake.
    (DEFUN C:DUMP () (PRINC "\nDump object props to screen")
    (vlax-dump-Object (vlax-ename->vla-object (CAR (ENTSEL "\nPick Object:"))))(textscr)(PRINC))

  • I would probably try something like this. It is not tested. It would look a lot better if the forum software did not strip out the indents.
    [code]
    (defun c:byl (/ ss)
    (if (null (tblsearch "layer" "P_DN125"))
    (command "-layer" "make" "P_DN125" "color" 140 "paper" "")
    )
    (if (null (tblsearch "layer" "P_DN110"))
    (command "-layer" "make" "P_DN110" "color" 112 "paper" "")
    )
    (if (setq ss (ssget "x" '((8 . "Main and secondary pipes"))))
    (progn
    (setq ss (vle-selectionset->list ss)
    (foreach memb ss
    (cond ( (= (vle-entget 62 memb) 140)
    (vle-entmod 8 "P_DN125")
    (vle-entmod 62 256)
    )
    (cond ( (= (vle-entget 62 memb) 112)
    (vle-entmod 8 "P_DN110")
    (vle-entmod 62 256)
    )
    )
    )
    )
    )
    [/code]

  • here is the lisp. should look like:

  • The lines to create the layers should be
    (command "-layer" "make" "P_DN125" "color" 140 "P_DN125" "")
    and
    (command "-layer" "make" "P_DN110" "color" 112 "P_DN110" "")

    You may need to change the colers.

  • @H Martin Shoemaker
    I guess that is the whole trick, how to get a different layer name for each and use it. :)

  • James,

    Nice code. How did you get it to format? Pasted as an image?

  • Hey there,
    I actually composed in the autocad VLIDE, took a screenshot and added image.
    I have a long history with the VLIDE, and the BLADE will color code the same if you tweak its settings.
    I cannot get this forum's code formatting to behave. I tried several tricks but none worked...
    thx

  • Jason Bourhill
    edited October 2020

    Since you're making changes based on entity colour you can use ssget to build a selection set of just those entities. e.g.

    (defun Col2Lay (col laynm / sset no)
        (setq sset (ssget "x" (list (cons 62 col))))
        (cond (sset
                        (setvar 'CMDECHO 0)
                        (setq no (sslength sset))
                        (command "._-LAYER" "_M" laynm "_C" col laynm "")
                        (command "._CHPROP" sset "" "_LA" laynm "_C" 256 "")
                        (setvar 'CMDECHO 1)
                    )
    
        )
        no
    )
    

    Regards,
    Jason Bourhill
    BricsCAD V20 Ultimate
    CAD Concepts

  • @Jason Bourhill
    interesting. I call that "melting the snowman" as it eats away at the ss until no more.
    I would point out to the OP, what you are seeing is a range of approaches.
    Anything that uses a "(command...)" is called scripting in the computer world.
    In acad/bcad, its the slowest way to do anything.
    When you encounter a dwg with 500k entities, this stuff starts to matter.
    I think it would be funny to have a contest to see who could make this go the slowest, without unnecessary statements.
    I bet Owen would win!
    (yes, I finally got a dig on him, He will appreciate the humor...)

  • @James Maeding said:
    Anything that uses a "(command...)" is called scripting in the computer world.
    In acad/bcad, its the slowest way to do anything.
    When you encounter a dwg with 500k entities, this stuff starts to matter.

    I'm aware that command calls can be slow, but it would be easy to swap this portion of code out with other methods. The OP referenced command calls, so I stuck with it. I was really just trying to highlight you could use SSGET to find the entities of particular colour.

    BricsCAD LISP is not Autolisp, there are many differences!! Autolisp is really a subset of BricsCAD LISP. For example the LISP developer reference states that using COM based entity creation is the fastest method in BricsCAD. On top of that, for working with large amounts of data you can temporarily suspend database transactions. I recommend that people take a look at the Lisp Developer Support Package (LDSP) to see what is available.

    Regards,
    Jason Bourhill
    BricsCAD V20 Ultimate
    CAD Concepts

  • @Jason Bourhill
    I love how the bricscad team cares more about lisp than adesk. It will pay off more and more as time goes on.
    For others reading this, don't interpret this as meaning your lisps for acad will have issues in bcad.
    Its not the case. I have one codebase for both, and its extensive. I look forward to learning the bcad lisp advantages more.
    I could spend another lifetime doing tools for acad/bcad, there is so much to do.