Author Topic: Inserting Blocks on Curves  (Read 1921 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Inserting Blocks on Curves
« on: July 20, 2018, 05:59:50 PM »
I have rewritten my command for inserting blocks on lines, arcs, etc. that automatically rotates the block to align with the curve.

My new routine is now much faster and should be more reliable, but before I send it out to my users I would like to ask the experts here if I am missing anything.

I ask because I used to use some code from others and now I believe all of the code is mine, but I am wondering if maybe I managed to miss something as their code was a lot more complex.

I have attached the old code as an attachment, I have also attached the new blocks as an attachment. Any input would be greatly appreciated.

Code: [Select]
;*************************************************************************************************************************
;| VERSION HISTORY **
**
IB - VERSION 2.0 **
07/19/18 **
BY: CHRIS WADE **
**
- Insert Blocks on lines, arcs, polylines, other blocks, circles, etc. **
- Completely rewritten from scratch                                                                             **
- Runs faster                                                                                                   **
- Type: **
- W - Cycles from 3-6 wires **
- A - Changes to Arrowheads **
- C - Continuation Block **
- E - Use endpoint of object **
- O - Type block name or select block **
**
IB - VERSION 1.0 **
01/06/10 **
BY: CHRIS WADE **
**
- Insert Blocks on lines, arcs, polylines, other blocks, circles, etc. **
- Type: **
- W - Cycles from 3-5 wires **
- A - Changes to Arrowheads **
- C - Continuation Block **
- E - Use endpoint of object **
- T - Type block name. **
**
;*************************************************************************************************************************|;
(defun c:IB (/ *ThisDrawing* *Space* Scale Ent Pt1 Pt2 Pt3 Data Code Input Ang Obj Rot *Prompt* BlObj)
    (defun *error* (msg)
(eraseold BlObj)
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **")))
        (redraw)
(princ)
)
    ;Supporting Routines
    ; Convert value in radians to degrees
    (defun R2D (nbrOfRadians)
        (* 180.0 (/ nbrOfRadians pi))
    )
    ; Convert value in degrees to radians
    (defun D2R (numberOfDegrees)
        (* pi (/ numberOfDegrees 180.0))
    ) ;_ end of defun
    (defun EraseOld (Obj /);Erases the previous version of a VLA object
        (if Obj
            (progn
                (if (not (vlax-erased-p Obj))
                    (vla-delete Obj)
                )
            )
        )
    )
    ;End of Supporting Routines
    (defun GetVportScale ()
        (/ 1 (vla-get-CustomScale (vla-get-ActivePViewport (vla-get-activedocument (vlax-get-acad-object)))))
    )
    (vl-load-com)
    (princ "\n")
    (while (not Ent)
        (setq Ent (nentselp "\rSelect object to insert block on:"))
        (if
            (not
                (or (= "VERTEX" (cdr (assoc 0 (entget (car Ent)))))
                    (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list (car Ent)))))
                )
            )
            (progn
                (princ "\nInvalid object selected.\n")
                (setq Ent nil)
            )
        )
    )
    (setq Pt1 (trans (cadr Ent) 1 0)
          Obj (vlax-ename->vla-object (car Ent))
    )
    (if (not BlkName)
        (setq BlkName "3WIRE")
    )
    (if (not W)
        (setq W 3)
    )
    (command "._insert" BlkName)(command)
    (setq *ThisDrawing* (vla-get-activedocument (vlax-get-acad-object)) ;_ end of vla-get-activedocument
      *Space*
            (if (zerop (vla-get-activespace *ThisDrawing*))
                (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
                    (vla-get-modelspace *ThisDrawing*) ; active VP
                    (vla-get-paperspace *ThisDrawing*)
                )
                (vla-get-modelspace *ThisDrawing*)
            )
)
    (if (zerop (vla-get-activespace *ThisDrawing*))
        (if (= (vla-get-mspace *ThisDrawing*) :vlax-true)
            (setq Scale (GetVportScale))
            (setq Scale 1.0)
        )
        (setq Scale (getvar "DimScale"))
    )
    (princ "\n")
    (while
        (If (= Snap "_End")
            (setq *Prompt* "\rSelect insertion point [Wires/Arrow/Continuation/Other/** Endpoint **]:")
            (setq *Prompt* "\rSelect insertion point [Wires/Arrow/Continuation/Other/Endpoint]:")
        )
        (princ *Prompt*)
        (setq Input (grread t 15 0)
              Data (cadr Input)
              Code (car Input)
        )
        (cond
            ((and (= Code 2) (or (= Data 69) (= Data 101)))
                (if (= Snap "_End")
                    (setq Snap nil)
                    (setq Snap "_End")
                )
            )
            ((and (= Code 2) (or (= Data 79) (= Data 111)))
                (setq BlkName nil)
                (while (or (not BlkName) (= BlkName ""))
                    (setq BlkName (getstring T "\nEnter Block Name [enter to select file]: "))
                    (if (or (not BlkName) (= BlkName ""))
                        (progn
                            (setq BlkName (getfiled "Select a block" "H:\\0 - ACAD Support\\05 - Custom Commands" "dwg" 8))
                            (command "._insert" BlkName)(command)
                            (setq BlkName (vl-filename-base BlkName))
                        )
                    )
                )
            )
           
            ((and (= Code 2) (or (= Data 87) (= Data 119)))
                (if (< W 6)
                    (setq W (+ W 1))
                    (setq W 3)
                )
                (setq BlkName (strcat (rtos W 2 0) "WIRE")
                      Snap nil
                )
                (command "._insert" BlkName)(command)
            )
            ((and (= Code 2) (or (= Data 65) (= Data 97)))
                (setq BlkName "arw"
                      Snap nil
                )
                (command "._insert" BlkName)(command)
            )
            ((and (= Code 2) (or (= Data 67) (= Data 99)))
                (setq BlkName "cbk"
                      Snap "_End"
                )
                (command "._insert" BlkName)(command)
            )
            ((= Code 3)
                (setq blobj nil)
            )
            (T
                (setq Pt3 (trans Data 1 0)
                      Pt2 (vlax-curve-getclosestpointto Obj Pt3)
                      Ang (+ (angle Pt2 Pt3) (d2r 90))
                )
                (if Snap
                    (setq Pt2 (osnap Pt2 Snap))
                )
                (eraseold blobj)
                (setq blobj (vla-InsertBlock *Space* (vlax-3d-point Pt2) BlkName Scale Scale Scale Ang))
                (vla-put-layer BlObj (vla-get-layer Obj))
            )
        )
    )
)

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Inserting Blocks on Curves
« Reply #1 on: July 20, 2018, 07:29:44 PM »
Just a quick look ( and probably doesn't achieve much except a locked layer check )  :)  but you could simplify your 'eraseold' to:

Code - Auto/Visual Lisp: [Select]
  1. (defun eraseold (obj /)                 ;Erases the previous version of a VLA object
  2.   (and (= 'vla-object (type obj))
  3.        (not (vlax-erased-p obj))
  4.        (vlax-write-enabled-p obj)
  5.        (vla-delete obj)
  6.   )
  7. )
« Last Edit: July 20, 2018, 07:35:11 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Inserting Blocks on Curves
« Reply #2 on: July 20, 2018, 07:33:11 PM »
This part is actually a copy from another command and I had previously had that code, but under certain circumstances it caused errors, I never could figure out why, but they went away when I changed it to this one, so I figured I would stick with it for simplicity's sake.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Inserting Blocks on Curves
« Reply #3 on: July 20, 2018, 07:36:14 PM »
I added (vlax-write-enabled-p obj) .. might help.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Inserting Blocks on Curves
« Reply #4 on: July 20, 2018, 07:47:21 PM »
I added (vlax-write-enabled-p obj) .. might help.
The code above should be more robust, but if you intend on using the original:
Code - Auto/Visual Lisp: [Select]
  1.    (defun EraseOld (Obj /);Erases the previous version of a VLA object                        
  2.  (if (and obj (not (vlax-erased-p Obj)))(vla-delete Obj)))


Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Inserting Blocks on Curves
« Reply #5 on: July 20, 2018, 07:56:58 PM »
I added (vlax-write-enabled-p obj) .. might help.
The code above should be more robust, but if you intend on using the original:
Code - Auto/Visual Lisp: [Select]
  1.    (defun EraseOld (Obj /);Erases the previous version of a VLA object                        
  2.  (if (and obj (not (vlax-erased-p Obj)))(vla-delete Obj)))
The code above does indeed seem to work fine, thank you. Not really the part I was most concerned about, but every little bit helps.