LISP to draw/move XLINE on a specific layer

LISP to draw/move XLINE on a specific layer

joseph_hermogenes_042016
Advocate Advocate
2,830 Views
12 Replies
Message 1 of 13

LISP to draw/move XLINE on a specific layer

joseph_hermogenes_042016
Advocate
Advocate

Hi,

 

Shown below is a code I've created to do construction line. It creates X-line that gets drawn along X-axis with Z elevation = 0.

 

(defun c:CCX (/ pt)
     (setq pt (getpoint "\nSpecify insertion point: "))
     (setq pt (list (car pt) (cadr pt) 0))
  (progn
     (command "XLINE" "H" pt)
    (while (> (getvar 'CMDACTIVE) 0)
          (command ".Z" "_non" pt pause )
     )

   )
)

 

I need some help on how to get the X-lines drawn/moved to layer name "Construction line" with colour  21 and line-type continuous.  The code needs to check if the layer name exist, and if not create the layer.

 

To get it more complicated. I was hoping the code will keep the active layer once the X-lines are drawn.

 

Any suggestion is much appreciated.

 

Regards,

Joseph

0 Likes
Accepted solutions (1)
2,831 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable

@joseph_hermogenes_042016 something like ?

 

(defun c:CCX (/ p a)
  (while
    (setq p (getpoint "\nSpecify insertion point: "))
    (if (not (tblsearch "layer" "Construction line"))
      (entmake
	'(
	  (000 . "LAYER")
	  (100 . "AcDbSymbolTableRecord")
	  (100 . "AcDbLayerTableRecord")
	  (002 . "Construction line")
	  (070 .  0)
	  (006 . "Continuous")
	  (062 .  21)
	  (290 .  0)
	  (370 . -3)
	  )
	)
      )
    (setq a '(1.0 0.0 0.0))
    (entmake
      (list
	'(000 . "XLINE")
	'(008 . "Construction line")
	'(062 .  21)
	'(100 . "AcDbEntity")
	'(100 . "AcDbXline")
	(cons 10 (trans p 1 0))
	(cons 11 (trans a 1 0 t))
	)
      )
    );while
  );defun
Message 3 of 13

joseph_hermogenes_042016
Advocate
Advocate

Thank you very much @Anonymous.

 

Almost what I'm need with just a few issues.

1. The location of the Xline needs to be always 0 along Z-axis. At the moment, Z value is the location of the selected insertion points.

2.  I have total 3 separate similar codes. One for Xline "Hor" (as shown previouly), one for "Vert" and one "Ang". Not sure how this will work for the "Ang".

 

Regards,

Joseph

0 Likes
Message 4 of 13

Kent1Cooper
Consultant
Consultant
Accepted solution

I would do it this way:

 

(defun C:CCX (/ pt)
  (command
    "_.layer" "_make" "Construction line" "_color" 21 "" ""
    "_.xline" "_hor"
  ); command [leaves you in Xline]
  (while (setq pt (getpoint "\nSpecify through point: "))
    (command ".Z" "0,0,0" pt)
  ); while
  (command "" "_.layerp"); end Xline command, set Layer back
  (princ)
); defun

[It's never necessary to check whether a Layer exists to decide whether to create it -- just Make it, and it will become current in the process, and it won't matter  if it's already there.]

Kent Cooper, AIA
0 Likes
Message 5 of 13

Anonymous
Not applicable

@joseph_hermogenes_042016 I do not understand you, could you post an example of your .dwg?

 


2.  I have total 3 separate similar codes. One for Xline "Hor" (as shown previouly), one for "Vert" and one "Ang". Not sure how this will work for the "Ang".

 


Do you want me to put them together in a single .lsp file?
Choose from the command line [Horizontal / Vertical / Ang]

0 Likes
Message 6 of 13

Kent1Cooper
Consultant
Consultant

@joseph_hermogenes_042016 wrote:

 

….

2.  I have total 3 separate similar codes. One for Xline "Hor" (as shown previouly), one for "Vert" and one "Ang". Not sure how this will work for the "Ang".

 

….

 

You may be able to use ConstLines.lsp with its RX command, available >here<.  It does all that in one  command, and you can switch between them [and Rays with similar directional options] at will, all without leaving the command and starting another.  And it sets up a Layer [you can change the name] and returns you to it when you're done.  See more in that thread and in comments in the file. The only thing it doesn't do is force all to Z=0, but that can be added similarly to in my previous Reply.

Kent Cooper, AIA
Message 7 of 13

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

….
(while (setq p (getpoint "\nSpecify insertion point: ")) (if (not (tblsearch "layer" "Construction line")) (entmake '( (000 . "LAYER") ….
(006 . "Continuous")
….

 

Apart from the fact that just Making the Layer eliminates the need to check whether it exists [see my first Reply], I would not  put that check [and the Layer creation if needed] inside  the (while) loop asking for points.  It certainly doesn't need to check for the Layer for every Xline.

 

Also, it's not necessary to spell out Continuous as the linetype for a new Layer, even when using (entmake) -- that's always the default.  [You could likely do without some of those other entries, too -- you can experiment to find the minimum ingredients needed.]

Kent Cooper, AIA
Message 8 of 13

joseph_hermogenes_042016
Advocate
Advocate

Hi @Kent1Cooper. Thank you very much. Exactly what I need.

 

Regards,

Joseph

0 Likes
Message 9 of 13

joseph_hermogenes_042016
Advocate
Advocate

Hi @Anonymous. Thank you for the help.

 

I got the result I'm looking

 

Regards,

Joseph

0 Likes
Message 10 of 13

dlanorh
Advisor
Advisor

This should do Horizontal, Vertical or Angled Xlines.

 

(defun c:ccx ( / *error* c_doc c_lyrs ms n_lyr x_lst x_typ pt1 pt2 x_lyr x_obj)
(vl-load-com)
  (defun *error* ( msg )
(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc)) (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nOops an Error : " msg " occurred.")))
(princ) );_end_*error*_defun (setq c_doc (vla-get-activedocument (vlax-get-acad-object)) c_lyrs (vla-get-layers c_doc) ms (vla-get-modelspace c_doc) n_lyr "Construction line" x_lst (list "Horiz" "Vert" "Angled") );end_setq (initget 1 (vl-string-right-trim " "(apply 'strcat (mapcar '(lambda (x) (strcat x " ")) x_lst)))) (setq x_typ (getkword (strcat "["(vl-string-right-trim "/"(apply 'strcat (mapcar '(lambda (x) (strcat x "/")) x_lst)))"]")) pt1 (getpoint "\nSpecify Insertion Point : ") pt1 (list (car pt1) (cadr pt1) 0.0) );end_setq (if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc)) (vla-startundomark c_doc) (cond ( (not (tblsearch "layer" n_lyr));if it doesn't exist (vla-add c_lyrs n_lyr) (setq x_lyr (vla-item c_lyrs n_lyr)) (vlax-put-property x_lyr 'color 21) ) );end_cond (cond ( (= x_typ "Horiz") (setq pt2 (mapcar '+ pt1 (list 1 0 0))) ) ( (= x_typ "Vert") (setq pt2 (mapcar '+ pt1 (list 0 1 0))) ) ( (= x_typ "Angled") (setq pt2 (getpoint "\nSpecify Rotation Point : ") pt2 (list (car pt2) (cadr pt2) 0.0) ) ) );end_cond (setq x_obj (vla-addxline ms (vlax-3d-point pt1) (vlax-3d-point pt2))) (vlax-put-property x_obj 'layer n_lyr) (if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc)) );end_defun

I am not one of the robots you're looking for

Message 11 of 13

joseph_hermogenes_042016
Advocate
Advocate

Hi @dlanorh,

 

Lisp do not let me select multiple points.  But thank you very much for the codes.

 

My query had been answered.

 

Regards,

Joseph

0 Likes
Message 12 of 13

pavankumar14394
Community Visitor
Community Visitor

Hii am noob in lisp but I need the cross x lines with hidden by selecting rectangle. Please help...

0 Likes
Message 13 of 13

Kent1Cooper
Consultant
Consultant

That is a very different question.  It should be a new Topic, but first do some Searching -- this kind of thing has come up before.   One example is >here<., along with other approaches if the selected rectangle is orthogonal.

Kent Cooper, AIA
0 Likes