chizifreshi Posted July 22, 2011 Share Posted July 22, 2011 (edited) Hi to all. I am a new member who has just joined the CADtutor forum. I need help with part of the lsp file shown below. The current output of chainage with the lsp file below is shown as a real number e.g 1240. could someone kindly let me know what changes i can make to it so that the output will be in the form 1+240. ;; ********** Miscellaneous Entries ********** ; Alternative GROUND LEVEL, PIPE INVERT LEVEL, PIPE DEPTH AND CHAINAGE entry (defun C:GROUND ( ) (points) (lines) (texts) ) ; Entry points for Ground Level and Pipe Invert Level (defun points ( ) (setq glp (getpoint "\nEnter Ground Level (endpoint): ") ; Ground Level Entry plp (getpoint "\nEnter Pipe Invert Level (intersection): ")) ;Pipe Invert Level Entry ) ;Vertical lines sub-routine (defun lines ( ) ; ground level (setq lingl1 (list (nth 0 glp) (nth 1 ptdat1)) lingl2 (list (nth 0 glp) (nth 1 ptgl1))) (command "line" lingl1 lingl2 "") ; pipe invert and depth of pipe (setq linpinv (list (nth 0 plp) (nth 1 ptgeo1)) lindep (list (nth 0 plp) (nth 1 ptdep1))) (command "line" linpinv lindep "") ; chainage (setq linch1 (list (nth 0 glp) (nth 1 ptslp1)) linch2 (list (nth 0 glp) (nth 1 ptch1))) (command "line" linch1 linch2 "") ) (defun texts ( ) ; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch (rtos (car glp) 2 2)) ; Chainage ; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5 )) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5 )) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5 )) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5 ))) ; co-ordinate position for chainage text ; Labelling levels and chainages (command "text" psnglvl 2.2 90.0 glvl) (command "text" psnplvl 2.2 90.0 plvl) (command "text" psndep 2.2 90.0 deplvl) (command "text" psnch 2.2 90.0 ch) ) Edited July 22, 2011 by SLW210 Quote Link to comment Share on other sites More sharing options...
SLW210 Posted July 22, 2011 Share Posted July 22, 2011 Please read CODE POSTING GUIDELINES!!!! Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 22, 2011 Author Share Posted July 22, 2011 Sorry my mistake for not reading Code Posting Guidelines. Yes i and a colleague are authors of the lisp file shown below Hi to all. I am a new member who has just joined the CADtutor forum. I need help with part of the lsp file shown below. The current output of chainage with the lsp file below is shown as a real number e.g 1240. could someone kindly let me know what changes i can make to it so that the output will be in the form 1+240. ;; ********** Miscellaneous Entries ********** ; Alternative GROUND LEVEL, PIPE INVERT LEVEL, PIPE DEPTH AND CHAINAGE entry (defun C:GROUND ( ) (points) (lines) (texts) ) ; Entry points for Ground Level and Pipe Invert Level (defun points ( ) (setq glp (getpoint "\nEnter Ground Level (endpoint): ") ; Ground Level Entry plp (getpoint "\nEnter Pipe Invert Level (intersection): ")) ;Pipe Invert Level Entry ) ;Vertical lines sub-routine (defun lines ( ) ; ground level (setq lingl1 (list (nth 0 glp) (nth 1 ptdat1)) lingl2 (list (nth 0 glp) (nth 1 ptgl1))) (command "line" lingl1 lingl2 "") ; pipe invert and depth of pipe (setq linpinv (list (nth 0 plp) (nth 1 ptgeo1)) lindep (list (nth 0 plp) (nth 1 ptdep1))) (command "line" linpinv lindep "") ; chainage (setq linch1 (list (nth 0 glp) (nth 1 ptslp1)) linch2 (list (nth 0 glp) (nth 1 ptch1))) (command "line" linch1 linch2 "") ) (defun texts ( ) ; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch (rtos (car glp) 2 2)) ; Chainage ; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5 )) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5 )) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5 )) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5 ))) ; co-ordinate position for chainage text ; Labelling levels and chainages (command "text" psnglvl 2.2 90.0 glvl) (command "text" psnplvl 2.2 90.0 plvl) (command "text" psndep 2.2 90.0 deplvl) (command "text" psnch 2.2 90.0 ch) ) Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 22, 2011 Share Posted July 22, 2011 (vl-load-com) (setq staTxt (rtos 1240 2 2)) (setq staTxt (vl-string-subst (strcat (setq prefix (substr staTxt 1 1)) "+") prefix staTxt)) Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 22, 2011 Share Posted July 22, 2011 Using this as a sub routine may also be beneficial, as it appears that you're doing this same process multiple times for each glvl, plvl, delvl, and ch variables: (defun _AddSta (str pos / prefix) (vl-load-com) (vl-string-subst (strcat (setq prefix (substr str 1 pos)) "+") prefix str)) Here are some examples: _$ (_AddSta (rtos 1240 2 2) 1) "1+240.00" _$ (_AddSta (rtos 1240 2 2) 2) "12+40.00" _$ Hope this helps! Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 22, 2011 Author Share Posted July 22, 2011 Hi Renderman Thanks for your responses. I am trying to fit the codes you have posted into my lsp file but i still keep getting the chainage as a real number. I am trying to insert your code under (defun text () ...where ch appears in my lsp file. I Think thats where i am to insert your code. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 22, 2011 Share Posted July 22, 2011 If I understand your code correctly, this *should* work: (defun texts ( / _AddSta) (defun _AddSta (str pos / prefix) (vl-load-com) (vl-string-subst (strcat (setq prefix (substr str 1 pos)) "+") prefix str)) ;; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch [color=red](_AddSta [/color][color=blue](rtos (car glp) 2 2)[/color] [color=red]1)[/color]) ; Chainage ;; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5)) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5)) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5)) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5))) ; co-ordinate position for chainage text ;; Labelling levels and chainages (command "text" psnglvl 2.2 90.0 glvl) (command "text" psnplvl 2.2 90.0 plvl) (command "text" psndep 2.2 90.0 deplvl) (command "text" psnch 2.2 90.0 ch) ) Also, consider localizing your variables, as I have done with the "_AddSta" function. Hope this helps! Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 23, 2011 Author Share Posted July 23, 2011 Hi RenderMan Thanks again for your response. The chainage is now appearing the way i want. However, it appears not to represent output of chainages less then 1000m correctly. e.g output for chainage of say 560m appears as 5+60 instead of 0+560. Similarly for chainagesin excess of 10,000m, say 11,490m is represented as 1+1490 instead of 11+490 For chainages between 1,000m and 10,000m, the output is fine. Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 What about changing the _AddSta to this: (defun _AddSta (num / ) (vl-string-translate ",." "++" (rtos (/ num 1000.0) 2 3) ) ) Then you have the calling portion thus: ch (_AddSta (car glp)) Or am I misunderstanding this? Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 And if your chainages can run to more than 1km: (defun _AddSta (num / _AddSta_Help str) (defun _AddSta_Help (num /) (if (>= num 1000.0) (strcat (_AddSta_Help (fix (/ num 1000.0))) (_AddSta_Help (fix (rem num 1000.0)))) (strcat "+" (substr (rtos (+ num 1000.0) 2 0) 2)) ) ) (if (wcmatch (setq str (_AddSta_Help num)) "+*") (strcat "0" str) str ) ) Sorry, I'm not sure how you want your numbers formatted in such a case. Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 23, 2011 Author Share Posted July 23, 2011 Hi irneb, I have tried to insert your code as shown below. when i try to load the lsp file i receive this message: ";error : malformed list on input". Could it be i am inserting your code inappropriately? (defun texts ( / _AddSta) (defun _AddSta (num / _AddSta_Help str) (defun _AddSta_Help (num /) (if (>= num 1000.0) (strcat (_AddSta_Help (fix (/ num 1000.0))) (_AddSta_Help (fix (rem num 1000.0)))) (strcat "+" (substr (rtos (+ num 1000.0) 2 0) 2)) ) ) (if (wcmatch (setq str (_AddSta_Help num)) "+*") (strcat "0" str) str ) ) ;; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch (_AddSta (car glp)) ; Chainage ;; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5)) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5)) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5)) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5))) ; co-ordinate position for chainage text ;; Labelling levels and chainages (command "text" psnglvl 5.5 90.0 glvl) (command "text" psnplvl 5.5 90.0 plvl) (command "text" psndep 5.5 90.0 deplvl) (command "text" psnch 5.5 90.0 ch) ) Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 23, 2011 Author Share Posted July 23, 2011 Hi irneb I have inserted your code as shown below and when i try to load the lsp file i get this error message: ";error: malformed on list input" Could it be i am inserting your code incorrectly (defun texts ( / _AddSta) (defun _AddSta (num / _AddSta_Help str) (defun _AddSta_Help (num /) (if (>= num 1000.0) (strcat (_AddSta_Help (fix (/ num 1000.0))) (_AddSta_Help (fix (rem num 1000.0)))) (strcat "+" (substr (rtos (+ num 1000.0) 2 0) 2)) ) ) (if (wcmatch (setq str (_AddSta_Help num)) "+*") (strcat "0" str) str ) ) ;; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch (_AddSta (car glp)) ; Chainage ;; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5)) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5)) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5)) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5))) ; co-ordinate position for chainage text ;; Labelling levels and chainages (command "text" psnglvl 5.5 90.0 glvl) (command "text" psnplvl 5.5 90.0 plvl) (command "text" psndep 5.5 90.0 deplvl) (command "text" psnch 5.5 90.0 ch) ) Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 Uhmm, yes well ... there was a missing closing parenthesis. But I just realized my 2nd function didn't work so well Anyhow, this one should work fine for anything smaller than 1000000. I'll revamp the thing later to work on larger numbers.: (defun texts (/ _AddSta) (defun _AddSta (num /) (vl-string-translate ",." "++" (rtos (/ num 1000.0) 2 3) ) ) ;; Level/Chainage texts (setq glvl (rtos (/ (cadr glp) 10) 2 2) ; Ground level plvl (rtos (/ (cadr plp) 10) 2 2) ; Pipe invert level deplvl (rtos (/ (- (cadr glp) (cadr plp)) 10) 2 2) ; Depth of pipe invert ch (_AddSta (car glp)) ; Chainage [color=red])[/color] ;; Text co-ordinates (setq psnglvl (list (- (car glp) 0.75) (+ (cadr ptgl1) 0.5)) ; co-ordinate position for ground level text psnplvl (list (- (car plp) 0.75) (+ (cadr ptinv1) 0.5)) ; co-ordinate position for pipe invert level text psndep (list (- (car plp) 0.75) (+ (cadr ptdep1) 0.5)) ; co-ordinate position for depth of pipe text psnch (list (- (car glp) 0.75) (+ (cadr ptch1) 0.5)) ; co-ordinate position for chainage text ) ;; Labelling levels and chainages (command "text" psnglvl 5.5 90.0 glvl) (command "text" psnplvl 5.5 90.0 plvl) (command "text" psndep 5.5 90.0 deplvl) (command "text" psnch 5.5 90.0 ch) ) The omitted closing parent is marked as red. I'm just having difficulty in testing. Your lines function refers to a set of variables ptdat1, ptgl1, etc. I don't have these and they cause an "error: bad argument type: consp nil" on this line: ; ground level (setq lingl1 (list (nth 0 glp) [color=red](nth 1 ptdat1)[/color]) Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 Just a query, where do you edit the lisp? Inside VLIDE? Have you made your formatting in some custom way? I just prefer the default where the closing parenthesis is on the same indentation as its matching open parenthesis. Your formatting looks a lot more like Scheme / Python's formatting than the "usual" AutoLisp. There's nothing "wrong" with that, I just find it difficult to visually see if the open & close paren's match. IMO that type of formatting should be left for languages where there's no open and close token, like you get with python. Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 Also, consider localizing your variables, as I have done with the "_AddSta" function.+1 on this score! You're using global variables extensively. The error I get is one of many examples why globals aren't the greatest. Just keep in mind for the future. It makes your life simpler when you keep to localized variable as much as possible. Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 BTW, here's my amended code for working with larger numbers from and above one million: (defun _AddSta (num /) (if (>= num 1000.0) (strcat (vl-string-left-trim "+0" (_AddSta (fix (/ num 1000.0)))) (vl-string-left-trim "0" (_AddSta (fix (rem num 1000.0)))) ) (vl-string-translate ",." "++" (rtos (/ num 1000.0) 2 3)) ) ) Test code: _$ (_AddSta 11234) "11+234" _$ (_AddSta 1123356774) "1+123+356+774" _$ (_AddSta 774) "0+774" _$ (_AddSta 112335674) "112+335+674" _$ Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 23, 2011 Author Share Posted July 23, 2011 Hi irneb Let me start by saying thank you very much. The lsp file works exactly the way i want. For editing my lsp file i actually open the file itself and make amendments to it. I guess i am not familiar with an other way as i am still quite new to AUTOlisp. Quote Link to comment Share on other sites More sharing options...
irneb Posted July 23, 2011 Share Posted July 23, 2011 You're welcome! So you mean you edit it in something like Notepad? I'd suggest you use AutoCAD's built-in editor. Just type VLIDE at autocad's command prompt. The "major" benefit is you can debug your code a lot more easily there, you can even step through the code and add watches so you can see the values inside each variable at every point in the code. Also pressing Ctrl+F1 while your cursor is on a particular word in the editor will open the developer help for that function (if it exists in help). And then also you can load the LSP by clicking one button on the toolbar, instead of going through AppLoad / typing (load "filename") / any other stuff. And lastly there's the VisualLisp console where you can type your testcode so it runs immediately. There's some other nice features, but these are enough to get you started. If you want a much more comprehensive editor, look at things like Notepad++ or PSPad, etc. Though those won't give you any debugging and direct help like you get with VLIDE. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 23, 2011 Share Posted July 23, 2011 I'd suggest you use AutoCAD's built-in editor. 1+ These may help in that respect. Quote Link to comment Share on other sites More sharing options...
chizifreshi Posted July 23, 2011 Author Share Posted July 23, 2011 Thanks irneb. Though i have to come back to you again. i have started inserting chainages and i have noticed chainages below 1000m, say 560 appears as 0+56 and not 0+560. Similarly, chainages less than 100m e.g 30m appears as 0+03 and not 0+030. Also i would like the chainage to have 2 decimal places e.g for the case of 560, 0+560.00 Yes i use notepad to edit the lsp file. I will certainly try the AuTOCAD built in editor. had not known about it at all. Quote Link to comment Share on other sites More sharing options...
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.