Jump to content

Recommended Posts

Posted (edited)
  On 3/26/2025 at 10:42 AM, jim78b said:

Command: MLB
Select object in paper space,select start point of MLeader,exit with esc'._MSPACE
Command: ._PSPACE
Command:
Cannot invoke (command) from *error* without prior call to (*push-error-using-command*).
Converting (command) calls to (command-s) is recommended.

Expand  

 

@jim78b The Error you got tells you what to do;

                        change the (command) statement in the error handler to (command-s).

 

I.E.

(defun trap1 (errmsg)
   (command-s "._PSPACE") ;<--- HERE
   (SETVAR "OSMODE" osnp)
   (princ)
)

 

Edited by pkenewell
Posted

SO what code should i use? however ALWAYS the code reset my osnap!! is possible?

Posted

THE PROBLEM NOW IS that my osnaps every time reset!

Posted (edited)

use this

(defun c:MLeaderWBlname( / *error* trap1 osnp tm tagname ptms ss ensel obj obj1 nam)
 
 (setq temperr *error*);store *error*
 (setq *error* trap1);re-assign *error*

 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)
 (setq tm (getvar "TILEMODE"))
 
(princ "Select object in paper space,select start point of MLeader,exit with esc'")
 (while (getpoint) ;;get point in paper space on the target object
  (if (= tm 0);if in paper space
   (progn
     (command "._MSPACE")
     (setq ptms (cadr (grread t)));;get the point where the cursor is on the target object in model space
     (setq ss (ssget ptms));;selction set of the object crossing the ptms point
     (setq obj(ssname ss 0))
     (command "._PSPACE")
   );progn 
   (progn ;in model space  
     (setq ensel (entsel "\nSelect Block: ")) ;select the block object to copy
     (setq obj (car ensel)) ;set the block object to varaible
   );progn
  );if

  (setq obj1 (vlax-ename->vla-object obj)) 
  (setq nam 
   (vlax-get-property obj1
       (if (vlax-property-available-p obj1 'effectivename)
            'effectivename
            'name
       )
   ) 
  )
    
  (command "_mleader" "H" pause pause nam)
 );end while 

 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)

 (princ)
)

(defun trap1 (errmsg)
   (command-s "._PSPACE") ;<--- HERE
   (SETVAR "OSMODE" osnp)
   (princ)
)

 

other way (the one I use) is this version:

(defun c:MLeaderWBlname( / *error* osnp tm tagname ptms ss ensel obj obj1 nam)
 
    (defun *error* ( msg )
        (command "._PSPACE")
        (setvar "OSMODE" osnp)
        (princ)
     )


 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)
 (setq tm (getvar "TILEMODE"))
 
(princ "Select object in paper space,select start point of MLeader,exit with esc'")
 (while (getpoint) ;;get point in paper space on the target object
  (if (= tm 0);if in paper space
   (progn
     (command "._MSPACE")
     (setq ptms (cadr (grread t)));;get the point where the cursor is on the target object in model space
     (setq ss (ssget ptms));;selction set of the object crossing the ptms point
     (setq obj(ssname ss 0))
     (command "._PSPACE")
   );progn 
   (progn ;in model space  
     (setq ensel (entsel "\nSelect Block: ")) ;select the block object to copy
     (setq obj (car ensel)) ;set the block object to varaible
   );progn
  );if

  (setq obj1 (vlax-ename->vla-object obj)) 
  (setq nam 
   (vlax-get-property obj1
       (if (vlax-property-available-p obj1 'effectivename)
            'effectivename
            'name
       )
   ) 
  )
    
  (command "_mleader" "H" pause pause nam)
 );end while 

 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)

 (princ)
)

 

damm lisp error traping...🙇‍♂️

Edited by aridzv
Posted

SORRY THIS RESET MY OSNAPS!! AFTER I END TO USE THE CODE!

Posted (edited)

try this code.

it is with out error trap - it looks for errors by checking the selection set and the object properties:

(defun c:MLeaderWBlname( / osnp tm tagname ptms ss ensel obj obj1 ifobj nam)

 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)
 (setq tm (getvar "TILEMODE"))
 (setq ifobj 0)
(princ "Select object in paper space,select start point of MLeader,exit with esc'")
 (while (getpoint) ;;get point in paper space on the target object
  (if (= tm 0);if in paper space
   (progn
     (command "._MSPACE")
     (setq ptms (cadr (grread t)));;get the point where the cursor is on the target object in model space
       (if (=(setq ss (ssget ptms)) nil) ;;selction set of the object crossing the ptms point
        (progn
         (alert "There is no selection set please run again")
         (setq ifobj 0)
         (command "._PSPACE")
        );progn
        (progn
         (command "._PSPACE")
         (setq obj(ssname ss 0))      
         (setq ifobj 1)
        );progn
       );if
   );progn 
   (progn ;in model space  
     (setq ensel (entsel "\nSelect Block: ")) ;select the block object to copy
     (setq obj (car ensel)) ;set the block object to varaible
     (setq ifobj 1) 
   );progn
  );if
(if (= ifobj 1) ;; check if there is a selection set
  (progn
    (if (= (cdr (assoc 0 (entget obj))) "INSERT");; check if selected object is block
     (progn
       (setq obj1 (vlax-ename->vla-object obj)) 
       (setq nam 
        (vlax-get-property obj1
          (if (vlax-property-available-p obj1 'effectivename)
            'effectivename
            'name
          )
        ) 
       ) 
      (command "_mleader" "H" pause pause nam)
      (setq ifobj 0)
     );progn
     (progn
      (alert "Not a Block")
     );progn
    );if
  );progn
   (alert "Not a Block")
 );end if
 );end while 

 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)

 (princ)
)

 

Edited by aridzv
Posted

now work after restart autocad but sometimes the osnap reset all my settings

Posted

see attached code and video.

EXIT WITH ENTER TO RESTORE OSNAP.

(defun c:MLeaderWBlname( / osnp tm tagname ptms ss ensel obj obj1 ifobj nam)

 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)
 (setq tm (getvar "TILEMODE"))
 (setq ifobj 0)
(princ "Select object in paper space,select start point of MLeader,exit with ENTER")
 (while (getpoint) ;;get point in paper space on the target object
  (if (= tm 0);if in paper space
   (progn
     (command "._MSPACE")
     (setq ptms (cadr (grread t)));;get the point where the cursor is on the target object in model space
       (if (=(setq ss (ssget ptms)) nil) ;;selction set of the object crossing the ptms point
        (progn
         (alert "There is no selection set please run again")
         (setq ifobj 0)
         (command "._PSPACE")
        );progn
        (progn
         (command "._PSPACE")
         (setq obj(ssname ss 0))      
         (setq ifobj 1)
        );progn
       );if
   );progn 
   (progn ;in model space  
     (setq ensel (entsel "\nSelect Block: ")) ;select the block object to copy
     (setq obj (car ensel)) ;set the block object to varaible
     (setq ifobj 1) 
   );progn
  );if
(if (= ifobj 1) ;; check if there is a selection set
  (progn
    (if (= (cdr (assoc 0 (entget obj))) "INSERT");; check if selected object is block
     (progn
       (setq obj1 (vlax-ename->vla-object obj)) 
       (setq nam 
        (vlax-get-property obj1
          (if (vlax-property-available-p obj1 'effectivename)
            'effectivename
            'name
          )
        ) 
       ) 
      (command "_mleader" "H" pause pause nam)
      (setq ifobj 0)
     );progn
      (alert "Not a Block")
    );if
  );progn
   (alert "Not a Block")
 );end if
);end while 

 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)
 (princ)
)

 

Posted
  On 3/26/2025 at 4:23 PM, jim78b said:

now work after restart autocad but sometimes the osnap reset all my settings

Expand  

yap.

with the code above you must use ENTER to exit the lisp and get OSNAP restored back...

Posted

Ok thanks very much. But it happened i think during command running. If  i turn off the osnap with f3. 

Posted (edited)
  On 3/26/2025 at 4:34 PM, jim78b said:

Ok thanks very much. But it happened i think during command running. If  i turn off the osnap with f3. 

Expand  

try the last code and finish the command with ENTER,

not escape or right click.

only ENTER.

Edited by aridzv
  • Thanks 1
Posted

@jim78b

for some reason when I finish with right click it dose restore the OSNAP.

try restart autocad,

open the drawing and load the lisp again.

Posted (edited)
  On 3/26/2025 at 4:34 PM, jim78b said:

Ok thanks very much. But it happened i think during command running. If  i turn off the osnap with f3. 

Expand  

Of course it happens in the lisp, that's the idea.

see at the start of the code

 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)

 

and at the end of the code restore the OSNAP mode:

 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)
 (princ)

 

Try selecting the block with the cursor trying to snap at every entity around...

Edited by aridzv
  • Thanks 1
Posted

Ok for now i appreciate your help. I will work better tomorrow . I think is a good job.

Posted

4 suggestions, 

(getpoint "\nSelect object in paper space,select start point of MLeader,exit with ENTER")

 

(setq ifobj 0)
(setvar 'osmode osnp)
(command "._PSPACE")

 

Create a Error check so if Esc is pressed the osnap is reset.

 

I use a simple defun to reset my osnaps if lost. Just set your osnaps to what you like to use and then type osmode a number will appear use that number for the osmode, i just made the defun name a number, it is autoloaded on start up.

(defun c:47 ( / )(setvar 'osmode 47))

 

Posted (edited)

here is the code with proper error handling and @BIGAL revised getpoint:

(defun c:MLeaderWBlname( / trap1 olderr osnp tm tagname ptms ss ensel obj obj1 ifobj nam)

 (defun trap1 ( msg );define error trap
   (setq *error* olderr);restore *error* symbol
   (SETVAR "OSMODE" osnp);reset OSMODE  
   (princ)
 )

 (setq olderr *error*); assign current function defintion held by the *error* symbol to a local variable - olderr
 (setq *error* trap1); pointing the *error* symbol to new function definition - trap1

 (setq osnp (getvar "OSMODE"))
 (setvar "OSMODE" 0)
 (setq tm (getvar "TILEMODE"))
 (setq ifobj 0)

 (while (getpoint "\nSelect object in paper space,select start point of MLeader,exit with ENTER/ESC/Right Click") ;;get point in paper space on the target object
  (if (= tm 0);if in paper space
   (progn
     (command "._MSPACE")
     (setq ptms (cadr (grread t)));;get the point where the cursor is on the target object in model space
       (if (=(setq ss (ssget ptms)) nil) ;;selction set of the object crossing the ptms point
        (progn
         (alert "There is no selection set please run again")
         (setq ifobj 0)
         (command "._PSPACE")
        );progn
        (progn
         (command "._PSPACE")
         (setq obj(ssname ss 0))      
         (setq ifobj 1)
        );progn
       );if
   );progn 
   (progn ;in model space  
     (setq ensel (entsel "\nSelect Block: ")) ;select the block object to copy
     (setq obj (car ensel)) ;set the block object to varaible
     (setq ifobj 1) 
   );progn
  );if
(if (= ifobj 1) ;; check if there is a selection set
  (progn
    (if (= (cdr (assoc 0 (entget obj))) "INSERT");; check if selected object is block
     (progn
       (setq obj1 (vlax-ename->vla-object obj)) 
       (setq nam 
        (vlax-get-property obj1
          (if (vlax-property-available-p obj1 'effectivename)
            'effectivename
            'name
          )
        ) 
       ) 
      (command "_mleader" "H" pause pause nam)
      (setq ifobj 0)
     );progn
      (alert "Not a Block")
    );if
  );progn
   (alert "Not a Block")
 );end if
);end while 

 (setq *error* olderr); restore *error* symbol
 (command "._PSPACE")
 (SETVAR "OSMODE" osnp)
 (princ)
)

 

Edited by aridzv
  • Like 1
Posted

so is this lisp correct? yhanks very much

 

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