Jump to content

a little help for the new guy....


Recommended Posts

Posted

Hello all! New to the forums here...

 

Ok, so I have tinkered with Autolisp a little but I have never created anything that complex. I have a lot of ideas on things i want to write to help make things at work easier but I am kind of just jumping into the deep end after learning to doggy paddle.

Anyway, the routine I am currently working on is a function to create a list of all the instances of a block in a drawing and create a schedule based on some info in the block. I thought I had a pretty good starting point to work from but my first little trick in the code is giving me fits. Each block is on a separate layer that is just a number. I am trying to update the block to change an attribute within it to contain the layer name when i run the routine.

 

The code I am working on looks right to me but I keep getting strange errors in the section that substitutes the values, like a type mismatch or something. But I thought that they were both strings??!!??

 

LOL, as you might be able to tell, I have exhausted my resources in trying to figure out this particular snag and am looking for some other peoples point of view.

 

Here is what I have so far.

(defun getpart ()
 (SETQ SSID (ssget "X" (list (cons 2 "PARTID"))))
 (IF SSID
   (PROGN
     (SETQ COUNT 0
       EMAX  (SSLENGTH SSID)
     )
     (WHILE (< COUNT EMAX)
   (SETQ EN     (SSNAME SSID COUNT)
         ED     (ENTGET EN)
         Blkn   (dxf 2 ED)
         ELAYER (dxf 8 ed)
         curid  (ssname ssid count)
   )
   (setq curid
          (substr (cons 1 "elayer") (assoc 1 partnumber) curid)
   )
   (entmod curid)
   (entupd en)
   (SETQ COUNT EMAX
         found T
   )
   (SETQ COUNT (1+ COUNT))
     )                    ;while

   )                    ;progn

                   ;there are no blocks in the drawing
   (alert
     "\nCannot find any blocks to edit.
     \n     Check your drawing."
   )

 )                    ;if

                   ;finish clean    
 (princ)

)                    ;defun

Any advice would just make my day. Be gentle though, I admit my newbness:?:?

Posted

Yes your noobness is showing, but we all have been there :)

 

-Looks Ok up to the point where it retrieves the block data. It appears then you are trying to substitute the "group 1" data with the layer name. But the initial block data does not contain attribute information, you need to step through the block data (using 'entnext' until a particular tag is reached, or the 'seqend' (GC 0) is reached.) When you've found the desired attribute tag, then substitute the GC 1 data. IWill be simpler if it is known that block has only 1 attribute, then just one 'entnext' is needed to get to the attribute data.

 

Also you didn't use the correct data to subsitute on, in your case the block data is in the 'ED' variable. (also you used wrong function, substr vs subst - close). Lets say there is one attribute, then the attribute data could be:

 

(setq Attdat (entget (entnext EN)))

 

Then the substitute line should look like;

 

(setq curid

(subst (cons 1 "elayer") (assoc 1 Attdata) Attdata))

 

 

hope this gives you enogh to work with until the next version-

enjoy!

Posted

Something like this?

 

(defun c:blkupd    (/ ss i bEnt bLay aEnt)
   (if    (setq ss (ssget "X" (list (cons 0 "INSERT") (cons 410 (getvar "ctab")))))
   (progn
       (setq i   (sslength ss) blk 0)
       (while (not (minusp (setq i (1- i))))
       (setq bEnt (ssname ss i)
             bLay (cdr (assoc 8 (entget bEnt)))
             aEnt (entget (entnext bEnt)))
       (if (/= "SEQEND" (cdr (assoc 0 aEnt)))
           (progn
           (setq aEnt (subst (cons 1 bLay) (assoc 1 aEnt) aEnt))
           (entmod aEnt))
           (princ "\n<!> Block Contains No Attributes <!>")))
       (vl-cmdf "_regenall"))
   (princ "\n<!> No Blocks Found <!>"))
   (princ)
) ;_  end defun

Posted

hardeight

 

That's pretty good for a newbie.

 

As Carl noted, the assumption is that it is the first ATTRIB you wish to work with:

 

[b][color=BLACK]([/color][/b]defun c:lay2att [b][color=FUCHSIA]([/color][/b]/ ss i en ed an ad[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]cons 2 [color=#2f4f4f]"PARTID"[/color][b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]sslength ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1- i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b]
                   ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]entmod [b][color=GREEN]([/color][/b]subst [b][color=BLUE]([/color][/b]cons 1 [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 8 ed[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]assoc 1 ad[b][color=BLUE])[/color][/b] ad[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]entupd en[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

(66 . 1) Indicates ATTRIB follow

 

I still use (0 . "INSERT") because of SHAPE entities have a group 2 string name as well. You don't find them much any more but they are still around.

 

Good Luck -David

Posted
hardeight

 

That's pretty good for a newbie.

 

As Carl noted, the assumption is that it is the first ATTRIB you wish to work with:

 

[b][color=BLACK]([/color][/b]defun c:lay2att [b][color=FUCHSIA]([/color][/b]/ ss i en ed an ad[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]cons 2 [color=#2f4f4f]"PARTID"[/color][b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]sslength ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1- i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b]
                   ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]entmod [b][color=GREEN]([/color][/b]subst [b][color=BLUE]([/color][/b]cons 1 [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 8 ed[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]assoc 1 ad[b][color=BLUE])[/color][/b] ad[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]entupd en[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

(66 . 1) Indicates ATTRIB follow

 

I still use (0 . "INSERT") because of SHAPE entities have a group 2 string name as well. You don't find them much any more but they are still around.

 

Good Luck -David

 

Nice one David, I completely missed the Block Name and Attribute dxf in my ssget... i'm getting careless I think.. :P

Posted

All your help makes perfect sense now that I see I written. And for the time being, I think I will just edit my block to put the desired attribute change in the first position and that will get me a little further on down the road in my routine. But just while we are on this subject. How would I cycle through the attributes to find the one I wanted? Is there a NEXTTAB variable or function? Or would I have to do some type of LAMBDA code to get a list of all of them and then look for the one I want? or am I just making this way too complicated? lol:D

Posted

If you knew the name of your Attribute, just use something like:

 

(while (/= "SEQEND" (cdr (assoc 0 (entget Ent1))))
   (cond (= "ATT1" (cdr (assoc 2 (entget Ent1))))
     ... change this attribute ...
     (= "ATT2" (....etc etc))
   )
   (setq Ent1 (entnext Ent1))
)
   

 

i.e, cycle through the attributes, until seqend is reached, and if the attribute name matches one that the user has entered, then change it to something else...

Posted

For example, this is one that David wrote for another guy on here to change multiple atts:

 

[b][color=BLACK]([/color][/b]defun c:chg-att [b][color=FUCHSIA]([/color][/b]/ ov nv ss i en an ad[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not ov[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq ov [b][color=MAROON]([/color][/b]getstring t [color=#2f4f4f]"\nOld ATTRIB Value:   "[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not nv[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq nv [b][color=MAROON]([/color][/b]getstring t [color=#2f4f4f]"\nNew ATTRIB Value:   "[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b]
                                [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                    [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
       [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]sslength ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
       [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1- i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b]
                    an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                    ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]/= [color=#2f4f4f]"SEQEND"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                     [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]= [b][color=RED]([/color][/b]strcase ov[b][color=RED])[/color][/b]
                             [b][color=RED]([/color][/b]strcase [b][color=PURPLE]([/color][/b]cdr [b][color=TEAL]([/color][/b]assoc 1 ad[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                          [b][color=BLUE]([/color][/b]entmod [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons 1 nv[b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]assoc 1 ad[b][color=PURPLE])[/color][/b] ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                          [b][color=BLUE]([/color][/b]entupd en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                     [b][color=GREEN]([/color][/b]setq an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                           ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])

[/color][/b]

Posted

This request keeps coming up from time to time, so here is a streamlined ATTEDIT to change all instances of a specified ATTRIB Tag name to a given value in selected INSERTS:

[b][color=BLACK]([/color][/b]defun c:chgattvl [b][color=FUCHSIA]([/color][/b]/ tn av ss i en ed an ad[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not tn[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq tn [b][color=MAROON]([/color][/b]getstring [color=#2f4f4f]"\nATTRIB Tag Name:   "[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not av[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq av [b][color=MAROON]([/color][/b]getstring t [color=#2f4f4f]"\nATTRIB New Value:   "[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]princ [color=#2f4f4f]"\nSelect ATTRIButed INSERTs To Edit:   "[/color][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b]
                            [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b]
                                [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]sslength ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1- i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b]
                   ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]= [b][color=RED]([/color][/b]strcase tn[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]strcase [b][color=PURPLE]([/color][/b]cdr [b][color=TEAL]([/color][/b]assoc 2 ad[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]entmod [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons 1 av[b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]assoc 1 ad[b][color=PURPLE])[/color][/b] ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]entupd en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                          ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

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...