Jump to content

Recommended Posts

Posted

Currently I have like 10 lisp routines names P1, P2, ..., P10. They create a ployline with a width matching the corresponding number. P1 makes a 1" wide polyline, P2 makes a 2" polyline, etc. Would it be possible to create a generic 'P' command that would pass the number value the user puts after the 'P' and create a polyline with that width? For example, typing in P2.5 would create a polyline that is 2.5" wide. Doable?

Posted
(defun c:pg nil
     (if (not width)
           (setq width 1.00))
     (setq width (cond
                      ((getreal
                             (strcat "\nEnter Width <"
                                     (rtos width 2 2)
                                     ">: ")))
                      (width)))
     (setvar 'Plinewid width)
     (command "_pline")
     )

Posted

Here's a hack using a Command Reactor and Windows Script Host (WSH) to demonstrate that, yes, it is possible:

 

([color=BLUE]vl-load-com[/color])
([color=BLUE]if[/color] ([color=BLUE]null[/color] *command-reactor*)
   ([color=BLUE]setq[/color] *command-reactor* ([color=BLUE]vlr-command-reactor[/color] [color=BLUE]nil[/color] '(([color=BLUE]:vlr-unknowncommand[/color] . callback))))
)

([color=BLUE]defun[/color] callback ( reactor params [color=BLUE]/[/color] pw )
   ([color=BLUE]if[/color]
       ([color=BLUE]and[/color]
           ([color=BLUE]wcmatch[/color] ([color=BLUE]setq[/color] params ([color=BLUE]strcase[/color] ([color=BLUE]car[/color] params))) [color=MAROON]"P#*"[/color])
           ([color=BLUE]setq[/color] pw ([color=BLUE]distof[/color] ([color=BLUE]substr[/color] params 2)))
           ([color=BLUE]<[/color] 0 ([color=BLUE]setq[/color] pw ([color=BLUE]fix[/color] pw)))
       )
       ([color=BLUE]progn[/color]
           ([color=BLUE]eval[/color]
               ([color=BLUE]list[/color] '[color=BLUE]defun[/color]
                   ([color=BLUE]read[/color] ([color=BLUE]strcat[/color] [color=MAROON]"C:P"[/color] ([color=BLUE]itoa[/color] pw)))
                  '( [color=BLUE]/[/color] pw )
                  '([color=BLUE]setq[/color] pw ([color=BLUE]getvar[/color] [color=MAROON]"PLINEWID"[/color]))
                   ([color=BLUE]list[/color] '[color=BLUE]setvar[/color] [color=MAROON]"PLINEWID"[/color] pw)
                  '([color=BLUE]command[/color] [color=MAROON]"_.pline"[/color])
                  '([color=BLUE]while[/color] ([color=BLUE]=[/color] 1 ([color=BLUE]logand[/color] 1 ([color=BLUE]getvar[/color] [color=MAROON]"CMDACTIVE"[/color]))) ([color=BLUE]command[/color] [color=BLUE]pause[/color]))
                  '([color=BLUE]setvar[/color] [color=MAROON]"PLINEWID"[/color] pw)
                  '([color=BLUE]princ[/color])
               )
           )
           (LM:SendKeys ([color=BLUE]strcat[/color] [color=MAROON]"P"[/color] ([color=BLUE]itoa[/color] pw) [color=MAROON]"~"[/color]))
       )
   )
   ([color=BLUE]princ[/color])
)

[color=GREEN];; Send Keys  -  Lee Mac[/color]
[color=GREEN];; A wrapper function for the SendKeys method of the WSH[/color]

([color=BLUE]defun[/color] LM:SendKeys ( keys [color=BLUE]/[/color] wsh )
   ([color=BLUE]setq[/color] wsh ([color=BLUE]vlax-create-object[/color] [color=MAROON]"WScript.Shell"[/color]))
   ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vlax-invoke[/color] ([color=BLUE]list[/color] wsh 'sendkeys keys))
   ([color=BLUE]vlax-release-object[/color] wsh)
   ([color=BLUE]princ[/color])
)
([color=BLUE]princ[/color])

But this idea can only work for integers, since commands cannot include a decimal point.

 

The command "P3.2" will be interpreted as "P3"

Posted

@Lee, this is really great! As usually.

 

But this idea can only work for integers, since commands cannot include a decimal point.

 

The command "P3.2" will be interpreted as "P3"[/b]

 

Since typically the width of polylines in a drawing not vary into a wide range, I think the OP can adjust your tool to use more digits to store the width and extract it by dividing with an appropriate divider (100 in below example):

(< 0 (setq pw (/ (fix pw) 100.0)))

P35    -->   0.35
P245   -->   2.45

 

Regards,

Mircea

Posted
@Lee, this is really great! As usually.

 

Since typically the width of polylines in a drawing not vary into a wide range, I think the OP can adjust your tool to use more digits to store the width and extract it by dividing with an appropriate divider.

 

Thanks Mircea! - that's a good idea :)

Posted

Wouldn't it be easier and less labor intensive if you just used a subroutine, instead of creating a command for each P* call?

 

eg.

(vl-load-com)
(if (null *command-reactor*)
 (setq *command-reactor* (vlr-command-reactor nil '((:vlr-unknowncommand . callback))))
)

(defun _plineDraw (pw / *error* ow)

 (defun *error* (msg)
   (and ow (setvar 'PLINEWID ow))
   (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
     (princ (strcat "\nError: " msg))
   )
 )

 (setq ow (getvar 'PLINEWID))
 (setvar 'PLINEWID pw)
 (command "_.pline")
 (while (eq (logand 1 (getvar 'CMDACTIVE)) 1) (command PAUSE))
 (*error* nil)
 (princ)
)

(defun callback (reactor params / pw)
 (if (and (wcmatch (setq params (strcase (car params))) "P#*")
          (> (setq pw (distof (substr params 2))) 0)
     )
   (vla-sendcommand
     (cond (*AcadDoc*)
           ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
     )
     (strcat "(_plineDraw " (rtos pw) ")\n")
   )
 )
 (princ)
)

Posted

Its an alternative, but I didn't want the reactor to fire and "Unknown command" message to be printed more than it needed to, so once the command is defined, the reactor ceases to fire for that command.

Posted
Its an alternative, but I didn't want the reactor to fire and "Unknown command" message to be printed more than it needed to, so once the command is defined, the reactor ceases to fire for that command.

Meh, aesthetics are overrated.

 

 

Now that I think about it, wasn't there a lengthy thread similar to this about OFFSET?

Posted

Now that I think about it, wasn't there a lengthy thread similar to this about OFFSET?

 

I had a similar thought, but couldn't identify the thread quickly, so I went back to work - I thought it was an AUGI thread, and Irneb tested the hypothesis. Perhaps I'm thinking of a different thread.

 

Edit - Besides, aren't we supposed to avoid invoking commands from within a command reactor's callback?

Posted
I had a similar thought, but couldn't identify the thread quickly, so I went back to work - I thought it was an AUGI thread, and Irneb tested the hypothesis. Perhaps I'm thinking of a different thread.

Nah, it was here and there was LOTS of flaming.

Posted

Thanks for looking this request over and giving me some help. However, when I try to load Lee Mac's file and run it, I get this error:

 

Command: p1 ; error: Exception occurred: 0xC0000005 (Access Violation)

; warning: unwind skipped on exception

; error: Exception occurred: 0xC0000005 (Access Violation)

Unknown command "P1". Press F1 for help.

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