Command to draw a line on a specific layer and change the layer back to 0 when done

Command to draw a line on a specific layer and change the layer back to 0 when done

SMMJRC90
Contributor Contributor
1,883 Views
7 Replies
Message 1 of 8

Command to draw a line on a specific layer and change the layer back to 0 when done

SMMJRC90
Contributor
Contributor

I would like to make a command that will draw a line to a specific layer (even if I’m not on that layer when the comman is run). When the command is done, I would like to have the layer return to 0, or if possible, return to the layer I was on when the command began. 

command name: elecline

target layer: electrical

0 Likes
1,884 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

If you really mean a line [single], in simplest terms:

 

(defun C:ELECLINE ()

  (command

    "_.layer" "_set" "ELECTRICAL" ""

    "_.line" pause pause ""

    "_.layerp"

  )

  (princ)

)

 

If you mean a LINE command with the standard possibility to make multiple Lines with it:

(defun C:ELECLINE ()

  (command "_.layer" "_set" "ELECTRICAL" "")

  (command-s  "_.line")

  (command "_.layerp")

  (princ)

)

 

Or:

 

(defun C:ELECLINE ()

  (command "_.layer" "_set" "ELECTRICAL" "" "_.line")

  (while (> (getvar 'cmdactive) 0) (command pause))

  (command "_.layerp")

  (princ)

)

Kent Cooper, AIA
0 Likes
Message 3 of 8

calderg1000
Mentor
Mentor

Regards @SMMJRC90 

This code creates the Electrical layer if it does not exist and continues adding consecutive lines, finally keeping the current working layer

(defun c:elecline (/ spm ltab ly ln l p1 p2)
  (setq actdoc (vla-get-activedocument (vlax-get-acad-object)))
  (setq spm  (vla-get-modelspace actdoc)
        ltab (vla-get-layers actdoc)
  )
  (if (not (tblsearch "LAYER" "Electrical"))
    (vla-add ltab "Electrical")
  )
  (setq p1 (vlax-3d-point (Getpoint "\nEnter Point")))
  (while
    (setq l (vla-addline
              spm
              p1
              (setq p2 (vlax-3d-point
                         (Getpoint (vlax-safearray->list (vlax-variant-value p1))
                                   "\nEnter Point"
                         )
                       )
              )
            )
    )
     (vla-put-layer l "Electrical")
     (setq p1 p2)
  )
)

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 4 of 8

hak_vz
Advisor
Advisor

This code creates layer ELECLINE if not created, sets it as current before creating line.

Line is created in continuous mode. Uses command LINE to retain Undo option

 

(defun C:ELECLINE ( / *error* adoc curr_layer)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(if adoc (vla-endundomark adoc))
		(setvar 'cmdecho 1)
		(setvar 'clayer curr_layer)
		(princ)
	)
	(setq
		adoc (vla-get-ActiveDocument (vlax-get-acad-object))
		curr_layer (getvar 'clayer)
	)
	(vla-endundomark adoc)
	(vla-startundomark adoc)
	(setvar 'cmdecho 0)
	(command "_.layer" "_make" "ELECTRICAL" "")
	(setvar 'cmdecho 1)
	(command "_.line")
	(while (> (getvar 'cmdactive) 0) (command pause))
	(setvar 'clayer curr_layer)
	(vla-endundomark adoc)
	(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant

If you want the Layer created if it doesn't exist, and if it's possible that the Layer already exists but is frozen [so that it cannot be made current], then it should be more like this:

 

(defun C:ELECLINE ()

  (command "_.layer" "_thaw" "ELECTRICAL" "_make" "ELECTRICAL" "")

  (command-s  "_.line")

  (command "_.layerp")

  (princ)

)

 

It won't care if the Layer doesn't exist yet -- a message will go by that it couldn't find such a Layer to thaw, but it will carry on.  The Make option won't care if it already exists either, but if it does, will set it current in the process.

 

An advantage of using a LAYER command, rather than the approach in Message 3 [drawing the Line and then putting it on the Layer], is that if the Layer is currently off when you start the routine, either the Set or Make option in LAYER will turn it on in the process, with no need to use the On option.

Kent Cooper, AIA
0 Likes
Message 6 of 8

Kent1Cooper
Consultant
Consultant

@calderg1000 wrote:
....
  (while
    (setq l (vla-addline
              spm
              p1
              (setq p2 (vlax-3d-point
                         (Getpoint (vlax-safearray->list (vlax-variant-value p1))
                                   "\nEnter Point"
                         )
                       )
              )
            )
    )
     (vla-put-layer l "Electrical")
....

Another disadvantage of that approach [making the Line based on an unrelated-to-the-purpose User location pick, and then putting it on the Layer], in my opinion, is that the User is deprived of the

  [Close/eXit/Undo]

options available in the ordinary LINE command.

Kent Cooper, AIA
0 Likes
Message 7 of 8

calderg1000
Mentor
Mentor
Dear @Anonymous, your appreciation is correct, the command has its advantages, on the other hand I believed that it was not necessary to change layers and then return to the same layer

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

@calderg1000 wrote:
.... on the other hand I believed that it was not necessary to change layers and then return to the same layer

Certainly not "necessary," but it is, after all, what the OP was looking for in Message 1.  But if it's deemed desirable to not change the current Layer setting, I would suggest this:

 

(defun C:ELECLINE ()
  (command "_.layer" "_new" "ELECTRICAL" ""); in case it doesn't exist, but not made current
  (command-s  "_.pline")
  (command
    "_.chprop" "_last" "" "_layer" "ELECTRICAL" ""
    "_.explode" "_last" ; for result as LINEs; omit if OK to keep as Polyline
  )
  (princ)
)

 

It won't matter if the Layer does already exist, but to cover all possibilities, it should probably also be turned ON just in case, because the New option won't turn it on as the Make and Set options will:

 

....
  (command "_.layer" "_new" "ELECTRICAL" "_on" "ELECTRICAL" "")
....

 

Kent Cooper, AIA
0 Likes