+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Sep 2010
    Location
    UAE / India
    Posts
    55

    Default Error While Running Lisp More Than 2 Times

    Registered forum members do not see this ad.

    Hi,
    The below lisp code is to import points from a txt file. In this file I am facing an error , like when I run this lisp for the first time it does its job properly, but when I delete the points and run it again, some points are not placed in correct coordinate. If anyone has an idea about correcting this issue please let me know.

    Please down load attached txt files for coordinate and check the points 08, 29,30, 39 etc
    Code:
    ;; ----------------------------------sub function error--------------------------------
     
    (defun trap1 (errmsg)
               (setvar "attdia" ad)
        (setvar "attreq" aq)
               (setq *error* temperr)
               (prompt "\n *** File not Found or Incorrect Format ***")
    (princ)
    ) ;defun
    ;;-----------------------------------sub function to create block-----------------------
    
    ;;;--- create block function -----
    (defun crb ( )
        
        (if (not (tblsearch "BLOCK" "CRBLK"))
            (progn
                (if (not (tblsearch "STYLE" "Gen-Text"))
                    (entmake
                        (list
                            (cons 0 "STYLE")
                            (cons 100 "AcDbSymbolTableRecord")
                            (cons 100 "AcDbTextStyleTableRecord")
                            (cons 2 "Gen-Text")
                            (cons 70 0)
                            (cons 40 2.5)
                            (cons 3 "Arial.ttf")
                        )
                    )
                )
                (entmake
                    (list
                        (cons 0 "BLOCK")
                        (cons 8 "0")
                        (cons 370 0)
                        (cons 2 "CRBLK")
                        (cons 70 2)
                        (cons 4 "Block to Place Coordinate Points")
                        (list 10 0.0 0.0 0.0)
                    )
                )
                (entmake
                    (list
                        (cons 0 "CIRCLE")
                        (cons 8 "0")
                        (cons 370 0)
                        (list 10 0.0 0.0 0.0)
                        (cons 40 1.25)
                    )
                )
                (entmake
                    (list
                        (cons 0 "ATTDEF")
                        (cons 8 "0")
                        (cons 370 0)
                        (cons 7 "Gen-Text")
                        (list 10 3.0 2.5 0.0)
                        (list 11 3.0 2.5 0.0)
                        (cons 40 2.5)
                        (cons 1 "00")
                        (cons 3 "Coordinate Point")
                        (cons 2 "00")
                        (cons 70 0)
                        (cons 72 0)
                        (cons 74 2)
                    )
                )
                (entmake
                    (list
                        (cons 0 "ENDBLK")
                        (cons 8 "0")
                    )
                )
                
       ;;;--- To set block units in metre 70-6
                   
                    (
                        (lambda ( lst )
                            (regapp "ACAD")
                            (entmod
                                (append (subst (cons 70 6) (assoc 70 lst) lst)
                                    (list
                                       (list -3
                                           (list "ACAD"
                                               (cons 1000 "DesignCenter Data")
                                               (cons 1002 "{")
                                               (cons 1070 1)
                                               (cons 1070 1)
                                               (cons 1002 "}")
                                           )
                                       )
                                   )
                                )
                            )
                        )
                        (entget (cdr (assoc 330 (entget (tblobjname "BLOCK" "CRBLK")))))
                    )
                 
     ;;;--- To make block annotative
               
               (
                    (lambda ( lst )
                        (regapp "ACAD")
                        (regapp "AcadAnnotative")
                        (entmod
                            (append (subst (cons 70 1) (assoc 70 lst) lst)
                                (list
                                   (list -3
                                       (list "ACAD"
                                           (cons 1000 "DesignCenter Data")
                                           (cons 1002 "{")
                                           (cons 1070 1)
                                           (cons 1070 1)
                                           (cons 1002 "}")
                                       )
                                       (list "AcadAnnotative"
                                           (cons 1000 "AnnotativeData")
                                           (cons 1002 "{")
                                           (cons 1070 1)
                                           (cons 1070 1)
                                           (cons 1002 "}")
                                       )
                                   )
                               )
                            )
                        )
                    )
                    (entget (cdr (assoc 330 (entget (tblobjname "BLOCK" "CRBLK")))))
                )
            )
        )
       
     ;;;--- to disable allow explod-----
       
              (vl-load-com)
              (setq BLOCKS
              (vla-get-Blocks
               (vla-get-activedocument
                (vlax-get-acad-object)
               )
              )
             BLK (vla-Item BLOCKS "CRBLK")
           )
          (vla-put-explodable (vla-Item BLOCKS "CRBLK") :vlax-false)   
     
       (princ)
    )
    ;;;--- create function block end -----
     
    
    ;; ----------------------------------Sub function--------------------------------
    (DEFUN IMPPS (/ TXTF)
      (SETQ TXTF (GETVAR "dwgname"))
      (IF (WCMATCH (STRCASE TXTF) "*`.DWG")
          (SETQ
          TXTF
          (STRCAT (GETVAR "dwgprefix") TXTF)
          TXTF
          (SUBSTR TXTF 1 (- (STRLEN TXTF) 4))
        ) 
      )
      TXTF
    )
    
    ;; ----------------------------------Main function--------------------------------
    (defun C:IMPO (/ CRDS ENCR ENNO ENNO1 CRPT DATA)
               (command "cmdecho"0)
               (setq clay (getvar "clayer"))
               (setq ad (getvar "attdia"))
               (setq aq (getvar "attreq"))
               (setq temperr *error*)
               (setq *error* trap1)
               (setvar "attdia" 0)
               (setvar "attreq" 1)
    
       (SETQ Data (GETFILED "Select Text File to Import" (IMPPS) "" 0))
       (SETQ CRDS (OPEN Data "r"))
    
       (setq ENCR 0)
       (command "UCS" "WORLD")
      
     (while (/= ENCR nil)
          (setq ENCR (read-line CRDS)) ;Read ENCR from input file
          
         
     (if (/= ENCR nil)
             (progn
                (crb) ;create block            
                (setq ENCR (strcat "(" ENCR ")")) ;Correct format
                (setq ENCR (read ENCR))           ;Convert to list
                (setq ENNO (nth 0 ENCR))          ;Get the CRPT number
           (cond 
                 ((= (type ENNO) 'STR)       
                  (setq ENNO1 ENNO)
                 )
                 ((and (< ENNO 10.0)(= (type ENNO) 'INT))       
                       (setq ENNO1 (strcat "0" (rtos ENNO 2 0)))
                 )
                 ((and (>= ENNO 10.0)(= (type ENNO) 'INT))       
                       (setq ENNO1 (rtos ENNO 2 0))
                 )
            
          
            ) ;cond
                
                (setq CRPT
                   (list                  
                      (nth 1 ENCR) ;Get easting
                      (nth 2 ENCR) ;Get northing
                   )
                )
     ;create new layer 
               (if (not (tblsearch "layer" "Coordinate Points")) (command "-LAYER" "N" "Coordinate Points" "C" "7" "Coordinate Points" "LT" "Continuous" "Coordinate Points""LW" "0.00" "Coordinate Points" ""))      
               (command "CLAYER" "Coordinate Points")                      
               (command "-insert" "CRBLK" CRPT "1" "1" "0" ENNO1)           
            )
          )
       )
       
       (command "UCS" "P")   
       (setvar "clayer" clay)
       (close CRDS)
       (prompt "\n Zoom Extend to View All Points")
    ) ;defun
    
    (prompt "\nLisp Command:IMPO (to Import Coordinates Points)   ")
    Attached Files

  2. #2
    Forum Deity MSasu's Avatar
    Discipline
    Construction
    MSasu's Discipline Details
    Occupation
    engineer
    Discipline
    Construction
    Details
    AutoLISP programmer
    Using
    AutoCAD 2013
    Join Date
    Mar 2009
    Location
    Brasov, Romania
    Posts
    3,942

    Default

    The issue is from auto OSNAP and the use of command call in your routine. It is also related to current zoom level, thus the error doesn't occur every time. Please fix this line as below:
    Code:
    (command "-insert" "CRBLK" "_NON" CRPT "1" "1" "0" ENNO1)
    Regards,
    Mircea

    AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3

  3. #3
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Sep 2010
    Location
    UAE / India
    Posts
    55

    Default

    Got it, Thank you so much....

    regards
    Bijoy

  4. #4
    Forum Deity MSasu's Avatar
    Discipline
    Construction
    MSasu's Discipline Details
    Occupation
    engineer
    Discipline
    Construction
    Details
    AutoLISP programmer
    Using
    AutoCAD 2013
    Join Date
    Mar 2009
    Location
    Brasov, Romania
    Posts
    3,942

    Default

    You're welcome!
    Regards,
    Mircea

    AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3

  5. #5
    Luminous Being
    Using
    Civil 3D 2013
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    5,139

    Default

    An oldie but it works use on most of my routines saves problems like above

    Code:
    :at start
    (setq oldsnap (getvar "osmode"))
    (setvar "osmode" 0)
    .
    .
    .
    ; at end 
     
    (setvar "osmode" oldsnap)
    A man who never made mistakes never made anything

  6. #6
    Forum Deity MSasu's Avatar
    Discipline
    Construction
    MSasu's Discipline Details
    Occupation
    engineer
    Discipline
    Construction
    Details
    AutoLISP programmer
    Using
    AutoCAD 2013
    Join Date
    Mar 2009
    Location
    Brasov, Romania
    Posts
    3,942

    Default

    BIGAL, this is exactly what I’m using for my routines too, but for cases like OP’s one (only a single command call) I prefer the "_NON" OSNAP mode instead due to simplicity.
    Regards,
    Mircea

    AutoCAD's happy user equation: FILEDIA + PICKADD² + PICKFIRST = 3

  7. #7
    Forum Deity BlackBox's Avatar
    Computer Details
    BlackBox's Computer Details
    Operating System:
    Windows 8.1 Professional, Server 2012 R2 Datacenter
    Computer:
    Dell Precision T3600
    CPU:
    Hex-Core Intel Xeon 3.2Ghz
    RAM:
    32GB
    Graphics:
    3GB NVIDIA Quadro K4000
    Primary Storage:
    256GB SSD
    Secondary Storage:
    1TB Western Digital VelociRaptor 10K RPM
    Monitor:
    Dual 24" Dell Ultrasharp Widescreen Monitors
    Using
    Civil 3D 2015
    Join Date
    Nov 2009
    Posts
    4,621

    Default

    ... Beware of *error*.

    Yeah, error handling is more overhead, but many users at my work think pressing escape repeatedly is how you properly terminate a command... , thusly:

    Psuedo code:
    Code:
    (defun c:FOO (/ *error*)
      (vl-load-com)
    
      (defun *error*  (msg)
        (and oldNomutt (setvar 'nomutt oldNomutt))
        (if acDoc (vla-endundomark acDoc))
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
        (princ))
    
      ((lambda (acDoc oldNomutt / ss)
         (vla-startundomark acDoc)
         (if (and (princ "\nMy custom select prompt: ")
                  (setvar 'nomutt 1)
                  (setq ss (ssget)))
           (progn
             ;; <- Do something
             (*error* nil))
           (*error* "Nothing selected")))
        (vla-get-active-document (vlax-get-acad-object))
        (getvar 'nomutt)))
    "Potential has a shelf life." - Margaret Atwood

    Autodesk Exchange Apps
    ~ Autoloader ~ AutoCAD Security

  8. #8
    Forum Deity pBe's Avatar
    Computer Details
    pBe's Computer Details
    Operating System:
    Windows XP
    Discipline
    Construction
    pBe's Discipline Details
    Discipline
    Construction
    Details
    Camp Construction planning and details
    Using
    AutoCAD 2009
    Join Date
    Apr 2010
    Posts
    2,635

    Default

    Quote Originally Posted by RenderMan View Post
    Yeah, error handling is more overhead, but many users at my work think pressing escape repeatedly is how you properly terminate a command... , thusly:
    What? you mean its not?

    I know that so well Renderman, one user i know presses the 'Esc key about a hundred tiems per minute (average) the key itself is worned out.

    I myself is guilty of not including an error routine on my codes.

    I noticed you've been using this construct quite frequently on your code these days.
    Code:
    ((lambda (acDoc oldNomutt / ss)
         (vla-startundomark acDoc)....
           .....(vla-get-active-document (vlax-get-acad-object))
        (getvar 'nomutt)))
    I ddint understand it at first, and then it clicked

  9. #9
    Super Member irneb's Avatar
    Computer Details
    irneb's Computer Details
    Operating System:
    Win7 Pro 64bit
    Computer:
    Antec One Hundred
    Motherboard:
    ASUS P8P67-Pro P67
    CPU:
    Intel i7 2600 @ 3.4GHz
    RAM:
    16GB-1600MHz
    Graphics:
    GeForce GT 430 (1GB)
    Primary Storage:
    Seagate1TB SATA2 - 7200rpm
    Monitor:
    Samsung 2333TN 23" 1920 x 1080 Full HD LCD Monitor2GW
    Discipline
    Architectural
    irneb's Discipline Details
    Occupation
    Architectural Technician and Programmer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    Sep 2010
    Location
    Jo'burg SA
    Posts
    1,737

    Default

    Another reason to use the "_NON" instead of setting and resetting the OSMode (as Mircea suggests) ... no need for an error trap to reset it back since it was never changed to begin with.

    Don't get me wrong, an *error* routine is a must in many instances. I just think adding such plus the OSMode set and reset is factors of extra coding over adding a "_NON" into your command call.
    Knowledge is proportional to experience, but wisdom is inversely proportional to ego!
    My little bit of "wisdom": Hind-sight is useless, unless used to improve the next forethought!

  10. #10
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,785

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by irneb View Post
    Another reason to use the "_NON" instead of setting and resetting the OSMode (as Mircea suggests) ... no need for an error trap to reset it back since it was never changed to begin with.

    Don't get me wrong, an *error* routine is a must in many instances. I just think adding such plus the OSMode set and reset is factors of extra coding over adding a "_NON" into your command call.
    1+ Especially if OSMODE is the only setting you are intending to change, and furthermore if the "_non" method is only going to be used a few times in the program so as not to render the code unreadable.

    Another serious issue to consider is the order in which System Variables are being reset (either in the *error* handler, or at the completion of code) relative to the start and end of Undo Groups.

    Since, when the user performs an Undo operation, all operations after the end of the last Undo group are undone before the Undo group itself is also undone. Depending on the order in which Undo Groups are closed and System Variables are reset, this can result in System Variables being changed when the user performs an Undo operation.

    Some example code to illustrate my point:
    Code:
    (defun c:GoodbyeOSnap ( / *error* doc osm p1 p2 )
    
        ;; Error Handler
        (defun *error* ( msg )
            (if doc (vla-endundomark doc)) ;; Close open Undo Group
            (if osm (setvar 'osmode osm))  ;; Reset OSMODE
            (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) ;; If the msg is not trivial
                (princ (strcat "\nError: " msg)) ;; Print critical errors
            ) ;; End if
            (princ) ;; Exit Cleanly
        ) ;; End defun
        
        (setq osm (getvar 'osmode)) ;; Store OSMODE setting
        (setvar 'osmode 1) ;; Set OSMODE to Endpoint
    
        (if
            (and
                (setq p1 (getpoint "\n1st Point: "))
                (setq p2 (getpoint "\n2nd Point: " p1))
            ) ;; End and
            (progn
                (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object)))) ;; Start Undo Group
                (entmake (list '(0 . "LINE") (cons 10 p1) (cons 11 p2))) ;; Construct a Line from the two points
                (vla-endundomark doc) ;; End Undo Group
            ) ;; End progn
        ) ;; End if
        
        (setvar 'osmode osm) ;; Reset OSMODE
        (princ) ;; Exit Cleanly
    ) ;; End defun
    The above code will prompt the user to pick two points with Object Snap forced to Endpoint, then construct a Line between those two points.

    On the surface, all looks fine: OSMODE is stored before being changed and reset at the end of the code and within the *error* handler; an Undo group is started before the Line is constructed and closed following this operation and also in the *error* handler. However, try running the code, then performing an Undo to remove the created Line...

    Furthermore, if the program throws an error for an operation within the Undo Group, although the code is equipped with an *error* handler to close the Undo group and reset System Variables, if the user decides to use Undo, the same issues will arise.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

Similar Threads

  1. Error Running The LISP Project. Help!
    By andresperezcera in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 17th Apr 2012, 11:21 pm
  2. Getting error while running Visual lisp program in ACAD2007.
    By drawings in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 20th Jul 2011, 11:31 am
  3. Continous Error "Fatal Error: Autocad is running out of memory"
    By capricorn in forum AutoCAD Bugs, Error Messages & Quirks
    Replies: 1
    Last Post: 4th Aug 2010, 10:40 am
  4. problem, trying to running a list of lisp from within a lisp
    By twind2000 in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 20th Aug 2007, 04:27 pm
  5. got error when running LISP
    By ali888 in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 5th Jan 2007, 12:26 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts