When I write code I always try to use the same naming convention. I use a Reddick style naming.
When I am quick and dirty I use the command pipe, but I much more prefer the activex methods.
I also try to make the routine handle a single instance and multiple instances in the code... like functions for Layer and Layers etc...
I always make a header that explains what the functions do and I alphabetize them.
I put the command line functions first and usually provide a hot key contraction...
I have been coding LISP for a LONG time... (Almost every day since March of 1986)
I wrote this library off the cuff... just to keep my skills up.
I am also a big fan of error management in the code.
It is not bullet proof but I do a try catch like syntax and try not to nest functions.
There is nothing worse for a customer to watch a function crash...
I would rather have it run and not do what it was supposed to do, rather than crash.
Code:
;___________________________________________________________________________________________________________|
;
; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2020 All Rights Reserved
;___________________________________________________________________________________________________________|
;
; Command Line Function Header List
;___________________________________________________________________________________________________________|
;
; Abstract: This program allows the user to specify a list of layers with linetype, color and plot setting.
; It will modify existing layers and/or create new layers with those settings.
; It also loads linetypes from acad.lin if they are not loaded.
;___________________________________________________________________________________________________________|
;* C:MLC
;* Command line function to create layers
;* C:MyLayersCreate
;* Command line function to create layers
;___________________________________________________________________________________________________________|
;
; General Function Header List
;___________________________________________________________________________________________________________|
;* (Errortrap symFunction)
;* Function to trap errors and prevent crashes
;* (LayerCreate strLayerName strLinetype intColor intPlot)
;* Function to create or modify a layer with specified linetype, color and plot setting. Returns layer object
;* (LayersCreate lstLayersInformation)
;* Function to create or modify a list of layers with specified linetype, color and plot setting.
;* (LineTypeLoad lstLineTypes)
;* Function to load a linetype
;* (LineTypesLoad lstLineTypes)
;* Function to load a list of linetypes
;$ Header End
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command Line Functions
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command line function to create layers
;___________________________________________________________________________________________________________|
(defun C:MLC ()(C:MyLayersCreate))
(defun C:MyLayersCreate (/ lstLayersInformation lstLinetypes objActiveDocument)
(if (and (setq lstLayersInformation (list (list "G-ANNO-NPLT" "hidden" 140 0)
(list "G-ANNO-PLOT" "continuous" 141 -1);< -1 is :vlax-true, 0 is :vlax-false
; You can add your more layers here
)
)
(setq objActiveDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq lstLinetypes (mapcar 'cadr lstLayersInformation))
(LineTypesLoad lstLineTypes)
)
(LayersCreate lstLayersInformation)
)
)
;___________________________________________________________________________________________________________|
;
; Functions and Subroutines
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Function to trap errors and prevent crashes
;___________________________________________________________________________________________________________|
(defun ErrorTrap (symFunction / objError result)
(if (not
(vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result)))))
(if result result 'T)
)
)
;___________________________________________________________________________________________________________|
;
; Function to create or modify a list of layers with specified linetype, color and plot setting.
;___________________________________________________________________________________________________________|
(defun LayersCreate (lstLayersInformation)
(mapcar '(lambda (X)(apply 'LayerCreate X)) lstLayersInformation)
)
;___________________________________________________________________________________________________________|
;
; Function to create or modify a layer with specified linetype, color and plot setting. Returns layer object
;___________________________________________________________________________________________________________|
(defun LayerCreate (strLayerName strLinetype intColor intPlot / colLayers entLayer objLayer )
(if (and (setq colLayers (vla-get-layers objActiveDocument))
(and (setq colLayers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
(or (errortrap '(setq objLayer (vla-item colLayers strLayerName)))
(errortrap '(setq objLayer (vla-add colLayers strLayerName)))
)
)
(errortrap '(vlax-put objLayer "color" intColor ))
(errortrap '(vlax-put objLayer "plottable" intPlot))
(errortrap '(vlax-put objLayer "linetype" strLineType))
)
objLayer
)
)
;___________________________________________________________________________________________________________|
;
; Function to load a linetype
;___________________________________________________________________________________________________________|
(defun LinetypeLoad (strLineType / colLinetypes)
(if (and (setq colLinetypes (vla-get-linetypes objActiveDocument))
(or (errortrap '(vla-load colLinetypes strLinetype (findfile "acad.lin")))
T
)
)
(errortrap '(vla-item colLinetypes strLinetype))
)
)
;___________________________________________________________________________________________________________|
;
; Function to load a list of linetypes
;___________________________________________________________________________________________________________|
(defun LineTypesLoad (lstLineTypes)
(mapcar 'LinetypeLoad lstLineTypes)
)
(vl-load-com)