Jump to content

Filter By Multileader Style


Recommended Posts

Posted

Ok. Have had a look. (ps - Nothing to be embarrassed about in that code.) So I understand that the only step left is to make sure that only multileaders that have the desired stylename are processed? Yes? While waiting for your reply I'll start adding in what I would do to achieve that, but let me know if I have misunderstood.

  • Replies 39
  • Created
  • Last Reply

Top Posters In This Topic

  • cwake

    21

  • ksperopoulos

    16

  • Lee Mac

    2

  • hmsilva

    1

Posted

You are correct. That is what I am trying to get out of this thread. The other thread I started is trying to get the values to plug into the table correctly.

Posted

Try this Kyle. I've tried to keep the structure of everything pretty much as you had it so you can most easily see what I did. Otherwise I've added comments.

 

(defun c:WMT ( / *error* ss1 style dec ename att atts ots doc ps pt numr numc rowht colwd tbl cell )

 (vl-load-com)
 ;***************************************************************
 (defun *error* ( msg )
   (if (not (member msg '("Function cancelled" "quit / exit abort")))
     (princ (strcat "\nError: " msg))
     )
   (princ)
   )

 ;***************************************************************
 (defun massoc ( key lst / item )
   (if (setq item (assoc key lst))
     (cons (cdr item) (massoc key (cdr (member item lst))))
     )
   )

 ;***************************************************************
 (if (setq ss1 (ssget "x" '((0 . "MULTILEADER")))) ;if there are ANY multileaders found in the drawing
   (progn
     (setq style (getstring T "\nEnter the Multileader Style Name to search for: ") ;or you could hard code the name if it will never change...
           dec (1- (sslength ss1))
           )
     (while (<= 0 dec)
       (setq ename (ssname ss1 dec))
       (if (= (vla-get-stylename (vlax-ename->vla-object ename)) style) ;if the multileader style matches the one we're looking for
         (progn
           (setq att (nth 1 (massoc 302 (entget ename))) ;get it's attribute value
                 atts (cons att atts) ;and record it in a list of attribute values which we'll use to populate the table with
                 dec (1- dec) ;decrement the counter
                 )
           (print att) ;and print it out before moving to the next multileader
           );progn
         );if
       )
     (if atts ;if any of the multileaders found matched the style name and we have data to tabulate
       (progn
         (setq ots (getvar "ctablestyle"))
         (setvar "ctablestyle" "MMC-Weld Map Table")
         (setq doc (vla-get-activedocument (vlax-get-acad-object))
               ps (vla-get-paperspace doc)
               pt (vlax-3d-point '(15.0 10.5 0.0))
               numr (+ 2 (length atts))
               numc 2
               rowht 0.25
               colwd 0.75
               tbl (vla-addtable ps pt numr numc rowht colwd)
               cell 2
               )

         (vla-settext tbl 0 0 "WELD MAP TABLE")
         (vla-settext tbl 1 0 "WELD ID")
         (vla-settext tbl 1 1 "WELDER")
         (foreach x (reverse atts) ;reverse may not be necessary? but the list would be in reverse creation order otherwise
           (vla-settext tbl cell 0 x)
           (setq cell (+ cell 1))
           )
         (setvar "ctablestyle" ots)
         );progn
       );if atts
     );progn
   );if multileaders

 (princ) ;exit silently
 )

 

I added the getstring mainly because I didn't know what the name of the style you would be looking for would be, but I guess it gives it some flexibility if ever you use different styles for different applications?

If I was writing a function for this application, I would imagine a data sort would be useful? At the moment you have no control over the order that the attributes will be populated in the table, it's just in the order that the multileaders were created.

Otherwise a couple of observations. You could localise massoc if you aren't using it outside of this program. If you wanted to, you could also drop a few variables that aren't changing in the program, especially around the table creation, where you could just feed the data directly into the function that needs it directly eg. column width, row height, insert point. There isn't any gain storing it first. But up to you.

Let us know how it goes.

Posted

I tested it out and it works perfectly!!!!! Thank you, thank you, thank you. I am going to study this a little harder tomorrow as it's quitting time here. I have been banging my head against the wall for days now so I am anxious to find out how you solved my problem(s). I also want to thank you for the suggestions. If I can fit any more information in my head after studying this code, I may give it a shot. The data sort may or may not be needed. This is searching for an incrementing weld ID, so in theory, the user should enter them in in sequential order. But probably better to be safe than sorry!

Posted

Well done to you too. Like I said earlier, it's satisfying isn't it? Once you get your head around the work we've done so far, consider whether putting fields in your table would be even better. Then if you ever updated the multileader attribute the table updates with it. It can be done :-)

Posted

I couldn't wait to get home so I stayed late to try and understand this better!

 

The first thing that jumps out at me is how you created the list. I knew I was having a problem converting the returned attribute values to a string. I wasn't quite sure how to do that. I knew the function CONS could append an element to the beginning of the list, but I never thought of using it inside the WHILE function to create the whole list as it cycled through all the mleaders.

 

I also tried numerous times to get the FOREACH function to work while filling in the values into the table. It looks like I was at least on the right track. If I would have got my list creation to work, maybe I would have had more success with this part.

 

I thank you again for all your help. This has definitely been an educational experience. Now, on to the next program!

 

(BTW - I went ahead and added the sorting using ACAD_STRLSORT. I couldn't resist)

Posted

The more you look at written codes, the more that you'll find that CONS has been used to construct a list. It works like the "stack" in a calculator, it just places each new item at the front of the list pushing the rest to the back. When you call CONS with two arguments and the second argument is equal to nil, say (cons "Hello" nil), it won't construct a dotted pair like ("Hello" . nil), but rather just ("Hello"). Call it again with (cons "Goodbye" '("Hello")), where the second argument is now a list, you will get ("Goodbye" "Hello"), and so on. It's advantage over LIST is that you don't need to know how long the list is going to be before you start, it will just keep adding to the "stack". It's advantage over APPEND is that both arguments don't need to be lists. The only potential downside is that it constructs the list backwards. If that's a problem that is where REVERSE comes in.

 

I think you are right in your observations on FOREACH. And good on you for implementing ACAD_STRLSORT as well.

 

Do you use the VLIDE for your LISP work? If so, do you make use of the ability to step through your program as it is executing to see what each function is returning? I've learnt a lot simply by exploring data through the VLIDE. For example, I would never have been able to write that first function for getting MULTILEADERS with a certain style (the one that extracted them from the drawing's dictionary) if I had never played around in the VLIDE. It would have alerted you to the fact that ATT was only holding one string instead of many, so that you would know where to focus your attention to get it to work.

 

Regards,

Posted

Great explanation of CONS!

 

I have briefly tried using VLIDE but once I got the code in there, I didn't know how to use any of the tools it has. It is definitely nice to have the color coded pieces to your code, but I just haven't had time to figure out what else it can do. So basically, I use the Windows Notepad, Autodesk's help file, and I copy and paste parts of the code to the command line to find out what the results are. I know, pretty bad huh?!!!

Posted

I ran into a snag this morning with the code. It seems it is locking up my computer when there is multiple mleaders styles in the drawing. Last night I only tested it with the style I am looking for placed in the drawing. I guess today.....I troubleshoot. :(

Posted

Actually you are right. I was very careless in adjusting the code. I could correct it and repost. But it could be a perfect opportunity to work through in the VLIDE. Let us know if you want any hints at what I did wrong.

Posted

Trying to stumble my way through VLIDE, and I think the hangup is somewhere in this portion. Looks like there is a "malformed list on input". Am I close?

 

(progn
  (setq att (nth 1 (massoc 302 (entget ename)))
[color=red]          atts (cons att atts)[/color]
         dec (1- dec)
   )
   (print att)
)

Posted (edited)

Positionally close. Look at the line after that. I kept all of the setq statements together as they were in your posted code. But the decrement of "dec" will only ever occur if the multileader style test (the "if" statement), returns 'true'. So what happens when it finds a multileader with a different style? Do you think you can fix it with that information?

Edited by cwake
Posted

Once you have that fixed you could (if you want) set yourself a goal to add a check for the presence of the MMC-Weld Map Table style as well, and if it's not present either a) exit with a message to the user that the style isn't there, b) create it programatically in your function, or c) use the current tablestyle instead. I'm using the phone again so I can't check it, but I imagine that trying to setvar "ctablestyle" to a non-existent style would error.

Posted

Got it. The counter will never reach -1 the way it was. So if I had 15 mleaders in the drawing, and only 4 of them were the style I was looking for, it would still be waiting to feed it more information until it was satisfied. Thank you for all your help.

 

      (while (<= 0 dec)
       (setq ename (ssname ss1 dec))
       (if (= (vla-get-stylename (vlax-ename->vla-object ename)) style)
         (progn
           (setq att (nth 1 (massoc 302 (entget ename)))
                atts (cons att atts)
           )
           (print att)
         )
       )
       (setq dec (1- dec))
     )

Posted

Exactly. Tell me if my being cryptic in my answers was annoying and I'll change. I had a girl at work the other day say (with some exasperation but a smile as well). "You ALWAYS have to teach!! You never just give the answer!" I guess it's true. But I see merit in that approach. Lol.

Posted

While the easy way out would be for you to just tell me the answer, I appreaciate and like your approach. I would rather be taught so I can figure it out on my own one day. Thanks for the help!

Posted

Thanks Kyle. Since I made the goof up it was really tempting to fix it. Glad you had a go at narrowing it down in the VLIDE though. You did identify the area where it was falling over. Well done.

Posted

I think my learning curve just got a little steeper. Now I need to learn how to use VLIDE on top of learning how to write code. Does this ever end!:?

Posted

Haha. No. It's 11:30pm here, and what am I doing? Reviewing matrix maths for applying to co-ordinate transformations. Am I insane? Actually I already know the answer to that...

Posted

I was thinking that the other day when you said you were still laying in bed checking this forum! I think we all are a little "insane" about this stuff as it is always on my mind as well. :lol:

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...