ediba Posted March 7 Posted March 7 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 Quote
Tomislav Posted March 10 Posted March 10 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) ) 1 Quote
Saxlle Posted March 10 Posted March 10 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. 1 Quote
BIGAL Posted March 10 Posted March 10 Just a comment provided the layer exists, you can use a (cons 8 "Linework") in the entmake to make object on a certain layer. 1 1 Quote
Saxlle Posted March 11 Posted March 11 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. 1 Quote
Recommended Posts
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.