Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

To add layer name in a lisp

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
inaamazmi
757 Views, 9 Replies

To add layer name in a lisp

I have lisp in which i want to add layer name so that it can create the text in same layer

Layer name : PROPOSED WATER BEND

Color: white

linetype: continous

lineweight: 0.00

 

here is the code

 

;From the Desk of ARUN P MAMGAIN

(defun c:bend()
(setq pt (getpoint "\nLeader from point: "))
(setq x(rtos(car pt)))
(setq y(rtos(cadr pt)))
(setq a(strcat "PROPOSED
BEND 100%%c 45%%d" ))
(setq b(strcat "E:" x))
(setq c(strcat "N:" y))
(setq pt1 (getpoint pt "\nTo Point: "))
(command "leader" pt pt1 "" a b c "" )
(princ)

)
(prompt "\nType XY to envoke the command")
(prompt "\nHead works")

9 REPLIES 9
Message 2 of 10
Moshe-A
in reply to: inaamazmi

@inaamazmi  hi,

 

check this 😀

 

(defun c:bend (/ pt0 pt1 Text East North)
 (if (and
       (setq pt0 (getpoint "\nLeader from point: "))
       (setq pt1 (getpoint pt0 "\nTo Point: "))
     )
  (progn 
   (setq Text  (strcat "PROPOSED BEND 100%%c 45%%d" ))
   (setq East  (strcat "E:" (rtos (car  pt0) 2)))
   (setq North (strcat "N:" (rtos (cadr pt0) 2)))
   (command "._layer" "_make" "PROPOSED WATER BEND" "_color" "white" "PROPOSED WATER BEND" "")
   (command "._leader" pt0 pt1 "" Text East North "" )
  ); progn
 ); if
   
 (princ)
)

 

Message 3 of 10
inaamazmi
in reply to: Moshe-A

wow..you solved my problem...its working

 

can i change the text angle from 0 degree to 33 degree?

Message 4 of 10
Moshe-A
in reply to: inaamazmi

@inaamazmi 

 


@inaamazmi wrote:

wow..you solved my problem...its working

 

can i change the text angle from 0 degree to 33 degree?


The text is MTEXT object and you probably also want to align the last landing line. i do not know if you can achieve that with LEADER or even MLEADER so one solution to this would be to define your own LEADER command using lwpolyline and add TEXT object. try to start creating this lisp and i'll be here to help 😀

 

Moshe

 

Message 5 of 10
Kent1Cooper
in reply to: inaamazmi


@inaamazmi wrote:

....

(setq a(strcat "PROPOSED
BEND 100%%c 45%%d" ))
....


A small point....  The (strcat) function is meaningless unless you are concatenating more than one string together.  The above can be simply:
(setq a "PROPOSED BEND 100%%c 45%%d")

Kent Cooper, AIA
Message 6 of 10
Kent1Cooper
in reply to: Moshe-A


@Moshe-A wrote:
....
   (command "._layer" "_make" "PROPOSED WATER BEND" "_color" "white" "PROPOSED WATER BEND" "")
....

Another small point....  The Make option in a command-line LAYER command sets the Layer as current in the process, so it becomes the default for assignment of options.  So the above can be just:
  (command "._layer" "_make" "PROPOSED WATER BEND" "_color" "white" "" "")

But also, white[/black] is the default color for new Layers, so there's no need to assign it.  It can be just:

  (command "._layer" "_make" "PROPOSED WATER BEND" "")

unless there's the possibility that it already exists with the wrong color, and you want that to be corrected.

Kent Cooper, AIA
Message 7 of 10
Moshe-A
in reply to: Kent1Cooper

@Kent1Cooper ,

 


@Kent1Cooper wrote:

@Moshe-A wrote:

 

....
   (command "._layer" "_make" "PROPOSED WATER BEND" "_color" "white" "PROPOSED WATER BEND" "")
....

 


Another small point....  The Make option in a command-line LAYER command sets the Layer as current in the process, so it becomes the default for assignment of options.  So the above can be just:
  (command "._layer" "_make" "PROPOSED WATER BEND" "_color" "white" "" "")

But also, white[/black] is the default color for new Layers, so there's no need to assign it.  It can be just:

  (command "._layer" "_make" "PROPOSED WATER BEND" "")

unless there's the possibility that it already exists with the wrong color, and you want that to be corrected.


Yes, were you concern i didn't know that? 😀 i took the second option in consideration 

 

Message 8 of 10
Moshe-A
in reply to: inaamazmi

@inaamazmi 

 

can i change the text angle from 0 degree to 33 degree?

try to start creating this lisp and i'll be here to help 

 

well i understand you still have difficulty to start such a lisp so i jumped in 😀

 

attach beautiful command (called it) LEA-DER imitating standard LEADER command but the text content is aligned to the last landing segment.

 

the leader arrow size + mtext height is taken from the current running dimension style variables DIMASZ & DIMTXT  and scale is controlled by DIMSCALE just the same as done by the standard command. make sure to properly set the  dimension style.

 

the program starts with a loop (while) to draw short lines segments at finish, the short lines is replaced by pline that have an arrow head, than goes to a second loop pausing for you to enter leader texts contents and then the mtext is  aligned to last segment.

 

note the use of some anonymous functions (i call them 'stabbing' functions) their objectivity is to process data and return a value makes the code readable and well structured 😀

 

Moshe

 

 

(vl-load-com) ; load ActiveX support

; custom leader
; support ucs rotate about Z axis only
(defun c:lea-der (/ break askpoint drawLine asktext _content _space _midseg _readang ucs->wcs ; local functions
                    acadobj adoc DIMASZ DIMTXT DIMSCL ss points^ p0 p1
		    str strings^ base wth text MTextObj MinPoint MaxPoint)

 (defun break (msg)
  (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
   (princ (strcat "\nError: " msg))
  )
   
  (vla-endUndoMark adoc)
  (setvar "cmdecho" 1)

  (vlax-release-object adoc) 
  (setq *error* nil)
  (princ)
 ); break

  
 (defun askpoint (anchor)
  (apply
    	'getpoint
		(if (not anchor)
		 (list "\nSpecify leader start point: ")
		 (list anchor "\nSpecify leader next point: ")
		)
  ) 
 ); askpoint


 (defun drawLine ()
  (command "._line" (cadr points^) (car points^) "")
  (ssadd (entlast) ss)
 ); drawLine

  
 (defun asktext (def)
  (apply 'getstring (list (strcat "\nEnter " (if (not def) "first" "next") " line of annotation: ")))
 ); asktext

  
 ; =============== anonymous functions ================================

 ; generate mtext content 
 (setq _content (lambda (lst) (substr (apply 'strcat (mapcar (function (lambda (str) (strcat "\\P" str))) lst)) 3)))
 ; return paper space or model space object
 (setq _space (lambda () (if (= (getvar "cvport") 1) (vla-get-paperSpace adoc) (vla-get-modelSpace adoc))))
 ; return 1/2 mtext box height
 (setq _midseg (lambda (v1 v2) (* 0.5 (abs (apply '- (mapcar (function (lambda (v0) (car (vlax-safearray->list v0)))) (list v1 v2)))))))
 ; return readable text angle 
 (setq _readang (lambda (ang) (if (and (> ang (/ pi 2)) (< ang (* pi 1.5))) (+ ang pi) ang)))
 ; convert points from ucs to wcs 
 (setq ucs->wcs (lambda (lst) (mapcar (function (lambda (pt) (trans pt 1 0))) lst)))
 
  
 ; here start (c:lea-der)
 (setq *error* break) ; error handler
  
 (setq acadObj (vlax-get-acad-object))
 (setq adoc (vla-get-ActiveDocument acadObj))

 (vla-startUndoMark adoc)
 (setvar "cmdecho" 0) 

 ; define some constants
 (setq DIMASZ (getvar "dimasz"))
 (setq DIMTXT (getvar "dimtxt"))
 (setq DIMSCL (getvar "dimscale"))

  
 ; =============== draw temporary short lines ============================
 (setq ss (ssadd))

 (cond
  ((progn
    (while (setq p0 (askpoint p0))
     (setq points^ (cons p0 points^))
     (cond
      ((= (vl-list-length points^) 1)) ; do noting
      ((= (vl-list-length points^) 2)
       (cond
        ; first segment length must comply to 2 x DIMASZ 
        ((< (distance (cadr points^) (car points^)) (* 2 DIMASZ))
         (setq points^ (cdr points^) p0 (car points^))
         (vlr-beep-reaction)
         (prompt "\nFirst segment too short, must be 2 times arrow size.")
        ); case
        ( t
         (drawLine) 
        ); case
       ); cond
      ); case
      ( t
       (drawLine) 
      ); case
     ); cond
    ); while
    (command ".erase" "_si" ss) ; remove teporary lines
   ); progn
  ); case
  ((> (vl-list-length points^) 2)
   ; ================= replace lines by pline ============================
    
   (setq points^ (reverse points^))
   (setq p0 (car points^) p1 (cadr points^))
   (command "._pline" p0 "_width" 0.0 (* (/ DIMASZ 3) DIMSCL)
            (polar p0 (angle p0 p1) (* DIMASZ DIMSCL)) "_width" 0 0)
  
   (foreach pt (cdr points^)
    (command pt)
   )
   (command "") ; finish pline
   
   ; ================= pause for leader contents===========================
   
   (while (/= (setq str (asktext str)) "")
    (setq strings^ (cons str strings^))    
   ); while

   (setq points^ (ucs->wcs points^)) ; convert points to wcs

   ; ======================= Add MText ====================================
   
   (setq base (vlax-3d-point (last points^)))
   (setq wth (* (car (vl-sort (_width (reverse strings^)) '>)) DIMSCL))
   (setq text (_content (reverse strings^)))
   
   (setq MTextObj (vla-AddMText (_space) base wth text))
   (vla-put-height MTextObj (* DIMTXT DIMSCL))

   (vla-getBoundingBox MTextObj 'MinPoint 'MaxPoint)
   (setq p0 (cadr (reverse points^)) p1 (car (reverse points^)))
   
   (vla-put-attachmentpoint MTextObj acAttachmentPointMiddleCenter)
   (vla-put-insertionpoint  MTextObj (vlax-3d-point (polar p1 (angle p0 p1) (+ (/ DIMASZ 3) (_midseg MinPoint MaxPoint)))))

   ; rotate mtext
   (vla-put-rotation MTextObj (_readang (- (angle p0 p1) (atan (cadr (getvar "ucsxdir")) (car (getvar "ucsxdir"))))))

   (vlax-release-object MTextObj)
  ); case
 ); cond

 (setvar "cmdecho" 1) 
 (vla-endUndoMark adoc)
 (vlax-release-object adoc)

 (setq *error* nil) ; reset
 (princ) 
); c:lea-der

 

 

 

Message 9 of 10
inaamazmi
in reply to: Moshe-A

its not showing the text

Message 10 of 10
Moshe-A
in reply to: inaamazmi

@inaamazmi ,

 

sorry my mistake 😀

 

made more fine tunning

 

Moshe

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report