Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

An Autolisp Lesson 1

Status
Not open for further replies.

shadow

Computer
Mar 1, 2001
321
ok so tigrek and i were talking about creating a lsp routine that takes a selection set of lines and places textw/mc justification and places it 6" from the midpoint of each line.

"not worried about that now just need text inserted into the drawing w/ justification of MC with the insertion point of the text 6" from the mid point of the line but i would like to be able to sellect all 504 of them and have it do it all at once im trying to peice a lsp together now but having a hard time figureing a few things oout like how to get text to insert w/ mc justification and get it to use the length of the line as the text info??? i would be very happy if you might just hint to me how to goabout it like give me an example and let me try to piece it together as im going to have to learn soon i can read lsp but cant write it :))

is the way i get the text to input the length like this


(Command "_text" "j" "mc" "6"" "Rotation angle of line"("object.length"))

if this is right how do i get it to also find the rotation angle of the line and use this for the rotation of the text as well
would it be liek so

(Command "_text" "j" "mc" "6"" ("object.Angle")("object.length"))


"
"Yes tigrek assume drawing in model space is 1 to 1
1unit is 1 ft im working in arch units "
if everyone helps everybody the world will be a better place
 
Replies continue below

Recommended for you

So tigrek make sure i have this right this line of code

(defun C:LLTXTMC ()
(setvar "cmdecho" 0)
(ssget "W" (setq p1 (getpoint "First point: ")) (getcorner p1 "Other point: ")) ;First get a selection set from Window
(setq l1 (entsel "What layer needs lines Labled"))

where "ssget "W" (setq p1 (getpoint "First point: "))(getcorner p1 "Other point: "))" means get a selection set from a window now i have a book called the abcs of autolisp and it says that you can use p(previous),l(last),w(window),c(crossing window) now it also says if you dont specify you can use any of the standard selection methods so inother words all am i right so i might want to not specify window in my routine am i right??
But now what is the selection set called i mean does it have a name how do i recall it exactly??
cause what i need to do next is get the layer that these objects are on and set it current or if it varies place the text on the corrisponding layers how does this work??



if everyone helps everybody the world will be a better place
 
So this is what i have so far

(setvar "cmdecho" 1)
(ssget "W" (setq p1 (getpoint "First point: ")) (getcorner p1 "Other point: ")) ;First get a selection set from Window
(Setq entla (entget(ssname(ssget p1)8)))
(Command "_-la" "S" "(entla)")

does that look right cause Vlide says bad argument type if everyone helps everybody the world will be a better place
 
So this is what i have so far

(setvar "cmdecho" 1)
(ssget "W" (setq p1 (getpoint "First point: ")) (getcorner p1 "Other point: ")) ;First get a selection set from Window
(Setq entla (entget(ssname(ssget p1)8)))
(Command "_-la" "S" "(entla)")

does that look right cause Vlide says bad argument type
and what should i do next if everyone helps everybody the world will be a better place
 
Hi Shadow

here it is - command Linelength.lsp as tutorial

writes lengths of all selected lines in architectural units near the line centers, inclined from line start towards line end (which means some text comes upside down) and on the current layer (you may modify to write to layer of each line)

tigrek@hotpop.com
-------------
; I wrote this code today for Shadow - would appreciate debug feedback from all interested

; extensively commented to serve as tutorial.
; sure all kinds of other things can be added - your homework
;all text is placed on the current layer - you may modify to put it on the layer of the line.

;To label lines with lengths


; TO WRITING YOUR OWN LISP CODE - OR MODIFY THIS
;1- open Acad2000, a new drawing, put some lines into it, save as linelength.dwg

;2- type VLIDE on command line
;3- in VLIDE window, type a title like this, save as linelength.lsp
; save where linelength.dwg is. Makes easy to load for now.
; later, after all tests are OK, save under Acad2000/support
;4- be clear about problem definition:
;to put a text along every selected line, giving the line length in ft-inch

;start the new command
(defun C:Linelength()
;5- select lines by any or all methods
(setq SUZY (ssget '((0 . "LINE"))))
;6- number of lines selected
(setq LENSUZY (sslength SUZY))
(setq COUNTER 0)
;7- loop through the set of selected lines
(while (setq EDNA (ssname SUZY COUNTER))
(setq ED (entget EDNA))
;8- get startpoint coordinates
(setq starting (dxf 10 ED))

;9- get endpoint coordinates
(setq ending (dxf 11 ED))

;10 get the inclination
(setq angleradian (angle starting ending))
(setq angledegree (/ (* angleradian 180. 7. ) 22.))
;11 calculate line length in drawing units
(setq linelength (sqrt (+
(expt (- (car starting)(car ending)) 2)
(expt (- (cadr starting)(cadr ending)) 2)
;(expt (- (cddr starting)(cddr ending)) 2)
)))
(setq textheight (getvar "TEXTSIZE"))
;12 estimate the midpoint coordinates
(setq midpoint (polar starting angleradian (/ linelength 2)))
;lets put text a textsize away from the line
;replace textheight in the following line with other distances, if you wish,
(setq distfromline textheight)
(setq midpoint (polar midpoint (+ angleradian (/ 22. 14)) distfromline))
;13 convert length into text string
(setq linelength (rtos linelength 4 2))

;14 insert linelength as text
;sure text is upside down for some lines
;this is the homework for you - get it top side up
;I personally prefer it as is, because reveals which way the start/end of line is.
(setq oldSnap (getvar "osmode"))
(setvar "osmode" 0)

(COMMAND "_.text" "_j" "_c" midpoint textheight angledegree linelength)
(setvar "osmode" oldSnap)

;increase the counter - otherwise no way out of the Nexus
(setq COUNTER (+ 1 COUNTER))
;temp - this is just for debugging
(princ "\nLine ")(princ COUNTER)
(princ "\nstart ")(princ starting)(princ " end ")(princ ending)
(princ " radian ")(princ angleradian)
(princ " degree ")(princ angledegree)
(princ " length ")(princ linelength)


);end while

(princ)
);end defun

;----The only other function used above
;called like (dxf groupcode ED)
;ED being complete list of entity named EDNA
;returns the entity info corresponding to the group code
; (dxf 10 ED) returns list of startpoint coordinates
; (dxf 11 ED) returns list of endpoint coordinates
(defun dxf (n ed) (cdr (assoc n ed)))
;-----------------
 
tigrek yea thanks you just saved me 4 hrs of picking thru lines and not only that i kinda have an idea of what to do lsp wise cause now i can read yours and edit mine but i still have some questions regarding lisp could you answer them for me sir :)

first line of ur lisp

(setq SUZY (ssget '((0 . "LINE"))))

I know that setq is for setting varables right, and in this line your variable is called "suzy".Ssget is selection set get for creating selection sets, but what does the single quote mean '. im assuming it tells ssget to use all methods of selection right. then you have ((0 . "line")) why is that in ()'s does the "line" in quotes tell ssget to only get line objects is that what it refers too...

Where exactly are you specifying the lsp to place the text? IC ;14 insert linelength as text
also been playing with it in acad and now it only will do decimal lengths so everything is in decimal lengths and i cant get it back to ft ans inches
also i was reading that in your lsp routines you could set up a undo mark so that if you need to undo all that you have done you just click undo 1 time not the amount of times that txt was inserted
if everyone helps everybody the world will be a better place
 
Hey tigrek how come when you were setting up the variable
linelength you didnt just use object.length im not sure exactly how it works or how it fits into the big picture but im assuming it would help and reduce many lines of code here is what i found in the help

"Gets the length of a line.

Signature

object.Length

object Line
The object or objects this property applies to.
Length Double; read-only
The length of the line." if everyone helps everybody the world will be a better place
 
here tigrek
this is what my command line says now while i run it but its not the same line all the way thru but i did notice that if you look at the command line its inputting the same txt for each line

"Command:
Line 255
start (634.5 65.0 0.0) end (634.5 149.0 0.0) radian 1.5708 degree 89.9638
length 7'_.text
Current text style: "Lables" Text height: 0'-6"
Specify start point of text or [Justify/Style]: _j Enter an option
[Align/Fit/Center/Middle/Right/TL/TC/TR/ML/MC/MR/BL/BC/BR]: _c
Specify center point of text:
Specify rotation angle of text <6d>: 6.000000000000000
Enter text: 89.96378962552581
Command: 6'-11 1/2&quot; Unknown command &quot;6'-11 1/2&quot;&quot;. Press F1 for help.&quot; if everyone helps everybody the world will be a better place
 
ok i figured out something look at line

(COMMAND &quot;_.text&quot; &quot;_j&quot; &quot;_c&quot; midpoint textheight angledegree linelength)

now this works the 1st time you run the lisp in the drawing but and this is a big one after you run it once it will not expect you to put the textheight in at all so after you have ran this once the following line works so how am i doing so far

(COMMAND &quot;_.text&quot; &quot;_j&quot; &quot;_c&quot;midpoint angledegree linelength)

so the queestion this time is how do you say IF linelength.lsp has been run/loaded 1 time dont use code 1 us 2nd code if everyone helps everybody the world will be a better place
 
Hi Shadow

Of course there is a way to make that code fail -

if you go and put a text size into the current text style definition, then it will fail.

Would be kind of wrong to put in code for all such eventualities. Why should I increase the code to the size of textbook for quantum mechanics - the user should simple refrain from putting a text size into the text style definition etc.

Take care
 
tigrek
So what your trying to say is this
Use the following code
(COMMAND &quot;_.text&quot; &quot;_j&quot; &quot;_c&quot;midpoint angledegree linelength)
if you plan to use a preset txt style size right


ohh also found that some of my lines that are say 8 1/8&quot; are comming out 8&quot; or 8 1/4&quot; what does that mean cause my units presision is set to 1/16&quot; and its not consistant either like i saw where there were 2 lines exactly 8 1/8&quot; and 1 was labled 8&quot; while the other was 8 1/4&quot;
so weird
if everyone helps everybody the world will be a better place
 
hey i think i got a solution to the length problem but im wondering how to merge the 2 lisps together
ok i have an old lsp called ll.lsp that gets the line length by selecting the line the code is as follows

&quot;;returns lenght of line,
(defun c:ll (/ pt1 e1 e2 blname layname)
(setq pt1 nil e1 nil e2 nil blname nil layname nil)
(setq pt1 (cadr(entsel&quot;\nChoose line for length:&quot;)))
(setq e1(ssget pt1))
(setq e2 (entget (ssname e1 0)))
(setq blname (cdr (assoc 0 e2 )))
(cond
((= blname &quot;LINE&quot;)
(princ &quot;\nLine length = &quot;)
(princ (eval (rtos (distance (cdr (assoc 11 e2)) (cdr (assoc 10 e2)) ) )))
(princ))
)
(setq pt1 nil e1 nil e2 nil blname nil layname nil)
(princ)
)&quot;

now couldn't we adapt this to the current lsp and replace the area noted here

&quot;;11 calculate line length in drawing units
(setq linelength (sqrt (+
(expt (- (car starting)(car ending)) 2)
(expt (- (cadr starting)(cadr ending)) 2)
;(expt (- (cddr starting)(cddr ending)) 2)
)))&quot;
Not trying to make the lsp longer but just more acurate
if everyone helps everybody the world will be a better place
 
Probably a stupid question but could we do this copy n paste ll into linelength.lsp and remove the print portions and set it up so that it goes and gets its line(s) from the selection set maybe some thing like this correct me if im wrong


(defun c:ll (/ pt1 e1 e2 blname layname)
(setq pt1 nil e1 nil e2 nil blname nil layname nil)
(setq pt1 (cadr(entsel Ed)))
(setq e1(ssget pt1))
(setq e2 (entget (ssname e1 0)))
(setq blname (cdr (assoc 0 e2 )))
(setq length ((distance (cdr (assoc 11 e2)) (cdr (assoc 10 e2)))
))

does this sound right i think there may be more here than what i need either that or i have it in the wrong order
if everyone helps everybody the world will be a better place
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor