Script to -INSERT a block

Script to -INSERT a block

arshadmirza786
Collaborator Collaborator
2,081 Views
16 Replies
Message 1 of 17

Script to -INSERT a block

arshadmirza786
Collaborator
Collaborator

Trying to -INSERT a "ARROW" block with a script

-insert arrow 3263923.0305,1263407.6526   246.7 

 

when copy on command line it says 

arshadmirza786_0-1727681263368.png

 

How to overcome this "Substitute block name" dialoge box and run the SCRIPT

 

0 Likes
Accepted solutions (2)
2,082 Views
16 Replies
Replies (16)
Message 2 of 17

ryanatkins49056
Enthusiast
Enthusiast

I've had similar issues with resurrecting old blocks I find on shared drives. My issues were mostly because of weird characters that were used in the file name or even spaces (which I suspect may be the case here).  More info on your dialog box can be found here

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Error-message-The-...

 

I dived into your drawing and while there is a block called "ARROW" it is nested within another block. The top level block has a random string assigned to it. You have a few options. Assuming that the file you shared is the one your pulling the block from, open that file, select the block and use "EXPLODE". It should still come up as a block when you check the properties pallet but the name will now read "ARROW". Perform the "PURGE" command and purge all unused block references which should have that random string named block listed. Save the drawing and you should now be good to go.

 

Another option is to get a new file and create the block from scratch.

 

Keep me posted about your results.

0 Likes
Message 3 of 17

arshadmirza786
Collaborator
Collaborator

arshadmirza786_0-1727696639791.png

Thanks for a good advise but sotion is same

if u see the command line after writing arrow, it does not goto next line to get the coordinates 

I think thats problem

0 Likes
Message 4 of 17

paullimapa
Mentor
Mentor

Assuming the 246.7 is rotation try lisp code

(command "-insert" "arrow" 3263923.0305,1263407.6526 "" "" 246.7)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 17

paullimapa
Mentor
Mentor

Another thing to try is surround the name of block with quotes 

-insert "arrow" 3263923.0305,1263407.6526 1 1 246.7 

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 17

arshadmirza786
Collaborator
Collaborator

Coordinates, Rotation angle Data is in a EXCEL File 

How to call excel cells in this line , as these a 100s line Excel sheet 

0 Likes
Message 7 of 17

paullimapa
Mentor
Mentor

Include a sample excel file so others here can jump in to come up with code to read each line and insert into AutoCAD. Also you can open excel file do a find and replace arrow with “arrow” and try copy and paste then


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 17

Kent1Cooper
Consultant
Consultant

If this is a Script, then use Enter to a new line after the "arrow" Block name, rather than a space.  Block names can contain spaces [there was a time when they could not, which I thought was better], and those are honored in a Script, so it thinks your coordinates etc. are part of the intended Block name, which therefore includes characters not allowed in Block names [see Help about the -BLOCK command and the EXTNAMES System Variable].

Kent Cooper, AIA
0 Likes
Message 9 of 17

arshadmirza786
Collaborator
Collaborator

Pls look in xls file 

Pline column works perfect but -INSERT does not work as explained earlier

0 Likes
Message 10 of 17

paullimapa
Mentor
Mentor

Copy & pasting the PLINE command works because the coordinates are what's requested immediately following the command sequence:

pline 3263923.0305,1263407.6526 @14.184<-156.7 

But when it comes to the INSERT command (assuming your drawing does have a block named "arrow") since AutoCAD supports long file names which includes spaces it does not know when the block name ends.

This is why the Insert line from your Excel file does not work. AutoCAD thinks you want to insert a block name that includes all those numbers following arrow:

-insert arrow 3263923.0305,1263407.6526 -156.7 

The way to do this is to use lisp which surrounds the block name along with the other entries with quotes like this:

(command "insert" "arrow" "3263923.0305,1263407.6526" "1" "1" "-156.7")

Now in a Script file the way to make it work would be to separate each entry into a different line like this:

-insert
arrow
3263923.0305,1263407.6526
1
1
-156.7

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 17

arshadmirza786
Collaborator
Collaborator

How to WRITE in EXCEL cell ??? 

OR is there a SYSTEM VERIABLE to change the LONG FILE NAME to a old short names

so acad not use a long name

0 Likes
Message 12 of 17

paullimapa
Mentor
Mentor
Accepted solution

No variable that I'm aware of to force AutoCAD to only take short block names.

As for Excel, you can enter the cell like this:

="(command ""-insert"" ""arrow"" """&$G6&","&$H6&""" ""1"" ""1"" "&$F6&")"

paullimapa_3-1727768384709.png

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 17

Sea-Haven
Mentor
Mentor

There is 2 ways to do this write a macro in excel that has 2 function do a pline and insert a block. 

 

The second for me is read the cells direct from excel and draw the pline then add the block.

 

In saying above there is a draw pline with arrow head it uses the method of changing the pline width as you construct the 2 part pline. Much easier than having 2 objects. It uses, pline startpt w 0 0 nextpoint w arrowsize 0 next point. Try it.

SeaHaven_0-1727770902585.png

Added to "my to do" will read direct excel.

 

 

 

 

 

0 Likes
Message 14 of 17

arshadmirza786
Collaborator
Collaborator
Accepted solution

The ACAD says

Command: (command "-insert" "arrow" "3263961.1069,1263279.6398" "1" "1" -211)
-insert Enter block name or [?] <arrow>: arrow Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]: 3263961.1069,1263279.6398
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>: 1 Enter Y scale factor <use X scale factor>: 1
Specify rotation angle <0>: -211
Command: nil

 

NOTHING on screen to show

0 Likes
Message 15 of 17

Sea-Haven
Mentor
Mentor

Did you read what I posted ? Don't need a arrow block. I just need time working on some thing else at moment.

Copy and paste this to command line into a blank dwg, zoom in around 0,0 and should see a arrow pline.

 

(command "pline" "0,0" "w" 0 0 "100,0" "w" 5 0 "110,0" "")

 

Change the variable 5 that is arrow head size.

 

All the info is in the Excel file just have it open before running a lisp that reads the cells. Watch this space.

0 Likes
Message 16 of 17

Sea-Haven
Mentor
Mentor

Try this, just open the excel, load the program, select the range, go back to CAD should be done.

 

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/script-to-insert-a-block/td-p/13054011


((defun c:wow ( / myxl oldsnap ahead x val pt dist ang pt2 pt3)

(defun dtr (a)
(* pi (/ a 180.0))
)


; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Str$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str$ / Num#)
  (if (= 0 (setq Num# (strlen Str$)))
    0
    (+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
       (Alpha2Number (substr Str$ 2))
    )
  )
)

; ColumnRow - Returns a list of the Column and Row number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Cell$ = Cell ID
; Syntax example: (ColumnRow "ABC987") = '(731 987)
;default to "A1" if there's a problem
;-------------------------------------------------------------------------------
(defun ColumnRow (Cell$ / Column$ Char$ Row#)
  (setq Column$ "")
  (while (< 64 (ascii (setq Char$ (strcase (substr Cell$ 1 1)))) 91)
    (setq Column$ (strcat Column$ Char$)
          Cell$ (substr Cell$ 2)
    )
  )
  (if (and (/= Column$ "") (numberp (setq Row# (read Cell$))))
    (list (Alpha2Number Column$) Row#)
    '(1 1)
  )
)


; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
  (if (< Num# 27)
    (chr (+ 64 Num#))
    (if (= 0 (setq Val# (rem Num# 26)))
      (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
      (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
    )
  )
);defun Number2Alpha

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma 9 is tab 34 is space 58 is colon
(defun _csv->lst58 ( str / pos )
	(if (setq pos (vl-string-position 58 str))
		(cons (substr str 1 pos) (_csv->lst58 (substr str (+ pos 2))))
		(list str)
    )
)

; this code was done by the great FIXO
; no longer with us
(defun getrangexl2 ( / lst UR CR RADD )
(vl-catch-all-error-p
	   (setq Rng
		  (vl-catch-all-apply
		    (function (lambda ()
				(vlax-variant-value
				  (vlax-invoke-method
				    (vlax-get-property myxl 'Application)
				    'Inputbox
				    "Select a Range: "
				    "Range Selection "
				    nil
				    nil
				    nil
				    nil
				    nil
				    8))))))
)
(setq xrng (vlax-get-property rng "address"))
(setq xxrng xrng)
(repeat 4 (setq xxrng(vl-string-subst "" "$" xxrng)))
(setq xxxrng (_csv->lst58 xxrng))
(setq rngst (columnrow (nth 0 xxxrng)) rngend (columnrow (nth 1 xxxrng)))
(setq *ExcelData@ nil )
(setq Row# (nth 1 rngst))
(repeat (+ (- (nth 1 rngend)(nth 1 rngst) ) 1)
(setq Data@ nil)
(setq Column# (nth 0 rngst))
(repeat (+  (- (nth  0 rngend)(nth 0 rngst) ) 1)
(setq Range$ (strcat (Number2Alpha Column#)(itoa Row#)))
(setq ExcelRange (vlax-get-property myxl "Range" range$))
(setq ExcelVariant (vlax-get-property ExcelRange 'Value))
(setq ExcelValue (vlax-variant-value ExcelVariant))
(setq Data@ (append Data@ (list ExcelValue)))
(setq Column# (1+ Column#))
)
(setq *ExcelData@ (append *ExcelData@ (list Data@)))
(setq Row# (1+ Row#))
)
(princ *ExcelData@)
(princ)
)

(or (setq myxl (vlax-get-object "Excel.Application"))
    (setq myxl (vlax-get-or-create-object "excel.Application"))
)
(vla-put-visible myXL :vlax-true)
(vlax-put-property myxl 'ScreenUpdating :vlax-true)
(vlax-put-property myXL 'DisplayAlerts :vlax-true)

(alert "Select the range of values start at say A6 end IX \n\n dont include average \n Press ok. then go to Excel")
(getrangexl2)

(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)

(setq ahead 1 x 1)
(foreach val *ExcelData@
(princ (setq x (1+ x)))
(setq pt (list (nth 6 val) (nth 7 val)))
(setq dist (nth 3 val) ang (nth 5 val))
(setq pt2 (polar pt (dtr ang) dist))
(setq pt3 (polar pt2 (+ (dtr ang) pi) 3))
(command "pline" pt "w" 0.0 0.0 pt3 "w" ahead 0.0 pt2 "")
)

(command "zoom" "e")

(setvar 'osmode oldsnap)
(princ)

)

 

 

SeaHaven_0-1727782332457.png

 

SeaHaven_1-1727782651960.png

 

 

0 Likes
Message 17 of 17

paullimapa
Mentor
Mentor

Now enter command 

zoom

e

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes