 ; J - join a number of entities into a polyline.
 ; Copyright 1994, 1999, 2004, 2006, 2016 by Rocket Software Ltd.
 ; It is not possible for an organization with more than three members to
 ; accomplish anything which makes any sense.
 (DEFUN C:J (/ *error* ss nogo typ enam entt closd)
  (setvar "cmdecho" 0)
  (command "undo" "be")
  (DEFUN *error* (shk)
   (if (/= shk "Function cancelled") (write-line shk))
   (if snapp (setvar "snapmode" snapp))
   (if (type command-s)
       (command-s ".undo" "end")
       (command ".undo" "end"))
  (princ))
  (setq snapp (getvar "snapmode"))
  (setvar "snapmode" 0)
  (write-line "Select entities to join: ")
  (setq ss (ssget))
  (if ss
     (progn
 ; Now search the ss for a line, an arc, or an open polyline.
          (setq nogo T)
          (while (and (< 0 (sslength ss)) nogo)
                (setq typ (cdr (assoc 0 (setq entt 
                                         (entget (setq enam (ssname ss 0)))))))
                (setq closd (cdr (assoc 70 entt)))
                (cond ((= typ "LINE")
                       (setq nogo ()))
                      ((= typ "ARC")
                       (setq nogo ()))
                      ((or (= typ "POLYLINE") (= typ "LWPOLYLINE"))
                       (if (= (logand closd 1) 1)    ; closed polyline?
                           (ssdel enam ss)
                           (setq nogo ())))
                      (T
                       (ssdel enam ss))))
 ; If there are any entities left in the ss:
          (if (< 0 (sslength ss))
              (if (or (= typ "POLYLINE") (= typ "LWPOLYLINE"))
                  (command "pedit" enam "j" ss "" "")
                  (command "pedit" enam "y" "j" ss "" "")))))
  (setvar "snapmode" snapp)
  (command "undo" "end")
 (princ))