Create new layer from object lsp

Create new layer from object lsp

lambertb
Collaborator Collaborator
661 Views
6 Replies
Message 1 of 7

Create new layer from object lsp

lambertb
Collaborator
Collaborator

I am working on updating a lsp. I want to gather layer information including name, color, linetype, lineweight, and plot style, and then create a new layer with the properties and suffix. But I can't get the tool to the end. Thanks for any help.

C3D 2022 and 2024
0 Likes
662 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

[I can't experiment here, in Acad2020, where Plot Style is not an option for a Layer setting, so I get an invalid option error.  It does, however, create the Layer and assign the other properties to it.]

 

If your intent is always to make a new Layer with all the same properties as that of a selected object except for a suffix on the Layer name, that can be done rather simply without the dialog box.  Even more simply if the suffix is always to be the same "-DEMO."  Would that serve your purpose?

Kent Cooper, AIA
0 Likes
Message 3 of 7

komondormrex
Mentor
Mentor

quick correction to start it breathe

(defun c:demolayer( / lyr lyr1 lyr2 lyr3 lyr3a lyr4 lyr5 lyr6 lyrsuff )

(defun cancel1 (/)
(setq lyr5 nil)
(princ))
;-------------------------------
(defun makedemolyr ( / lyrsuff2)
(setq lyr33 (get_tile "lyrcolor1"))
(setq lyr33a (atoi lyr33))
(setq lyr6 (nth (1- (atoi (get_tile "lyrweight1"))) '("ByLayer" "ByBlock" "Default" 0 0.05 0.09 0.13 0.15 0.18 0.20 0.25 0.30 0.35 0.40 0.50 0.53 0.60 0.70 0.80 0.90 1.00 1.06 1.20 1.40 1.58 2.00 2.11)))
(setq lyrsuff2 (get_tile "lyrsuff1"))
(setq newlyr (strcat lyr lyrsuff2))     ;this line will append the suffix to the end of the object layer name
;;;(done_dialog)

)
;-------------------------------
(setq lyr5 1)
(setq lyr1 (entget (car (entsel "\nSelect object: "))))
(setq lyr (cdr (assoc 8 lyr1)))    ;this line will capture the layer name from an object you select

(setq lyr2 (tblsearch "layer" lyr))

(setq lyr3 (cdr (assoc 62 lyr2)))   ;layer color
(setq lyr3a (itoa lyr3))
(setq lyr4 (cdr (assoc 6 lyr2)))    ;layer linetype

(setq lyrsuff (load_dialog "demolayers.dcl"))
(new_dialog "demolayers" lyrsuff)
(set_tile "lyrname1" lyr)
(set_tile "lyrcolor1" lyr3a)
(set_tile "lyrweight1" "0.13")
(set_tile "lyrsuff1" "-DEMO")
(action_tile "Make_Layer" "(makedemolyr) (done_dialog 1)")
(action_tile "cancel" "(cancel1)")
(setq lyr5 (start_dialog))
(unload_dialog lyrsuff)

(if (= lyr5 1)
    (progn (if (< lyr33a 256)
               (progn (command "-layer" "M" newlyr "c" lyr33 "" "lt" lyr4 "" "lw" lyr6 "" ""); "PS" "Existing" "" "")
                      (prompt (strcat "\nLayer " newlyr " created.")))
               (alert "Color number must be 1-255"))
    )
)

  (princ)
)
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

.... If your intent is always to make a new Layer with all the same properties as that of a selected object except for a suffix on the Layer name....


Given the names of both the .lsp and .dcl files and the defined command, and the default suffix built into the command, along with your original description about the same Layer properties, I make so bold as to assume that is what's wanted.  In that case, this is enough, with no dialog box needed, only selection of an object:

(defun C:DEMOLAYER (/ esel ldata)
  (setq
    esel (entsel "\nSelect object to make -DEMO-suffixed Layer the same as its Layer: ")
    ldata (entget (tblobjname "layer" (cdr (assoc 8 (entget (car esel))))))
  ); setq
  (entmake
    (subst
      (cons 2 (strcat (cdr (assoc 2 ldata)) "-DEMO"))
      (assoc 2 ldata)
      ldata
    ); subst
  ); entmod
  (prin1)
)

 

Kent Cooper, AIA
0 Likes
Message 5 of 7

paullimapa
Mentor
Mentor

I've made the following modifications to your dcl & lsp file (attached) to get you started:

1. use AutoCAD's built-in color dialog selection function to pick the 1-255 color

2. select using a drop down from AutoCAD's standard lineweight list

I've made the following changes to your dcl:

Note: lines beginning with // are not executed

//: edit_box { label = "Enter color number 1-255 ";
//             key = "lyrcolor1";
//}
: text { label = "Select Layer Color Number 1-255:"; 
}
: text { key = "lyrcolor1";
         width = 3;
}
: image_button {
  key = "show_image";
  height = 2;
  width = 10;
}
}
//: edit_box { label = "Enter line weight 0.0mm - 2.11mm ";
//             key = "lyrweight1";
//}
: popup_list {
  label = "Select Layer Lineweight:";
  value = "0";
  key = "lyrweight1";
}

Lots of the lisp functions I included are actually taken from DDModify.lsp which originally was included way back in AutoCAD r12.

The color selection functions are:

chk_color, col_tile, get_color, set_color
The lineweight selection functions are:
getindex, make_lw_lists, set_lweight
The only item missing from your OP is Plot Style
If you download my Text Apps there's a lisp file called DDAttMod.lsp which includes functions on getting and setting Plot Styles ie:
getpstynam, make_ps_lists

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 7

lambertb
Collaborator
Collaborator

Thank you all. I am going to dig into some of the suggestions and see what I can produce.

C3D 2022 and 2024
0 Likes
Message 7 of 7

paullimapa
Mentor
Mentor

I just realized I forgot to include Linetype which was also in your OP.

I added the following row to your dcl to show the Linetype as a Text & a Button for selecting another:

: row { 
: button { label = "Linetype";
  key = "lyrltype1";
  width = 10;
}
: text { key = "lyrltype2";
         width = 23;
}
}

The code for Linetype selection implements the built-in AutoCAD Linetype command which brings up the Linetype  Manager window. But this requires your current dialog to be dismissed using done_dialog before the Linetype Manager window can successfully appear. So a condition instead of an if statement is used to test if the done_dialog setting lyr5 = 2 or 1 . Also a while loop is used so that the dialog will reappear again after Linetype selection is completed. Only after clicking OK setting lyr5 = 1 or Cancel setting lyr5 = nil will this stop the while loop and permanently close out to unload_dialog.

; settings to start dialog while loop
(setq lyr5 2)
(while (> lyr5 1)
 (new_dialog "demolayers" lyrsuff)
(action_tile "lyrltype1"  "(done_dialog 2)") ; end dialog to select ltype
; not needed after changed edit box to text label
;(action_tile "lyrcolor1"  "(chk_color (atoi $value))") ; checks color #
(action_tile "show_image" "(get_color lyr3)") ; action to select color
;;
(action_tile "accept" "(makedemolyr) (done_dialog 1)") ; action to make layer
(action_tile "cancel" "(cancel1)")
(setq lyr5 (start_dialog))
(cond 
 ((= lyr5 2) ; sel ltype
  (setq celtype (getvar"celtype")) ; save current setting
  (setvar"celtype" lyr4)           ; set as current ltype for ltype mgr
  (initdia)(vl-cmdf"_.Linetype")   ; launch ltype mgr
  (if(or(eq (strcase(getvar"celtype")) "BYLAYER")(eq (strcase(getvar"celtype")) "BYBLOCK"))
   (setq lyr4 "Continuous")        ; set as default linetype
   (setq lyr4 (getvar"celtype"))   ; set as layer linetype
  )
  (setvar "celtype" celtype)       ; restore original setting
 )
 ((= lyr5 1) ; accept
   (command "_.Layer" "_M" newlyr "_C" lyr33 "" "_Lt" lyr4 "" "_Lw" lyr6 "" "")
   (prompt (strcat "\nLayer " newlyr " created."))
 )
) ; cond
) ; while loop when lyr5 value is greater than 1
(unload_dialog lyrsuff)

So the revised DemoLayer dialog appears as shown:

paullimapa_0-1705604731103.png

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes