Jump to content

Having trouble with lisp to insert blocks


chelsea1307

Recommended Posts

I have a lisp that inserts piping symbols, use the exact same lisp for 18 different blocks with the only thing changed is the block name and the command to call it below are the two that keep returning nil

(defun c:bfly (/ *error* scl dis ov vl
               lne pnt ang pt1 pt2)

 ;; Always localise your variables!
 
 (or (not (zerop (setq scl (getvar "DIMSCALE"))))
     (setq scl 1.))

 ;; Dimscale may be zero - need to check for this
 
 (setq dis (* scl 0.0703))

 ;; Use an Error Handler as we are tampering with Sys Vars
 
 (defun *error* (msg)
   (if ov (mapcar 'setvar vl ov)) ; Reset Sys Vars    
   (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
     (princ (strcat "\n<< Error: " msg " >>")))
   (princ))

 (setq vl '("CMDECHO" "OSMODE" "CLAYER") ; Sys Var list
       ov (mapcar 'getvar vl)) ; Get Old Sys Var Values
 
 (if (or (tblsearch "BLOCK" "bfly") ; Check for Block in Dwg...
         (findfile "bfly.dwg"))   ;... And in Search bflyth
   (progn
     
     (while ; While the following returns T
       
       (progn ; Wrap the following expressions
         
         (setq lne (entsel "\nSelect a Line: ")) ; Select a Line
         
         (bflynd ((null lne) t) ; Stay in Loop
               ((eq "LINE"
                  (cdr (assoc 0 (setq elst (entget (car lne)))))) nil) ; Exit Loop
               (t (princ "\n** Inbflyrrect Selection **"))))) ; Stay in Loop
     
     (if (setq pt (getpoint "\nPick Insertion Point: ")) ; Prompt for Point
       (progn
         (setq ang (angle (cdr (assoc 10 elst)) (cdr (assoc 11 elst)))) ; Get Line Angle
         
         (mapcar 'setvar vl (list 0 0 (cdr (assoc 8 (entget (car lne))))))

         ; Set Sys Vars to how we want them - CMDECHO=0, OSMODE=0, CLAYER= <line layer>
         
         (bflymmand "-insert" "bfly" pt scl scl (* 180. (/ ang pi))) ; insert Block

         ;; Prefix bflymmands with "_." to make them language bflymbflytible
         
         (setq pt1 (polar pt ang dis)
               pt2 (polar pt (- ang pi) dis))
         
         (bflymmand "_.break" lne "_F" pt1 pt2))))
   
   (princ "\n<< Block Not Found >>")) ; Else the Block was not found
 
 (mapcar 'setvar vl ov) ; Reset Sys Vars
 
 (princ)) ; Exit Cleanly

 

(defun c:cotf (/ *error* scl dis ov vl
               lne pnt ang pt1 pt2)

 ;; Always localise your variables!
 
 (or (not (zerop (setq scl (getvar "DIMSCALE"))))
     (setq scl 1.))

 ;; Dimscale may be zero - need to check for this
 
 (setq dis (* scl 0.0703))

 ;; Use an Error Handler as we are tampering with Sys Vars
 
 (defun *error* (msg)
   (if ov (mapcar 'setvar vl ov)) ; Reset Sys Vars    
   (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
     (princ (strcat "\n<< Error: " msg " >>")))
   (princ))

 (setq vl '("CMDECHO" "OSMODE" "CLAYER") ; Sys Var list
       ov (mapcar 'getvar vl)) ; Get Old Sys Var Values
 
 (if (or (tblsearch "BLOCK" "cotf") ; Check for Block in Dwg...
         (findfile "cotf.dwg"))   ;... And in Search cotfth
   (progn
     
     (while ; While the following returns T
       
       (progn ; Wrap the following expressions
         
         (setq lne (entsel "\nSelect a Line: ")) ; Select a Line
         
         (cotfnd ((null lne) t) ; Stay in Loop
               ((eq "LINE"
                  (cdr (assoc 0 (setq elst (entget (car lne)))))) nil) ; Exit Loop
               (t (princ "\n** Incotfrrect Selection **"))))) ; Stay in Loop
     
     (if (setq pt (getpoint "\nPick Insertion Point: ")) ; Prompt for Point
       (progn
         (setq ang (angle (cdr (assoc 10 elst)) (cdr (assoc 11 elst)))) ; Get Line Angle
         
         (mapcar 'setvar vl (list 0 0 (cdr (assoc 8 (entget (car lne))))))

         ; Set Sys Vars to how we want them - CMDECHO=0, OSMODE=0, CLAYER= <line layer>
         
         (cotfmmand "-insert" "cotf" pt scl scl (* 180. (/ ang pi))) ; insert Block

         ;; Prefix cotfmmands with "_." to make them language cotfmcotftible
         
         (setq pt1 (polar pt ang dis)
               pt2 (polar pt (- ang pi) dis))
         
         (cotfmmand "_.break" lne "_F" pt1 pt2))))
   
   (princ "\n<< Block Not Found >>")) ; Else the Block was not found
 
 (mapcar 'setvar vl ov) ; Reset Sys Vars
 
 (princ)) ; Exit Cleanly

 

Any idea why it doesnt work for these two? I also attached the two blocks they call

COTF.DWG

BFLY.DWG

Link to comment
Share on other sites

I have a lisp that inserts piping symbols, use the exact same lisp for 18 different blocks with the only thing changed is the block name and the command to call it below are the two that keep returning nil

(defun c:bfly (/ *error* scl dis ov vl
               lne pnt ang pt1 pt2)

 ;; Always localise your variables!
 
 (or (not (zerop (setq scl (getvar "DIMSCALE"))))
     (setq scl 1.))

 ;; Dimscale may be zero - need to check for this
 
 (setq dis (* scl 0.0703))

 ;; Use an Error Handler as we are tampering with Sys Vars
 
 (defun *error* (msg)
   (if ov (mapcar 'setvar vl ov)) ; Reset Sys Vars    
   (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
     (princ (strcat "\n<< Error: " msg " >>")))
   (princ))

 (setq vl '("CMDECHO" "OSMODE" "CLAYER") ; Sys Var list
       ov (mapcar 'getvar vl)) ; Get Old Sys Var Values
 
 (if (or (tblsearch "BLOCK" "bfly") ; Check for Block in Dwg...
         (findfile "bfly.dwg"))   ;... And in Search bflyth
   (progn
     
     (while ; While the following returns T
       
       (progn ; Wrap the following expressions
         
         (setq lne (entsel "\nSelect a Line: ")) ; Select a Line
         
         ([color=Red]bflynd[/color] ((null lne) t) ; Stay in Loop
               ((eq "LINE"
                  (cdr (assoc 0 (setq elst (entget (car lne)))))) nil) ; Exit Loop
               (t (princ "\n** Inbflyrrect Selection **"))))) ; Stay in Loop
     
     (if (setq pt (getpoint "\nPick Insertion Point: ")) ; Prompt for Point
       (progn
         (setq ang (angle (cdr (assoc 10 elst)) (cdr (assoc 11 elst)))) ; Get Line Angle
         
         (mapcar 'setvar vl (list 0 0 (cdr (assoc 8 (entget (car lne))))))

         ; Set Sys Vars to how we want them - CMDECHO=0, OSMODE=0, CLAYER= <line layer>
         
         ([color=Red]bflymmand[/color] "-insert" "bfly" pt scl scl (* 180. (/ ang pi))) ; insert Block

         ;; Prefix bflymmands with "_." to make them language bflymbflytible
         
         (setq pt1 (polar pt ang dis)
               pt2 (polar pt (- ang pi) dis))
         
         ([color=Red]bflymmand[/color] "_.break" lne "_F" pt1 pt2))))
   
   (princ "\n<< Block Not Found >>")) ; Else the Block was not found
 
 (mapcar 'setvar vl ov) ; Reset Sys Vars
 
 (princ)) ; Exit Cleanly

(defun c:cotf (/ *error* scl dis ov vl
               lne pnt ang pt1 pt2)

 ;; Always localise your variables!
 
 (or (not (zerop (setq scl (getvar "DIMSCALE"))))
     (setq scl 1.))

 ;; Dimscale may be zero - need to check for this
 
 (setq dis (* scl 0.0703))

 ;; Use an Error Handler as we are tampering with Sys Vars
 
 (defun *error* (msg)
   (if ov (mapcar 'setvar vl ov)) ; Reset Sys Vars    
   (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
     (princ (strcat "\n<< Error: " msg " >>")))
   (princ))

 (setq vl '("CMDECHO" "OSMODE" "CLAYER") ; Sys Var list
       ov (mapcar 'getvar vl)) ; Get Old Sys Var Values
 
 (if (or (tblsearch "BLOCK" "cotf") ; Check for Block in Dwg...
         (findfile "cotf.dwg"))   ;... And in Search cotfth
   (progn
     
     (while ; While the following returns T
       
       (progn ; Wrap the following expressions
         
         (setq lne (entsel "\nSelect a Line: ")) ; Select a Line
         
         ([color=Red]cotfnd[/color] ((null lne) t) ; Stay in Loop
               ((eq "LINE"
                  (cdr (assoc 0 (setq elst (entget (car lne)))))) nil) ; Exit Loop
               (t (princ "\n** Incotfrrect Selection **"))))) ; Stay in Loop
     
     (if (setq pt (getpoint "\nPick Insertion Point: ")) ; Prompt for Point
       (progn
         (setq ang (angle (cdr (assoc 10 elst)) (cdr (assoc 11 elst)))) ; Get Line Angle
         
         (mapcar 'setvar vl (list 0 0 (cdr (assoc 8 (entget (car lne))))))

         ; Set Sys Vars to how we want them - CMDECHO=0, OSMODE=0, CLAYER= <line layer>
         
         ([color=Red]cotfmmand[/color] "-insert" "cotf" pt scl scl (* 180. (/ ang pi))) ; insert Block

         ;; Prefix cotfmmands with "_." to make them language cotfmcotftible
         
         (setq pt1 (polar pt ang dis)
               pt2 (polar pt (- ang pi) dis))
         
         ([color=Red]cotfmmand[/color] "_.break" lne "_F" pt1 pt2))))
   
   (princ "\n<< Block Not Found >>")) ; Else the Block was not found
 
 (mapcar 'setvar vl ov) ; Reset Sys Vars
 
 (princ)) ; Exit Cleanly

Any idea why it doesnt work for these two? I also attached the two blocks they call

 

I haven't looked too closely at it but I noticed you might have done a find and replace on "co" and changed a few things that should be cond & command (highlighted). I'd suggest not using find & replace. :)

 

On another note, why don't you just set a variable at the start as a block name so you don't have to edit every lisp...?

Link to comment
Share on other sites

I feel like a dork now, i did a find/replace on co, which is also one of the block names and the two i did after that bfly and cotf didnt work duh! thanks for spotting that...

 

edit: just tested them out and they still arent working.

 

edit2: Started over with a different .lsp and did the find replace, making sure i wasnt messing something important like command up and now they work fine!

Link to comment
Share on other sites

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...