Jump to content

Switching between layers for creating a multileader


Recommended Posts

Posted (edited)

Hello everyone!

 

I try to complete a lisp that creates a multileader in a similar but different layer than the one which is active and then switches back to the last layer before.

ex: the active layer is named ''DDD-1009'' and while creating the mleader i need it to change to ''DDD-1009-txt'' which is the right layer for the mleader.

 

ive posted the whole code but i need help at the layers part (almost last part)

VLIDE shows me that the part which is broken is the ''itoa'' part. i dunno what to do to fix it.. plz help?

 

(defun C:TEST (/ COORD TEXT PGL PH PTL TL GL H ANS LINELAYER LAYER TEXTLAYER )
  (setq COORD ( getpoint "\nPick coordinate point: " ))
  (setq TEXT ( getpoint "\nPick text location: " ))

  (setq PGL ( getreal "\nGround level: " ))




  (setq PH ( getreal "\nHight: " ))




  (setq PTL (- PGL PH ))


  (setq TL (rtos PTL 2 2 ))

  (setq GL (rtos PGL 2 2 ))

  (setq H (rtos PH 2 2 ))




  (setq ANS (strcat "G.L=" GL "\nH=" H "\nT.L=" TL  ))


  (setq LINELAYER (getvar "clayer" ))

  (setq LAYER (itoa LINELAYER ))


  (setq TEXTLAYER (strcat LAYER "-txt" ))

  (setvar "clayer" TEXTLAYER )



  (command "mleader" COORD TEXT ANS  )

  (setvar "clayer" LINELAYER )

  (princ)) ; end



 

Edited by Mugna101
Posted

You need to be sure is that the layer name that you are trying to set it as current layer is existed into your current opened drawing which is in this case: "DDD-1009-txt".

 

(setq LINELAYER (getvar "clayer"))
(setvar "clayer" "DDD-1009-txt")
(command "mleader" COORD TEXT ANS)
(setvar "clayer" LINELAYER)

 

Posted
42 minutes ago, Tharwat said:

You need to be sure is that the layer name that you are trying to set it as current layer is existed into your current opened drawing which is in this case: "DDD-1009-txt".

 


(setq LINELAYER (getvar "clayer"))
(setvar "clayer" "DDD-1009-txt")
(command "mleader" COORD TEXT ANS)
(setvar "clayer" LINELAYER)

 

i have plenty of layers and they all start with DDD followed by a series of numbers.

for every layer like that i have a layer for text named the same but ending with -txt

 

i want the lisp to be able to recognize the current layer's name, make a string out of it in order to add -txt at the end and then use it to change layer to the layer that matches the newly formed string

 

if the current layer is ''DDD-1009'' the steps that will follow might be explained like this:

1. get the name of the current layer: ''DDD-1009''

2.add -txt at the end of the name: ''DDD-1009-txt''

3.set current layer to ''DDD-1009-txt''

4.run command mleader blah blah blah

5.set current layer to the old layer which is ''DDD-1009''

 

I have over 70 layers and a text layer for each one.

 

i dont need it to create another layer or edit the name, both layers ''DDD-1009'' and ''DDD-1009-txt'' already exist

 

''DDD-1009'' isnt the ONLY layer, there are lots and lots. though DDD is a constant in every layer, the numbers are changing, but for each number there are 2 layers, one that ends with a number and the other that ends with -txt after the number.

Posted

Here is an easy way for you to make it working although you need to enhance your codes with the use of 'IF' and 'AND' functions to get sure the inputs from the users are correct then process to next step. ;)

Don't forget to localize the variable 'lay' as in the following codes.

(setq LINELAYER (getvar "clayer"))
(if (tblsearch "LAYER" (setq lay (strcat LINELAYER "-txt")))  
  (progn
    (setvar "clayer" lay)
    (command "mleader" COORD TEXT ANS)
    (setvar "clayer" LINELAYER)
    )
  (alert (strcat "layer name < " lay " > is unavailable in this drawing "))
  )

 

  • Thanks 1
Posted
30 minutes ago, Tharwat said:

Here is an easy way for you to make it working although you need to enhance your codes with the use of 'IF' and 'AND' functions to get sure the inputs from the users are correct then process to next step. ;)

Don't forget to localize the variable 'lay' as in the following codes.


(setq LINELAYER (getvar "clayer"))
(if (tblsearch "LAYER" (setq lay (strcat LINELAYER "-txt")))  
  (progn
    (setvar "clayer" lay)
    (command "mleader" COORD TEXT ANS)
    (setvar "clayer" LINELAYER)
    )
  (alert (strcat "layer name < " lay " > is unavailable in this drawing "))
  )

 

INCREDIBLE! works perfect!

 

is it possible for you to explain a bit what you wrote and why it made such a difference?

on my drawings i had all the layers so no reason for the alert to pop.

THX A BUNCH BUD!

Posted

Hopefully the following would be clear to you to know how the process goes on.

 

;; this is to get the current active layer name
(setq LINELAYER (getvar "clayer")) 
;; the variable 'lay' holds the target layer name that you are after along wiht the
;; suffix '-txt' and the function tblsearch with "LAYER" is to check if the target
;; layer is already existed into current opened drawing, if it is not, then the alert
;; function would interfere to popup a message with the target layer name to the user
;; to let them know what's going on and this is much better than just exit quitely.
(if (tblsearch "LAYER" (setq lay (strcat LINELAYER "-txt"))) 
  (progn ;; so if the layer is already existed then
    (setvar "clayer" lay) ;; set the target layer name current
    (command "mleader" COORD TEXT ANS) ;; then draw the Mleader
    (setvar "clayer" LINELAYER) ;; and finally reset the layer active layer name current.
    )
  (alert (strcat "layer name < " lay " > is unavailable in this drawing ")) ;; here is the message with the unavailable layer name 
  )

 

  • Thanks 1
Posted (edited)

@Mugna101 Some more food for thought :)

(defun c:test (/ coord layer linelayer pgl ph ptl text textlayer)
  ;; Check if layer exists .. no need to get all that input then not have the layer you need ;)
  (if (tblobjname "layer" (setq textlayer (strcat (setq linelayer (getvar "clayer")) "-txt")))
    ;; Validate entries before moving on
    (if	(and (setq coord (getpoint "\nPick coordinate point: "))
	     (setq text (getpoint "\nPick text location: "))
	     (setq pgl (getreal "\nGround level: "))
	     (setq ph (getreal "\nHeight: "))
	     (setq ptl (- pgl ph))
	)
      (progn ;; Removed 'tl' 'gl' 'h' 'ans' vars. They only are used once.
	     (setvar "clayer" textlayer)
	     (command "mleader"
		      coord
		      text
		      (strcat "G.L=" (rtos pgl 2 2) "\nH=" (rtos ph 2 2) "\nT.L=" (rtos ptl 2 2))
	     )
	     (setvar "clayer" linelayer)
      )
    )
    (print "Layer needed not found!")
  )
  (princ)
)

 

Edited by ronjonp
*moved layer check to top since we're not creating it
  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...