Author Topic: Help with code - Join Lines  (Read 3113 times)

0 Members and 1 Guest are viewing this topic.

nekonihonjin

  • Newt
  • Posts: 103
Help with code - Join Lines
« on: December 31, 2015, 01:04:31 PM »
Good day everyone I have the next code that creates a tunnel profile wich I use at work.

I'm trying to join the lines and arcs but the join command does nothing, if I do it mannually it join them.

Code: [Select]
(defun c:xro2()

(setq EsqInfIzq (getpoint "\nEsquina Inferior Izquierda: "))
(setq EsqSupDer (getcorner EsqInfIzq "\nEsquina Superior Derecha: "))
(command "_line" EsqInfIzq "@1,0" "" )
(setq linea1 (entlast))
(command "_line" EsqInfIzq "@0,1" "" )
(setq linea2 (entlast))
(command "_line" EsqSupDer "@-1,0" "" )
(setq linea3 (entlast))
(command "_line" EsqSupDer "@0,-1" "" )
(setq linea4 (entlast))
(command "_filletrad" "0.0" )
(command "_.fillet" linea1 linea4 ) 
(command "_filletrad" "0.8" )
(command "_.fillet"  linea3 linea4 )
(setq curva1 (entlast))
(command "_.fillet"  linea2 linea3 )
(setq curva2 (entlast))


(command "_join" linea1 linea2 linea3 linea4 curva1 curva2 "")


(princ)
)


edit kdub: Changed post title to be more descriptive.


« Last Edit: December 31, 2015, 04:36:24 PM by Kerry »

mailmaverick

  • Bull Frog
  • Posts: 494
Re: Help with code - Join Lines
« Reply #1 on: January 01, 2016, 02:02:40 AM »
It is working absolutely fine for me in AUTOCAD 2015.

Maybe your osmode, orthmode and snapmode etc are not correct. Try following code :-

Code: [Select]
(defun c:xro2 ()
  (setq oldosmode (getvar 'osmode))
  (setq oldorthomode (getvar 'orthomode))
  (setq oldsnapmode (getvar 'snapmode))
  (setvar 'osmode 0)
  (setvar 'orthomode 0)
  (setvar 'snapmode 0)
  (setq EsqInfIzq (getpoint "\nEsquina Inferior Izquierda: "))
  (setq EsqSupDer (getcorner EsqInfIzq "\nEsquina Superior Derecha: "))
  (command "_line" EsqInfIzq "@1,0" "")
  (setq linea1 (entlast))
  (command "_line" EsqInfIzq "@0,1" "")
  (setq linea2 (entlast))
  (command "_line" EsqSupDer "@-1,0" "")
  (setq linea3 (entlast))
  (command "_line" EsqSupDer "@0,-1" "")
  (setq linea4 (entlast))
  (command "_filletrad" "0.0")
  (command "_.fillet" linea1 linea4)
  (command "_filletrad" "0.8")
  (command "_.fillet" linea3 linea4)
  (setq curva1 (entlast))
  (command "_.fillet" linea2 linea3)
  (setq curva2 (entlast))
  (command "_join" linea1 linea2 linea3 linea4 curva1 curva2 "")
  (setvar 'osmode oldosmode)
  (setvar 'orthomode oldorthomode)
  (setvar 'snapmode oldsnapmode)
  (princ)
)

Beekee

  • Mosquito
  • Posts: 19
Re: Help with code - Join Lines
« Reply #2 on: January 01, 2016, 08:40:33 AM »
You can use a polyline... then no need to join anything.

Code: [Select]
(defun c:xro3 ( / EsqInfIzq EsqSupDer enlast)
  (if (and (setq EsqInfIzq (getpoint "\nEsquina Inferior Izquierda: "))
   (setq EsqSupDer (getcorner EsqInfIzq "\nEsquina Superior Derecha: "))
   (setq enlast (entlast))
   )
    (progn
      (command "_PLINE"
       "_none" (list (car EsqSupDer) (cadr EsqInfIzq) 0.)
       "_none" EsqSupDer
       "_none" (list (car EsqInfIzq) (cadr EsqSupDer) 0.)
       "_none" EsqInfIzq
       "")
      (if (not (equal enlast (entlast)))
(command "_.FILLET" "_R" "0.8"
"_.FILLET" "_P" (entlast)
"_.PEDIT" (entlast) "_CL" ""))))
  (princ)
)

ribarm

  • Gator
  • Posts: 3293
  • Marko Ribar, architect
Re: Help with code - Join Lines
« Reply #3 on: January 04, 2016, 08:14:27 AM »
No, JOIN is working through command line interface like :

Code: [Select]
(command "_.JOIN" (ssname ss 0) ss "")

And it's also working like command - you use interface and type JOIN and supply your selection...

But unfortunately it's not working through ALISP routine... I've tested this on A2014...
Another quirk...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

nekonihonjin

  • Newt
  • Posts: 103
Re: Help with code - Join Lines
« Reply #4 on: January 04, 2016, 09:50:30 AM »
It is working absolutely fine for me in AUTOCAD 2015.

Maybe your osmode, orthmode and snapmode etc are not correct. Try following code :-

Code: [Select]
(defun c:xro2 ()
  (setq oldosmode (getvar 'osmode))
  (setq oldorthomode (getvar 'orthomode))
  (setq oldsnapmode (getvar 'snapmode))
  (setvar 'osmode 0)
  (setvar 'orthomode 0)
  (setvar 'snapmode 0)
  (setq EsqInfIzq (getpoint "\nEsquina Inferior Izquierda: "))
  (setq EsqSupDer (getcorner EsqInfIzq "\nEsquina Superior Derecha: "))
  (command "_line" EsqInfIzq "@1,0" "")
  (setq linea1 (entlast))
  (command "_line" EsqInfIzq "@0,1" "")
  (setq linea2 (entlast))
  (command "_line" EsqSupDer "@-1,0" "")
  (setq linea3 (entlast))
  (command "_line" EsqSupDer "@0,-1" "")
  (setq linea4 (entlast))
  (command "_filletrad" "0.0")
  (command "_.fillet" linea1 linea4)
  (command "_filletrad" "0.8")
  (command "_.fillet" linea3 linea4)
  (setq curva1 (entlast))
  (command "_.fillet" linea2 linea3)
  (setq curva2 (entlast))
  (command "_join" linea1 linea2 linea3 linea4 curva1 curva2 "")
  (setvar 'osmode oldosmode)
  (setvar 'orthomode oldorthomode)
  (setvar 'snapmode oldsnapmode)
  (princ)
)

Thanks for your answer, but it's the same result, I have autocad 2013, don't know if the version is the problem.

nekonihonjin

  • Newt
  • Posts: 103
Re: Help with code - Join Lines
« Reply #5 on: January 04, 2016, 09:53:32 AM »
You can use a polyline... then no need to join anything.

Code: [Select]
(defun c:xro3 ( / EsqInfIzq EsqSupDer enlast)
  (if (and (setq EsqInfIzq (getpoint "\nEsquina Inferior Izquierda: "))
   (setq EsqSupDer (getcorner EsqInfIzq "\nEsquina Superior Derecha: "))
   (setq enlast (entlast))
   )
    (progn
      (command "_PLINE"
       "_none" (list (car EsqSupDer) (cadr EsqInfIzq) 0.)
       "_none" EsqSupDer
       "_none" (list (car EsqInfIzq) (cadr EsqSupDer) 0.)
       "_none" EsqInfIzq
       "")
      (if (not (equal enlast (entlast)))
(command "_.FILLET" "_R" "0.8"
"_.FILLET" "_P" (entlast)
"_.PEDIT" (entlast) "_CL" ""))))
  (princ)
)


It worked perfectly, thank you very much.

ribarm

  • Gator
  • Posts: 3293
  • Marko Ribar, architect
Re: Help with code - Join Lines
« Reply #6 on: February 14, 2016, 04:03:46 AM »
No, JOIN is working through command line interface like :

Code: [Select]
(command "_.JOIN" (ssname ss 0) ss "")

And it's also working like command - you use interface and type JOIN and supply your selection...

But unfortunately it's not working through ALISP routine... I've tested this on A2014...
Another quirk...

Sorry for late reply, but only recently I've discovered that this fix works...

Code: [Select]
(defun c:jj ( / ss )
  (setq ss (ssget "_:L" '((0 . "LINE,ARC"))))
  (command "_.PEDIT" (ssname ss 0) "")
  (command "_.JOIN" (entlast) ss "")
  (princ)
)

HTH, M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Help with code - Join Lines
« Reply #7 on: February 17, 2016, 01:39:46 PM »
I have had similar issues in the past, so I wrote my own join all command, you may be able to adapt it into your code.

Here is what I use:
Code: [Select]
;Written by: Chris Wade
;Permission to use/modify/distribute is granted as long as this notice remains and all changes are documented.
(DEFUN C:ja ( / *error* uFlag doc StopLoop SelSet SelLen LoopCT)
(defun ja-work (SelSet)
(if (/= SelSet nil)
(progn
(vla-StartUndoMark doc)
(setq uFlag t)
(setq SelLen (sslength SelSet))
(setq LoopCT 0)
(while (< LoopCT SelLen)
(vl-cmdf "._join" (ssname SelSet LoopCT) SelSet "")
(setq LoopCT (+ LoopCT 1))
)
(vla-EndUndoMark doc)
(setq uFlag nil)
)
)
)
(vl-load-com)
(setq doc   (vla-get-ActiveDocument
                (vlax-get-acad-object)))
(defun *error* (msg)
(if (and uflag doc) (vla-EndUndoMark doc))
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **")))
(princ)
)
(while (= StopLoop nil)
(princ  "\nPlease select the objects that you would like to join:  ")
(setq SelSet (ssget))
(cond
((/= SelSet nil)
(ja-work SelSet)
)
(T
(setq StopLoop T)
)
)

    )
(princ)
);defun c:ja

ribarm

  • Gator
  • Posts: 3293
  • Marko Ribar, architect
Re: Help with code - Join Lines
« Reply #8 on: February 17, 2016, 03:51:32 PM »
Here are my snippets...

Code - Auto/Visual Lisp: [Select]
  1. ;; Return - LWPOLYLINE
  2. (defun c:jj1 ( / ss )
  3.   (setq ss (ssget "_:L" '((0 . "LINE,ARC"))))
  4.   (command "_.PEDIT" (ssname ss 0) "")
  5.   (command "_.JOIN" (entlast) ss "")
  6.   (princ)
  7. )
  8.  
  9. ;| This isn't working...
  10. (defun c:jj2 ( / ss )
  11.   (setq ss (ssget "_:L" '((0 . "LWPOLYLINE,LINE,ARC"))))
  12.   (command "_.PEDIT" (ssname ss 0) "")
  13.   (command "_.JOIN" (entlast) ss "")
  14.   (princ)
  15. )
  16. |;
  17.  
  18. ;; Return - SPLINE(LWPOLYLINE)
  19. (defun c:jj2 ( / ss )
  20.   (setq ss (ssget "_:L" '((0 . "LINE,ARC"))))
  21.   (sssetfirst nil ss)
  22.   (c:2ndss2spls)
  23.   (setq ssp (ssget "_I"))
  24.   (command "_.JOIN" (ssname ssp 0) ssp "")
  25.   (princ)
  26. )
  27.  
  28. ;; Return - SPLINE (2D or 3D)
  29. (defun c:jj3 ( / ss )
  30.   (setq ss (ssget "_:L" '((0 . "SPLINE"))))
  31.   (command "_.JOIN" (ssname ss 0) ss "")
  32.   (princ)
  33. )
  34.  
  35. ;;; Conclusion - You can NOT join frogs and grandmas... You can join entities with the compatibile properties...
  36.  
  37. ;;; Additional routine : (c:2ndss2spls)
  38.  
  39. (defun c:2ndss2spls ( / *error* arc2spl line2spl loop sss i ent ssss )
  40.  
  41.  
  42.   (defun *error* ( msg )
  43.     (if msg (prompt msg))
  44.     (princ)
  45.   )
  46.  
  47.   (defun arc2spl ( e / make_spline points q1 q2 a pc f pe ps w )
  48.  
  49.            (setq q1 (vlax-curve-GetStartParam e)
  50.                  q2 (vlax-curve-GetEndParam e)
  51.                  a  (/ (- (vlax-curve-GetEndParam e) (vlax-curve-GetStartParam e)) 3.0) ; a - parameter interval... and angle
  52.                  pc (mapcar                              ; pc - points on contur
  53.                       (function
  54.                         (lambda (p)
  55.                          (vlax-curve-GetPointAtParam e p)
  56.                           )
  57.                         )
  58.                       (list q1 (+ q1 a) (- q2 a) q2)
  59.                     )
  60.                  f  (mapcar                               ; f - first deriv on pc
  61.                       (function
  62.                         (lambda (p)
  63.                           (vlax-curve-GetFirstDeriv e p)
  64.                           )
  65.                         )
  66.                       (list q1 (+ q1 a) (- q2 a) q2)
  67.                     )
  68.                  pe (mapcar                              ; pe - extra control points for spline construction
  69.                       (function
  70.                         (lambda (p1 p2 d1 d2)
  71.                           (inters p1 (mapcar '+ p1 d1)
  72.                                   p2 (mapcar '+ p2 d2)
  73.                                   nil
  74.                                   )
  75.                         )
  76.                       )
  77.                      pc (cdr pc) f (cdr f)
  78.                     )
  79.                  ps  (list (car pc) (car pe) (cadr pc) (cadr pe) (caddr pc) (caddr pe) (cadddr pc)) ; ps - control points for spline
  80.                  w   (list 1.0 (cos (/ a 2)) 1.0 (cos (/ a 2)) 1.0 (cos (/ a 2)) 1.0)  ; weights for spline
  81.            )
  82.  
  83.     (defun make_spline ( pts )
  84.       (entmakex
  85.         (append
  86.            '((0 . "SPLINE") (100 . "AcDbEntity") (100 . "AcDbSpline")
  87.               (70 . 4) (71 . 2) (72 . 10) (73 . 7) (74 . 0)
  88.               (42 . 1.0e-010) (43 . 1.0e-010)
  89.               (40 . 0.0) (40 . 0.0) (40 . 0.0) (40 . 1.0) (40 . 1.0)
  90.               (40 . 2.0) (40 . 2.0) (40 . 3.0) (40 . 3.0) (40 . 3.0))
  91.            pts
  92.         )
  93.       )
  94.     )
  95.  
  96.     (defun points ( p w )
  97.       (apply 'append (mapcar '(lambda (a b) (list (cons 10 a) (cons 41 b))) p w))
  98.     )
  99.  
  100.     (entdel e)
  101.     (make_spline (points ps w))
  102.    
  103.   )
  104.  
  105.   (defun line2spl ( e / sp ep d )
  106.  
  107.     (setq sp (cdr (assoc 10 (entget e)))
  108.           ep (cdr (assoc 11 (entget e)))
  109.           d (distance sp ep)
  110.     )
  111.  
  112.     (entdel e)
  113.  
  114.     (entmakex
  115.       (list
  116.         '(0 . "SPLINE") '(100 . "AcDbEntity") '(100 . "AcDbSpline") '(210 0.0 0.0 1.0) '(71 . 1) '(73 . 2)
  117.         '(42 . 1.0e-010) '(43 . 1.0e-010) '(40 . 0.0) '(40 . 0.0) (cons 40 d) (cons 40 d) (cons 10 sp) (cons 10 ep)
  118.       )
  119.     )
  120.    
  121.   )
  122.  
  123.   (setq loop T)
  124.   (setq sss (ssget "_I"))
  125.   (if
  126.     (and
  127.       sss
  128.       (vl-some '(lambda ( x ) (wcmatch (cdr (assoc 0 (entget x))) "LINE,ARC,*POLYLINE")) (vl-remove-if 'listp (mapcar 'cadr (ssnamex sss))))
  129.     )
  130.     (setq loop nil)
  131.   )
  132.   (while loop
  133.     (setq sss (ssget "_:L" (list '(-4 . "<or") '(0 . "LINE,ARC,LWPOLYLINE") '(-4 . "<and") '(0 . "POLYLINE") '(-4 . "<or") '(70 . 0) '(70 . 1) '(70 . 8) '(70 . 9) '(70 . 128) '(70 . 129) '(-4 . "or>") '(-4 . "and>") '(-4 . "or>"))))
  134.     (if sss (setq loop nil))
  135.   )
  136.   (setq ssss (ssadd))
  137.   (repeat (setq i (sslength sss))
  138.     (setq ent (ssname sss (setq i (1- i))))
  139.     (cond
  140.       ( (eq (cdr (assoc 0 (entget ent))) "LINE")
  141.         (line2spl ent)
  142.         (ssadd (entlast) ssss)
  143.       )
  144.       ( (eq (cdr (assoc 0 (entget ent))) "ARC")
  145.         (arc2spl ent)
  146.         (ssadd (entlast) ssss)
  147.       )
  148.       ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
  149.         (sssetfirst nil (ssadd ent))
  150.         (c:lw2spl)
  151.         (ssadd (entlast) ssss)
  152.         (sssetfirst nil nil)
  153.       )
  154.       ( (and
  155.           (eq (cdr (assoc 0 (entget ent))) "POLYLINE")
  156.           (or
  157.             (eq (cdr (assoc 70 (entget ent))) 0)
  158.             (eq (cdr (assoc 70 (entget ent))) 1)
  159.             (eq (cdr (assoc 70 (entget ent))) 128)
  160.             (eq (cdr (assoc 70 (entget ent))) 129)
  161.           )
  162.         )
  163.         (command "_.CONVERTPOLY" "_L" ent)
  164.         (while (> (getvar 'cmdactive) 0) (command ""))
  165.         (sssetfirst nil (ssadd ent))
  166.         (c:lw2spl)
  167.         (ssadd (entlast) ssss)
  168.         (sssetfirst nil nil)
  169.       )
  170.       ( (and
  171.           (eq (cdr (assoc 0 (entget ent))) "POLYLINE")
  172.           (or
  173.             (eq (cdr (assoc 70 (entget ent))) 8)
  174.             (eq (cdr (assoc 70 (entget ent))) 9)
  175.           )
  176.         )
  177.         (sssetfirst nil (ssadd ent))
  178.         (c:3p2spl)
  179.         (ssadd (entlast) ssss)
  180.         (sssetfirst nil nil)
  181.       )
  182.     )
  183.   )
  184.   (sssetfirst nil ssss)
  185.   (*error* nil)
  186. )
  187.  

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube