Jump to content

error in my code


guitarguy1685

Recommended Posts

I wrote this LISP to draw leaders and annotate objects in my drawings. Let me explain the set up. We have blocks with names like G###, B####, A### then we have another like 21111. In the first part of the lisp I get the name of the block using entsel then it goes through the 1st check.

 

(if (> (atoi PartTxt) 0)

 

the blocks with Letters in the begining will always be 0 and the others will always be > 0. I need to do this because the blocks with names like 21111 are assigned another number like A10. When the first check is T it will go through a .dat file (it's file extension is .txt for upload purposes) to retrieve the corresponding "A" Number. So if the blocks are of the type B#### or G#### it skips that part of the lisp (at least that's my plan).

 

If the 21111 is not listed in the .dat file then it will be assigned the number A00.

 

Then it will parse the block names and change them to, for example, A1234 to A12.34, G1234 to G12.34. But A11 will remain A11. I hope this makes sense.

 

Here is my problem. when I first try to tag a G### or F### block I get the following error

Command: bad argument type: streamp nil

 

BUT if I first try to tag the block that is named 21345 for example that works. then when I tag the other blocks they also work. I'm not sure what I"m missing here. Any help is greatly appreciated.

 

PS. i didn't not declare any locals since i'm not done atm.

 
(defun C:TEST (/)
 (setq CRcecho (getvar "cmdecho")) ;;;store curret sysvar
 (setq CRdimas (getvar "dimassoc"))
 (setq CRortho (getvar "orthomode"))
 (setq CRAttRq (getvar "ATTREQ"))
 (setq CRAttDi (getvar "ATTDIA"))
 (setvar "cmdecho" 0)
 (setvar "dimassoc" 2)
 (setq CRdmsty (getvar "DIMSTYLE"))
 (graphscr)
;------------------------------------------------------------------------
 (setq *error*    ;;;resets sysvar incase of
    (lambda (msg)   ;;;an error, esc, exit, cancel
      (setvar "cmdecho" CRcecho)
      (setvar "dimassoc" CRdimas)
      (command "-dimstyle" "r" CRdmsty)
      (princ msg)
      (princ)))
;------------------------------------------------------------------------
 (setq CRuprec (getvar "LUPREC"))
 (setq CRLunit (getvar "LUNITS"))
 (setq Partblk (entsel "\nSelect Part or [ENTER] to input part number: "))

 (if (= Partblk nil)
     (progn
  (princ "\n***No block selected***")
  (setq PartTxt (strcase (getstring "\nEnter part number: "))
 P1 (getpoint "\nSpecify leader start point: "))
);end prog1
     (progn
(setq PartLst (entget (car Partblk)))
(if (/= (cdr (assoc 0 PartLst)) "INSERT")
    (progn
      (princ "\n***Object is not a block***")
      (setq PartTxt (strcase (getstring "\nEnter part number: "))
     P1 (getpoint "\nSpecify leader start point: "))
      )
    (progn
      (setq PartTxt (cdr (assoc 2 PartLst))
     P1 (car (cdr PartBlk)))
)
)
)
   )
[color=red] (if (> (atoi PartTxt) 0)[/color]
   (progn
     (setq ExtNbr (strcat "*" PartTxt)    ;add asterix to ExtNbr
    file  (findfile "L:/WSICustomAutoCAD/LISP/Development/VALB_EXT.dat")   ;find data file
    fp    (open file "r")    ;open file to read
    item  (read-line fp) ;first line is label for file
    );setq
     (while item      ;process each line of file
(if (= item ExtNbr)     ;compare values
  (setq PartTxt (read-line fp)    ;read a line
 item nil     ;stop searching for item
 );setq
  (setq item (read-line fp)
 PartTxt "A000")   ;keep searching for item
  );if
);while
     (princ "\n***dat file name***")
     )
   (setq PartTxt PartTxt)
   )
 (close fp)   ;allocate to variables

 (if (> (strlen PartTxt) 4)
     (setq PartTxtIns (strcase (strcat (substr PartTxt 1 3) "." (substr PartTxt 4 2))))
     (setq PartTxtIns PartTxt)
   );end if
 (if (> (strlen PartTxtIns) 4)
      (cond
 ((= (substr PartTxt 1 1) "A")(setq TagR "L:/DrawingUtilities/SA-PARTR"
          TagL "L:/DrawingUtilities/SA-PARTL"))
 ((= (substr PartTxt 1 1) "B")(setq TagR "L:/DrawingUtilities/B-PARTR"
         TagL "L:/DrawingUtilities/B-PARTL"))
 ((= (substr PartTxt 1 1) "F")(setq TagR "L:/DrawingUtilities/F-PARTR"
        TagL "L:/DrawingUtilities/F-PARTL"))
 ((= (substr PartTxt 1 1) "G")(setq TagR "L:/DrawingUtilities/G-PARTR"
        TagL "L:/DrawingUtilities/G-PARTL"))
 ((= (substr PartTxt 1 1) "H")(setq TagR "L:/DrawingUtilities/H-PARTR"
        TagL "L:/DrawingUtilities/H-PARTL"))
);end 1st cond
       (cond
  ((= (substr PartTxt 1 1) "A")(setq TagR "L:/DrawingUtilities/A-PARTR"
           TagL "L:/DrawingUtilities/A-PARTL"))
  ((= (substr PartTxt 1 1) "M")(setq TagR "L:/DrawingUtilities/M-PARTR"
         TagL "L:/DrawingUtilities/M-PARTL"))
  ((= (substr PartTxt 1 1) "S")(setq TagR "L:/DrawingUtilities/S-PARTR"
         TagL "L:/DrawingUtilities/S-PARTL"))
  );end 2nd cond
   );end if

 (setq CRDSCL (getvar "DIMSCALE"))
 ;(set_wsitextstyle)
 ;(_dimleader_setup)
 (if (= (tblsearch "dimstyle" "WSILEADER") nil)
     (vl-cmdf "_.dimstyle" "s" "WSILEADER")
     (vl-cmdf "_.dimstyle" "s" "WSILEADER" "y")
   )

 (setvar "orthomode" 0)
 (setvar "ATTREQ" 1)
 (setvar "ATTDIA" 0)

 ;(setq P1 (getpoint "\nSpecify leader start point: "))
 (setq P2 (getpoint "\nSpecify point of leader landing" P1))
 (if (<= (car P2) (car P1))
 (setq FPTag TagR)
 (setq FPTag TagL)
   );end if
 (if (<= (car P2) (car P1))
 (setq P3 (list (- (car P2) 0.125)  (cadr P2)))
 (setq P3 (list (+ (car P2) 0.125)  (cadr P2)))
  );end if
 (setvar "cmdecho" 0)
   (if (tblsearch "layer" "PARTMARKS")
     (progn
   (vl-cmdf "_.layer" "s" "PARTMARKS" "")
(setvar "cmdecho" 1)
(princ "\n"))
     (progn
    (vl-cmdf "_.layer" "N" "PARTMARKS" "c" "2" "PARTMARKS" "s" "PARTMARKS" "")
(setvar "cmdecho" 1)
(princ "\n")))

 (command "_.leader" P1 P2 P3 "" "" "b" FPTag P3 CRDSCL CRDSCL "0" PartTxtIns)

 ;--------------------------------------------------------------------------
(setvar "cmdecho" CRcecho) ;restore previous settings
(setvar "dimassoc" CRdimas)
  (setvar "orthomode" CRortho)
  (setvar "ATTREQ" CRATTRQ)
  (setvar "ATTDIA" CRAttDi)
  (command "-dimstyle" "r" CRdmsty)


 (princ)

 )

test.dwg

EXT.lsp

VALB_EXT.txt

Link to comment
Share on other sites

ok well I removed the line (close fp) and the lisp seems to work. Not sure why that would've caused a problem.

Link to comment
Share on other sites

GuitarGuy,

 

You need to check that the file is found, and that the open has performed correctly.

 

Look into the return values of the close function for clues on why you were receiving your error message. :wink:

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