Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rewrite function using lambda

5 REPLIES 5
Reply
Message 1 of 6
bhull1985
467 Views, 5 Replies

Rewrite function using lambda

Hey everyone- i've had to add a massive list of data hardcoded into this cad to excel program.

There are static information that needs to be placed into excel based off of what attributes the program finds associated with the blocks this version of the program is set to run on.

 

(if
(setq cell (strcat "L" (itoa (vl-bb-ref 'excelrow))))
(acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist))
);if
(setq c 0)
(setq len (length vallist))
(setq cell (string_ cell))
(repeat len
(setq data (nth c vallist))
(setq typedata (cadr data))
(putcell cell typedata)

(if (= typedata "U2TR-H2")
(setq desccell (strcat "J" (itoa (vl-bb-ref 'excelrow))))
(setq descdata (car u2tr-h2))
(setq desccell descdata)

(setq tempcell (strcat "F" (itoa (vl-bb-ref 'excelrow))))
(setq tempdata (cadr u2tr-h2))
(setq tempcell tempdata)

(setq presscell (strcat "E" (itoa (vl-bb-ref 'excelrow))))
(setq pressdata (caddr u2tr-h2))
(setq presscell pressdata)

(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr u2tr-h2))
(putcell mrfcell mrfdata)

(setq cell (string_ cell))
(acet-ui-progress-safe c)
(setq c (1+ c))
);repeat
(acet-ui-progress-done)
(if (= c len)(dotag vallist));;goto do-tag sub
);defun doname

 My longer version of the function, meant to take one attribute "Type/typedata" and look up the associated data from that variable.

I've entered all of the data in this structure..

 

(SETQ L6TN-T1-2 (LIST "Valve, Gate, NPT, 800#, A352-LCB/LCC or A350-LF2 Body, SS Wedge, HF-SS Seat, API Trim 10, 12 or 16, HW Op., Reduced Port, Bolted Bonnet-OS&Y, Graphite 
Packing, Solid Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "250" "550" "KF N12-125HT w/367"))

(SETQ L6TN-T1-3 (LIST "Valve, Needle, MxFNPT, 10,000#, with Bleed Plug, CS Body/Bonnet, SS/TFE or Graphite Packed Stem, -20 to 400 F, T-Handle, Screwed Bonnet, Metal/Metal Seat" "150" "270" "KF N12-125HT w/367"))

(SETQ L6TN-U1 (LIST "Valve, Needle, MxFNPT, with Bleed Valve & 1/2\" FNPT Calibration Port, CS Body/Bonnet, SS/TFE or Graphite Packed Stem, 10,000#, -20 to 400 F, T- Handle,
Screwed Bonnet, Metal/Metal Seat" "150" "200" "KF 11-125HT W/367"))

(setq U2TF-H2 (list "Valve, Floating Ball, NPT, 2000#, A-216Gr.WCB Body, SS Stem/Ball, PTFE Seat, -20 to 400 F, Lever Op., Full Port, Hydrocarbon Trim, Anti-blowout Stem, 
API-607, ASME B16.34, 500ppm VOC, Bubble Tight Seal" "175" "1100" "KF 1477-21911"))

(SETQ U2TF-H2-2 (LIST "Valve, Trunnion Ball, 900# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -20 to 400 F, Gear Op., Red. Port,
Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "200" "675" "KF 1477-21911"))

(SETQ U2TF-H2-3 (LIST "Valve, Piston Check, SW, 800#, A-105 Body, Alloy Piston/Disc, SS Spring, API Trim 5 or 8, -20 to 800 F, Bolted Bonnet, Std. Port, Renew. Seats, API- 602
& 598, ASME B16.10 & B16.34" "250" "550" "KF 1477-21911"))

(SETQ U2TF-S2 (LIST "Valve, Floating Ball, NPT, A-182 F316/A-351Gr.CF8M Body, SS Stem/Ball, PTFE Seat, Chemical Compatible Trim, 2000#, -20 to 400 F, Lever Op., Red. Port,
RTFE Packing, Anti-blowout Stem, ASME B16.34, 500ppm VOC, Bubble Tight Seal" "150" "1100" "Contromatics 8000-M3"))

(setq U2TR-H1 (list "Valve, Floating Ball, NPT, 2000#, A-216Gr.WCB Body, SS Stem/Ball, PTFE Seats, -2 to 400 F, Lever Op., Red. Port, Hydrocarbon Trim, Anti-Blowout Stem, 
API-607, ASME B16.34, 500ppm, VOC, Bubble Tight Seal" "175" "1100" "FORTUNE 415TF007R20N1AN"))

(setq U2TR-H2 (list "Valve, Floating Ball, NPT, 2000#, A-216Gr.WCB Body, SS Stem/Ball, PTFE Seats, -20 to 400 F, Lever Op., Red. Port, Hydrocarbon Trim, Anti-Blowout Stem, 
API-607, ASME 516.34, 500ppm VOC, Bubble Tight Seal" "175" "1100" "FORTUNE 415TF005R20N1AN"))

(SETQ U2TR-H2-2 (LIST "Valve, Gate, NPT, 800#, A-182 F316/A-351Gr.CF8M Body, SS
 Stem/Wedge/Seat/Bonnet, API Trim 10, 12 or 16, HW Op., Red. Port, Sealed SS Bolted Extended Bonnet, Graphite/TFE Packing, Flex Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "150" "550" "Fortune 415TF007R20N1AN"))

(SETQ U2TR-H2-3 (LIST "Valve, Trunnion Ball, 300# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -20 to 400 F, Gear Op., Red. Port, 
Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "150" "550" "Fortune 415TF007R20N1AN"))

(SETQ U2TR-H2-4 (LIST "Valve, Needle, MxFNPT, 10,000#, with Bleed Valve & 1/2\" FNPT Calibration Port, CS Body/Bonnet, SS/TFE or Graphite Packed Stem, -20 to 400 F, T- Handle,
Screwed Bonnet, Metal/Metal Seat" "150" "550" "Fortune 415TF005R20N1AN"))

(SETQ U2TR-H2-5 (LIST "Valve, Swing Check, NPT, 800#, A-105 Body, Alloy Seat/Disc, API Trim 5 or 8, -20 to 800 F, Bolted Bonnet, Std. Port, Renew. Seats, API-602 & 598, ASME 
B16.10 & B16.34" "250" "550" "Fortune 415TF007R20N1AN"))

(SETQ U2TR-H2-6 (LIST "Valve, Globe, SW, 800#, A-105 Body, CS/13Cr Disc, HF-CS Seat, MS Stem, API Trim 5 or 8, -20 to 800 F, HW Op., Bolted Bonnet-OS&Y, Graphite Packing, 
Renew. Seats, API-602 & 598, ASME B16.34, 500ppm VOC" "250" "550" "Fortune 415TF007R20N1AN"))

(SETQ U2TR-S1 (LIST "Valve, Floating Ball, 300# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Engineer Specified Trim, -20 to 400 F, Lever Op. \(Gear Op>8\" 300#\), Reduced Port, 
Anti-blowout Stem, ASME B16.34 & B16.10, Bubble Tight Seal" "150" "550" "Fortune 415TF007R20N1AN"))

(SETQ U2TR-S2 (LIST "Valve, Piston Check, NPT, A-182 F316/A-351Gr.CF8M Body, SS Piston/Disc, SS Spring, API Trim 10, 12 or 16, 800#, -20 to 800 F, Bolted Bonnet, Std. Port, Renew. Seats, API-602 & 598, ASME B16.34" "150" "1100" "KF U5002-D21"))

(SETQ U2TR-S2-2 (LIST "Valve, Floating Ball, NPT, A-182 F316/A-351Gr.CF8M Body, SS Stem/Ball, PTFE Seats, Chemical Compatible Trim, 2000#, -20 to 400 F, Lever Op., Full Port, RTFE Packing, Anti-blowout Stem, ASME B16.34, 500ppm VOC, Bubble Tight Seal" "150" "1100" "KF U5002-D21"))

(setq U2TR-T1 (list "Valve, Gate, NPT, 800#, A352-LCB/LCC or A350-LF2 Body, SS Wedge, HF-
SS Seat, API Trim 10, 12 or 16, HW Op., Reduced Port, Bolted Bonnet- OS&Y, Graphite Packing, Solid Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "150" "1100" "KF U5002-D21"))

(SETQ U2TR-T1-2 (LIST "Valve, Globe, SW, 800#, A352-LCB/LCC or A350-LF2 Body, SS Disc, HF-SS Seat, MS Stem, API Trim 10, 12 or 16, 800#, -50 to 800 F, HW Op.,
Bolted Bonnet-OS&Y, Graphite Packing, API-602 & 598, ASME B16.34 & B16.10, 500ppm VOC" "150" "1100" "KF U5002-D21"))

(SETQ U2TR-T1-3 (LIST "Valve, Needle, MxFNPT, 10,000#, with SS Bleed Plug, SS Body/Bonnet/Stem, TFE Packed Stem, -50 to 200 F, T-Handle, Screwed Bonnet, Metal/Metal Seat" "150" "1100" "KF U5002-D21"))

(SETQ U2TR-T1-4 (LIST "Valve, Floating Ball, 150# RF, A352-LCB/LCC or A350-LF2, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -50 to 400 F, Lever Op. \(Gear 
Op>8\" 300#\), Red. Port, Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "150" "1100" "KF U5002-D21"))

(SETQ U2TR-T1-5 (LIST "Valve, Trunnion Ball, 150# RF, A352-LCB/LCC or A350-LF2, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -50 to 400 F, Gear Op., Full Port,
Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "150" "270" "KF U5003-D21"))

(setq U2TR-U1 (list "Valve, Floating Ball, NPT, CS 1 Piece Body, Mfr. Std. Stem/Ball/Seat/O- Rings/Packing, 2000#, -20 to 400 F, Lever Op., Red. Port, Anti-blowout Stem, ASME B16.34, Bubble Tight Seal" "200" "200" "KF U5002-Q11"))

 Which is only a small excerpt from the entire data structure, which is much larger. So as you may be able to see, what I'm trying to do is once the "Typedata" is found, to match it up with the list of (setq) containing all of the data. My method uses (if (equal)) but I know it'll be snail-slow compared to a mapcar statement. I'm a bit too out of practice (for now!) to be able to determine how to effectively use mapcar to accomplish this task, and as such was hoping someone may step in and show me how to.

 

Thanks in advance---again

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
5 REPLIES 5
Message 2 of 6
bhull1985
in reply to: bhull1985

For completeness, let me give some more information, and show how this is screwing up for me...

Command: (setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
"A6"

Command: (setq mfrdata (cadddr X1TR-H2))
"DSI 4121A8-18"

Command: (PUTCELL mrfcell mrfdata)
; error: bad argument type: stringp nil

;;not sure why routine did not put "DSI 4121A8-18" because...

Command: !MFRDATA
"DSI 4121A8-18"

Command: !MFRCELL
"A6"

Even though these are showing as strings to me the routine gave a stringp nil error. So, I tried it manually, and it worked. The correct string was placed into the correct excel field, using:

Command: (PUTCELL "A6" "DSI 4121A8-18")

This one worked! Why, why why :/

 

 

here's the subroutine again, with 2 of the data entries coded up to send to excel. They are not sending, as shown just above. Here is the code for them as it is currently, the code i'd like to modify..

(defun doTYPE ( vallist / c cell namedata)
(if
(setq cell (strcat "K" (itoa (vl-bb-ref 'excelrow))))
(acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist))
);if
(setq c 0)
(setq len (length vallist))
(setq cell (string_ cell))
(repeat len
(setq data (nth c vallist))
(setq typedata (cadr data))
(putcell cell typedata)

(if (= typedata "U2TR-H2")
(progn
(setq desccell (strcat "I" (itoa (vl-bb-ref 'excelrow))))
(setq descdata (car u2tr-h2))
(PUTCELL desccell descdata)

(setq tempcell (strcat "F" (itoa (vl-bb-ref 'excelrow))))
(setq tempdata (cadr u2tr-h2))
(PUTCELL tempcell tempdata)

(setq presscell (strcat "E" (itoa (vl-bb-ref 'excelrow))))
(setq pressdata (caddr u2tr-h2))
(PUTCELL presscell pressdata)

(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr u2tr-h2))
(putcell mrfcell mrfdata)

);PROGN
);IF U2TR-H2

(setq X1TR-H2 (list "Valve, Gate, NPT, 800#, A-105 Body, CS/13Cr Wedge, HF-CS Seat, MS Stem, API Trim 5 or 8, -20 to 800 F, HW Op., Red. Port, Bltd. Bonnet-OS&Y, Graphite Packing, Solid Wedge, Renew. Seats, API-602 & 598, ASME B16.34, 

500ppm VOC" "595" "1100" "DSI 4121A8-18"))

(if (= typedata "X1TR-H2")
(progn
(setq desccell (strcat "I" (itoa (vl-bb-ref 'excelrow))))
(setq descdata (car X1TR-H2))
(PUTCELL desccell descdata)

(setq tempcell (strcat "F" (itoa (vl-bb-ref 'excelrow))))
(setq tempdata (cadr X1TR-H2))
(PUTCELL tempcell tempdata)

(setq presscell (strcat "E" (itoa (vl-bb-ref 'excelrow))))
(setq pressdata (caddr X1TR-H2))
(PUTCELL presscell pressdata)

(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr X1TR-H2))
(PUTCELL mrfcell mrfdata)

);PROGN
);IF X1TR-H2

(setq cell (string_ cell))
(acet-ui-progress-safe c)
(setq c (1+ c))
);repeat
(acet-ui-progress-done)
(if (= c len)(dotag vallist));;goto do-tag sub
);defun doname

 

Thanks in advance!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 3 of 6
dbroad
in reply to: bhull1985

If you focus on a smaller code fragment, you might attract more help.  These are a few things I see:

1. For a single document application, is there really a need to use a blackboard reference?

2. See code comments below.

3. Lambda is only slightly better than defining a function and using it. Function is another optimized option inside a mapcar.

4. Nice use of progress bar.

5. To get values from a list to individual vars, try (mapcar 'set '(a b c d e ..) (list val1 val2 val3 val4 val5))  This could avoid cars and nths if you need to work with the individual parts of lists.

 

(IF (SETQ cell (STRCAT "L" (ITOA (VL-BB-REF 'excelrow))));<-will crash if no value excelrow so need for "if" is debateable.
  (acet-ui-progress-init "Writing valve TYPES to Excel."
			 (LENGTH vallist)
			 )
  )					;if
(SETQ c 0)
(SETQ len (LENGTH vallist))
(SETQ cell (string_ cell))
(REPEAT	len  ;<- probably should be a (foreach data vallist...
  (SETQ data (NTH c vallist)) ;<-eliminates need for this
  (SETQ typedata (CADR data)) 
  (putcell cell typedata)
  (IF (= typedata "U2TR-H2") ;<-Need a progn here to wrap group
    (SETQ desccell (STRCAT "J" (ITOA (VL-BB-REF 'excelrow))));<value gets overridden
    (SETQ descdata (CAR u2tr-h2))
    (SETQ desccell descdata) ;<-overwrites needlessly?
    (SETQ tempcell (STRCAT "F" (ITOA (VL-BB-REF 'excelrow))));<you use (itoa....) repeatedly.  Store the value and use it.
    (SETQ tempdata (CADR u2tr-h2))
    (SETQ tempcell tempdata) ;<-overwrites needlessly
    (SETQ presscell (STRCAT "E" (ITOA (VL-BB-REF 'excelrow))));overwritten line after next without first being used.
    (SETQ pressdata (CADDR u2tr-h2))
    (SETQ presscell pressdata);<-overwrites previous values
    (SETQ mfrcell (STRCAT "A" (ITOA (VL-BB-REF 'excelrow))))
    (SETQ mfrdata (CADDDR u2tr-h2))
    (putcell mrfcell mrfdata)
    (SETQ cell (string_ cell))
    (acet-ui-progress-safe c)
    (SETQ c (1+ c))
    )					;repeat
  (acet-ui-progress-done)
  (IF (= c len)
    (dotag vallist)
    )
  ;;goto do-tag sub
  )					;defun doname

 

Architect, Registered NC, VA, SC, & GA.
Message 4 of 6
bhull1985
in reply to: dbroad

GD I am rusty, need to get back into it.

The (progn) wraps will surely help, hah!

 

Thanks Dbroad

 

And to answer/reply to your comments...

 

1. For a single document application, is there really a need to use a blackboard reference?

 

This is definetly a multiple document application, here's proof in how the dcl looks- to show the multidoc applications of the program

cadtoexcel.bmp

 

I was just pulled to complete something else, get back to this asap.

Thanks in advance

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 5 of 6
martti.halminen
in reply to: bhull1985

As to the question why, you might contemplate the difference between MRF and MFR.

 

Regarding your larger part of the program, I'd start by separating the static data to its own file, so you can edit it without fearing mixing up the rest of the program.

- Might build a little sanity checker to run when reading the data, to find missing doublequotes etc.

 

After that, your program can be simplified with a few auxiliary functions:

 

(defun cell-addr (column)
  ;; creates a Excel cell address: "K6" or similar
  (strcat column (itoa (vl-bb-ref 'excelrow))))

(defun desc-data (key)
  (car (vl-symbol-value key)))

(defun temp-data (key)
  (cadr (vl-symbol-value key)))

(defun press-data (key)
  (caddr (vl-symbol-value key)))

(defun mfr-data (key)
  (cadddr (vl-symbol-value key)))


(defun doTYPE ( vallist / c cell namedata typekey)
  (if (setq cell (strcat "K" (itoa (vl-bb-ref 'excelrow))))
      (acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist)))
  (setq c 0)
  (setq len (length vallist))
  (setq cell (string_ cell))
  (repeat len
    (setq data (nth c vallist))
    (setq typedata (cadr data))
    (putcell cell typedata)
    (setq typekey (read typedata)) ;; convert to symbol

    (PUTCELL (cell-addr "I") (desc-data typekey))
    (PUTCELL (cell-addr "F") (temp-data typekey))
    (PUTCELL (cell-addr "E") (press-data typekey))
    (PUTCELL (cell-addr "A") (mfr-data typekey))
    
    (setq cell (string_ cell))
    (acet-ui-progress-safe c)
    (setq c (1+ c))
    );repeat
  (acet-ui-progress-done)
  (if (= c len)(dotag vallist));;goto do-tag sub
  );defun doTYPE

 - untested. of course...

 

-- 

 

Message 6 of 6
bhull1985
in reply to: martti.halminen

Martti, thank you so much for your help. I was able to tinker with the snippet you provided , testing testing testing, until I got it to work! WHOO!
It was pure joy getting that last line saved and then running the program, watching it fill out the excel sheet automatically. So COOL!

 

Anyhow, here's how I managed to do it, using the same data structure as I posted a day or two ago.

 

(if vallist
(acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist))
);if

(setq c 0)
(setq len (length vallist))

(repeat len
(setq cella (CELL-ADDR "K"))
(setq data (nth c vallist))
(setq cell (itoa (vl-bb-ref 'excelrow)))
(setq typedata (cadr data))
(putcell cella typedata)
(setq typekey (read typedata))

    (putcell (cell-addr "I") (desc-data typekey))
    (PUTCELL (cell-addr "F") (temp-data typekey))
    (PUTCELL (cell-addr "E") (press-data typekey))
    (PUTCELL (cell-addr "A") (mfr-data typekey))

(setq cell (string_ cell))
(setq cellb (atoi cell))
(vl-bb-set 'excelrow cellb)
(acet-ui-progress-safe c)
(setq c (1+ c))
);repeat

(acet-ui-progress-done)
(setvar "cmdecho" 0)
(princ "\n\t\t*************************************************************")(princ)
(princ "\n\t\t***\tAutoExcel Report \t*******************************")(princ)
(princ (strcat "\n\t\t***\t" (itoa (length @dupeslist)) " unique items duplicated in excel file \t\t***"))(princ)
(princ "\n\t\t***\t")
(princ lst)
(princ "\n\t\t***\tThese items have had their cells colored Grey\t***")(princ)
(princ "\n\t\t***\tFinished!\t\t\t\t\t*****************************")(princ)
(princ "\n\t\t*************************************************************")(princ)
(if vallist (setq svflg 1))
(setq vallist nil)
(vl-bb-set 'dupeslist nil)
(setq @dupeslist nil)
(setq @excel_row "6")
(setq *excelApp% nil)
(princ "\n")
;;end of excel input, routine will alert to user that it is complete
(princ)
(setq *error* nil)
(gc)

);defun dotype

 Naturally this won't be of much use for someone attempting to use the routine, code it for their purposes, but it shows how I accomplished getting all of that data into a nice and easy transfer into excel...just wanted to let people know that I got past it, and thank those who helped out!!

 

WHOOOO!!!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost