Jump to content

IF linetype exists


Happy Hobbit

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Happy Hobbit
Link to comment
Share on other sites

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."))
)

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...