jason_a Posted May 12, 2016 Posted May 12, 2016 (edited) Hi, I have a lisp routine that is looking for entities and changing properties. In one section of the routine it's looking for an 8.00015 hole diameter and changing the diameter to 8. The problem is if the hole doesn't exist, the routine stops and will not finish. Here is the routine. [color="royalblue"]; Looking for 3mm holes... 1.5mm radius (if (ssget "_X" '((0 . "CIRCLE")(40 . 1.5))) (COMMAND "CHANGE" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Find 8.00015 change to 8mm (setq ss1 (ssget "_X" '((0 . "CIRCLE")(40 . 4.000075)))) (setq LE (sslength ss1)) (setq CNT 0) (while (< CNT LE) (progn (setq OBJ (entget (ssname ss1 CNT))) (setq NewScale (* (cdr (assoc 40 OBJ)) 0.99998125035)) (setq OBJ (subst (cons 40 NewScale) (assoc 40 OBJ) OBJ )) (entmod OBJ) (setq CNT (+ CNT 1)) ) ;end progn ) ;end do while (PRINC) ; it's looking for the following entities/blocks/linetypes and deleting them (if (ssget "_X" '((0 . "INSERT") (2 . "POCKET"))) (COMMAND "ERASE" "P" "") )[/color] So I incorporated an if statement but that didn't work either. Like this.. [color="royalblue"]; Looking for 3mm holes... 1.5mm radius (if (ssget "_X" '((0 . "CIRCLE")(40 . 1.5))) (COMMAND "CHANGE" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Find 8.00015 change to 8mm [color="red"](if[/color] (setq ss1 (ssget "_X" '((0 . "CIRCLE")(40 . 4.000075)))) (setq LE (sslength ss1)) (setq CNT 0) (while (< CNT LE) (progn (setq OBJ (entget (ssname ss1 CNT))) (setq NewScale (* (cdr (assoc 40 OBJ)) 0.99998125035)) (setq OBJ (subst (cons 40 NewScale) (assoc 40 OBJ) OBJ )) (entmod OBJ) (setq CNT (+ CNT 1)) ) ;end progn ) ;end do while (PRINC) [color="red"])[/color] ; it's looking for the following entities/blocks/linetypes and deleting them (if (ssget "_X" '((0 . "INSERT") (2 . "POCKET"))) (COMMAND "ERASE" "P" "") ) [/color] I will eventually be adding other hole diameters to search for and change but I need to overcome this initial hiccup in order to proceed. Please advise, thanks. Edited May 12, 2016 by jason_a Quote
Lee Mac Posted May 12, 2016 Posted May 12, 2016 The problem is that you are supplying the if function with more than three arguments. The if function requires two or three arguments: the test expression, the 'then' expression and optionally, the 'else' expression. Therefore, to allow the evaluation of multiple expressions within the 'then' argument, such expressions will need to be arguments for another function in order to yield a single expression. The easiest way to accomplish this is through the use of the progn function: (if [i]<test-expression>[/i] (progn [i] <then-expression> <then-expression> <then-expression>[/i] ) ) PS: Please edit your post and enclose your code with code tags: [highlight][noparse] [/noparse][/highlight]Your code here[highlight][noparse] [/noparse][/highlight] Quote
Lee Mac Posted May 12, 2016 Posted May 12, 2016 The three operations could also become: (if (setq s (ssget "_X" '((0 . "CIRCLE") (40 . 1.5)))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i))))) (entmod (subst '(8 . "vertical15p00") (assoc 8 e) e)) ) ) (if (setq s (ssget "_X" '((0 . "CIRCLE") (40 . 4.000075)))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i))))) (entmod (subst '(40 . 4.0) (assoc 40 e) e)) ) ) (if (setq s (ssget "_X" '((0 . "INSERT") (2 . "POCKET")))) (repeat (setq i (sslength s)) (entdel (ssname s (setq i (1- i)))) ) ) This is using Method 2a of my Selection Set Processing tutorial. Quote
Lee Mac Posted May 12, 2016 Posted May 12, 2016 You could also combine all three operations into a single selection set: (if (setq s (ssget "_X" '( (-4 . "<OR") (-4 . "<AND") (0 . "CIRCLE") (-4 . "<OR") (40 . 1.5) (40 . 4.000075) (-4 . "OR>") (-4 . "AND>") (-4 . "<AND") (0 . "INSERT") (2 . "POCKET") (-4 . "AND>") (-4 . "OR>") ) ) ) (repeat (setq i (sslength s)) (setq e (ssname s (setq i (1- i))) x (entget e) ) (cond ( (= "INSERT" (cdr (assoc 0 x))) (entdel e) ) ( (= 1.5 (cdr (assoc 40 x))) (entmod (subst '(8 . "vertical15p00") (assoc 8 x) x)) ) ( (entmod (subst '(40 . 4.0) (assoc 40 x) x))) ) ) ) Quote
jason_a Posted May 12, 2016 Author Posted May 12, 2016 Thanks for your quick reply, it'll take me some time to digest your answers! This is the full code from my routine and I wrote it this way because I find it easy to simply copy and paste additional blocks of code as it continues to evolve. In the case of the hole diameter change, i'll be copy and pasting new blocks of text and simply changing the radius values. Hopefully I can figure out how to incorporate your answers and get my routine working smoothly. Much appreciated thank you. ; Looking for 3mm holes... 1.5mm radius (if (ssget "_X" '((0 . "CIRCLE")(40 . 1.5))) (COMMAND "CHANGE" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Looking for 76.2mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 38.1))) (COMMAND "CHANGE" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 77.2mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 38.6))) (COMMAND "CHANGE" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 38mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 19.0))) (COMMAND "CHPROP" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 5mm dia Z15mm holes (if (ssget "_X" '((0 . "CIRCLE")(40 . 2.50075))) (COMMAND "CHPROP" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Find 8.00015 change to 8mm (setq ss1 (ssget "_X" '((0 . "CIRCLE")(40 . 4.000075)))) (setq LE (sslength ss1)) (setq CNT 0) (while (< CNT LE) (progn (setq OBJ (entget (ssname ss1 CNT))) (setq NewScale (* (cdr (assoc 40 OBJ)) 0.99998125035)) (setq OBJ (subst (cons 40 NewScale) (assoc 40 OBJ) OBJ )) (entmod OBJ) (setq CNT (+ CNT 1)) ) ;end progn ) ;end do while (PRINC) ; it's looking for the following entities/blocks/linetypes and deleting them (if (ssget "_X" '((0 . "INSERT") (2 . "POCKET"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "LKCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "PTCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "NLCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "GRAIN"))) (COMMAND "ERASE" "P" "") ) (if (ssget "x" (list (cons 0 "DIM*"))) (COMMAND "ERASE" "P" "") ) (if (ssget "X" '((6 . "DASHED*"))) (COMMAND "ERASE" "P" "") ) Quote
jason_a Posted May 12, 2016 Author Posted May 12, 2016 Thank you very much I got it working! Additional lines of code in red. ; Looking for 3mm holes... 1.5mm radius (if (ssget "_X" '((0 . "CIRCLE")(40 . 1.5))) (COMMAND "CHANGE" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Looking for 76.2mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 38.1))) (COMMAND "CHANGE" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 77.2mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 38.6))) (COMMAND "CHANGE" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 38mm dia grommets (if (ssget "_X" '((0 . "CIRCLE")(40 . 19.0))) (COMMAND "CHPROP" "P" "" "P" "LA" "ROTOR" "") ) ; Looking for 5mm dia Z15mm holes (if (ssget "_X" '((0 . "CIRCLE")(40 . 2.50075))) (COMMAND "CHPROP" "P" "" "P" "LA" "VERTICAL15P00" "") ) ; Find 8.00015 change to 8mm [color="red"](if (ssget "_X" '((0 . "CIRCLE")(40 . 4.000075))) (progn[/color] (setq ss1 (ssget "_X" '((0 . "CIRCLE")(40 . 4.000075)))) (setq LE (sslength ss1)) (setq CNT 0) (while (< CNT LE) (progn (setq OBJ (entget (ssname ss1 CNT))) (setq NewScale (* (cdr (assoc 40 OBJ)) 0.99998125035)) (setq OBJ (subst (cons 40 NewScale) (assoc 40 OBJ) OBJ )) (entmod OBJ) (setq CNT (+ CNT 1)) ) ;end progn ) ;end do while (PRINC) [color="red"]) )[/color] ; it's looking for the following entities/blocks/linetypes and deleting them (if (ssget "_X" '((0 . "INSERT") (2 . "POCKET"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "LKCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "PTCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "NLCLIP"))) (COMMAND "ERASE" "P" "") ) (if (ssget "_X" '((0 . "INSERT") (2 . "GRAIN"))) (COMMAND "ERASE" "P" "") ) (if (ssget "x" (list (cons 0 "DIM*"))) (COMMAND "ERASE" "P" "") ) (if (ssget "X" '((6 . "DASHED*"))) (COMMAND "ERASE" "P" "") ) Quote
BIGAL Posted May 13, 2016 Posted May 13, 2016 Re 8.00015 it does not make sense to hard code the radius value and make copies for other radius's just imply a radius and a tolerance factor add this to the ssget as a and Second suggestion just do ssget X circle and use a cond to check what radius, you will need a repeat to loop through selection sset. not true code (cond ((= (cdr (assoc 40 ssname etc)) 38.6 )(do something)) ((= (cdr (assoc 40 ssname etc)) 38.1 )(do something2)) Quote
jason_a Posted May 13, 2016 Author Posted May 13, 2016 Hi, thanks for your input. What i'm trying to accomplish is automated layer management to prepare dxf files in autocad for eventual cnc programming. The files originate from solidworks, import into autocad and then dxf's are compiled and flipped over to cnc software. Proper layer management and accurate hole diameters are essential in order to process properly in the cnc software. The cnc software can derive hole diameter information from a circle entity in a 2d drawing, but it still does not know what the depth of the hole is. That's where the autocad layer management comes into play. If the hole is placed on a correctly labelled layer that contains hole depth information, the cnc software uses that information to process hole depth when post processing. One way to place the hole on the proper layer is to manually pick each hole in the dwg and move to the appropriate layer. Tedious at best. OR In solidworks, for example let's say an 8mm diameter hole is to be drilled 15mm deep, we draw that hole at a diameter of 8.0015. (the .0015 is like a tag indicating depth of drilling) Then when the 2d file is imported into Autocad and with my scripts and lisp routines, it will find any hole with a diameter of 8.0015, change it to a 15mm deep labelled layer, and scale the diameter to a true size of 8mm. The dxf file is now fully prepared for cnc post processing and it was fully automatic! Therefore tolerances wouldn't be acceptable in this case because a hole size of 8.0016 would be indicative of an 8mm diameter hole, drilled 16mm deep. It sounds a bit convoluted but it's a very nice automated system when dealing with many different sized holes of varying depths and diameters. Tomorrow at work I will see if I can figure out how to incorporate your second suggestion into my lisp routine. I'm not very well versed in autolisp but hopefully I can figure it out. Now that you know what my routine is being used for, if you have any other suggestions to accomplish what I need in a better manner, please let me know i'd be happy to hear them. Thanks again for your help, I really appreciate it. Quote
Recommended Posts
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.