Alaskachick Posted September 27, 2010 Posted September 27, 2010 I am trying to extract the value of the 4th attribute in a block with around 40 attributes. The value is a path to a website that I want to open in a separate window. I am at the step where I am extracting the value of the attribute. I am using a lisp I found on Jeffery Sanders website and now I am trying to tweak it to do exactly what I want. I can get the first 2 attribute values (which I don't need) so I have tried to replace that request with a request for the 4th value. It's not working. Can anyone help me? (if (setq ent (entsel "\nSelect a Block: ")) (progn (setq en (car ent)) (setq enlist (entget en)) (setq blkType (cdr(assoc 0 enlist))) (if (= blkType "INSERT") (progn (if(= (cdr(assoc 66 enlist)) 1) (progn (setq en2(entnext en)) (setq enlist2(entget en2)) (while (/= (car (cadar (cdddr 1st (assoc 0 enlist2)) "SEQEND"))) (princ "/n ") (princ enlist2) (setq en2(entnext en2)) (setq enlist2(entget en2)) ) ) ) ; Close the if group code 66=1 statement ) ) ; Close the if block type = "ATTRIB" statement ) ) ; Close the if an Entity is selected statement. Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 You can use the nth function to return an item in a specific position in a list. For example: ;; Get fourth item in a list (nth 3 <list>) However, instead of using nth, I would suggest that you use a while statement to step through your attributes until a pair of conditions has been met where the entity is an attribute, and the attribute tag has been matched. For example: ... (while (and (/= (cdr (assoc 0 eData)) "ATTRIB") (/= (cdr (assoc 2 eData)) "[color=red]YourTagHere[/color]")) (progn (setq eName (entnext eName)) (setq eData (entget eName)))) ... Quote
Lt Dan's legs Posted September 27, 2010 Posted September 27, 2010 this is one I wrote a while ago. slightly modified (defun c:test (/ ent) (while (not (and (setq ent (car (Nentsel "\nSelect Attribute: "))) (eq "ATTRIB" (cdr (assoc 0 (setq ent (entget ent))))) ) ) (prompt "\n**Please select an attribute to modify!**") ) (entmod (subst (cons 1 "Cadtutor.net")(assoc 1 ent) ent)) (entupd (cdr (car ent))) (princ) ) Just click on the attribute you wish to modify *Just to clarify* "When I said this is better" I was talking about my code and the reason for me editing. Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 I am using a lisp I found on Jeffery Sanders website and now I am trying to tweak it to do exactly what I want. I'm sure Jeff would appreciate the compliment by you're using his work as a foundation for 'your' routine, but I think he might appreciate it even more, if you'd include the appropriate attribution (i.e., give him credit in your code)... Seems like the professional thing to do, don't you think? :wink: Quote
Alaskachick Posted September 27, 2010 Author Posted September 27, 2010 Just when I think I understand...I read a little more and confusion climbs back onto the throne of my brain. Just to clarify--All the attributes are invisible and I don't want to change any of them. I just want to be able to click on a block and have the value of the 4th attribute returned. RenderMan--I plugged your bit into my existing code in, I think, the correct spot. It works until I get to the end of what is copied below. I am using the VisualLISP editor and running the code line by line. (if (setq ent (entsel "\nSelect a Block: ")) (progn (setq en (car ent)) (setq enlist (entget en)) (setq blkType (cdr(assoc 0 enlist))) (while (and (/= (cdr (assoc 0 eData)) "ATTRIB") (/= (cdr (assoc 2 eData)) "website")) after this point I get this error error: bad argument type: lentityp nil I know just enough to not be able to complete ANYTHING. I think I need to take a break. Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 Please post a drawing which contains your block (in 2007 file format, or older). Quote
Alaskachick Posted September 27, 2010 Author Posted September 27, 2010 I totally do. Let me get this right. ; Original Code from "The AutoCAD Advanced Tutorial: Attributes" ; www.jeffreypsanders.com (defun c:blockpath () (if (setq ent (entsel "\nSelect a Block: ")) (progn (setq en (car ent)) (setq enlist (entget en)) ...code continues Is that an appropriate credit? There is no title or anything for the code in the tutorial I was learning from, so the title is from the website title. Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 No worries, that is more than sufficient. Again, I'm certainly not trying to harp on you. Had you not specified you got it from his website, I'd have never known. As you took the time to credit him in your OP description, I felt you might feel it appropriate to do in this manor. :: BACK ON TOPIC :: As soon as you post your block, I (or someone smarter!?) can try to help you. Quote
Alaskachick Posted September 27, 2010 Author Posted September 27, 2010 Please post a drawing which contains your block (in 2007 file format, or older). Here is a sample file I am using for my experiments. The original block I will apply this too has sensitive info, so a simulated block will have to do. testdoc.dwg Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 Try this: (defun c:FOO (/ eName blockObj tagString) (vl-load-com) (if (and (setq eName (car (entsel "\n >> Select an Attributed Block: "))) (= "INSERT" (cdr (assoc 0 (entget eName)))) (setq blockObj (vlax-ename->vla-object eName)) (vlax-method-applicable-p blockObj 'getattributes)) (foreach x (vlax-invoke blockObj 'getattributes) (if (= "WEBSITE" (setq tagString (vla-get-tagstring x))) (prompt (strcat "\n <!> Attribute Value = " (vla-get-textstring x) " <!> "))))) (princ)) Edit: Yes, this code looks very different! lol That's okay, I chose to do this with ActiveX instead, but both methods will work. I simply felt this would be easier to understand given the function terminology. Hope this helps! Quote
Alaskachick Posted September 27, 2010 Author Posted September 27, 2010 That works perfectly! In a whole different language at that. So now, when I was going to take the variable that had the value of the attribute attached to it and open it in a browser window I am back to being totally lost because there is no variable, just a textstring. (not that it's bad, I'm just not there yet in skills) My class has only taught me one language so far. But, now I have drafting work to do and have to take a break from the fun, yet frustrating, Lisp work I've been doing. Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 *Assuming* that the attribute value is correct, opening a browser to that address is too easy. :wink: Give me a minute.... Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 Revised code to open an internet browser: (defun c:FOO (/ eName blockObj tagString) (vl-load-com) (if (and (setq eName (car (entsel "\n >> Select an Attributed Block: "))) (= "INSERT" (cdr (assoc 0 (entget eName)))) (setq blockObj (vlax-ename->vla-object eName)) (vlax-method-applicable-p blockObj 'getattributes)) (foreach x (vlax-invoke blockObj 'getattributes) (if (= "WEBSITE" (setq tagString (vla-get-tagstring x))) [color=red](command "._browser" (vla-get-textstring x))[/color]))) (princ)) Quote
Lee Mac Posted September 27, 2010 Posted September 27, 2010 Had a bit of time to spare, so hopefully you can glean something worthwhile reading from this snippet of mumblings: [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] [color=black]c:test[/color] [b][color=RED]([/color][/b] [b][color=BLUE]/[/color][/b] entity [b][color=RED])[/color][/b] [i][color=#990099];; Define function, localise variable 'entity' used.[/color][/i] [i][color=#990099];; You can try to think of a better function syntax :-)[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [i][color=#990099];; IF the following expression returns True[/color][/i] [i][color=#990099];; (any non-nil value in fact, string, number you name it - just not 'nil')[/color][/i] [i][color=#990099];; we will proceed to the 'then' expression...[/color][/i] [i][color=#990099];; IF 'Test' Expression Starts Here {[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [i][color=#990099];; All of the expressions enclosed must return True[/color][/i] [i][color=#990099];; for the AND function to return True. AND will stop[/color][/i] [i][color=#990099];; evaluating expressions as soon as an expression returns[/color][/i] [i][color=#990099];; False.[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] entity [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#a52a2a]"\nSelect Attributed Block: "[/color][/b][b][color=RED])))[/color][/b] [i][color=#990099];; Prompt user for selection.[/color][/i] [i][color=#990099];; If user misses, entsel returns nil, hence (car (entsel)) also returns nil.[/color][/i] [i][color=#990099];; [Try it! (car nil) = nil][/color][/i] [i][color=#990099];; Else, if user picks object, entsel returns list of entity and picked point[/color][/i] [i][color=#990099];; [Try it! type (entsel) at the command line, pick an object and...[/color][/i] [i][color=#990099];; (<Entity> (x y z)), so (car (entsel)) returns <Entity> [bingo!][/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED]))))[/color][/b] [i][color=#990099];; Check that our user has picked a block and not some other unwanted[/color][/i] [i][color=#990099];; object like a pesky line.[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]=[/color][/b] [b][color=#009900]1[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]66[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED]))))[/color][/b] [i][color=#990099];; Check that the block is attributed, the 66 code means that ATTRIB[/color][/i] [i][color=#990099];; entities follow the block entity terminated by a SEQEND entity.[/color][/i] [i][color=#990099];; It is these ATTRIBute entities that we will query.[/color][/i] [i][color=#990099];; Source: http://autodesk.com/techpubs/autocad/acad2000/dxf/insert_dxf_06.htm[/color][/i] [b][color=RED])[/color][/b] [i][color=#990099];; End AND[/color][/i] [i][color=#990099];; } IF 'Test' Expression Ends Here[/color][/i] [i][color=#990099];; All of our conditions have returned True, so lets rock and roll...[/color][/i] [i][color=#990099];; IF 'Then' Expression Starts Here {[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]while[/color][/b] [i][color=#990099];; While the following expression returns a non-nil value:[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"ATTRIB"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] entity [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] entity[b][color=RED]))))))[/color][/b] [i][color=#990099];; We now look at the entity following the INSERT entity by using the[/color][/i] [i][color=#990099];; ENTNEXT function (pretty self-explanatory, 'next entity' so we use 'ent next'[/color][/i] [i][color=#990099];; We've checked that the INSERT entity has ATTRIBute entities following it[/color][/i] [i][color=#990099];; (DXF code 66=1) hence we know that the loop will run through at least once.[/color][/i] [i][color=#990099];; So: WHILE we are dealing with an ATTRIB entity and not a SEQEND Entity...[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]strcat[/color][/b] [b][color=#a52a2a]"\nAttribute: "[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED])))))[/color][/b] [i][color=#990099];; Print the Attribute TAG string to the screen for all to see[/color][/i] [i][color=#990099];; (DXF code 2 of the ATTRIB entity)[/color][/i] [i][color=#990099];; Source: http://autodesk.com/techpubs/autocad/acad2000/dxf/attrib_dxf_06.htm[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]strcat[/color][/b] [b][color=#a52a2a]" Value: "[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]1[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED])))))[/color][/b] [i][color=#990099];; Print the Attribute VALUE string to the screen for all to see[/color][/i] [i][color=#990099];; (DXF code 1 of the ATTRIB entity)[/color][/i] [i][color=#990099];; Source: You can't have missed it if you studied the previous comment ;-)[/color][/i] [b][color=RED])[/color][/b] [i][color=#990099];; End WHILE[/color][/i] [i][color=#990099];; Doh! No 'Else' expression to evaluate...[/color][/i] [b][color=RED])[/color][/b] [i][color=#990099];; End IF[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b] [i][color=#990099];; Lets keep the return of the last function a secret and exit quietly[/color][/i] [b][color=RED])[/color][/b] [i][color=#990099];; End DEFUN[/color][/i] Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 *Lavender* is a pretty color on you Lee... it really adds a *soft touch* to the bombardment of knowledge you just shared. Joking aside, well explained documentation (as always!). :wink: Quote
Lee Mac Posted September 27, 2010 Posted September 27, 2010 Another way to invoke the browser [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] [color=black]c:FooBar[/color] [b][color=RED]([/color][/b] [b][color=BLUE]/[/color][/b] entity site [color=black]elist [/color][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] entity [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#a52a2a]"\nSelect an Attributed Block: "[/color][/b][b][color=RED])))[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED]))))[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]=[/color][/b] [b][color=#009900]1[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]66[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] entity[b][color=RED]))))[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]while[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"ATTRIB"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] [color=black]elist[/color] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] entity [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] entity[b][color=RED])))[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]not[/color][/b] site[b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"WEBSITE"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] [color=black]elist[/color][b][color=RED])))[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] site [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]1[/color][/b] [color=black]elist[/color][b][color=RED])))[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] site [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED]([/color][/b][color=black]LM:NavigateTo[/color] site[b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] [color=black]LM:NavigateTo[/color] [b][color=RED]([/color][/b] url [b][color=BLUE]/[/color][/b] ie [b][color=RED])[/color][/b] [i][color=#990099];; © Lee Mac 2010[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]vl-load-com[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] ie [b][color=RED]([/color][/b][b][color=BLUE]vlax-create-object[/color][/b] [b][color=#a52a2a]"InternetExplorer.Application"[/color][/b][b][color=RED]))[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]vlax-put[/color][/b] ie [b][color=DARKRED]'[/color][/b]Visible [b][color=BLUE]:vlax-true[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]vlax-invoke[/color][/b] ie [b][color=DARKRED]'[/color][/b]Navigate url[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]vlax-release-object[/color][/b] ie[b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] [b][color=RED])[/color][/b] Quote
Lee Mac Posted September 27, 2010 Posted September 27, 2010 *Lavender* is a pretty color on you Lee... it really adds a *soft touch* to the bombardment of knowledge you just shared. Joking aside, well explained documentation (as always!). :wink: Hehehe thanks Quote
BlackBox Posted September 27, 2010 Posted September 27, 2010 Another way to invoke the browser Yes, of course. I have been neglecting the *full* capabilities of ActiveX (going outside of the application). Today was my first attempt, but I could not properly invoke the LaunchBrowserDialog method. The command line gave no useful feedback either. I'm leaving for the day now, but it bothered me I couldn't get it to work. Quote
Alaskachick Posted September 27, 2010 Author Posted September 27, 2010 oh.my.word. RenderMan, you are brilliant. Lee Mac, I will study all that stuff and hopefully move closer to being able to do this on my own. Of course, you realize I will continue tweaking so that something in there is mine. This is for a class and while the learning process is churning away in my head, at least some of the code needs to be mine as well. lol So much to learn, so little time... Quote
Lee Mac Posted September 27, 2010 Posted September 27, 2010 oh.my.word. RenderMan, you are brilliant. Lee Mac, I will study all that stuff and hopefully move closer to being able to do this on my own. Of course, you realize I will continue tweaking so that something in there is mine. This is for a class and while the learning process is churning away in my head, at least some of the code needs to be mine as well. lol So much to learn, so little time... You're welcome AlaskaChick. As far as Visual LISP goes, as in Renderman's examples - although it immediately looks quite intuitive as the functions are in English and not DXF mumbo jumbo, I wouldn't disregard Vanilla AutoLISP methods completely. They are definitely worth knowing. Some situations are better coded in VL, some have far more concision (take Dictionary manipulation) and far quicker executed in Vanilla (take entity creation for example), whereas VL can strive outside the AutoCAD 'box' into other apps that support OLE Automation. 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.