Happy Hobbit Posted September 30, 2015 Share Posted September 30, 2015 I am writing a lisp which calls a custom linetype from Acadiso.lin The linetype doesn't exist on most users machines, but does on some. What I'd like to put into my code is something like: (If (linetype "DASHED2.5" exists) (progn (command "._linetype" "_load" "DASHED2.5" "Acadiso.lin" ""); then (command "-layer" "M" "BOX" "L" "DASHED2.5" "" "LW" "0.5" "" "C" "6" "" "" ) );end prong (progn ((command "._linetype" "_load" "hidden2" "Acadiso.lin" ""); else (command "-layer" "M" "BOX" "L" "hidden2" "" "LW" "0.5" "" "C" "6" "" "" ) );end progn ) );end if Linetype "DASHED2.5" is preferred to "hidden2" which is a standard linetype Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 30, 2015 Share Posted September 30, 2015 Please refer to my Load Linetypes function. Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted September 30, 2015 Author Share Posted September 30, 2015 Please refer to my Load Linetypes function. Hello again Lee That's quite a lot of code to add My lisp is already over 260 lines long, I was hoping for something a little shorter Plus I do not totally understand your code as it's uncommented Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 30, 2015 Share Posted September 30, 2015 If you wish to test whether a linetype is already defined in the active drawing, use: (if (tblsearch "ltype" "YourLinetypeHere") ... ) My Load Linetypes function includes two options for testing whether a linetype is defined within a *.lin file: the LM:ltdefined-p function will read a .lin file with the given filename and will return T if a linetype with the supplied name is defined in the file. Alternatively, the expression: (vl-some '(lambda ( lin ) (vl-catch-all-apply 'vla-load (list ltc typ lin)) (tblsearch "ltype" typ) ) lst ) will iterate over the set all of .lin files found in all support file search paths (which has already been assigned to the variable 'lst'), and will attempt to load the target linetype from each file, ceasing if successful. Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted September 30, 2015 Author Share Posted September 30, 2015 Soooo, using the first option like this: (defun c:test2 () (setvar "Expert" 3) (if (tblsearch "ltype" "noddy") (progn (command "._linetype" "_load" "Noddy" "Acadiso.lin" "") (command "-layer" "M" "Big-Ears" "L" "Noddy" "" "LW" "0.5" "" "C" "6" "" "" ) (princ "\n Linetype Noddy Loaded") ) (progn (command "._linetype" "_load" "DASHED2.5" "Acadiso.lin" "") (command "-layer" "M" "Big-Ears" "L" "DASHED2.5" "" "LW" "0.5" "" "C" "6" "" "" ) (princ "\n Linetype DASHED2.5 Loaded") ) ) (setvar "Expert" 3) (princ) ) It seems to work! I wish I understood the second option better PS I don't actually have a linetype called Noddy, it was a good way of making the if statement go to 'else' Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 30, 2015 Share Posted September 30, 2015 Please note that the expression (tblsearch "ltype" "YourLinetypeHere") will only test whether the given linetype is defined in the active drawing, and will not check whether the given linetype is defined in a .lin file (which is what you seem to want to test, since you are subsequently loading the linetype). Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted September 30, 2015 Author Share Posted September 30, 2015 That's odd Lee Because I just ran it in a drawing where the linetype wasn't loaded & the lisp loaded it. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 30, 2015 Share Posted September 30, 2015 That's odd Lee Because I just ran it in a drawing where the linetype wasn't loaded & the lisp loaded it. Your code is currently set to load a linetype for either case - in plain terms, your code says: IF Noddy linetype is already defined THEN: Load Noddy linetype from acadiso.lin Create Big-Ears layer and assign various properties Print message ELSE: Load DASHED2.5 linetype from acadiso.lin Create Big-Ears layer and assign various properties Print message Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted September 30, 2015 Author Share Posted September 30, 2015 I see... Would something like this solve it? (vl-some '(lambda ( lin ) (vl-catch-all-apply 'vla-load (list ltc typ lin)) (tblsearch "DASHED2.5" typ) ) lst ) (if (/= lst t) (progn (vl-some '(lambda ( lin ) (vl-catch-all-apply 'vla-load (list ltc typ lin)) (tblsearch "hidden2" typ) ) lst ) ;My create layer macro here for DASHED2.5 ) ;My create layer macro here for hidden2 ) (vl-load-com) Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 1, 2015 Share Posted October 1, 2015 You need to go back to basics of how a IF works if something is true do this line if not do this line Your look for noddy will return False so runs second part "else" of IF (if (= (tblsearch "ltype" "noddy")T) (PRINC "LINE 1") (PRINC "LINE 2") ) Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted October 1, 2015 Author Share Posted October 1, 2015 (edited) That's what I was doing The if statement was asking if linetype "DASHED2.5" (1st choice) existed in the acadiso.lin. If yes, then load it. Else load "hidden2" (2nd choice) At least that's what I was attempting to do Edited October 1, 2015 by Happy Hobbit Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 1, 2015 Share Posted October 1, 2015 Try the following: (defun ltload ( typ lin ) (eval (list 'defun 'ltload '( typ lin ) (list 'vl-catch-all-apply ''vla-load (list 'list (vla-get-linetypes (vla-get-activedocument (vlax-get-acad-object))) 'typ 'lin ) ) '(tblsearch "ltype" typ) ) ) (ltload typ lin) ) (cond ( (ltload "DASHED2.5" "acadiso.lin") (princ "\nLoaded DASHED2.5 linetype.") ) ( (ltload "HIDDEN2" "acadiso.lin") (princ "\nLoaded HIDDEN2 linetype.") ) ( (princ "\nUnable to load HIDDEN2 linetype.")) ) Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted October 1, 2015 Author Share Posted October 1, 2015 Bang on, thank you very much Lee. To test I ran it in a new drawing & it loaded dashed2.5 I then deleted dashed2.5 from acadiso, saved it, purged the drawing & re-ran the lisp Bingo, it loaded hidden2 I semi-guess that: ( (princ "\nUnable to load HIDDEN2 linetype.")) is condition T ? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 1, 2015 Share Posted October 1, 2015 Bang on, thank you very much Lee. To test I ran it in a new drawing & it loaded dashed2.5 I then deleted dashed2.5 from acadiso, saved it, purged the drawing & re-ran the lisp Bingo, it loaded hidden2 You're welcome Happy Hobbit - the method is essentially the same as that used by my Load Linetypes function, though significantly condensed as you already know the .lin file in which the linetypes reside. I semi-guess that: ( (princ "\nUnable to load HIDDEN2 linetype.")) is condition T ? You are correct - this expression will be evaluated if the program fails to load either linetype and hence the preceding test expressions have returned nil. Note that there needn't be an explicit 'T' as the test expression for this final condition, as the first expression (the test expression) will always be evaluated. Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted October 1, 2015 Author Share Posted October 1, 2015 In post #9 I was attempting to use an IF statement to find whether the code was returning T or not. Clearly it was the wrong way to go about it. Now I've inserted your defun ltload (as a sub function) into the main lisp I an writing & the cond function within the main list, it works just great Thank you very very much indeed Lee Top marks to the ever helpful guy in London Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 1, 2015 Share Posted October 1, 2015 In post #9 I was attempting to use an IF statement to find whether the code was returning T or not. Clearly it was the wrong way to go about it. For what its worth, you could still write the code using a series of IF statements (in general, any COND expression can always be written as a series of nested IF expressions), e.g.: (if (ltload "DASHED2.5" "acadiso.lin") (princ "\nLoaded DASHED2.5 linetype.") (if (ltload "HIDDEN2" "acadiso.lin") (princ "\nLoaded HIDDEN2 linetype.") (princ "\nUnable to load HIDDEN2 linetype.") ) ) I just find COND clearer in this case. Now I've inserted your defun ltload (as a sub function) into the main lisp I an writing & the cond function within the main list, it works just great Thank you very very much indeed Lee Top marks to the ever helpful guy in London Excellent to hear, and thank you for your kind words & gratitude. Lee Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted October 1, 2015 Author Share Posted October 1, 2015 No thanks Lee, I'd sooner use cond every time. It's far clearer than nested 'if then else' statements, even using vlide. I'm even thinking of updating some of my early attempts (only about 12 months ago) at lisp to replace ifs with cond. If I ever find the time that is. 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.