Author Topic: BLOCK2END  (Read 5239 times)

0 Members and 1 Guest are viewing this topic.

Romero

  • Newt
  • Posts: 25
BLOCK2END
« on: August 01, 2019, 07:25:29 PM »
First, an apology for the audacity to ask for your help.


The reason for my publication is to ask for help with a code that I found in the following link: https://www.theswamp.org/index.php?topic=42452.0


I am very interested in the modification; and I need to insert the blocks at the end point of lines, plines, ellipses and arcs.

I am trying to modify to simply insert a block at one end (I did) by placing it at the end point.

But what I really need is that the block is inserted at the nearest end depending on the selection of plines or bows.

Maybe someone here can help me a little, I would be totally grateful. since I'm something basic in Lisp


I attach my modified version of Lisp

Code: [Select]
;;  BlockAddEOC.lsp 
;;  Block Add to End Of Curve
;;  CAB 11.13.08  Version 1.1
;;  M.R. modified 10.08.12

(defun c:block2end (/ ss s1 bname ent ename lset doc space s sa e ea i )
  (vl-load-com)
  ;;  sub to insert the block
  (defun InsertBlock (bn pt ang lay aspace)
      (setq blk (vla-insertblock
  aspace
  (vlax-3d-point pt) ; insert point
  bn ; block name
  1.  ; scale x
  1.
  1.
  ang  ; radians
)
      )
     (vla-put-layer blk lay)
    blk
  )
  ;;; Returns object to active space (ModelSpace or PaperSpace )
  ;;  CAB 05/31/07
  (defun activespace (doc)
    (if (or (= acmodelspace (vla-get-activespace doc))
            (= :vlax-true (vla-get-mspace doc)))
        (vla-get-modelspace doc)
        (vla-get-paperspace doc)
    )
  )

  (defun getss@ (pt typs lay / ss sz vs pb swp ar wsd ppdu box)
    ;;==============================================
    ;;  CAB version
    ;; do a ssget crossing the size of the pickbox at the pick point
    (setq SZ   (getvar "SCREENSIZE") ; screen size in pixels
          VS   (getvar "VIEWSIZE") ; screen height in drawing units
          PB   (getvar "pickbox") ; get current pickbox size
          SWP  (car SZ) ; width of screen in pixels
          SHP  (cadr SZ) ; height of screen in pixels
          AR   (/ SWP SHP) ; aspect ratio width/height
          WSD  (* VS AR) ; width of screen dwg units = ratio times height
          PPDU (/ WSD SWP) ; pixels per drawing unit
          BOX  (/ (* VS (* 2 PB)) SHP) ; drawing units per pixel
    )
    (setq ss (ssget "_C"
                    (polar pt 0.785 box)
                    (polar pt 3.93 box)
                    (list (cons 0 typs) (cons 8 lay))
             )
    )
  )
 
  ;;=============================
  ;;  Start Here
  (setq CurveLay "*") ; filter for curve layer
  (setq blklay "0")  ; filter for block layer
  (prompt "\nSelect block to use or ENTER key to name block.")
  (if
    (and
      (or
       (and (setq ss (ssget "_+.:E:S" '((0 . "INSERT"))))
            (setq ent (ssname ss 0))
            (setq bname (cdr(assoc 2 (entget ent)))))
       (and (setq bname (getstring t "\nName of block to copy: "))
            (or (tblsearch "Block" bname)
                (prompt (strcat "Cannot find Block " bname ", Ending."))))
      )
      (or (prompt "\nSelect curves to test.")
          (setq lset (ssget (list '(0 . "*line,ellipse,arc")(cons 8 CurveLay))))   ;filter curve in selection set
      )
    )
      (progn
        (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
              space (activespace doc))
        (vla-EndUndoMark doc)
        (vla-StartUndoMark doc)
        (vla-zoomextents (vlax-get-acad-object))
        (setq i -1)
        (while (setq ename (ssname lset (setq i (1+ i))))
          (setq ea (angle (vlax-curve-getfirstderiv (vlax-ename->vla-object ename) (vlax-curve-getendparam (vlax-ename->vla-object ename))) '(0.0 0.0 0.0))
                 e (vlax-curve-getendpoint (vlax-ename->vla-object ename)))
                    (if (= (sslength (getss@ e "*LINE,ELLIPSE,ARC" Curvelay)) 1)
            (InsertBlock bname e ea blklay space)
          )
        )
        (vla-zoomprevious (vlax-get-acad-object))
        (vla-EndUndoMark doc)
      )
  ) ; endif

 (princ "\nThe blocks have been copied")
 (princ)               
)
(princ "\nType block2end to invoke")
(princ)



and dwg file for more details

BIGAL

  • Swamp Rat
  • Posts: 1414
  • 40 + years of using Autocad
Re: BLOCK2END
« Reply #1 on: August 01, 2019, 10:02:08 PM »
You can in simple terms check the distance from a point to the endpoint startpoint and use that to swap if required what you need as start point. As your using a window select say centre point of window and be a little bit tighter on the window pick.

Code: [Select]
(setq d1 (distance endpt pt2))
(setq d2 (distance startpoint pt2))
    (if (> d1 d2)
    (progn
    (setq temp endpt)
    (setq endpt startpoint)
    (setq startpoint temp)
    )
    )
A man who never made a mistake never made anything

Romero

  • Newt
  • Posts: 25
Re: BLOCK2END
« Reply #2 on: August 05, 2019, 10:43:21 PM »
You can in simple terms check the distance from a point to the endpoint startpoint and use that to swap if required what you need as start point. As your using a window select say centre point of window and be a little bit tighter on the window pick.

Code: [Select]
(setq d1 (distance endpt pt2))
(setq d2 (distance startpoint pt2))
    (if (> d1 d2)
    (progn
    (setq temp endpt)
    (setq endpt startpoint)
    (setq startpoint temp)
    )
    )


Muchas gracias por señalarme el camino. Sin embargo, sigo intentándolo y no puedo hacerlo funcionar como deseo. Quizás puedas ayudarme un poco más. Estaría muy agradecido

Gasty

  • Newt
  • Posts: 90
Re: BLOCK2END
« Reply #3 on: August 06, 2019, 07:45:45 PM »
Hola Romero:

Una forma más robusta de asegurarte de tomar los puntos correctos es utilizar una prueba de "co0ntainment", es decir ver si los puntos extremos están al interior de la caja de selección, para eso basta comprobar si las coordenadas x,y de los puntos extremos están en el rango (xmin,xmax) para  y (ymin, ymax) para y ,que son las coordenadas extremas abajo izquierda (xmin, ymin) y arriba derecha (xmax,ymax)  de la caja de selección. Ahora, la caja de selección se puede obtener con un getcorner, y le pasas los puntos (el punto base de getcorner y el devuelto por getcorner) a un ssget crossing filtrado.

Saludos,

Gaston Nunez

ronjonp

  • Needs a day job
  • Posts: 7529
Re: BLOCK2END
« Reply #4 on: August 07, 2019, 11:58:28 AM »
Give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ _a a b e p p1 p2 s)
  2.   ;; RJP » 2019-08-07
  3.   (cond ((null (tblobjname "block" "FL1")) (print "Block 'FL1' is not defined in this drawing!"))
  4.         ((and (setq p1 (getpoint "\nPick first corner: "))
  5.               (setq p2 (getcorner p1 "\nPick second corner: "))
  6.               (setq s (ssget "_C" p1 p2 '((0 . "arc,line,*polyline"))))
  7.          )
  8.          (setq p1 (mapcar '/ (mapcar '+ p1 p2) '(2. 2. 2.)))
  9.          (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
  10.            (setq a (list (setq p (vlax-curve-getstartpoint x)) (_a x p)))
  11.            (setq b (list (setq p (vlax-curve-getendpoint x)) (+ pi (_a x p))))
  12.            (and (> (distance p1 (car a)) (distance p1 (car b))) (setq a b))
  13.            (setq e (entmakex (list '(0 . "INSERT")
  14.                                    '(100 . "AcDbEntity")
  15.                                    '(8 . "Arrow")
  16.                                    '(100 . "AcDbBlockReference")
  17.                                    '(2 . "FL1")
  18.                                    (cons 10 (car a))
  19.                                    (cons 50 (cadr a))
  20.                              )
  21.                    )
  22.            )
  23.            (grdraw p1 (car a) 3)
  24.            (redraw e 3)
  25.          )
  26.         )
  27.   )
  28.   (princ)
  29. )
  30.  
« Last Edit: August 07, 2019, 12:26:24 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

GDF

  • Water Moccasin
  • Posts: 2081
Re: BLOCK2END
« Reply #5 on: August 07, 2019, 12:12:14 PM »
Nice one Ron...
I would add a redraw to the end
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

ronjonp

  • Needs a day job
  • Posts: 7529
Re: BLOCK2END
« Reply #6 on: August 07, 2019, 12:26:40 PM »
Good call Gary .. now they glow!

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Romero

  • Newt
  • Posts: 25
Re: BLOCK2END
« Reply #7 on: August 07, 2019, 11:19:37 PM »
Give this a try:

Ronjonp thank you very much for the code. :-D The truth is that it works perfectly. My sincere recognition. I'm in debt to you. You saved me hours of hard work. :yay!:

ronjonp

  • Needs a day job
  • Posts: 7529
Re: BLOCK2END
« Reply #8 on: August 08, 2019, 12:00:50 AM »
Give this a try:

Ronjonp thank you very much for the code. :-D The truth is that it works perfectly. My sincere recognition. I'm in debt to you. You saved me hours of hard work. :yay!:
Glad to help.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Romero

  • Newt
  • Posts: 25
Re: BLOCK2END
« Reply #9 on: August 08, 2019, 06:20:37 PM »
Give this a try:

Ronjonp thank you very much for the code. :-D The truth is that it works perfectly. My sincere recognition. I'm in debt to you. You saved me hours of hard work. :yay!:
Glad to help.  :-)

Ronjon


Hi Ronjonp, I hope and I don't bother you with my comments, but abusing your trust, I want to know if you can help me with two requests regarding the code.

1.- Is it possible that the user can designate a block in the drawing?  :idea:
 (sometimes I have to use other blocks for the same purpose)

I know I can edit the code and write the name of the block. But I would have to have multiple codes for each of the blocks and I would not like that.

2.- What is the possibility that the blocks obtain the layer of the selected object? :reallysad:

I really appreciate the help.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: BLOCK2END
« Reply #10 on: August 09, 2019, 02:12:36 PM »
Give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ _a a b bn e l p p1 p2 s)
  2.   ;; RJP » 2019-08-07
  3.   (cond
  4.     ((null
  5.        (and (setq e (car (entsel "\nPick a block to use: "))) (setq bn (cdr (assoc 2 (entget e)))))
  6.      )
  7.      (print "Bye")
  8.     )
  9.     ((and (setq p1 (getpoint "\nPick first corner: "))
  10.           (setq p2 (getcorner p1 "\nPick second corner: "))
  11.           (setq s (ssget "_C" p1 p2 '((0 . "arc,line,*polyline"))))
  12.      )
  13.      (setq p1 (mapcar '/ (mapcar '+ p1 p2) '(2. 2. 2.)))
  14.      (setq l (cdr (assoc 8 (entget e))))
  15.      (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
  16.        (setq a (list (setq p (vlax-curve-getstartpoint x)) (_a x p)))
  17.        (setq b (list (setq p (vlax-curve-getendpoint x)) (+ pi (_a x p))))
  18.        (and (> (distance p1 (car a)) (distance p1 (car b))) (setq a b))
  19.        (setq e (entmakex (list '(0 . "INSERT")
  20.                                '(100 . "AcDbEntity")
  21.                                (cons 8 l)
  22.                                '(100 . "AcDbBlockReference")
  23.                                (cons 2 bn)
  24.                                (cons 10 (car a))
  25.                                (cons 50 (cadr a))
  26.                          )
  27.                )
  28.        )
  29.        (grdraw p1 (car a) 3)
  30.        (redraw e 3)
  31.      )
  32.     )
  33.   )
  34.   (princ)
  35. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Romero

  • Newt
  • Posts: 25
Re: BLOCK2END
« Reply #11 on: August 09, 2019, 04:59:11 PM »
Give this a try:


Ronjon thank you very much for your help. Just what I needed, Everything is going very well now. cheers  :yay!:

ronjonp

  • Needs a day job
  • Posts: 7529
Re: BLOCK2END
« Reply #12 on: August 10, 2019, 12:00:58 AM »
Give this a try:


Ronjon thank you very much for your help. Just what I needed, Everything is going very well now. cheers  :yay!:
Glad to help. 🙂

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

BIGAL

  • Swamp Rat
  • Posts: 1414
  • 40 + years of using Autocad
Re: BLOCK2END
« Reply #13 on: August 10, 2019, 01:24:26 AM »
Hi ronjonp was going to post something rather than using rectangle use a fence option that just drags a pline across the lines-plines etc (ssget "F" pts then compare the intersectwith point to determine start or end point. This means can do more random choice that a window would not allow. I accidently overwrote what I had should not call a lisp test.lsp.
A man who never made a mistake never made anything