Wire number tags based upon connected component tag-ID at each end.
Some time ago we had this posting that dealt with a wire numbering tool utility. It would generate a wire number tag based upon combining the tag-ID and termination pin number of the component connection at each end of the wire. This generated explicit but really “long” wire numbers.
Here’s an example:
Yes, really long wire numbers ( ! )
Request for a Simplified Version
A user in Australia wanted to do this but make the tags shorter. He just wanted to concatenate the two tag-IDs and then add an alpha suffix to keep each wire number unique. No connection pin numbers embedded in the wire number.
For example, in the above drawing, the upper right-hand wire number would be the two device tags jammed together, “SS406TD406”, and an alpha sufffix, let’s say “A”.
Now there are two more wires that tie the same two devices together: the right-hand wire leaving the two child contacts on the selector switch. Both will be “SS406TD406”. So the utility would need to add a “B” and a “C” to each respectively to keep the wire numbers unique.
Modifying the existing milnum_alldwg.lsp utility
So, let’s start with the previous post’s lsp utility, copy it to a new file name, “milnumx_alldwg.lsp”. Open it up in AutoCAD’s Visual Lisp editor (type vlide [enter] at the “Command:” prompt) or open in any ASCII text editor (like WordPad.exe) and let’s have some fun.
Step 1 – this is the easiest part. We want to bypass the original utility’s task of adding in the connection pin numbers. Here is the before and after code:
(if (= flipit 1) (progn ; reverse the order (setq new_wireno comp2_tag) (if (AND pin2 (/= pin2 ""))(setq new_wireno (strcat new_wireno "-" pin2))) (setq new_wireno (strcat new_wireno "/" comp1_tag)) (if (AND pin1 (/= pin1 ""))(setq new_wireno (strcat new_wireno "-" pin1))) ) ; ELSE (progn ; normal order of wire number format (setq new_wireno comp1_tag) (if (AND pin1 (/= pin1 ""))(setq new_wireno (strcat new_wireno "-" pin1))) (setq new_wireno (strcat new_wireno "/" comp2_tag)) (if (AND pin2 (/= pin2 ""))(setq new_wireno (strcat new_wireno "-" pin2))) ) ) (c:wd_putwnf en new_wireno) ; put the wire number on wire AFTER: (if (= flipit 1) (progn ; reverse the order (setq new_wireno (strcat comp2_tag comp1_tag)) ; just jam two tags together, no delimiter ) ; ELSE (progn ; normal order of wire number format (setq new_wireno (strcat comp2_tag comp1_tag)) ; just jam two tags together, no delimiter ) ) (c:wd_putwnf en new_wireno) ; put the wire number on wire
Step 2 – the harder part. Okay, we have the base wire number figured out above and pushed into lisp variable “new_wireno”. But we need to check each wire number and make sure it is unique. We add an incrementing alpha suffix to the wire number until it is unique.
This means that we need to first extract a list of all existing wire numbers on the active drawing (could do for all dwgs in project but that will be an upcoming blog posting). Then, when we generate a new wire number, we compare against the existing wire number list and, if a repeat found, keep incrementing the suffix for our new wire number until we find one that isn’t already in the list.
So, here’s a little subroutine we’ll add to our milnumx_alldwg.lsp utility. It runs at the front end of our utility to establish a list of all existing wire number assignments.
Here’s how it works… it creates a “selection set” of all wire number block inserts found on the active drawing. AutoCAD Electrical wire numbers have block names that begin with either “WD_WNH” or WD_WNV”. So the “ssget” line below gets all instances of wire number block inserts on the active drawing.
Then we cycle through this list, one at a time, and get each wire number block insert instance, the “(setq en (ssname sswn ixwn))” line below. Then we get the wire number attribute value, WIRENO or WIRENOF. Add to the running list of wire numbers, loop back to the top of the loop, and process the next wire number block insert instance. When done, all of the drawing’s wire numbers are sitting in AutoLISP variable “lst”. This data is returned from this function.
(defun milnumx_get_all_existing_wirenums ( / sswn ixwn slenwn lst ) ; Extract a list of all wire numbers found on the active drawing. Create a selection ; set of all block insert instances that have block name "WD_WNH*" or "WD_WNV*". There ; are AutoCAD Electrical wire number block names. (setq lst nil) (setq sswn (ssget "_X" '((-4 . "<AND")(0 . "INSERT")(2 . "WD_WNH*,WD_WNV*")(-4 . "AND>")))) (if (/= sswn nil) (progn ; some wire number block instances found. Process them. (setq slenwn (sslength sswn)) ; number of block instances found (setq ixwn 0) ; this will be used to index through the blocks (while (< ixwn slenwn) (setq en (ssname sswn ixwn)) ; get next block instance entity name ; Get WIRENO* attribute value off of this wire number block insert (setq wn (c:wd_getattrval en "WIRENO*")) (if (AND wn (/= wn "") (not (member wn lst))) (progn ; add to the running list (setq lst (cons wn lst)) ) ) (setq ixwn (1+ ixwn)) ) ) ) (setq sswn nil) ; release the selection set lst ; return the list of existing wire numbers found on active drawing )
Step 3 – Putting it all together. Now we add a call to our wire number collection function to capture a list of all wire numbers and dump into AutoLISP variable name “existing_wnum_list”. We do this toward the beginning of the utility as shown here:
; -- main routine starts here -- ; Two options here. Uncomment first two lines and uncomment third line to prompt ; user to "window" what wires to process. Or, comment out the first two lines and ; uncomment the third line to process all wires on active drawing without any ; user prompts. ;;; (princ "\nSelect wire or wires to process for special source/dest wire number format") ;;; (setq ss (ssget '((0 . "LINE")))) ; user windows what to process (setq ss (ssget "_X" '((0 . "LINE")))) ; automatically process all wires on active dwg (if (/= ss nil) (progn ; Get list of all existing wire numbers (setq existing_wnum_list (milnumx_get_all_existing_wirenums)) ; ** 12-Nov-09 NEHolt (setq slen (sslength ss)) ; number of LINE entities selected (setq lines_processed_entlist nil) ; used to track what has been processed (setq ix 0) ; used to index through the selection set (while (< ix slen)
… and we go back to what we did in Step 1. We add a “foreach” loop just below the point where we have generated the raw “component1-component2” tag. We want to find the next suffix character that keeps this proposed wire number unique.
(if (= flipit 1) (progn ; reverse the order (setq new_wireno (strcat comp2_tag comp1_tag)) ; just jam two tags together, no delimiter ) ; ELSE (progn ; normal order of wire number format (setq new_wireno (strcat comp1_tag comp2_tag)) ; just jam two tags together, no delimiter ) ) ; Now add an incrementing alpha suffix to the wire number until ; a unique wire number is found (one that does not appear to have ; already been used elsewhere on this drawing). ; "existing_wnum_list" is the list of existing wire numbers ; collected for the active drawing. (setq alphalst (list "A" "B" "C" "D" "E" "F" "G" "H" "J" "K" "L" "M" "N" "P" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "A1" "A2")) (setq hit nil) (foreach suffix alphalst (if (not hit) (progn ; don't have a unique number yet. Try this one. (setq x (strcat new_wireno suffix)) (if (not (member x existing_wnum_list)) (progn ; Yes, found next available suffix that yields a ; unique wire number! ; Add this new wire number to the running list (setq existing_wnum_list (cons x existing_wnum_list)) (setq hit T) ; to bypass the rest of this "foreach" loop (setq new_wireno x) ) ) ) ) ) (if (not hit) (progn ; didn't find a unique wire number suffix. Add "???" (setq new_wireno (strcat new_wireno "???")) ) ) (c:wd_putwnf en new_wireno) ; put the wire number on wire
There it is. Let’s test. Save the above file. APPLOAD it. Then type MILNUM_ALLDWG [Enter] at the Command: prompt.
Download full file here
UPDATE: found typo in the uploaded file (13-Nov-2009). Fixed and reposted.