Join all by layer for every layer

Join all by layer for every layer

Anonymous
Not applicable
2,085 Views
17 Replies
Message 1 of 18

Join all by layer for every layer

Anonymous
Not applicable

I have hit the (low) roof of my lisp knowledge on this one. I am trying to join everything on layer1, then layer2 etc... for all layers. No user input/selection required. Any pointers would be great, cheers.

 

 

(defun joinAllByLayer ( / layName ss ) ; 

	(vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) ;Iterate through layer collection
		(setq layName (vla-get-name lay)) ;find the layer name
		(if (setq ss (ssget "_X" (list (cons 8 layName)))) ;ss all on that layer
			(command "_join" ss))) ;join all on that layer

	(vl-load-com) (princ)

) ; defun -- joinAllByLayer

 

0 Likes
Accepted solutions (1)
2,086 Views
17 Replies
Replies (17)
Message 2 of 18

Anonymous
Not applicable

Hi @Anonymous 

I did not understand your request, do you intend to give the Join command on all entities in the drawing ??? Or do you want to put all the entities in your drawing on one layer and alternate within a loop?

0 Likes
Message 3 of 18

Anonymous
Not applicable

Hi @Anonymous 

Essentially yes, I want to join all entities in the drawing. By running the join command on everything at once I have sometimes noticed a few entities seem to jump into a different layer. I was hoping to prevent this by running join all on every layer separately - but for every layer.

0 Likes
Message 4 of 18

Anonymous
Not applicable
Accepted solution

Quickly tested, try this:

(defun c:joinAllByLayer ()
  (setq lyr 
    (vlax-for 
      lay (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
      (setq lst (cons (vla-get-Name lay) lst))
    )
  )  
  (mapcar 
    '(lambda ( x )
       (progn
         (setq sel (ssget "_X" (list (cons 8 x))))
         (if sel
           (if (= (sslength sel) 1)
             (command "_.pedit" sel "_join" "_all" "" "")
             (command "_.pedit" "_multiple" sel "" "_join" "0.0" "")
           )
         )
       )
     )
    lyr
  )
  (princ)
)

 

0 Likes
Message 5 of 18

hak_vz
Advisor
Advisor
(defun c:JBL( / layers)
(setq layers (vla-get-layers(vla-get-activedocument(vlax-get-acad-object))))
(vlax-for lay layers (command "_.join" (ssget "X" (list (cons  8 (vlax-get lay 'Name)))) "")) 
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 6 of 18

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

I have hit the (low) roof of my lisp knowledge on this one. I am trying to join everything on layer1, then layer2 etc... for all layers. No user input/selection required. Any pointers would be great, cheers.

 

 

(defun joinAllByLayer ( / layName ss ) ; 

	(vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) ;Iterate through layer collection
		(setq layName (vla-get-name lay)) ;find the layer name
		(if (setq ss (ssget "_X" (list (cons 8 layName)))) ;ss all on that layer
			(command "_join" ss ""))) ;join all on that layer

	(vl-load-com) (princ)

) ; defun -- joinAllByLayer

 


 

You need to close the selection set.

0 Likes
Message 7 of 18

Anonymous
Not applicable

Thanks for the replies, will have to test them tomorrow. LISP seems to chew into sleep time something chronic... love it!

0 Likes
Message 8 of 18

Kent1Cooper
Consultant
Consultant

[ @ВeekeeCZ beat me to it....]  I think you may simply need to complete the selection:

....

  (command "_join" ss "")

.... 

Kent Cooper, AIA
0 Likes
Message 9 of 18

Anonymous
Not applicable

@hak_vz Spent some time on this one as it was closest to how I thought it should work but didn't seem to do it for me for some reason.

 

@Anonymous Works perfectly thanks, gave me some homework though. Using mapcar & lambda I am not familiar with and will have to spend some time investigating.

 

Closing the selection set: this is what I originally had and removed to see what difference it made, didnt seem to make any but have put it back in.

 

Thanks heaps.

0 Likes
Message 10 of 18

cadffm
Consultant
Consultant

And don't forget the CTAB filter in your ssget filter list.

 

 

Sebastian

0 Likes
Message 11 of 18

eng_minamaged
Advocate
Advocate

@Anonymous  @Kent1Cooper  If I have error can't join closed polyline If I want to select only line arc and opened polylines for every layer

0 Likes
Message 12 of 18

Kent1Cooper
Consultant
Consultant

@eng_minamaged wrote:

.... If I have error can't join closed polyline If I want to select only line arc and opened polylines for every layer


Explain in more detail -- how are you getting an error?  When using the JOIN command [which I assume because you mentioned me, and my only Message here is related to JOIN], I can include closed Polylines in the selection without any error resulting.  It reports that it didn't Join everything [discarded some object(s) from the operation], but there's no error.  Similarly, when using PEDIT's Join option, if the selected Polyline is closed, it merely reports that I can't Join to it, but just goes back to the prompt, without error.

Kent Cooper, AIA
0 Likes
Message 13 of 18

eng_minamaged
Advocate
Advocate

When I use PEDIT if polyline is closed it give me error and the lisp stop

0 Likes
Message 14 of 18

Kent1Cooper
Consultant
Consultant

@eng_minamaged wrote:

When I use PEDIT if polyline is closed it give me error and the lisp stop


That must mean you are using the code in Message 4, the only entry that uses PEDIT.  Studying that, one thing doesn't make sense to me:  If there is only one object on a given Layer, why does it [try to] Join everything [without distinction of Layer] to it?  Shouldn't it just be left alone?  It must be in that attempt that you are getting the error, when that one thing is closed, because then it goes back to the initial prompt, and what it is given is not appropriate for that.  I would think you would only Join things when there are more than one on a given Layer.  Something like this modification [untested]:

 

(defun c:joinAllByLayer (/ lyr lst sel)
  (setq lyr
    (vlax-for
      lay (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
      (setq lst (cons (vla-get-Name lay) lst))
    )
  )
  (mapcar
    '(lambda (x)
      (if (setq sel (ssget "_X" (list (cons 8 x))))
        (if (> (sslength sel) 1)
          (command "_.pedit" "_multiple" sel "" "_join" "0.0" "")
        )
      )
    )
    lyr
  )
  (princ)
)

 

[That would have trouble, too, if the only things on a given Layer are not in the current space, because they will not be "seen" by the PEDIT object selection.]

 

Or, a little more simply [lightly tested]:

(defun c:JoinAllByLayer (/ lay ss)
  (while (setq lay (tblnext "layer" (not lay)))
    (if (setq ss (ssget "_X" (list (cons 8 (cdr (assoc 2 lay))) (cons 410 (getvar 'ctab)))))
      (if (> (sslength ss) 1)
        (command "_.pedit" "_multiple" ss "" "_join" "0.0" "")
      )
    )
  )
  (princ)
)
Kent Cooper, AIA
0 Likes
Message 15 of 18

eng_minamaged
Advocate
Advocate

I don't want to insert layer or select objects........... 
I want to take every layer in drawing then  for each layer take only  lines arcs and open polylines to join by pedit
.... I want to filter selection for only lines , arcs and open polylines before pedit

(setq sel (ssget "_X" (list '(0 . "LINE,ARC,*POLYLINE") (cons 8 x))))

But I want open polylines not all polylines

0 Likes
Message 16 of 18

Kent1Cooper
Consultant
Consultant

Open Polylines will simply be ignored by PEDIT/Join under the Multiple option [unlike under the pick-something approach and then the Join option], so I don't think there's any harm in allowing them.  But it is possible to prevent selection of them:

 

(defun c:JoinAllByLayer (/ lay ss)
  (while (setq lay (tblnext "layer" (not lay)))
    (if
      (setq ss
        (ssget "_X"
          (list
            '(-4 . "<AND")
              (cons 8 (cdr (assoc 2 lay))); on the given Layer
              (cons 410 (getvar 'ctab)); in the current space
              '(-4 . "<OR")
                '(0 . "LINE,ARC"); any
                '(-4 . "<AND")
                  '(0 . "LWPOLYLINE")
                  '(-4 . "<NOT") '(-4 . "&") '(70 . 1) '(-4 . "NOT>"); open only
                '(-4 . "AND>")
              '(-4 . "OR>")
            '(-4 . "AND>")
          ); list
        ); ssget
      ); setq
      (if (> (sslength ss) 1)
        (command "_.pedit" "_multiple" ss "" "_join" "0.0" "")
      )
    )
  )
  (princ)
)

 

Be sure PEDITACCEPT is set to 1 [that can be built in if desired].  And consider whether you want to account for the possibility of locked Layers, one way or the other.

 

By the way, any Lines or Arcs that do not meet something else eligible at an end, and are therefore not Joined to anything, will be turned into single-segment Polylines, i.e. they will not be left as Lines/Arcs.  If you'd rather they be left alone, that's possible [though it actually involves Exploding them back to Lines/Arcs after PEDITing] -- see PolylineJoin.lsp >here<.

Kent Cooper, AIA
Message 17 of 18

eng_minamaged
Advocate
Advocate

It is like a charm.Thank you For Your Help ....Thank you for your time

0 Likes
Message 18 of 18

david.waight
Contributor
Contributor

What would need to change in Kent's message 16, so you could select what you wanted to join (still in separate layers)  as opposed to all the entities in the drawing?

0 Likes