Author Topic: How can I catch a bad entity during an offset command?  (Read 5081 times)

0 Members and 1 Guest are viewing this topic.

DMSS1

  • Guest
How can I catch a bad entity during an offset command?
« on: December 04, 2009, 03:09:32 PM »
I'm writing a lisp program in AutoCAD '04 using the offset command to use on polylines the have combinations of lines and arc's in it (inside & outside arc's).

Sometimes an anomaly occurs and a bad entity is created with "no vertices"... CRASH!
Or no offset really occurs, and the entlast sets the variable with the wrong entity (wrong entity changes properties).

In a nut-shell:
I'm offsetting the same entity multiple times, at different distances, to different layers.

The problem is, the offset distances are really small most of the time (which allows the offset to work correctly... sometimes... and sometimes the anomaly occurs).
But when the offset is greater than 0.04, I divide the distance by 0.0156, and offset the new entity X-amount of times to equal the target offset distance (erasing the temporary entities).

My biggest problem is:
This works fine in '04, but in '02 an entity is created (in the temporary entities), that has "no vertices", then when the temporary entity is offset again, or changed to different properties, it locks-up (because it's a bad entity).

If the offset command issues "cannot offset that entity" (the internal offset command error) error and makes a mistake, how can I trap it and insure the offset fails or succeeds, every time?
(I tried the "vl-cmdf" command, but it always returns T, even when it fails)

Also, when the offset doesn't offset, "entlast" changes the "last" entity (but it shouldn't change anything if a new "valid" entity isn't created).

Thanks!

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #1 on: December 04, 2009, 03:12:58 PM »
command call errors are tough to catch...

I suppose wrapping it in a vl-catch-all-apply statement would stop the program crashing, but I don't think it will catch your error..

You could use the LASTPROMPT sys var I suppose?

Have you checked this?


Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How can I catch a bad entity during an offset command?
« Reply #2 on: December 04, 2009, 04:45:23 PM »
Look into this with adding an error trap.
Code: [Select]
(vlax-invoke-method obj "Offset" dist)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #3 on: December 04, 2009, 05:42:00 PM »
Look into this with adding an error trap.
Code: [Select]
(vlax-invoke-method obj "Offset" dist)

Haha,..... I don't know why I did not suggest that!  :oops:

ronjonp

  • Needs a day job
  • Posts: 7531

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DMSS1

  • Guest
Re: How can I catch a bad entity during an offset command?
« Reply #5 on: December 05, 2009, 07:50:30 AM »
Thanks guys!

Lee Mac , interesting link...

Ronjanop, that's a nice piece of code (staggeredoffset.lsp) (May I use and change your code to my needs?  I like mine, but may incorporate a part of yours into mine, with you permission).

Cab, I haven't ever used any of the vlax- methods an commands.

I want to make sure I have this right:
(vlax-invoke-method obj "Offset" dist)
What happens when the anomaly (bad entity) occurs?
Does it issue a T or nill?  (or null)
Is there some way to determine if this entity was created?

Is there a way to determine is an entity is "bad", without crashing?
Like a validate command?

Thanks all!

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #6 on: December 05, 2009, 08:54:06 AM »
If vla-offset or vlax-invoke obj 'Offset  (same thing effectively), cannot perform an offset, they will throw an error which may be caught by a vl-catch-all-apply statement.

As an example:

Code: [Select]
(setq obj (vlax-ename->vla-object (car (entsel)))) ;; Object to offset

(if (vl-catch-all-error-p
      (vl-catch-all-apply 'vla-offset (list obj 10)))
  (princ "\n** Object could not be Offset **"))

Alternatively:

Code: [Select]
(setq obj (vlax-ename->vla-object (car (entsel)))) ;; Object to offset

(if (vl-catch-all-error-p
      (vl-catch-all-apply 'vlax-invoke (list obj 'Offset 10)))
  (princ "\n** Object could not be Offset **"))

Hope this helps!

Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How can I catch a bad entity during an offset command?
« Reply #7 on: December 05, 2009, 09:21:35 AM »
Here is a more robust example.
Code: [Select]
(defun c:test (/ obj err offsetAmount)
  (setq offsetAmount 10)
  (if (setq obj (car (entsel))) ; entity to offset
    (progn
      (setq obj (vlax-ename->vla-object obj))
      (if (vlax-method-applicable-p obj 'offset) ; test to see if it can be offset
        ;; OK to offset but catch any error when attempting to offset
        (if (vl-catch-all-error-p (setq err
              (vl-catch-all-apply 'vla-offset (list obj offsetAmount)))
            )
          (progn   ; caught an error
            (princ "\n** Object could not be Offset **\n")
            (princ (vl-catch-all-error-message Err))
          )
        )
        (princ "\n** Can not Offset that object **")
      )
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #8 on: December 05, 2009, 09:34:32 AM »
True, I wrote those in the posting box...  :oops:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How can I catch a bad entity during an offset command?
« Reply #9 on: December 05, 2009, 09:40:00 AM »
No harm in that. :-)

From the Help File

Quote
Object   Arc, Circle, Ellipse, Line, LightweightPolyline, Polyline, Spline, XLine
         The object or objects this method applies to.

Distance   Double; input-only
         The distance to offset the object. The offset can be a positive or
         negative number, but it cannot equal zero. If the offset is negative,
         this is interpreted as being an offset to make a "smaller" curve
         (that is, for an arc it would offset to a radius that is "Distance less"
         than the starting curve's radius). If "smaller" has no meaning, then it
         would offset in the direction of smaller X, Y, and Z WCS coordinates.

RetVal   Variant (array of objects)
         An array of the newly created objects resulting from the offset.

Remarks

For many curves, the result of this operation will be a single new curve
(which may not be of the same type as the original curve). For example,
offsetting an ellipse will result in a spline because the result does fit
the equation of an ellipse. In some cases it may be necessary for the
offset result to be several curves.

So the problem becomes "Which side is positive & which is negative"

I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How can I catch a bad entity during an offset command?
« Reply #10 on: December 05, 2009, 09:41:48 AM »
A double offset is easy.

Code: [Select]
;;  CAB 04/12/06
(defun c:dbloffset (/ dist ent obj)
  (and
    (null (initget 7))
    (setq dist (getdist "\nEnter offset distance: "))
    (while (setq ent (entsel "\nSelect object to offset."))
      (if (and (setq obj (vlax-ename->vla-object (car ent)))
               (vlax-method-applicable-p obj "Offset")
          )
        (progn
          (vlax-invoke-method obj "Offset" dist)
          (vlax-invoke-method obj "Offset" (- dist))
        )
        (prompt "\n***  Can not offset that object, try again.  ***")
      )
    )
  )
  (princ)
)
(prompt "\nDouble Offset Loaded, Enter dbloffset to run.")
(princ)

For an example of complex offset problem see this:
http://www.theswamp.org/index.php?topic=2123.msg27165#msg27165
« Last Edit: December 05, 2009, 09:47:52 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #11 on: December 05, 2009, 10:20:20 AM »

DMSS1

  • Guest
Re: How can I catch a bad entity during an offset command?
« Reply #12 on: December 05, 2009, 10:44:26 AM »
Thanks everyone!

I was looking to find a command like "vlax-offset".
After 15 minutes, I came back here...

I think this will give me the answer I was looking for.
Again, Thanks!

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: How can I catch a bad entity during an offset command?
« Reply #13 on: December 05, 2009, 10:46:06 AM »
FYI "vla-offset"

Glad it helps  :-)

ronjonp

  • Needs a day job
  • Posts: 7531
Re: How can I catch a bad entity during an offset command?
« Reply #14 on: December 05, 2009, 02:07:32 PM »
Thanks guys!

Lee Mac , interesting link...

Ronjanop, that's a nice piece of code (staggeredoffset.lsp) (May I use and change your code to my needs?  I like mine, but may incorporate a part of yours into mine, with you permission).

....

Use away  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC