AutoCAD LISP - Please help add (LAYERP) function to LISP routine.

AutoCAD LISP - Please help add (LAYERP) function to LISP routine.

BigBoyCAD
Enthusiast Enthusiast
477 Views
9 Replies
Message 1 of 10

AutoCAD LISP - Please help add (LAYERP) function to LISP routine.

BigBoyCAD
Enthusiast
Enthusiast

Hi.

I have put together a LISP routine (please see below) that creates a dimension with specific text 'REFER SCHEDULE'.

It allocates the layer for the new dimension as '25CONT'.

That all works fine.


Please help with the following:

Once having executed the routine I would like to return the current layer '25CONT' to the previously used layer.

I am trying to use 'LAYERP' to achieve this though something isn't working.

Everything works fine though the layer remains on '25CONT' once having placed the dimension.

Any suggestions for improvement would be greatly appreciated.
Thank you.

 

(defun c:REF19 ( / pt1 pt2 angle dimEnt layName prevLay dimObj)
;; Define target layer
(setq layName "25CONT")

;; Save current layer name using a variable
(setq prevLay (getvar "CLAYER"))

;; Use command to change to 25CONT and allow LAYERP to track it
(if (not (tblsearch "LAYER" layName))
(command "._-LAYER" "Make" layName "Color" "7" "" "")
)
;; Use LAYER command to ensure LAYERP recognizes it
(command "._-LAYER" "Set" layName "")

;; Prompt for dimension points
(setq pt1 (getpoint "\nSpecify first extension line origin: "))
(setq pt2 (getpoint pt1 "\nSpecify second extension line origin: "))
(setq angle (getangle pt1 "\nSpecify rotation angle for dimension: "))

;; Create aligned dimension (user picks text location)
(command "._DIMALIGNED" pt1 pt2 pause)
(setq dimEnt (entlast))

;; Apply REFER/SCHEDULE text and rotation
(if (and dimEnt (entget dimEnt))
(progn
(setq dimObj (vlax-ename->vla-object dimEnt))
(vla-put-TextOverride dimObj "REFER\\XSCHEDULE") ; Above/Below text
(vla-put-Rotation dimObj angle)
(vla-put-TextDirection dimObj (vlax-3d-point '(1 0 0))) ; Left-to-right
)
)

(command "._LAYERP")
(princ)
)

0 Likes
Accepted solutions (2)
478 Views
9 Replies
Replies (9)
Message 2 of 10

ВeekeeCZ
Consultant
Consultant
Accepted solution

This works, but I'm not sure how the rotation angle should work. You use DIMALIGNED, which is aligned according to the given points.

 

(defun c:REF19 ( / *error* pt1 pt2 ang enl dimEnt layName prevLay dimObj)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if prevLay (setvar 'clayer prevLay))
    (princ))
  
  
  ;; Define target layer
  (setq layName "25CONT")
  
  ;; Save current layer name using a variable
  (setq prevLay (getvar "CLAYER"))
  
  (command "._-LAYER" "_T" layName "Make" layName "Color" "7" "" "")
  
  ;; Prompt for dimension points
  (if (and (setq pt1 (getpoint "\nSpecify first extension line origin: "))
	   (setq pt2 (getpoint pt1 "\nSpecify second extension line origin: "))
	   (setq ang (getangle pt1 "\nSpecify rotation angle for dimension: "))
	   (setq enl (entlast))
	   )
    ;; Create aligned dimension (user picks text location)
    (command "._DIMALIGNED" "_non" pt1 "_non" pt2 pause))
  
  ;; Apply REFER/SCHEDULE text and rotation
  (if (not (equal enl (setq enl (entlast))))
    (progn
      (setq dimObj (vlax-ename->vla-object enl))
      (vla-put-TextOverride dimObj "REFER\\XSCHEDULE") ; Above/Below text
      (vla-put-Rotation dimObj ang)
      (vla-put-DimTxtDirection dimObj 0) ; Left-to-right
      )
    )
  
  (*error* "end")
  (princ)
  )

 

0 Likes
Message 3 of 10

paullimapa
Mentor
Mentor

Instead of this line of code at the end:

(command "._LAYERP")

Why not use this:

(setvar "CLAYER" prevLay)

 


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

komondormrex
Mentor
Mentor
Accepted solution

hey, 

make it simpler

(defun c:REF19 ( / pt1 pt2 angle_ dimEnt layName prevLay dimObj)
  ;; Define target layer
  (setq layName "25CONT")

  ;; Prompt for dimension points
  (setq pt1 (getpoint "\nSpecify first extension line origin: "))
  (setq pt2 (getpoint pt1 "\nSpecify second extension line origin: "))
  (setq angle_ (getangle pt1 "\nSpecify rotation angle for dimension: "))

  ;; Create aligned dimension (user picks text location)
  (command "._DIMALIGNED" pt1 pt2 pause)
  (setq dimEnt (entlast))

  ;; Apply REFER/SCHEDULE text and rotation
  (if (and dimEnt (entget dimEnt))
    (progn
      (setq dimObj (vlax-ename->vla-object dimEnt))
      (vla-put-TextOverride dimObj "REFER\\XSCHEDULE") ; Above/Below text
      (vla-put-Rotation dimObj angle_)
      (vla-put-DimTxtDirection dimObj 0) ; Left-to-right
      (entmod (append (entget dimEnt) (list (cons 8 layName))))  
    )
  )
  (princ)
)
0 Likes
Message 5 of 10

BigBoyCAD
Enthusiast
Enthusiast

Hi Paul.


Swapping:

(command "._LAYERP")

For:

(setvar "CLAYER" prevLay)

 


Unfortunately doesn't revert the layer back to the previous layer with the way it was placed in the original code (REF19).

0 Likes
Message 6 of 10

BigBoyCAD
Enthusiast
Enthusiast

Hi komondormrex.

 

Thank you for the code.

 

It works really well. Reverting back to previous layer after executing LISP routine perfectly.

I notice however that at times the text is the wrong way around.

This seems to occur randomly no matter from what direction the dimension is initiated  i.e left to right / right to left or top to bottom / bottom to top.

 

I would have thought that designating 'text view direction' as 'left-to-right' should force this to not happen.

 

Perhaps there is another way to determine the direction text is read?

 

Please see attached .dwg showing an example of correct and incorrect result.

0 Likes
Message 7 of 10

BigBoyCAD
Enthusiast
Enthusiast

Upon further investigation I have realised how the 'left-to-right' aspect of the dimension text is being determined.

 

· Horizontal placement - Point 1 designates the left hand side.
· Vertical placement - Point 2 designates the left hand side.

 

Understanding this... For the initial purpose I consider this code a success.

 

Thank you very much. 🙂

 

One further thought however is if you select the 'Mtext' 'Text' 'Angle' option to edit the text the resulting dimension will be placed in the initial layer not the desired '25CONT' layer.


Is there a way around this?

0 Likes
Message 8 of 10

BigBoyCAD
Enthusiast
Enthusiast

Hi Beekee

 

This code works great.
Similarly to @komondormrex 's code to get text to read 'left-to-right' whether text is horizontal or vertical:

· Horizontal placement - Point 1 designates the left hand side.
· Vertical placement - Point 2 designates the left hand side.

 

Similarly to @komondormrex 's code if you select the 'Mtext' 'Text' 'Angle' option to edit the text the resulting dimension will be placed in the initial layer not the desired '25CONT' layer.

0 Likes
Message 9 of 10

ВeekeeCZ
Consultant
Consultant

Here's a simplified code that works for me the way I think it should. 

 

(defun c:REF19 ( / *error* pt1 pt2 layName sysLay prevLay)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if (and sysLay prevLay) (setvar syslay prevLay))
    (princ))

   
  ;; Define target layer
  (setq layName "25CONT")
   
  (if (and (setq pt1 (getpoint "\nSpecify first extension line origin: "))
	   (setq pt2 (getpoint pt1 "\nSpecify second extension line origin: "))
	   (setq sysLay (if (getvar 'dimlayer) "DIMLAYER" "CLAYER"))
	   (setq prevLay (getvar sysLay)) 
	   (setvar sysLay layName)
	   (or (tblsearch "layer" layName)
	       (vl-cmdf "._-LAYER" "New" layName "Color" "7" layName ""))
	   (vl-cmdf "._-LAYER" "_T" layName "")
	   )
    (command "._DIMALIGNED" "_non" pt1 "_non" pt2 "_T" "REFER\\XSCHEDULE" pause)
    )
  
  (*error* "end")
  (princ)
  )

 

0 Likes
Message 10 of 10

komondormrex
Mentor
Mentor

Hey you there,

i apologize, i missed that one.

 

@BigBoyCAD wrote:

I notice however that at times the text is the wrong way around.

This seems to occur randomly no matter from what direction the dimension is initiated  i.e left to right / right to left or top to bottom / bottom to top.


i think setting the rotation angle for a dimension is the cause.

text in the dimension is internally will be set to be readable according to the attached picture.

komondormrex_0-1757523197409.png


@BigBoyCAD wrote:

One further thought however is if you select the 'Mtext' 'Text' 'Angle' option to edit the text the ...


i implemented vl-catch-all-apply to overcome that. check the code attached. hth.

(defun c:REF19 ( / pt1 pt2 angle_ dimEnt layName prevLay dimObj)
  ;; Define target layer
  (setq layName "25CONT")

  ;; Prompt for dimension points
  (setq pt1 (getpoint "\nSpecify first extension line origin: "))
  (setq pt2 (getpoint pt1 "\nSpecify second extension line origin: "))
;  (setq angle_ (getangle pt1 "\nSpecify rotation angle for dimension: "))

  ;; Create aligned dimension (user picks text location)
  (vl-catch-all-apply 'command-s (list "._DIMALIGNED" pt1 pt2 pause))
  (setq dimEnt (entlast))

  ;; Apply REFER/SCHEDULE text and rotation
  (if (and dimEnt (entget dimEnt))
    (progn
      (setq dimObj (vlax-ename->vla-object dimEnt))
      (vla-put-TextOverride dimObj "REFER\\XSCHEDULE") ; Above/Below text
;      (vla-put-Rotation dimObj angle_)
      (vla-put-DimTxtDirection dimObj 0) ; Left-to-right
      (entmod (append (entget dimEnt) (list (cons 8 layName))))  
    )
  )
  (princ)
)

 

0 Likes