Combination of LISP scripts

Combination of LISP scripts

Anonymous
Not applicable
3,694 Views
21 Replies
Message 1 of 22

Combination of LISP scripts

Anonymous
Not applicable

Hello,

 

sorry if this is off-topic or forbidden to post due to credits to individual authors, but... I have an issue. I have to create a LISP script that will create a layer with a name based on a text object in AutoCAD. As a next step, user has 3 choices and any of them adds a corresponding string of text (which the user can enter manually inside the LISP itself) and it will add it as a suffix to the layer name and create a new layer for each and every individual suffix. I have LISP scripts that I've found on Google that do this, but I don't know how to combine them (LISP syntax makes me dizzy 😄

 

Thank you and kinds regards. 

0 Likes
Accepted solutions (4)
3,695 Views
21 Replies
Replies (21)
Message 2 of 22

rkmcswain
Mentor
Mentor

Post or attach the code here, unless forbidden by the code author. 

Message 3 of 22

Anonymous
Not applicable

Here they are. I explicitly apologise to every author of attached files, if these aren't allowed to be distributed. 

 

Basically, layer.create.lsp creates a new layer based on selected text object. 

 

Name.lsp creates new layers based on user choice (for e.g. EXC or SHC or ILC). 

 

Prefix.lsp creates a prefix to a layer based on a selected object.

 

My goal is to have a LISP script, which will create a new layer with a name from the text object user selected and it will have a string from user's commandline choice (like in the Name.lsp). I also need to add a prefix to every newly created layer (note that the prefix doesn't change, it's same for every layer).

 

Thank you and kind regards,

Dusan

 

0 Likes
Message 4 of 22

ronjonp
Mentor
Mentor

That prefix.lsp looks like something I wrote. Are all you needing is to select some objects and have them put on a new layer with a suffix appended to the objects layername?

Message 5 of 22

Anonymous
Not applicable

It quite possibly could be yours - I was relying on my Google skills until I found out that I have to combine different LISP scripts. I have some LISP skills and syntax understanding, but that's above my grade. 

 

Basically, yes. I have a profile name in a text form (basic text object in AutoCAD), I have a constant prefix (preferably, user can type that one out and it appends to the layer name) and a suffix that is basically a choice between 3 different suffixes based on user choice (user selects one choice and it creates a bunch of layers whose names are already prewritten in LISP script). I can sketch out a flowchart if that helps. Any help would be greatly appreciated and credited. 

 

Thanks and kind regards.

0 Likes
Message 6 of 22

ronjonp
Mentor
Mentor

@Anonymous wrote:

It quite possibly could be yours - I was relying on my Google skills until I found out that I have to combine different LISP scripts. I have some LISP skills and syntax understanding, but that's above my grade. 

 

Basically, yes. I have a profile name in a text form (basic text object in AutoCAD), I have a constant prefix (preferably, user can type that one out and it appends to the layer name) and a suffix that is basically a choice between 3 different suffixes based on user choice (user selects one choice and it creates a bunch of layers whose names are already prewritten in LISP script). I can sketch out a flowchart if that helps. Any help would be greatly appreciated and credited. 

 

Thanks and kind regards.


Post a drawing with before and after .. or your diagram. I'm having trouble following exactly what you want.

Message 7 of 22

Anonymous
Not applicable

Here it is, I hope it's understandable 🙂 

Thanks in advance. 

0 Likes
Message 8 of 22

ronjonp
Mentor
Mentor

To make this work I need all the information. What are the other two options after 'SHC'. Please post these as text on the forum not in an image.

Message 9 of 22

Anonymous
Not applicable

Sorry for not providing details. I'll post the info pronto. You can use it as a placeholder, afterwards I can finish the precise details. Here it is: 

User-generated constant prefix: -COMPANY-09

 

Type: RQ HR, BT Q, BT HR

 

User choices: EXC, SHC and ILC

 

User chooses option "EXC" - it creates 3 layers with ending suffixes TH, B and I (first is -COMPANY-09-RQ HR-EXC-TH,

second is -COMPANY-09-RQ HR-EXC-B and third one is -COMPANY-09-RQ HR- EXC - I)

 

EXC - TH, B, I; 

SHC -TH, TI, BW_A, BW_B, IN; (every individual layer is separated by a comma, for e.g. -COMPANY-09-

ILC - SH, LH, RH

 

So after all, you get the end result: -COMPANY-09      -                        RQ HR                        -                     EXC-TH

                                                                   prefix by user            user selection from text                        user choice from                                                                                                                                                                                       commandline

Every step in the layer name is separated by a "-". 

0 Likes
Message 10 of 22

ronjonp
Mentor
Mentor
Accepted solution

I don't think we're on the same page as to what you want. Here is some quick code from another post modified to do some of what you want I think...

(defun c:nameme	(/ el l pr s sf tm x)
  ;; RJP » 2019-08-19
  (or (setq pr (getenv "RJP_LayerPrefix")) (setq pr "-COMPANY-09"))
  (or (setq sf (getenv "RJP_LayerSuffix")) (setq sf "SHC"))
  (cond	((and (setq pr (cond ((/= "" (setq tm (getstring (strcat "\nEnter prefix [<" pr ">]: ")))) tm)
			     (pr)
		       )
	      )
	      (progn (initget 0 "EXC SHC ILC")
		     (setq sf (cond ((getkword (strcat "\nChoose [EXC/SHC/ILC]: <" sf ">")))
				    (sf)
			      )
		     )
	      )
	      (setq s (ssget ":L" (list (cons 8 (strcat "~" pr "*" sf)))))
	 )
	 (setenv "RJP_LayerPrefix" pr)
	 (setenv "RJP_LayerSuffix" sf)
	 (setq sf
		(cadr
		  (assoc sf
			 ;; Edit the descriptions to suit your needs
			 '
			  (("EXC" "EXC Description") ("SHC" "SHC Description") ("ILC" "ILC Description"))
		  )
		)
	 )
	 (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	   (setq l (cdr (assoc 8 (entget e))))
	   (setq el (entget (tblobjname "layer" l)))
	   (if (not (tblobjname "layer" (setq x (strcat pr "-" l "-" sf))))
	     (entmakex (subst (cons 2 x) (assoc 2 el) el))
	   )
	   (entmod (subst (cons 8 x) (assoc 8 (entget e)) (entget e)))
	 )
	)
  )
  (princ)
)
Message 11 of 22

Anonymous
Not applicable

Okay, this is pretty much spot on, this is exactly what we need. I'm gonna accept this as a solution.

 

Really, thank you immeasurably. 

 

Kind regards. 

 

EDIT: I've just noticed - the text object user selects should be immediately placed after prefix instead of a zero. 

Also, EXC, ILC descriptions - is there a way to create more layers based on description? Line of text in LISP script named "ILC description" - is it just one field or I can add more descriptions?

0 Likes
Message 12 of 22

dbhunia
Advisor
Advisor

I have made some changes (a quick alteration) in the file "layercreate.lsp" & "name.lsp"....... Try those.......

 

You need to add some functions for "ILC" options in the file "name.lsp"....... 

 

I think you do not need any other options for the function "allprops"....... 

 

Hopefully you can manage the rest....... (For Layer Naming alternation change the sequence of "strcat" in "layname")

 

 

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 13 of 22

Anonymous
Not applicable

This is great, too. I'd just like to know, why does it repeats the prefix 2 times if I enter something? Also, can you tell me wher in LISP file can I edit the part of the layer name, which is added by selecting either EXC, ILC or SHC? Because now, selecting any of those three options does not add anything. 

 

Thank you very kindly. 

 

(I'm gonna mark this too as a solution).

0 Likes
Message 14 of 22

dbhunia
Advisor
Advisor
Accepted solution

This is perfect for me ........ (you need to load both the file  "layercreate.lsp" & "name.lsp", then run "lc")

 

watch the attach video ....... 

 

or show me your problem.......

 

 

Or try with the attached LISP, I just combined the both file into one.......


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 15 of 22

Anonymous
Not applicable

Sir, this is great. I deeply apologise, I didn't load both of those scripts, so naturally, it didn't load anything after I selected EXC or SHC. Now I get it. This is the complete solution. 

 

Thank you for your time and knowledge.

 

Dusan

0 Likes
Message 16 of 22

ronjonp
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Okay, this is pretty much spot on, this is exactly what we need. I'm gonna accept this as a solution.

 

Really, thank you immeasurably. 

 

Kind regards. 

 

EDIT: I've just noticed - the text object user selects should be immediately placed after prefix instead of a zero. 

Also, EXC, ILC descriptions - is there a way to create more layers based on description? Line of text in LISP script named "ILC description" - is it just one field or I can add more descriptions?


Glad you got something working. Unfortunately I still don't understand what you're trying to achieve.

 

Can you elaborate on "the text object user selects should be immediately placed after prefix instead of a zero. " ?

 

Creating more descriptions for layers is quite easy see the code below. You can add as many descriptions to each as you need.

(defun c:nameme	(/ el l pr s sf tm x)
  ;; RJP » 2019-08-20
  (or (setq pr (getenv "RJP_LayerPrefix")) (setq pr "-COMPANY-09"))
  (or (setq sf (getenv "RJP_LayerSuffix")) (setq sf "SHC"))
  (cond	((and (setq pr (cond ((/= "" (setq tm (getstring (strcat "\nEnter prefix [<" pr ">]: ")))) tm)
			     (pr)
		       )
	      )
	      (progn (initget 0 "EXC SHC ILC")
		     (setq sf (cond ((getkword (strcat "\nChoose [EXC/SHC/ILC]: <" sf ">")))
				    (sf)
			      )
		     )
	      )
	      (setq s (ssget ":L" (list (cons 8 (strcat "~" pr "*" sf)))))
	 )
	 (setenv "RJP_LayerPrefix" pr)
	 (setenv "RJP_LayerSuffix" sf)
	 (setq sf
		(cdr (assoc
		       sf
		       ;; Edit the descriptions to suit your needs
		       '
			(("EXC" "EXC Description" "EXC Description2" "EXC Description3" "EXC Description4")
			 ("SHC" "SHC Description" "SHC Description2" "SHC Description3" "SHC Description4")
			 ("ILC" "ILC Description" "ILC Description2" "ILC Description3" "ILC Description4")
			)
		     )
		)
	 )
	 (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	   (setq l (cdr (assoc 8 (entget e))))
	   (setq el (entget (tblobjname "layer" l)))
	   (foreach a sf
	     (if (not (tblobjname "layer" (setq x (strcat pr "-" l "-" a))))
	       (entmakex (subst (cons 2 x) (assoc 2 el) el))
	     )
	   )
	   ;; Uncomment the code below to modify the object selected .. since we are creating
	   ;; multiple layers, I'm not sure which one is the 'correct' one to use ;)
	   ;; (entmod (subst (cons 8 x) (assoc 8 (entget e)) (entget e)))
	 )
	)
  )
  (princ)
)

image.png

 

Message 17 of 22

Anonymous
Not applicable

Yours is great, now that I see it, I just didn't know how to make a list in lisp, thought it had to be separated by a comma. Now I undertand it. The problem is that 0, as I see in your picture. Instead of a zero, it needs to be a text that the user selected. For e.g. text reads "RG PQ", then the name should be "company1-RG PQ-EXC Description". That's the only hiccup I see now, maybe I'm looking at it the wrong way.

 

Really thanks for your time.

 

Dusan

0 Likes
Message 18 of 22

ronjonp
Mentor
Mentor
Accepted solution

Give the code below a try. It will filter out text and retrieve the text string to include in the layer name.

(defun c:nameme	(/ el l pr s sf str tm x)
  ;; RJP » 2019-08-20
  (or (setq pr (getenv "RJP_LayerPrefix")) (setq pr "-COMPANY-09"))
  (or (setq sf (getenv "RJP_LayerSuffix")) (setq sf "SHC"))
  (cond	((and (setq pr (cond ((/= "" (setq tm (getstring (strcat "\nEnter prefix [<" pr ">]: ")))) tm)
			     (pr)
		       )
	      )
	      (progn (initget 0 "EXC SHC ILC")
		     (setq sf (cond ((getkword (strcat "\nChoose [EXC/SHC/ILC]: <" sf ">")))
				    (sf)
			      )
		     )
	      )
	      (setq s (ssget ":L" (list '(0 . "*TEXT") (cons 8 (strcat "~" pr "*" sf)))))
	 )
	 (setenv "RJP_LayerPrefix" pr)
	 (setenv "RJP_LayerSuffix" sf)
	 (setq sf
		(cdr (assoc
		       sf
		       ;; Edit the descriptions to suit your needs
		       '
			(("EXC" "EXC Description" "EXC Description2" "EXC Description3" "EXC Description4")
			 ("SHC" "SHC Description" "SHC Description2" "SHC Description3" "SHC Description4")
			 ("ILC" "ILC Description" "ILC Description2" "ILC Description3" "ILC Description4")
			)
		     )
		)
	 )
	 (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	   (setq l (cdr (assoc 8 (entget e))))
	   (setq str (cdr (assoc 1 (entget e))))
	   (setq el (entget (tblobjname "layer" l)))
	   (foreach a sf
	     (if (not (tblobjname "layer" (setq x (strcat pr "-" str "-" a))))
	       (entmakex (subst (cons 2 x) (assoc 2 el) el))
	     )
	   )
	   ;; Uncomment the code below to modify the object selected .. since we are creating
	   ;; multiple layers, I'm not sure which one is the 'correct' one to use ;)
	   ;; (entmod (subst (cons 8 x) (assoc 8 (entget e)) (entget e)))
	 )
	)
  )
  (princ)
)
(vl-load-com)
0 Likes
Message 19 of 22

Anonymous
Not applicable

This is it. Exactly what I've wanted. I am immeasurably grateful to both of you for these slick solutions. On a side note, do you have any resources for AutoLISP learning? I just want to brush up my skills more. 

 

Thank you, this is great. Both of these have been accepted as solutions. 

 

Thank you once again.

 

Dusan

0 Likes
Message 20 of 22

dbhunia
Advisor
Advisor

You welcome 🙂 

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes