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

Insert from a ssget'ed block not working

6 REPLIES 6
Reply
Message 1 of 7
kaiknux
373 Views, 6 Replies

Insert from a ssget'ed block not working

Hi,

 

I've been trying to create a routine that works similar to a copy to multiple layers, but with my specific layers. It worked once, I don't know why it doesn't work anymore.

 

(defun c:ToAllLayers (/ b1 )

     

     (setq b1 (ssget))                                                                                                    ;1  Trying to make lisp save my selected block as b1

     (setq p1 (getpoint "\nSelect the insertion point of the copied blocks... " ))         ;2  Trying to make lisp save my insertion point

 

         

          (setvar "clayer" "Layer 1" )                                                                             ;3 Choosing the first layer that block will be inserted.

          (command "_insert" b1 "_s" "1" p1 "0" )                                                        ;4 Inserting the block. All insertions will happen on the same insertion point.

 

          (setvar "clayer" "Layer 2" )

          (command "_insert" b1 "_s" "1" p1 "0" )

 

          (setvar "clayer" "Layer 3" )

          (command "_insert" b1 "_s" "1" p1 "0" )

 

;Hundreds of more layers. Lots of objects overlayed.

 

 (prompt "\n Work please. Please.")

(princ)


 )

Tags (1)
6 REPLIES 6
Message 2 of 7
marko_ribar
in reply to: kaiknux

I would do something like this codes...

 

(defun c:InsToLayers ( / b1 b1n p1 lay nlay k layn )
  (setq b1 (ssget "_+.:E:S" '((0 . "INSERT"))))
  (setq b1n (cdr (assoc 2 (entget (ssname b1 0)))))
  (setq p1 (getpoint "\nSelect the insertion point of the copied blocks... "))
  (setq lay "Layer")
  (initget 7)
  (setq nlay (getint "\nOn how many Layers to perform insertions : "))
  (setq k 0)
  (repeat nlay
    (if (tblsearch "LAYER" (setq layn (strcat lay (itoa (setq k (1+ k))))))
      (progn
        (setvar 'clayer layn)
        (command "_.-insert" b1n "_s" "1" p1 "0")
      )
    )
  )
  (princ)
)

(defun c:InsToAllLayers ( / cl b1 b1n b1l p1 )
  (setq cl (getvar 'clayer))
  (setq b1 (ssget "_+.:E:S" '((0 . "INSERT"))))
  (setq b1n (cdr (assoc 2 (entget (ssname b1 0)))))
  (setq b1l (cdr (assoc 8 (entget (ssname b1 0)))))
  (setq p1 (getpoint "\nSelect the insertion point of the copied blocks... "))
  (foreach lay (vl-remove b1l (ai_table "LAYER" 4))
    (setvar 'clayer lay)
    (command "_.-insert" b1n "_s" "1" p1 "0")
  )
  (setvar 'clayer cl)
  (princ)
)

 M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 7
Kent1Cooper
in reply to: kaiknux


@kaiknux wrote:

.... it doesn't work anymore.

 

(defun c:ToAllLayers (/ b1 )

     

     (setq b1 (ssget))                                                                                              ;1  Trying to make lisp save my selected block as b1

....

          (command "_insert" b1 "_s" "1" p1 "0" )                                                        ;4 Inserting the block. ....


Assuming that your selection is always one Block [and marko_ribar's suggestion includes a way to ensure that], what's in your b1 variable will be a selection set.  The Insert command is going to want a Block name instead, which you would need to extract from the selected Block.  Something like:

 

(setq

  b1 (ssget)

  blkname (cdr (assoc 2 (entget (ssname b1 0))))

); setq

....

  (command "_.insert" blkname "_s" ....

Kent Cooper, AIA
Message 4 of 7
Kent1Cooper
in reply to: kaiknux

I would also assume that your Layer names are not really Layer 1, Layer 2, Layer 3, etc., but something more meaningful, in which case marko_ribar's first suggested routine would need adjustment to accommodate presumably something like a list of actual Layer names.

 

Another approach to the whole thing would be to simply COPY the Block multiple times, and [by any of several possible methods] change the Layer of each copy.  That would also preserve anything about the selected Block such as other-than-1 Scale Factor(s) or other-than-zero Rotation, if you want the copies to be the same as to those things.  It would require extracting the insertion point rather than the name of the selected Block.  Is that worth pursuing?

Kent Cooper, AIA
Message 5 of 7
kaiknux
in reply to: Kent1Cooper

(setq

b1 (ssget)

blkname (cdr (assoc 2 (entget (ssname b1 0))))

); setq

or

(setq b1 (ssget "_+.:E:S" '((0 . "INSERT"))))
(setq b1n (cdr (assoc 2 (entget (ssname b1 0)))))


Ok, my code now works, but I don't understand why.

I mean, I see your setq b1- which is necessary. but i don't know how and why cdr and assoc, which are list "dealing" commands- are being used to insert my block to the "routine's database".

I don't understand the ssname - i understand that b1 became the first element of sort of a "list", but when? why? why assoc 2? why cdr and not car?

would be very helpful for my lisp understanding if you tell me why these commands are being used this way and the reason of this sequence.

--
yes, "layer 1", "layer 2",... is just an example. I'm using my company's standard layers, and there's no logical numbering such as an ascending sequence. Sorry i made it look it that way.

Message 6 of 7
kaiknux
in reply to: Kent1Cooper

I see that for some programming purpose, it can be somehow intelligent to use my setqs in sort of a list, but can a simple "setq" make it work? Just a student question.

Message 7 of 7
Kent1Cooper
in reply to: kaiknux

I really should send you to read Help about the various AutoLISP functions involved, but briefly:

 

What is returned by (ssget) is a selection set, which you can think of as a kind of a "list," in the sense that it is a group of things under one umbrella, but it's very different from what AutoLISP considers a list as it defines that word.

 

(ssname) returns an entity name out of a selection set, with the number at the end being the index number of the item within the set.  The indexing starts at 0, so that gets the first [and in this case presumably only] entity name out of the selection set.  That's the name of the insertion of the Block, or as it's sometimes called, the Block reference, of which there may be many with the same Block name, so we're not there yet.

 

(entget) returns the entity data list for a designated entity name.  That is a list of sub-lists.  Some of those sublists are regular lists, but some are a special variety of list called a "dotted pair."  One of those holds the name of the Block that the reference is an insertion of.  [Other entries hold the insertion point, the scale factors, the rotation, the Layer it's on, etc., etc.]

 

(assoc) gets the sublist that starts with the number following it, from an entity data list.  The number associated with a Block insertion's Block name is 2, and (assoc 2 ...) returns the sublist [in this case a dotted pair] starting with a 2, which will be in the form (2 . "TheBlockName") [note the dot between].

 

(cdr) applied to a list typically returns another list, the same as what it started with but with its first element removed.  However, in the special case of a dotted-pair type list, it returns the second piece of the pair after the dot, not in another list but as an item in itself, in this case a text string.  That extracts the Block name from the entity data entry starting with a 2 that holds it.  [(car) returns the first item in either a list or a dotted pair, which in this case would give you the 2 -- that won't be of any use to you.]

 

It's necessary to go through those steps to extract the Block name so that you can Insert another of the same Block, because the Insert command will not accept a selection set when it's asking for a Block name.  It can only work with a Block name in the form of a text string.

 

You could also get it in a different way from VLA Properties, and you can put more of the Block into the drawing [including assigning the different Layers directly] by using (entmake) instead of Insert commands, but that's another story....

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost