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 part 2 2

Status
Not open for further replies.

shadow

Computer
Mar 1, 2001
321
tigrek
ok here is what i have done since yesterday
From

;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)
)))
To

;11 calculate line length in drawing units
(setq linelength (Distance starting ending))

And this works but i still cannot figure out how to make this calc more accurately
give me a hint please :)

Also i have a question about this here

;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))

you have midpoint setqed 2 times althought vlide shows no error i dont undestand it very well
also you have there distfromline setqed from textheight but why ?? its not used anwhere else in the routine otehr than in the second midpoint setq in the routine

let me know but i would really like to figure out why i cannot get anything better than 1/4" precision when labeling
if everyone helps everybody the world will be a better place
 
Replies continue below

Recommended for you

Yea for me i figured out the precision problem its here in this part of the routine

;13 convert length into text string thats what RTOS does
(setq linelength (rtos linelength 4 8))

Where as the # 8 = the amout of places past the decimal

yea

if everyone helps everybody the world will be a better place
 
ok tigrek if your around today
now that i got the lisp working how do i go about exporting all lengths to either a txt comma delimeted file or en excel spreadsheet with ft & In in seperate columns so that i might get a total lenear length ???
I did find this on the news groups

;;;Length of your 14'-6 1/8" line as lisp sees it
(setq size 174.125)

(setq $feet (itoa (fix (/ (abs size) 12))))
(setq $inch (itoa (fix (- (abs size) (* (atoi $feet) 12)))))

$feet = "14"
$inch = "6"

you will need to fix this so as you get 6 1/8 instead of 6. HTH
if everyone helps everybody the world will be a better place
 
First,

You have done well with the
(distance start end)
function.

I thought the length from coordinates is more tutorial, kind of. Because the operations there will be needed in many cases but the distance function is pretty sterile, having no other use.

You are going for the shortest code. I thought we wanted something of tutorial value. Anyways... well done!

Second, you figured the decimal places pretty well too.
(setq linelength (rtos linelength unitsystem decimalPlaces))
see the help for rtos
4 is for architectural units - you wanted it so, remember?
can be 3, 2, 1 for other units. Was 2 for decimals? Check it out please.

Third, the tricky part:
You wanted the text some distance away from the line midpoint.
(setq midpoint (polar starting angleradian (/ linelength 2)))
brings us to the midpoint. We want to move perpendicularly away. Therefore
(setq midpoint (polar midpoint (+ angleradian (/ 22. 14)) distfromline))
Now to the distance from the line - you had named a distance but I figured, absolute distance is no good idea since drawings may have different scales. So, I used the size of text as the distance:
(setq textheight (getvar "TEXTSIZE"))
and I defined it on in a line of it:
(setq distfromline textheight)
instead of writing directly into
(setq midpoint (polar midpoint (+ angleradian (/ 22. 14)) (getvar "TEXTSIZE"))
so that it would be easy to replace this with any other value - it is a tutorial, remember.

Now to exporting to a text file etc. - well, that is all together a different cattle of fish and I do not think it very useful since those distances make sense only in connection with the graphic.
What would be the use of exporting dimensions of a drawing, for example? They are meaningful only within the graphic context.

Take care
 
To export any data to a ASCII text file, it should be in string format. To open a file and write data to it, you should follow a few simple guidelines.

1) Before opening a file to write, you should verify if there is an existing file, just in case you don't want to overwrite it.
2) Once you decide that it is ok to open a file, always make the writing procedure conditional upon a successful open, otherwise your program will fail miserably if the file cannot be opened, also the user won't know that the data did not get written out unless they are notified.
3) Always close an opened file AND create an error handler to close the file just in case the program gets canceled.

Keeping that in mind here we go:

;; first lets define an error handler
(setq oerr *error*)
(defun *error* ( err )
(close FILEHANDLE)
(setq *error* oerr)
)

;; find the file if it already exists and store whether the
;; user wants to overwrite it in variable ANS
(if (findfile "thisfile.xxx")
(progn
(initget "Yes No")
(setq ans (getstring &quot;\nFile exists overwrite?<N>/y: &quot;))
)
)

;; based on the value of ANS we will open the file to write

(if (/= ans &quot;No&quot;)

;; if the opening of the file is successful
;; keep in mind the &quot;w&quot; for write must be lower case
;; you can also open to read &quot;r&quot; or append &quot;a&quot;

(if (setq FILEHANDLE (open &quot;filename&quot; &quot;w&quot;))
(progn
;; here is where we will actually write the data
(write-line &quot;This is the first line in the file&quot; FILEHANDLE)
(write-line ans FILEHANDLE)
(write-line &quot;The above line should say Yes&quot; FILEHANDLE)
;; now we close the file
(close FILEHANDLE)
)
;; notify the user if the file was not opened
(princ &quot;\nFile opening failed&quot;)
)
)

;; reset the error handler to it's previous state

(setq *error* oerr)

You should be able to open a file and write out data using this example. To write out a comma delimited file, just write your data, then write a comma after each data write. However, you will need to use WRITE-CHAR instead of WRITE-LINE. The syntax is similar, you should check the online help for more information on WRITE-CHAR, alternatively you could pre-format your data with commas before writing it out.

Just remember the three initial points.

I am considering writing an in depth tutorial for LISP, if I get enough requests I will, otherwise it will be only as requested by users

Cheers to all...
 
Hey striker
That sounds like a winner let me know when you have a tutorial cause i would like a copy of it so put me on the list if everyone helps everybody the world will be a better place
 
Tigrek

Here found an old lsp that kinda does what i want but i need to make a few changes

(defun c:export ()

(setq file (strcat (getvar &quot;dwgname&quot; ) &quot;.DOC&quot;))

(setq ss (ssget (list (cons 0 &quot;TEXT&quot;))))

(setq number (sslength ss))

(setq counter 0)

(setq f (open file &quot;w&quot;))

(prompt &quot;Working... &quot;)

(repeat number

(setq entity (entget (ssname ss counter)))

(setq str (cdr (assoc 1 entity)))

(setq hand (cdr (assoc 5 entity)))

(setq hand_str (cons hand str))

(if (= counter 0)

(prin1 hand_str f)

(print hand_str f)

)

(setq counter (1+ counter))

)

(close f)

(princ)

)

first i always have to search for the file is there any way to force it to the directory that the drawing is in

also i have replaced the (&quot;.DOC&quot;) with (&quot;.TXT&quot;) is this all i need to do to force it to save as TXT

also i may want to make it comma delimeted and # teh lines 1 - Whatever

Could you push me in the right direction if everyone helps everybody the world will be a better place
 
Hi

This are to the point questions - very useful to anyone. Pleasure to answer:

1- If dwgname does not give the full path ( in VBA there has been some changes from ACAD14 to ACAD2000 on this issue, so I get confused):

Try this on the command line:

(setq MYPATH (getvar &quot;dwgprefix&quot;))

try it until a decent path is given back, like C:\Acadtemp\etc
I do not recall if it comes with a slash or without.
If slash missing, add it thus:
(setq MYPATH (strcat MYPATH &quot;/&quot;))
try on command line till it works right - &quot;/&quot; or &quot;\\&quot; or else.
Then attach dwgname to this path.
(setq MYPATH (strcat MYPATH (getvar &quot;dwgname&quot;)))

After on command line all works perfectly, combine all of the above into one line - not before - debugging is an art, not a science.

2. File ending, DOC or TXT or anything else makes no difference. As long as the writing statement is print, it is a txt file that is being written. Ending here is purely ornamental. Later when you click that file, the ending assumes a life of its own.

3- comma delimited:

Try each of these on the command line to see the results. Then you can combine as you like.

(princ &quot;, &quot; f) ;puts a comma and a space on the same line
(princ &quot;\n, &quot; f) ;the same on a new line - never needed I suppose but to illustrate the use of \n

(print &quot;, &quot; f) ; the same on a new line but with quotations

(prin1 &quot;, &quot; f) ; used to do something - I forgot.

Well, this much from memory - for more, I have to open Autocad and peep in there.

Take care
 
I said try these on command line but
f for filehandle is not defined!! Sorry.
first run on command line (copy/paste)

(setq file (strcat
(getvar &quot;dwgprefix&quot;)
&quot;\\&quot;
(getvar &quot;dwgname&quot; )
&quot;.TXT&quot;)
)

(setq f (open file &quot;w&quot;))

then try the print exercises above.

Then close the file:

(close f)

Hope to deserve a star for this.

Take care.


 
Tigrek
hmmm great info thats what i wanted to know but this lsp doesnot have any lines that i see that resemble the

(princ &quot;, &quot; f)

although i do know that it is making the file &quot;.&quot; delimeted
but where is it specifying this if everyone helps everybody the world will be a better place
 
It must be the one thing I have forgotten - namely - prin1

I thought you wanted to comma delimit because the code above is not doing it.

Why dont you try on command line and see what each prin... produces - irrespective of whether it is used above or not?
print makes the text quoted but in a new line.

I assumed you want comma delimited texts in the same line and provided the components for this.

Sorry if it was no help.
No wonder you did not give me a star
Thanks anyway for the feedback
take care

 
tigrek
ok sorry idint have a chance to star ya yesterday but you have them now :)

any way yes i did want it ,or- delimeted (- seems to work better cause i can use it in excel to seperate feet and inches)

But one thing i have noticed with this routine is that it places unnessacary ( &quot; &quot; ) and \es in the file how can i prevent this not that its a problem i just use the find replace feature but it can take up some time when you have a file with 500 or more entries

also with the
(setq file (strcat
(getvar &quot;dwgprefix&quot;)
&quot;\\&quot;
(getvar &quot;dwgname&quot; )
&quot;.TXT&quot;)
)
is there a way to tell it not to use teh dwg extentions in the name of the txt file its kind of anoying especially when trying to maintain naming conventions if everyone helps everybody the world will be a better place
 
The quotation marks come from
(print ...

that is why I introduced the
(princ &quot;\n ...
alternative above
It does the same as print except without quotation marks.

Please do the on command line exercize above for all print and princ variations.
Combinations may be infinite but building blocks are those above.

That was the easy one.
To cut off the dwg ending from a string is the difficult one.
You need to extract characters on the left, to the length of string length minus four (including the dot).

Please look up under the string functions for the synatx. I do not want to misspell from meory.

Thanks
 
To remove the extension from a file name use the following where DWGNAME is the name with extension and NAME is the name without the extension:

(setq NAME (substr DWGNAME 1 (- (strlen DWGNAME) 4)))

Remember that you must first place the drawing name in the variable DWGNAME which you can retrieve from the system variable DWGNAME as such:

(setq DWGNAME (getvar &quot;DWGNAME&quot;))

cheers...
 
tigrek

ok wouldnt you do something like this to remove the .dwg from the name

setq TFN (getvar &quot;dwgname&quot;) ;TFN is TEXT-FIlE-NAME
Setq FN (getstring TFN) ;Sets FN to The string equivelent of TFN

Now with this in mind just need to figure out how to set it so that it ignores a <.> and everything after it
also now my second line above is that right can you set a variable to a string
if everyone helps everybody the world will be a better place
 
ooh Striker pleas elaberate on the strring you just produced

(setq NAME (substr DWGNAME 1 (- (strlen DWGNAME) 4)))

where is it telling it to remove from the end of the string i think i see where you are specifying how many characters but is the minus specifying the end of the string

what does the 1 in the substr area do exactly do you need to set up a counter to cycle thru the strlen amount so that long names are ok and the proper amount is removed
if everyone helps everybody the world will be a better place
 
Well done Striker. it is indeed

(setq NAME (substr DWGNAME 1 (- (strlen DWGNAME) 4)))

(substr SomeText FromCharacterPosition SoManyCharacters)

So, Shadow, the 1 is the position 1 in the string, namely the path+dwgname

Length of string minus four is
(- (strlen DWGNAME) 4))
Why four? well, folks, .dwg is a four letter word.

Take care all.


 
Yea But Tigrek what specifies it to start from the end and work back to take off the extention and i kne the 4 was for the .dwg this could be usefull to me later on in other lsps
if everyone helps everybody the world will be a better place
 
well,
(substr ..
always starts from the beginning. Unfortunately there is no (trim to the left of him trim to the right) in lisp.

So, you have to say, I want a substring, starting from position such, like 1 here, and have length such - so, indirectly you leave the last four out.

It is a take it or leave it kind of offer from Lisp.

Have to live with it. Make the best of it, Shadow.
 
hey tigrek
just thought of something
i find that alot of times i use ctrl+c,x,v for copy cu paste
but was thinking can lisp read the clipboard for string info?? maybe if it can read and write to the clipboard that would be a good/useful way of doing a few things
oh on your other post about the cmd/filedia to prevent dialogs the dialog you see when doing that is not going to go away but use the -image command this is the equivalent of going to image manager there you can set a few things but if you use the sub command attach you get the same dialog but if there is already an image there you can repath it hope that helps you look in acad documentation

Chapter 20 -- Working with Raster Images
Managing Raster Images
Changing Image File Paths

also i found the systvar insunits which is image units
apparently the dialog in question may be controlled anotehr way i will look into it more if everyone helps everybody the world will be a better place
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor