Jump to content

Recommended Posts

Posted

Hi, I was working on creating a simple code to get familiar with AutoLISP. I'm not too familiar with the text commands that AutoLISP offers so I used _dtext. I attached my code and an image of what the issue is.

 

(defun c:AddLevelLine ()
  (setvar "OSMODE" 1)  ; Set osnap to endpoint

  ; Get points
  (setq pt1 (getpoint "\nSelect first point: "))
  (setq pt2 (getpoint pt1 "\nSelect second point: "))

  ; Force horizontal line by using same Y-coordinate
  (setq pt2 (list (car pt2) (cadr pt1) 0))

  ; Prompt for level number
  (setq levelnum (getstring "\nEnter level number: "))

  ; Create the line
  (command "_layer" "s" "Linework" "")
  (command "_line" pt1 pt2 "")

  ; Calculate text positions
  (setq text_string (strcat "LEVEL " levelnum))

  ; Create single-point text entities
  (command "_dtext" "j" "bc" pt1 0.2 0 text_string)
  (command "_dtext" "j" "bc" pt2 0.2 0 text_string)

  (princ)
)

 

image of the issue: image link

It is an issue because when regularly using the mtext command in autocad it only creates one point from which you can move the text element from. Also there are some arrows on it too which allow you to resize, which I still want the text elements to preserve for consistency sake.

Any insight into what the problem is would be great! Cheers

 

Posted

something like this

(defun c:AddLevelLine ()
  (setvar "OSMODE" 1)  ; Set osnap to endpoint
  (setvar 'ORTHOMODE 1)

  ; Get points
  (setq pt1 (getpoint "\nSelect first point: "))
  (setq pt2 (getpoint pt1 "\nSelect second point: "))


  ; Prompt for level number
  (setq levelnum (getstring "\nEnter level number: "))

  ; Create the line
  (command "_layer" "s" "Linework" "")
  (command "_line" pt1 pt2 "")
  
  ; Create single-point text entities
  (command"text" "j" "bc" pt1 "0.2" "0" (strcat "LEVEL " levelnum))
  (command "txt2mtxt" (entlast)"")
  (princ)
  (setvar 'ORTHOMODE 0)
)

 

  • Like 1
Posted

Hi @ediba,

 

Try with this modification:

 

(defun c:AddLevelLine ()
  (setvar "OSMODE" 1)  ; Set osnap to endpoint

  ; Get points
  (setq pt1 (getpoint "\nSelect first point: "))
  (setq pt2 (getpoint pt1 "\nSelect second point: "))

  ; Force horizontal line by using same Y-coordinate
  (setq pt2 (list (car pt2) (cadr pt1) 0))

  ; Prompt for level number
  (setq levelnum (getstring "\nEnter level number: "))

  ; Create the line
  ;;(command "_layer" "s" "Linework" "")
  ;;(command "_line" pt1 pt2 "")
  
  ; Check if layer "Linework" already exist or not, if it's not, it will make the new layer and set to be "Linework"
  (if (not (tblsearch "LAYER" "Linework"))
       (command "-layer" "m" "Linework" "")
       (command "-layer" "s" "Linework" "")
    )
    
  ;;; Create the line
  (entmake (list (cons 0 "LINE") (cons 10 pt1) (cons 11 pt2)))

  ; Calculate text positions
  (setq text_string (strcat "LEVEL " levelnum))

  ; Create single-point text entities
  ;;;(command "_dtext" "j" "bc" pt1 0.2 0 text_string)
  ;;;(command "_dtext" "j" "bc" pt2 0.2 0 text_string)
  
  ;;; This few lines from below is better than from above '(command "_dtext" ......)'
  (setq def_height 0.20
	rotation 0
	horizontal_justification 1
	vertical_justification 1
	)
  
  ;;; Instead of using "command", I prefer to use "entmake"
  (entmake (list (cons 0 "TEXT") (cons 10 pt1) (cons 40 def_height) (cons 1 text_string) (cons 50 rotation) (cons 11 pt1) 
  (cons 72 horizontal_justification) (cons 73 vertical_justification)))
  (entmake (list (cons 0 "TEXT") (cons 10 pt2) (cons 40 def_height) (cons 1 text_string) (cons 50 rotation) (cons 11 pt2) 
  (cons 72 horizontal_justification) (cons 73 vertical_justification)))
  (princ)
)

 

Also,  you can improve your code by adding somes checkings when user need to insert values (insted of (getstring "\nEnter level number: ") you can use (getint "\nEnter level number: ")), than you can convert it to string using "itoa" (strcat "LEVEL " (itoa levelnum)), localize the variables (defun c:AddLevelLine ( / pt1 pt2 levelnum ......) ......), etc.

 

Best regards.

  • Like 1
Posted

Just a comment provided the layer exists, you can use a (cons 8 "Linework") in the entmake to make object on a certain layer.

  • Like 1
  • Agree 1
Posted

Yes, the (cons 8 "Linework") can be added to entmake, but after executing this line of code:

 

(if (not (tblsearch "LAYER" "Linework"))
       (command "-layer" "m" "Linework" "")
       (command "-layer" "s" "Linework" "")
    )

 

it will be automatically set to current layer "Linework" and there is no needs for (cons 8 "Linework") inside the entmake.

  • Like 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...