Jump to content

Recommended Posts

Posted
…if I create a set with multiple blocks do I then need another loop inside that that will cycle through the block names and insert all blocks for each name?

 

Yes. In this case, you don’t need to worry about the associated pick point for each object, just the names of the entities in the set. Let’s name it blockset.

(while
 (not (and 
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
 )     )
)

Then, the first one can be retrieved using

(ssname blockset 0)

; the 2nd, by

(ssname blockset 1)

, up to

 (1- (sslength blockset))

Thus, your loop should go from 0 through the length of the selection set minus one, or vice versa. Here, repeat will work great. If you go backward,

(repeat (setq Index (sslength blockset))
 (setq
    Index (1- Index)
    bn (cdr (assoc 2 (entget (ssname blockset Index))))
 )
 ; do what you need with bn here, then repeat with next entity’s block name
) 

FYI: You should not have to worry about it here, but there used to (and still may) be an integer limit for this form, in which case working backward with while will work. This may be dated, as is a lot of stuff I “know”!:ouch:

  • Replies 36
  • Created
  • Last Reply

Top Posters In This Topic

  • woodman78

    21

  • BIGAL

    7

  • jammie

    4

  • neophoible

    4

Top Posters In This Topic

Posted Images

Posted

neophoible & LeeMac,

Thanks for your help. I got it to work. Excellent.

One thing is that I am getting an error "; error: too many arguments" after it completes.

The other thing is how to copy the imported map tiles to another blank drawing. Bigal was on about using a script but I couldn't figure out how to get it working. Any ideas?

 

(defun c:Getmaptile  (/ bn chk pp bnloc blockset)
(while (not (and
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
)
(repeat (setq Index (sslength blockset))
 (setq
    Index (1- Index)
    bn (cdr (assoc 2 (entget (ssname blockset Index))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "c:\\1\\" bn "*.dwg")))
(foreach Dwg Drawings
   (command "_.insert" Dwg "0,0,0" "1" "1" "0") 
 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "c:\\1\\" bn "*.dwg")))
(foreach Dwg Drawings
   (command "_.insert" Dwg "0,0,0" "1" "1" "0")
 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "c:\\1\\" bn "*.dwg")))
(foreach Dwg Drawings
   (command "_.insert" Dwg "0,0,0" "1" "1" "0") 
 )
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to list files from a wildcard match
(defun matchfiles (path / item dir match lst fil files)
 (setq path (strsplit "," path))
 (foreach item path
   (setq dir   (strcat (vl-string-right-trim "\\" (vl-filename-directory item)) "\\")
         match (substr item (1+ (strlen dir)))
         lst   (vl-directory-files dir match 1)
   )
   (foreach fil lst
     (setq files (cons (strcat dir fil) files))
   )
 )
 (reverse files)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to split a string on a specified character
(defun strsplit (c str / lst p n)
 (setq n 0)
 (while (setq p (vl-string-search c str n))
   (setq lst (cons (substr str (1+ n) (- p n)) lst))
   (setq n (1+ p))
 )
 (if (< n (strlen str))
   (setq lst (cons (substr str (1+ n)) lst))
 )
 (reverse lst)
)

 

Thanks again.

Posted

What I was getting at with a script is run your pick tile from a preset dwg with the larger grid, rather than inserting into this dwg use the lisp to write a script file with all the insert lines in it, the first line in the script is very simple, like wise the last line of your lisp is (command "script" "MYGRIDS")

 

So where you have a foreach dwg just change the (command to (writeline but you will have to Strcat the next lot of text

 

"NEW"

 

Insert maprgrdid1 etc

Insert maprgrdid2 etc

Insert maprgrdid3 etc

 

So where you have a foreach dwg just change the (command to (writeline but you will have to Strcat the next lot of text

(setq fileout (open "c:\mydirectory\mygrids" "W"))
(writeline "NEW" fileout)
(writeline " " fileout)
(writeline " " fileout)
(foreach Dwg Drawings
   (writeline (strcat  "_.insert" Dwg "0,0,0" "1" "1" "0") Fileout)
)

(close fileout)

Posted

Bigal,

Thanks for your help again. I'm a bit slow at getting this stuff. Anyway, I tried again to add your code and this is what I have so far:

 

(defun c:Getmaptile  (/ bn chk pp bnloc blockset fout dwgtile)
[color="red"](setq fout (open "C:\\b\\tiles.scr" "w"))
(setvar "filedia" 0)
(write-line "new  " fout) ; note 2 spaces check for using default dwt etc[/color]
(while (not (and
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
)
(repeat (setq Index (sslength blockset))
 (setq
    Index (1- Index)
    bn (cdr (assoc 2 (entget (ssname blockset Index))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\5000\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0") 
[color="red"](setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
 )
(close fout)
)[/color]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\2500\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0")
[color="red"](setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
 )
(close fout)
 )[/color]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\1000\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0") 
[color="red"](setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
 )
(close fout)
 )[/color]



(princ)
[color="red"](command "script" "C:\\b\\tiles")[/color]
(setvar "filedia" 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to list files from a wildcard match
(defun matchfiles (path / item dir match lst fil files)
 (setq path (strsplit "," path))
 (foreach item path
   (setq dir   (strcat (vl-string-right-trim "\\" (vl-filename-directory item)) "\\")
         match (substr item (1+ (strlen dir)))
         lst   (vl-directory-files dir match 1)
   )
   (foreach fil lst
     (setq files (cons (strcat dir fil) files))
   )
 )
 (reverse files)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to split a string on a specified character
(defun strsplit (c str / lst p n)
 (setq n 0)
 (while (setq p (vl-string-search c str n))
   (setq lst (cons (substr str (1+ n) (- p n)) lst))
   (setq n (1+ p))
 )
 (if (< n (strlen str))
   (setq lst (cons (substr str (1+ n)) lst))
 )
 (reverse lst)
)

 

I have made progress in that now the script file is being created but the only thing in it is the word "new". This is the error I get at the command line:

 

Command: GETMAPTILE
Select block(s)...
Select objects: 1 found

Select objects:
; error: stream is closed: #<FILE internal>

Command:

 

Can you see where I am going wrong? I don't understand how the write-line works and so don't know if I have incorporated it correctly.

 

Thanks again,

Woodman78

Posted

I have twigged the script file business and think I am beginning to understand it. This is the code I am using but the line

 

(setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))

 

generates a line in the script file that I think will not run. From my understanding the elements to run need to be on separate lines. So I have tried to do this but after the above line the code stops. Can any one help?

 

(defun c:Getmaptile  (/ bn chk pp bnloc blockset fout dwgtile)
(setq fout (open "C:\\b\\tiles.scr" "w"))
(setvar "filedia" 0)
(write-line "filedia" fout)
(write-line "0" fout)
(write-line "new" fout) ; note 2 spaces check for using default dwt etc
(write-line "T:\\Drawing Tools\\Templates\\CCC Blank Template ISO 01.dwt" fout) ; note 2 spaces check for using default dwt etc
(while (not (and
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
)
(repeat (setq Index (sslength blockset))
 (setq
    Index (1- Index)
    bn (cdr (assoc 2 (entget (ssname blockset Index))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\5000\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0") 
(setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
(Write-line "_.insert" fout)
 )
;(close fout)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\2500\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0")
(setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
 )
;(close fout)
 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\1000\\" bn "*.dwg")))
(foreach Dwg Drawings
   ;(command "_.insert" Dwg "0,0,0" "1" "1" "0") 
(setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(Write-line dwgtile fout)
 )
(close fout)
 )



(princ)
(command "script" "C:\\b\\tiles")
(setvar "filedia" 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(princ)
)

 

 

Script file written from the code:

filedia
0
new
T:\Drawing Tools\Templates\CCC Blank Template ISO 01.dwt
_.insertM:\os\Maps\AutoCAD\2500\6301-A.dwg0,0,0110
_.insertM:\os\Maps\AutoCAD\2500\6301-B.dwg0,0,0110
_.insertM:\os\Maps\AutoCAD\2500\6301-C.dwg0,0,0110
_.insertM:\os\Maps\AutoCAD\2500\6301-D.dwg0,0,0110

 

Thanks.

Posted

I have made progress. I have now have this:

 

(defun c:Getmaptile  (/ bn chk pp bnloc blockset fout dwgtile)
(setq fout (open "C:\\b\\tiles.scr" "w"))
;(setvar "filedia" 0)
;(write-line "filedia" fout)
;(write-line "0" fout)
(write-line "new" fout) ; note 2 spaces check for using default dwt etc
(write-line "acadiso.dwt" fout)
;(write-line "T:\\Drawing Tools\\Templates\\CCC Blank Template ISO 01.dwt" fout) ; note 2 spaces check for using 

(while (not (and
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
)
(repeat (setq Index (sslength blockset))
 (setq
    Index (1- Index)
    bn (cdr (assoc 2 (entget (ssname blockset Index))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq Drawings (matchfiles (strcat "C:\\1\\" bn "*.dwg")))
(foreach Dwg Drawings
(Write-line "_.insert" fout)
(Write-line Dwg fout)
(Write-line "0,0,0" fout)
(Write-line "1" fout)
(Write-line "1" fout)
(Write-line "0" fout)
)
(close fout)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

)

(command "_script" "C:\\b\\tiles.scr")
)
(princ)

;(setvar "filedia" 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to list files from a wildcard match
(defun matchfiles (path / item dir match lst fil files)
 (setq path (strsplit "," path))
 (foreach item path
   (setq dir   (strcat (vl-string-right-trim "\\" (vl-filename-directory item)) "\\")
         match (substr item (1+ (strlen dir)))
         lst   (vl-directory-files dir match 1)
   )
   (foreach fil lst
     (setq files (cons (strcat dir fil) files))
   )
 )
 (reverse files)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to split a string on a specified character
(defun strsplit (c str / lst p n)
 (setq n 0)
 (while (setq p (vl-string-search c str n))
   (setq lst (cons (substr str (1+ n) (- p n)) lst))
   (setq n (1+ p))
 )
 (if (< n (strlen str))
   (setq lst (cons (substr str (1+ n)) lst))
 )
 (reverse lst)
)

 

Which creates the script:

new
acadiso.dwt
_.insert
C:\1\6031-01.dwg
0,0,0
1
1
0
_.insert
C:\1\6031-25.dwg
0,0,0
1
1
0
_.insert
C:\1\6031-D.dwg
0,0,0
1
1
0
_.insert
C:\1\6031.dwg
0,0,0
1
1
0

 

But I get an error "too many arguments".

 

Any ideas? The new drawing isn't created.

Posted

My fault you can have all on 1 line forgot about the space bewteen values which is same as a return

 

[color=#ff0000](setq dwgtile (strcat "_.insert" dwg "0,0,0" "1" "1" "0"))
(setq spc " ")[/color]
[color=#ff0000](setq dwgtile (strcat "_.insert" spc dwg spc "0,0,0" spc  "1" spc  "1" spc "0"))
[/color]

Posted

Can anyone point out why I am getting an error "too many arguments"? The script file is being written but not running.

 

Thanks.

Posted

Can someone please help with this? I am frustrated with it at this stage. I have made many baby steps but have finally hit a wall. The lisp creates the script file but then gives the error

 

(defun c:Getmaptile  (/ bn chk pp bnloc blockset)
 (vl-load-com)
(setq fout (open "C:\\b\\tiles.scr" "w"))
(write-line "filedia" fout)
(write-line "0" fout)  
(write-line "new" fout) ; note 2 spaces check for using default dwt etc
(write-line "C:\\b\\CCC_Blank_Template_ISO_Map.dwt" fout)
 (while (not (and
   (princ "Select block(s)...")
   (setq blockset (ssget (list (cons 0 "INSERT"))))
)
(repeat (setq Index (sslength blockset))
 	(setq
    	Index (1- Index)
    	bn (cdr (assoc 2 (entget (ssname blockset Index))))
	)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\5000\\" bn "*.dwg")))
		(foreach Dwg Drawings
    		(Write-line "insert" fout)
		(Write-line Dwg fout)
		(Write-line "0,0,0" fout)
		(Write-line "1" fout)
		(Write-line "1" fout)
		(Write-line "0" fout) 
  		)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\2500\\" bn "*.dwg")))
		(foreach Dwg Drawings
    		(Write-line "insert" fout)
		(Write-line Dwg fout)
		(Write-line "0,0,0" fout)
		(Write-line "1" fout)
		(Write-line "1" fout)
		(Write-line "0" fout) 
 			)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	(setq Drawings (matchfiles (strcat "M:\\os\\Maps\\AutoCAD\\1000\\" bn "*.dwg")))
		(foreach Dwg Drawings
    		(Write-line "insert" fout)
		(Write-line Dwg fout)
		(Write-line "0,0,0" fout)
		(Write-line "1" fout)
		(Write-line "1" fout)
		(Write-line "0" fout) 
 			)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
);End Repeat
 (write-line "filedia" fout)
 (write-line "1" fout)
 (close fout)
 (command "script" "C:\\b\\tiles.scr") 
);End Not
);End While
(princ)
);End Getmaptile

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to list files from a wildcard match
(defun matchfiles (path / item dir match lst fil files)
 (setq path (strsplit "," path))
 (foreach item path
   (setq dir   (strcat (vl-string-right-trim "\\" (vl-filename-directory item)) "\\")
         match (substr item (1+ (strlen dir)))
         lst   (vl-directory-files dir match 1)
   )
   (foreach fil lst
     (setq files (cons (strcat dir fil) files))
   )
 )
 (reverse files)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to split a string on a specified character
(defun strsplit (c str / lst p n)
 (setq n 0)
 (while (setq p (vl-string-search c str n))
   (setq lst (cons (substr str (1+ n) (- p n)) lst))
   (setq n (1+ p))
 )
 (if (< n (strlen str))
   (setq lst (cons (substr str (1+ n)) lst))
 )
 (reverse lst)
)

 

"C:\b\tiles.scr":  Can't open file
Command: ; error: too many arguments

 

When I try to run the script file manually from within CAD I get an error "Can't open file" but if I copy the file and give it a new name it runs perfectly. I am thinking that the script isn't being released or closed off properly in some way but I can't see it.

 

I would appreciate it if someone could help.

 

Edit:The script file

 

filedia

0

new

C:\b\CCC_Blank_Template_ISO_Map.dwt

insert

M:\os\Maps\AutoCAD\2500\6301-A.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\2500\6301-B.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\2500\6301-C.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\2500\6301-D.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-03.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-04.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-08.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-09.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-13.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-14.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-18.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-19.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-23.dwg

0,0,0

1

1

0

insert

M:\os\Maps\AutoCAD\1000\6301-24.dwg

0,0,0

1

1

0

filedia

1

 

Thanks in advance.

Posted

Hi woodman,

 

You've made good progress with your program, well done! I believe you are getting close.

 

Just a small note on using script files in an AutoLISP program. Script files don't behave as you'd imagine when AutoCAD processes a lisp file. If my memory serves me correctly a script file will be the last item processed regardless of its call location in a lisp file. This could cause a problem if you need a script file to run mid way during a LISP routine. It won't run until after all the other parts of the program complete. I think the help files have a better explanation for this feature.

 

The issue could be towards the end of the program by tring to create drawings via multiple script calls. It looks like the script file is being called in the while loop which means AutoCAD will attempt to process it multiple times.

 

  (write-line "filedia" fout)
 (write-line "1" fout)
 (close fout)
[i][color="navy"] (command "script" "C:\\b\\tiles.scr") [/color][/i]
);End Not
);End While
(princ)
);

Posted

Thanks Jammie I didnt bother to check the code to tight just a how method, the position of the run script must be the last lisp item to be run as you say.

Posted

Thanks guys but when I do that I get the error "too many arguments" and the script won't run.

What could be causing this?

Posted

I have set the location of the script call to be the last item in the lisp but I am still getting the "too many arguments". Can someone please help to point out my mistake with this? I can't see where I have gone wrong.

 

    );End Repeat
 (write-line "filedia" fout)
 (write-line "1" fout)
);End Not
);End While
 (close fout)
(command "script" "C:\\b\\tiles.scr")
(princ)
);End Getmaptile

Posted

Hi woodman,

 

See the following. Hopefully it should work for you. I've made a few slight changes to your code but the core is your work to date. Have a review and if you've any questions let me know

 


(defun c:Getmaptile  (/ bn chk pp bnloc blockset fout dwgtile scr  rootDir template)

 
(setq rootDir "M:\\os\\Maps\\AutoCAD\\"
     template "T:\\Drawing Tools\\Templates\\CCC Blank Template ISO 01.dwt"
     subFolders (list "5000\\" "1000\\" "2500\\"))
   
 (princ "Select block(s)...")

 (if

   (setq blockset (ssget (list (cons 0 "INSERT"))))

   (progn

     (setq fout (open "C:\\b\\tiles.scr"  "w"))
     (setvar "filedia" 0)
     (write-line "filedia" fout)
     (write-line "0" fout)
     (write-line "new" fout) ; note 2 spaces check for using default dwt etc
     (write-line  template fout) ; note 2 spaces check for using default dwt etc

     (repeat
(setq Index (sslength blockset))

(setq Index (1- Index)
      bn (cdr (assoc 2 (entget (ssname blockset Index))))
      )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;	Loop through the dirrefernt directories
(foreach <folder> subFolders
  (if
    (setq Drawings (matchfiles (strcat  rootDir <folder> bn "*.dwg")))

    (foreach Dwg Drawings

      (princ (strcat "\nOS Tile : " Dwg  ))
      (Write-line "insert" fout)
      (Write-line (strcat "\""Dwg "\"") fout)
      (Write-line "0,0,0" fout)
      (Write-line "1" fout)
      (Write-line "1" fout)
      (Write-line "0" fout)
      )
    )
  )
)

     (setq fout (close fout))

     ;; Force AuotCAD to release its lock on the script file
     (gc)
     (setvar "filedia" 1)
     (command "script"  "\"C:\\b\\tiles.scr\"")
     )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  (princ "\nNo blocks selected")
   )
 )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to list files from a wildcard match
(defun matchfiles (path / item dir match lst fil files)
 (setq path (strsplit "," path))
 (foreach item path
   (setq dir   (strcat (vl-string-right-trim "\\" (vl-filename-directory item)) "\\")
         match (substr item (1+ (strlen dir)))
         lst   (vl-directory-files dir match 1)
   )
   (foreach fil lst
     (setq files (cons (strcat dir fil) files))
   )
 )
 (reverse files)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function to split a string on a specified character
(defun strsplit (c str / lst p n)
 (setq n 0)
 (while (setq p (vl-string-search c str n))
   (setq lst (cons (substr str (1+ n) (- p n)) lst))
   (setq n (1+ p))
 )
 (if (< n (strlen str))
   (setq lst (cons (substr str (1+ n)) lst))
 )
 (reverse lst)
)


(princ "\nGetmaptile loaded")

Posted

Thanks Jammie, that is brilliant. Works like a dream. Was there something structurally wrong with my code? Just asking as a way to try to learn from my mistake.

 

Anyway thanks again for your help and patience and to the others who helped along the way too.

 

Woodman78.

Posted

Your welcome, glad to help. I can appreciate how a program like this can be of use for working with OS tiles. Your code was structured fine, it was easy to follow.

 

When I tested the code you had posted , I came across the error you mentioned about AutoCAD keeping a lock on the script file, even after closing the stream. I used the gc function to force AutoCAD to free up unused memory which seemed to release the lock. I hadn't used that function before in this context so I learned something myself here

 

I introduced the variables at the start partly for testing but also it will make it easier for future edits - perhaps the root folder gets moved or you'd like to add some extra sub folders

 

  
(setq rootDir "M:\\os\\Maps\\AutoCAD\\"
     template "T:\\Drawing Tools\\Templates\\CCC Blank Template ISO 01.dwt"
     subFolders (list "5000\\" "1000\\" "2500\\"))

 

Anyhow keep up the good work with your coding

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...