Jump to content

Layer Creater Lisp Routine Issue


waders

Recommended Posts

..... "fixo" code on this post and his works but it's in "mm" & not in "inches" and it won't override all the setting on the layers if they already exist in the drawing. Any ideas? Anyone? Thanks

 

ActiveX API uses "metric" for the lineweights.

So 0.024" will that be 0.60 mm and 0.007 is 0.15 mm>?

 

if that is indeed the case..

(defun c:ArchExist2 (/ aDoc lyrotd _delFinder  _valid _nth YesNo file
             a lst i o c lnm lt_)
(vl-load-com)
(setq aDoc   (vla-get-activedocument (vlax-get-acad-object))
     lyrotd (vla-get-layers aDoc))     
(defun _delFinder  (str / d l str)
     (while (setq d (vl-string-position 44 str nil T))
           (setq l (cons (substr str (+ 2 d)) l)
                 str (substr str 1 d)))
     (cons str l)
     )
(defun _valid (l sym / a)
(setq a  (vl-remove-if-not '(lambda (x) (sym l x))
 '(0 5 9 13 15 18 20 25 30 35 40 50 53 60 70
         80 90 100 106 120  140 158 200 211)))
(if (eq sym <)(car a)(last a)))      
(setq _nth (lambda (j) (nth (setq c (1+ j)) lst)))
(setq YesNo (lambda (ans) (if (eq ans "Yes") 1 0)))     
(cond ((and  (setq LinPath (findfile "acad.lin"))
            (setq file (getfiled "Select layer file" (getvar 'dwgprefix) "csv" )
            (setq file (open (findfile file) "r"))
     (read-line file)(setq i 0)
            (while (setq a (read-line file))
      (setq lst (_delFinder a))
               (setq c   -1
                     Lnm (vl-catch-all-apply
                               'vla-add
                               (list lyrotd (_nth c))))
               (vla-put-description lnm (_nth c))
               (vla-put-color lnm (atoi (_nth c)))
               (if (= nil (tblsearch "LTYPE" (setq lt_ (_nth c))))
                   (vl-catch-all-apply
                         'vla-load
                         (list (vla-get-linetypes aDoc) lt_ LinPath)))
               (vla-put-linetype lnm lt_)
               (vla-put-lineWeight lnm
                     (if (eq (setq lw (_nth c)) "Default")
                           -3
                           (_valid (fix (* (atof lw) 25.4 100)) >=)))
               (vla-put-Plottable lnm (YesNo (_nth c)))
               (vla-put-Freeze lnm (YesNo (_nth c)))
               (setq i (1+ i))
                  )

              (close file)
            )
      )
     )(princ (strcat "\n\t\t<<<< "(itoa i) " Layers Created >>>>"))
     (princ)
    )

 

HTH

Edited by pBe
Link to comment
Share on other sites

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

  • waders

    11

  • alanjt

    8

  • pBe

    8

  • BlackBox

    6

Top Posters In This Topic

Hey fixo & pBe,

 

This seems to do it. Thanks for your help. Now just need to study the code a little more to see if I can fully understand it. Thanks again

Link to comment
Share on other sites

Hey fixo & pBe,

 

This seems to do it. Thanks for your help. Now just need to study the code a little more to see if I can fully understand it. Thanks again

 

Good for you waders.

Glad we could help

 

Cheers :)

Link to comment
Share on other sites

  • 4 weeks later...
Perhaps this will help (code written quickly):

 

(defun RM:CSV->Layers  (path / f *error* extract trim activeDoc l
                       layerTable layerName layerItem layerDescription
                       layerColor layerLinetype layerLineweight
                       layerPlottable layerFreeze)
 ;; © RenderMan 2011, CADTutor.net
 ;; Exampe: (RM:CSV->Layers "Z:\\my_layers_folder\\my_layers.csv")
 (vl-load-com)
 (if (and (findfile path)
          (setq f (open path "r")))
   (progn

     ;; Error handler
     (defun *error*  (msg)
       (cond
         ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
       (if f
         (close f))
       (princ))

     ;; Line extraction sub-function
     (defun extract  (l /)
       (substr l 1 (vl-string-search "," l)))

     ;; Line trim sub-function
     (defun trim  (v /)
       (vl-string-subst "" (strcat v ",") l))

     ;; Main code
     (vla-startundomark
       (cond (activeDoc)
             ((setq activeDoc
                     (vla-get-activedocument
                       (vlax-get-acad-object))))))
     (read-line f)                                                     ; <- Skip first line (headers)
     (setq layerTable (vla-get-layers activeDoc))

     ;; Linetype check
     [color=blue];; <- Add linetype import code here, to avoid errors[/color]

     (while (/= nil (setq l (read-line f)))
       (progn

         ;; Layer check
         (if (not (tblsearch "layer" (setq layerName (extract l))))
            (setq layerItem (vla-add layerTable layerName))
            (setq layerItem (vla-item layerTable layerName)))

         ;; Layer settings
         (setq l (trim layerName))
         (vla-put-description
           layerItem
           (setq layerDescription (extract l)))
         (setq l (trim layerDescription))
         (if (/= 7 (setq layerColor (extract l)))
           (vla-put-color layerItem layerColor))
         (setq l (trim layerColor))
         (vla-put-linetype layerItem (setq layerLinetype (extract l)))
         (setq l (trim layerLinetype))
         (if (= "BYLAYER" (strcase (setq layerLineweight (extract l))))
           (vla-put-lineweight layerItem aclnwtbylayer))
         (setq l (trim layerLineweight))
         (if (/= "YES" (strcase (setq layerPlottable (extract l))))
           (vla-put-plottable layerItem :vlax-false))
         (setq l (trim layerPlottable))
         (if (/= "NO" (strcase (setq layerFreeze (extract l))))
           (vla-put-freeze layerItem :vlax-true))))
     (close f)
     (vla-endundomark activeDoc)))
 (princ))

 

Please note the changes made in the .CSV file attached (simply change the .TXT file extension to .CSV).

 

RM... love this code, however unless the anticipated linetypes are preloaded in the drawing before running the lisp, the program stops at the line containing the linetype it cannot find... or is it just me?

TIA, Steve

Link to comment
Share on other sites

Hey stevsfr,

 

Lee had me put this at the end of the code and it took care of that issue for me.

 

(defun c:MasterLayerLoad (/)         ;;;Change to you custom command
(LM:LoadLinetype "*")
(RM:CSV->Layers "Put your .csv file location here .csv")    ;;;Change .csv path to your own location
(princ))
;;--------------------=={ Load Linetype }==-------------------;;
;;                                                            ;;
;;  Attempts to load a specified linetype from any linetype   ;;
;;  definition files (.lin) found in the ACAD Support Path    ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  lt - name of linetype to load                             ;;
;;------------------------------------------------------------;;
;;  Returns:  T if linetype loaded successfully, else nil     ;;
;;-------------------=={ String to List }==-------------------;;
;;                                                            ;;
;;  Separates a string into a list of strings using a         ;;
;;  specified delimiter string                                ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010 - www.lee-mac.com             ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - string to process                                   ;;
;;  del - delimiter by which to separate the string           ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of strings                               ;;
;;------------------------------------------------------------;;
;;--------------------=={ Load Linetype }==-------------------;;
(defun LM:LoadLinetype ( lt / acapp acdoc aclts ) (vl-load-com)
 ;; © Lee Mac 2010

 (cond
   ( (tblsearch "LTYPE" lt) )
   ( (progn
       (setq acdoc (vla-get-ActiveDocument (setq acapp (vlax-get-acad-object)))
             aclts (vla-get-Linetypes acdoc))
       (vl-some
         (function
           (lambda ( file )
             (vl-catch-all-apply 'vla-load (list aclts lt file))
             (and (tblsearch "LTYPE" lt))
           )
         )
         (apply 'append
           (mapcar '(lambda ( directory ) (vl-directory-files directory "acad.lin" 1)) ;;;Change name of .lin or put *.lin to load all
             (LM:str->lst
               (vla-get-SupportPath (vla-get-Files (vla-get-Preferences acapp))) ";"
             )
           )
         )
       )
     )
   )
 )  
)
;;-------------------=={ String to List }==-------------------;;
(defun LM:str->lst ( str del / pos )
 ;; © Lee Mac 2010
 (if (setq pos (vl-string-search del str))
   (vl-remove "" (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del)))
   (list str)
 )
)
;;======================================This is the end of code==================================================

Link to comment
Share on other sites

Steve - That is nice of you to say.

 

As the commented line (in blue) states linetypes need to be imported if not already present to avoid errors. This is not presently included in the public code I posted, as there are many different scenarios... Some may include all linetypes within their drawing template, others may have linetypes scattered between one of more .LIN files.

 

I cannot accommodate each variation, so I highlighted that this may present an issue, and leave that task for those that wish to customize. Does this make sense?

Link to comment
Share on other sites

Steve - That is nice of you to say.

 

As the commented line (in blue) states linetypes need to be imported if not already present to avoid errors. This is not presently included in the public code I posted, as there are many different scenarios... Some may include all linetypes within their drawing template, others may have linetypes scattered between one of more .LIN files.

 

I cannot accommodate each variation, so I highlighted that this may present an issue, and leave that task for those that wish to customize. Does this make sense?

 

RM: it makes perfectly good sense now ! I guess my eyes see, but don't always read ! ! thx, Steve

Link to comment
Share on other sites

RM: it makes perfectly good sense now ! I guess my eyes see, but don't always read ! ! thx, Steve

 

No worries; I frequently do the same thing trying teach myself both C#, and VB.NET. :rofl:

 

Hrm... the more I look at the CSV->Layers function, the more I want to rewrite it. Wrote it a while ago, and it could use some streamlining. :geek:

Link to comment
Share on other sites

  • 4 years later...

Hello all. I stumbled on this thread while trying to find an easy way to create a template with the NCS Layers already setup. I'm not terribly experienced in AutoCAD, much less in lisp but I figured I would try the routine that Fix posted (post #31, page 4) and others seem to have gotten working right away. No such luck.

 

Based on my very limited knowlege, I would have thought that after loading the app (using the appload function), I could then type in "loadlayers" at the command prompt and things would get going. Am I off base here?

 

Using AutoCAD MEP 2016, for what it's worth.

 

Thanks in advance.

Link to comment
Share on other sites

If you are referring specifically to the code posted by Fixo (post #31, page 4) [ side note = Have not heard from fixo in a long time :unsure: ]

"D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv" D:/Programming/Lisp/LAYERS

 

The csv file is located at post # 32 page 4

Link to comment
Share on other sites

If you are referring specifically to the code posted by Fixo (post #31, page 4) [ side note = Have not heard from fixo in a long time :unsure: ]

"D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv" D:/Programming/Lisp/LAYERS

 

The csv file is located at post # 32 page 4

 

Thank you for the response. I downloaded the .csv, saved it and updated the path to the .csv in the code. Got the routine successfully loaded as well. From what I've read, I should then be able to call up the routine on the command line (as named in the "defun" line of the code), but when I type in "loadlayers" it just says my command is invalid.

Link to comment
Share on other sites

Thank you for the response. I downloaded the .csv, saved it and updated the path to the .csv in the code. Got the routine successfully loaded as well. From what I've read, I should then be able to call up the routine on the command line (as named in the "defun" line of the code), but when I type in "loadlayers" it just says my command is invalid.

 

From what i can tell from looking at the code at post #31, the program will run upon loading. Meaning the layers will be loaded, no need to run any command. [ that is if the csv file is found ]

 

(loadlayers "D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv") 

 

If you want to call the routine from the command prompt

Change this

(defun loadlayers ( csv / mLayer adoc layers ltype opf csv_lines ly_lst main_layer_list)

 

to this

 

(defun [b][color="blue"]c:[/color][/b]loadlayers ( / mLayer adoc layers ltype opf csv_lines ly_lst main_layer_list)

 

and replace this

 

(setq opf (open csv "r"))

 

with

 

(setq opf (open "D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv" "r"))

 

or wherever that location and name of the csv file.

 

Better yet, use the csv format at post # 33

 

this also didn't work for me till I added the load all line type in #45

 

Might as well. :) >

 

[ @jlewis ] Are you only wanting to load the layers because you need them for the DWT file? [ Template ]. Which means one time only? or are you wanting to learn lisp programming?

 

Perhaps we can write you a BSN code for loadlayers if thats what you wish, just for you , and because we are nice people and very helpful at that....

Edited by pBe
Link to comment
Share on other sites

From what i can tell from looking at the code at post #31, the program will run upon loading. Meaning the layers will be loaded, no need to run any command. [ that is if the csv file is found ]

 

(loadlayers "D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv") 

 

If you want to call the routine from the command prompt

Change this

(defun loadlayers ( csv / mLayer adoc layers ltype opf csv_lines ly_lst main_layer_list)

 

to this

 

(defun [b][color="blue"]c:[/color][/b]loadlayers ( / mLayer adoc layers ltype opf csv_lines ly_lst main_layer_list)

 

and replace this

 

(setq opf (open csv "r"))

 

with

 

(setq opf (open "D:/Programming/Lisp/LAYERS/Layer Creater_NT/ArchExist.csv" "r"))

 

or wherever that location and name of the csv file.

 

Better yet, use the csv format at post # 33

 

 

 

Might as well. :) >

 

[ @jlewis ] Are you only wanting to load the layers because you need them for the DWT file? [ Template ]. Which means one time only? or are you wanting to learn lisp programming?

 

Perhaps we can write you a BSN code for loadlayers if thats what you wish, just for you , and because we are nice people and very helpful at that....

 

You are indeed very nice people. Honestly, I'm still working my way around AutoCAD itself. I'm not a designer or technician by trade, so beyond the lines and circles and Xrefs, I'm useless. Only looking to create a template with the layers preloaded at this point.

 

PS, the routine worked flawlessly. I just didn't realize that it ran immediately upon loading.

 

Thanks for the offer regarding the BSN code, if you were serious! I did turbo pascal 7 about 18 years ago, so the programmer in me is already intrigued, once I see real opportunities to be more efficient.

Link to comment
Share on other sites

....PS, the routine worked flawlessly. I just didn't realize that it ran immediately upon loading.

 

Well good for you :)

 

Thanks for the offer regarding the BSN code, if you were serious! I did turbo pascal 7 about 18 years ago, so the programmer in me is already intrigued, once I see real opportunities to be more efficient.

 

Of course I am. If you want to try your hand with programming again, we can start by writing this specific routine from scratch and you will find that there are may variables to consider for this task.

 

*Check the layers if already existing then ignore if it is, what if the colors / linetypes / lineweights are different? should it be change or remain as it is?

*Load the layers regardless

*Customize linetypes?

*Truecolor for layer color?

*Exisiting layers names are off by one character ? e.g. G-Anno should be G_Anno?

* and so on.....

 

Are you ready to take this on? :)

Link to comment
Share on other sites

Well good for you :)

 

 

 

Of course I am. If you want to try your hand with programming again, we can start by writing this specific routine from scratch and you will find that there are may variables to consider for this task.

 

*Check the layers if already existing then ignore if it is, what if the colors / linetypes / lineweights are different? should it be change or remain as it is?

*Load the layers regardless

*Customize linetypes?

*Truecolor for layer color?

*Exisiting layers names are off by one character ? e.g. G-Anno should be G_Anno?

* and so on.....

 

Are you ready to take this on? :)

 

I do. You got me! :-)

 

So you guys recommend anything for LISP beginners?

Link to comment
Share on other sites

Hey pBe, I would post the final working lisp for all but I'm sorry to say that it was abandon shortly after my last post on it in 2012. So what's here is the final. It works but it's just not cleaned up. But the purpose was to take an Excel or Access Master layer list convert it into a cvs file to either create, change/modify or control layers in either are project or database cad files. A couple of us here were talking a few month ago to see if we should re-look at this routine. So if we do finish it I'll defiantly re-post.

 

 

I totally agree everyone I have had contact with in this forum has been very helpful. Later

Link to comment
Share on other sites

I do. You got me! :-)

 

So you guys recommend anything for LISP beginners?

 

This is where i started AFRALISP

 

Hey pBe, I would post the final working lisp for all but I'm sorry to say...

 

No worries Waders. its okay. :)

Link to comment
Share on other sites

  • 5 months later...

BlackBox, this is great... except all the layers coem in frozen in spite of my "no" tag in the CSV.

 

Any thoughts? Here is the code of my test csv:

 

Layer Name,Layer Description,Layer Color,Layer Linetype,Layer Lineweight,Layer Plottable,Layer Freeze
N-GUIDE,Non-plotting construction lines,30,Continuous,ByLayer,No,NO
N-REF,Non-plotting construction lines,31,Continuous,ByLayer,No,NO
N-VPORT,Viewports,8,Continuous,ByLayer,No,NO

 

Thanks as always!

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