Author Topic: LineType Loader  (Read 4614 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
LineType Loader
« on: October 10, 2008, 03:25:37 PM »
I was messing with my Linetype load lisp found here:
http://www.theswamp.org/index.php?topic=23888.0
and noticed that there is a bug. When the linetype is already loaded it will not reload using vla-load
but will load using the (vl-cmdf "_-linetype". That is unless I am missing something.
Problem with using the command version that no catachable error is generated.

Here is the revised routine. As it loops through the list of line files I want to stop the loop when the
load is successful. The command version generated a message at the command line but how would one
detect this?
No matching linetypes found in file C:\PROGRAM FILES\ACAD2000\support\acad.lin.

Code: [Select]
;;;====================[ Linetype-load.lsp ]=======================
;;; Author: Copyright© 2008 Charles Alan Butler
;;; Version:  1.3 Oct. 10, 2008
;;; Purpose:  function to load a list of linetypes
;;; Sub_Routines: -None
;;; Requirements: (Linetype-load lt-list (list "cab.lin"))
;;; The first argument is a list of list containing the linetype name,
;;; the preferred linetype file to search, this may be nil or missing if <reload-flag> is missing
;;; and a t/nil flag to reload an existing linetype, this may be nil or missing
;;;'((<lt-list> <lt-file-name> <reload-flag>)...)
;;; <lt-list> (("hidden" "acad.lin" <lt-load-flag>)("dashed" "acad.lin") ...)
;;; <lt-file-name> Optional, name of a LT file "lt-file01.lin", may be nil or missing if <reload-flag> is missing
;;; <reload-flag> Optional, T / nil t=reload the linetype even if it exist in the DWG
;;;   default for missing flag is nil and will not reload an existing linetype
;;; Valid list are '((<lt-list>) (<lt-list> <lt-file-name>) (<lt-list> <lt-file-name> <reload-flag>))
;;;
;;; Second argument is a list of linetype files that will be searched after the preferred file if
;;; the preferred file fails. This argument may be nil. The default ACAD LT file is always searched last.
;;; <file-list> ("lt-file01.lin" "lt-file02.lin" ...)
;;;
;;; Returns: nil if successful else a list of failed linetype names
;;;==========================================================

(defun Linetype-load (lt-list   ; '((<lt-list> <lt-file-name> <reload-flag>)...)
                      file-list ; '("lt-file01.lin" "lt-file02.lin" ...)
                      / lt-data ltypes fname ltname def-ltfile tmp-file-list
                      lt-loaded reload-flag idx err result)
  (vl-load-com)
  (setq ltypes (vla-get-linetypes
                 (vla-get-activedocument (vlax-get-acad-object))))
  ;;  set a default linetype file
  (if (zerop (getvar "measurement"))
    (setq def-ltfile "acad.lin")
    (setq def-ltfile "acadiso.lin")
  )
  ;; load the linetypes
  (foreach lt-data lt-list
    (setq ltname (car lt-data)
          fname  (cadr lt-data)
          reload-flag (caddr lt-data)
    )
    (setq lt-loaded (tblsearch "ltype" ltname)) ; does the LineType exist in the DWG
    (if file-list
      (if (not (vl-position (strcase def-ltfile) (mapcar 'strcase file-list)))
        (setq file-list (append file-list (list def-ltfile)))
      )
      (setq file-list (list def-ltfile))
    )
    (cond
      ((and (not reload-flag) lt-loaded)
       (prompt (strcat "\nLinetype " ltname " already loaded."))
       ;;  stop here and do not reload the linetype
      )
      ;; test a specific linetype file, never stops here though
      ((and fname (not (findfile fname))
       (princ (strcat "\nCan not fine file " fname))
       (setq fname nil))
      )
      (t ; Try & load linetype from file list, quit if loaded OK
       (if (and fname ; add it to the file list as first choice
                (not (vl-position (strcase fname) (mapcar 'strcase file-list))))
         (setq tmp-file-list (cons fname file-list))
         (setq tmp-file-list file-list)
       )
       (setq idx 0)
       (if lt-loaded ; problem with loaded linetypes and vla-load
         (progn
         (while (and   ; CAB 10.10.08
                 (< idx (length tmp-file-list))
                 (vl-cmdf "_-linetype" "_l" ltname (findfile (nth idx tmp-file-list)) "")
                 (and (vl-string-search "NO MATCHING LINETYPES" (strcase (getvar 'LastPrompt)))
                      (setq result (cons ltname result))
                      nil ; force exit
                 )
                )
           (setq idx (1+ idx))
         )
         )
       ;;  loop until LT is loaded or out of file choices
       (while (and
                (< idx (length tmp-file-list))
                (vl-catch-all-error-p
                  (setq err (vl-catch-all-apply
                              'vla-load (list ltypes ltname (nth idx tmp-file-list)))))
                (if (vl-catch-all-error-p err)
                  (princ (strcat "\nFile " (nth idx tmp-file-list)
                                  ": "(vl-catch-all-error-message err) " " ltname))
                )
              ) ; and
         (setq idx (1+ idx))
       )
       )

       (if (= idx (length tmp-file-list))(setq idx (1- idx))) ; CAB 10.10.08
       
       (cond  ; Report status of Load Attempt
         ((vl-catch-all-error-p err)
          ;(prompt (strcat "\n" (vl-catch-all-error-message err) " " ltname))
          (setq result (cons ltname result))
         )
         ((vla-item ltypes ltname)
          (prompt (strcat "\nLinetype " ltname " loaded from file " (nth idx tmp-file-list) "."))
         )
         (t
          (prompt (strcat "\nError Linetype " ltname " not loaded."))
          (setq result (cons ltname result))
         )
       )
      )
    )
  )
  (if result ())
  result ; return a list of failed linetype names
)
This is my test rig.
Code: [Select]
(defun c:clam (/ lt-list fail-list)
  (or lt-ent (setq lt-ent (car (entsel "\nSelect obj to work with:"))))
  (setq lt-list '(("clam"      ; linetype name
                   "CAB2.lin"  ; linetype file
                   t           ; flag to reload LT
                   )))
  ;(setq lt-list '(("clam" nil t)))
  (if (setq fail-list (Linetype-load lt-list nil))
    (princ "\nFailed to load linetypes:")
  )
  (mapcar 'print fail-list)
  (setq el (entget lt-ent))
  (entmod (subst '(6 . "Continuous") (assoc 6 el) el))
  (entmod (subst '(6 . "CLAM") '(6 . "Continuous") el))
  (princ)
)
« Last Edit: October 10, 2008, 03:53:52 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: LineType Loader
« Reply #1 on: October 10, 2008, 03:34:46 PM »
<snip>
The command version generated a message at the command line but how would one detect this?
<snip>

(getvar 'LastPrompt) ??
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: LineType Loader
« Reply #2 on: October 10, 2008, 03:53:17 PM »
Thanks Tim, never used that one, you're a life saver. :)

Updated the code above with the quick fix.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: LineType Loader
« Reply #3 on: October 10, 2008, 05:56:49 PM »
Thanks Tim, never used that one, you're a life saver. :)

Updated the code above with the quick fix.
You're welcome.  Glad I could help.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

hooper94

  • Guest
Re: LineType Loader
« Reply #4 on: November 04, 2008, 02:04:44 PM »
I'm feeling like a total novice here.  I have an "personal" linetype file that has become a pain to load each time I need it and this looks exactly like what I need, but I'm not sure how to load it  :oops: Can someone please help?   

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: LineType Loader
« Reply #5 on: November 04, 2008, 04:02:28 PM »
Welcome to TheSwamp. :-)

You will need to copy the first bit of code & paste it in a text editor.
Save the file to a folder in the ACAD path using the file name "Line type-load.lsp"

Copy the next bit of code or better yet use this below.
Same procedure but name it what ever you like.
Replace MyLtName with your line type name
Replace MyLtFileName with the file where the line type can be found. Must be in the ACAD path.
You may replace MyLtLoader with something you like.

Add this routine to your 'Startup Suite'

You can then type MyLtLoader at the command line to load the line type. Note that you must close & reopen
the drawing for the startup load to take affect.

Code: [Select]
(defun c:MyLtLoader()
  (if (zerop (load "Linetype-load.lsp" 0))
    (prompt "\nError-could not load \"Linetype-load.lsp\"")
    (Linetype-load '(("MyLtName" "MyLtFileName") nil)
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

hooper94

  • Guest
Re: LineType Loader
« Reply #6 on: November 06, 2008, 03:12:05 PM »
Thanks for the help and the welcome.  I've benefitted quite a few times from all the knowledge and code floating around here.  Hopefully I can give back someday  :-)