(defun C:UtilitiesCOH()
(setvar "cmdecho" 0)
(setvar "expert" 3)
(setq planVar 0)
(while (= planVar 0)
(setq plan (substr (strcase (getstring "Include Plan Numbers? ((Y)es/(N)o):")) 1 1))
(cond
((= plan "Y") (setq planVar 1))
((= plan "N") (setq planVar 2))
(T (prompt "Invalid Entry\n"))
)
)
(setq tmpVar planVar)
;;Provide used layers
(if (null (tblsearch "layer" "GasUtilitiesCombo"))
(command "-layer" "make" "GasUtilitiesCombo" "color" "yellow" "" ""))
(if (null (tblsearch "layer" "GasUtilitiesStorm"))
(command "-layer" "make" "GasUtilitiesStorm" "color" "64" "" ""))
(if (null (tblsearch "layer" "GasUtilitiesSanitary"))
(command "-layer" "make" "GasUtilitiesSanitary" "color" "26" "" ""))
(if (null (tblsearch "layer" "GasUtilitiesWater"))
(command "-layer" "make" "GasUtilitiesWater" "color" "171" "" ""))
(setq sel (ssget '((0 . "LINE,*POLYLINE"))))
(repeat (setq i (sslength sel))
(setq ent (ssname sel (setq i (1- i))))
(setq startpoint (vlax-curve-getstartpoint ent))
(setq endpoint (vlax-curve-getendpoint ent))
(if (null (ade_odgetfield ent "SewerMain" "SYSTEMTYPE" 0))
(progn
(if (null (ade_odgetfield ent "TBL_WATER_MAIN" "DIAMETER" 0))
(setq od "TBL_SERVICE")
(setq od "TBL_WATER_MAIN")
)
(setq sys "Water")
(setq dia (ade_odgetfield ent od "DIAMETER" 0))
(setq planNum (strcat "(" (ade_odgetfield ent od "PLANNUMBER" 0) ")"))
)
(progn
(setq sys (ade_odgetfield ent "SewerMain" "SYSTEMTYPE" 0))
(setq dia (ade_odgetfield ent "SewerMain" "DIAMETER" 0))
(setq planNum (strcat "(" (ade_odgetfield ent "SewerMain" "MAPREF" 0) ")"))
)
)
(if (= planNum "()") (setq planNum ""))
;;get variables to usable string
(varConv)
;;set LT Definition
(setq LTdef (strcat "____" LTdia "'' " LTsys "____" LTdia "'' " LTsys "___\n"))
(if (null (tblsearch "ltype" lt))(createLT))
(changeProps)
(if (= planVar 1)
(progn
(tmaskon)
(if (/= tmpVar planVar)
(setq planVar tmpVar)
)
)
)
(if (= i 0) (setvar "expert" 0))
)
(setq text (ssget "_X" '((0 . "MTEXT"))))
(command "select" text "")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END MAIN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN CALLOUTS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;VARIABLE CONVERSION;;;;;;;;
(defun varConv()
(setq tester (/ dia 10))
(cond
((= sys "STORM") (setq LTsys "STM"))
((= sys "SANITARY") (setq LTsys "SAN"))
((= sys "COMBINED") (setq LTsys "CMB"))
((= sys "Water") (setq LTsys "W"))
(T (prompt "unknown system type"))
)
(cond
((< tester 1) (setq LTdia (substr (rtos dia) 1 1)))
((>= tester 10) (setq LTdia (substr (rtos dia) 1 3)))
(T (setq LTdia (substr (rtos dia) 1 2)))
)
(cond
((= LTdia "0") (setq lt LTsys))
((= sys "Water") (setq lt (strcat sys LTdia "''")))
(T (setq lt (strcat LTsys LTdia "''")))
)
)
;;;;;;;LINETYPE;;;;;;;;
(defun createLT ()
(setq diaLength (strlen LTdia))
(setq sysLength (strlen LTsys))
(setq total (+ dialength syslength))
(setq bspace (atof ".1"))
(setq x (float (* (atof ".008") (+ total 3))))
(setq espace (float (* (atof ".05") (+ total 3))))
(setq LTdef (strcat "____" LTdia "'' " LTsys "____" LTdia "'' " LTsys "___\n"))
(setq LTspacing (strcat "A,4,-"(rtos bspace)",[""\"" LTdia "'' " LTsys "\",LT_FONT,X=-"(rtos x)",y=-.03,S=.08],-"(rtos espace)"\n"))
;;Filepath/name
(setq fname "C:\\Users\\Public\\TeamFishel\\Autodesk\\Google Drive\\R19.1\\Linetypes\\Custom.lin")
;;Appends written text to Custom.lin file
(setq linFile (open fname "a"))
(write-line (strcat "*" lt ", " LTdef LTspacing) linFile)
(close linFile)
(command "-linetype" "load" lt "C:\\Users\\Public\\TeamFishel\\Autodesk\\Google Drive\\R19.1\\Linetypes\\Custom" "")
)
;;;;;;;CHANGE PROPERTIES;;;;;;;;
(defun changeProps()
(command "chprop" ent "")
(cond
((= LTsys "CMB")(command "layer" "GasUtilitiesCombo" "color" "bylayer"))
((= LTsys "SAN")(command "layer" "GasUtilitiesSanitary" "color" "bylayer"))
((= LTsys "STM")(command "layer" "GasUtilitiesStorm" "color" "bylayer"))
(T (command "layer" "GasUtilitiesWater" "color" "bylayer"))
)
(cond
((>= dia 24)
(progn
(command "")
(command "laymcur" ent)
(pipeOffset)
)
)
((> dia 0)
(progn
(command "ltype" lt "" )
(command "laymcur" ent)
(if (= planVar 1)
(command "mtext" "m2p" startpoint endpoint "j" "ml" "s" "ROMANS" "h" "1.3" "l" "" "2.1667"
"r" endpoint "w" "0" planNum "")
)
)
)
(T
(progn
(command "ltype" LTsys "" )
(command "laymcur" ent)
(if (= planVar 1)
(command "mtext" "m2p" startpoint endpoint "j" "ml" "s" "ROMANS" "h" "1.3" "l" "" "2.1667"
"r" endpoint "w" "0" planNum "")
)
)
)
)
)
;;;;;;;LINE OFFSET;;;;;;;;
(defun pipeOffset()
(setq ofd (float (/ (/ dia 12.00) 2.00)))
(if (setq ss (ssget "p"))
(repeat (sslength ss)
(setq ent (vlax-ename->vla-object (ssname ss 0)))
(if (vlax-method-applicable-p ent 'Offset)
(mapcar '(lambda (o)
(vl-catch-all-error-p
(vl-catch-all-apply
'vla-offset (list ent o))))(list ofd (- ofd)))
)
(ssdel (ssname ss 0) ss)
)
)
(princ)
(command "chprop" "p" "" "ltype" lt "layer" "Defpoints" "")
(if (= planVar 1)
(command "mtext" "m2p" startpoint endpoint "j" "ml" "s" "ROMANS" "h" "1.3" "l" "" "2.1667"
"r" endpoint "w" "0" (strcat LTdia "'' " LTsys " " planNum) "")
(command "mtext" "m2p" startpoint endpoint "j" "ml" "s" "ROMANS" "h" "1.3" "l" "" "2.1667"
"r" endpoint "w" "0" (strcat LTdia "'' " LTsys) "")
)
(setq planVar 1)
)
(defun TMaskOn ()
(vl-load-com)
(setq s (ssget "_L" '((0 . "MTEXT"))))
(if s
(mapcar '(lambda (x)
(setq ob1 (vlax-ename->vla-object x))
(if (= (vla-get-backgroundfill ob1) :vlax-false)
(vla-put-backgroundfill ob1 :vlax-true)
)
)
(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
)
)
(prin1)
)
ade_odgetfield was basically the start of what made me write this routine. We use the command MapImport to bring in shapefiles and attach ObjectData 'od' to the lines/block/points/etc and we store every bit of info into this. I found out about searching into these object data tables from the following site.Code: [Select];; RJP - What does 'ade_odgetfield' do?
(if (null (ade_odgetfield ent "SewerMain" "SYSTEMTYPE" 0))
Code: [Select](if (= planvar "Yes")
(progn (tmaskon)
;; RJP - This will always be true...
(if (/= tmpvar planvar)
(setq planvar tmpvar)
)
)
)
- Localise variable names
- Reduce hard-coded data like path names - use searching external data stores, or provide as an argument
- Error handling
- Always provide the leading zero ie. 0.008, not .008
- Don't convert values unnecessarily ie. 0.008, not (atof "0.008")
- Have functions consume arguments and return something meaningful
Thank you for your gracious response Ronjon.Lee has an excellent write up on locali(z)(s)ing variables HERE. (http://lee-mac.com/localising.html) :)ade_odgetfield was basically the start of what made me write this routine. We use the command MapImport to bring in shapefiles and attach ObjectData 'od' to the lines/block/points/etc and we store every bit of info into this. I found out about searching into these object data tables from the following site.Code: [Select];; RJP - What does 'ade_odgetfield' do?
(if (null (ade_odgetfield ent "SewerMain" "SYSTEMTYPE" 0))
https://map3d.wordpress.com/2010/06/17/greek-or-nerd-using-links-in-od-tables/ (https://map3d.wordpress.com/2010/06/17/greek-or-nerd-using-links-in-od-tables/)
I definitely have some questions to come about some of your other comments.
But for now what I remember is to ask what the importance of localizing the variables is?
As in, whats the difference from not calling them and calling them in the opening (defun cmdName(Variables) ?
Also,Code: [Select](if (= planvar "Yes")
(progn (tmaskon)
;; RJP - This will always be true...
(if (/= tmpvar planvar)
(setq planvar tmpvar)
)
)
)
I change the planVar in the pipeOffset section so that it will run into the text mask function even if i dont include the plan numbers because we have to include mtext with the diameter and type of pipe when we offset it instead of having a linetype with that information. I didnt want to end up calling the text mask function twice by throwing it into the pipeoffset section if that makes sense.
(initget 0 "Yes No")
(setq planvar (cond ((getkword (strcat "\nInclude Plan Numbers [Yes/No] <Yes>: ")))
("Yes")
)
)
(setvar "cmdecho" 0)
(setvar "expert" 3)
From a user point of view, I hate it when lisp programs change my system variables.Quote(setvar "cmdecho" 0)
(setvar "expert" 3)
You may want to consider combining all LAyer calls vs calling (command "_.LAYER") multiple times :Code - Auto/Visual Lisp: [Select]
;;Provide used layers
Editing / maintaining a master list works better for me
-David
This is my new conversions of the diameter of pipe from a real number to a precise, and usable string for generating linetypes... I'm still not happy with it but it works
@ Lee:
Why the use of vl-catch-all-apply? Are there numbers where rtos will error out?
: (LM:num->str 1.00000001)
"1"
: (LM:num->str 1.500000000000001)
"1.500000000000001"
... Some would argue that a function *should* throw an error when supplied with an invalid argument.
FWIW: I think this is strange:Code: [Select]: (LM:num->str 1.00000001)
"1"
: (LM:num->str 1.500000000000001)
"1.500000000000001"