Jump to content

Recommended Posts

Posted

My company is in the ship repair industry and a standard type of drawing we deal with is a system ripout drawing. Normally we take the original installation drawing and then hatch out the relevant equipment and cables in a new drawing by drawing a hatch over the lines representing anything that has to be removed.

 

The company I used to work for used an in-house developed AutoCAD add-on (developed by David Stein, the author of the VLISP Bible) which had a line hatch command. The use of this command was extremely beneficial expecially on large drawings. I recently started writing my own lisp to accomplish the same goal and the code below is what I've come up with.

 

The code works fine like it is, I just know that since I'm not the most efficient code writer, there are probably numerous ways I can improve it. I would appreciate any suggestions or constructive criticisms.

 

(defun *error* ( msg )
 (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
   (princ (strcat "\n ** ERROR: " msg " **")))
 (setq ssline nil)
 (princ)
)
(defun c:autohatch ( / ssline lineobj PtLst ptx1 ptx2 pty1 pty2 )

 (defun hatch_vert_line ( / x1 x2 x3 x4 )
   (setq x1 (- ptx1 0.1))
   (setq x2 (+ ptx1 0.1))
   (setq x3 (+ ptx2 0.1))
   (setq x4 (- ptx2 0.1))
   (entmakex
     (list
        (cons 0 "HATCH")
        (cons 100 "AcDbEntity")
        (cons 8 "0")
        (cons 100 "AcDbHatch")
        (cons 62 256)
        (cons 10 (list 0.0 0.0 0.0))
        (cons 210 (list 0.0 0.0 1.0))
        (cons 2 "ANSI31")
        (cons 70 1)
        (cons 71 0)
        (cons 91 1)
        (cons 92 1)
        (cons 93 4)
        (cons 72 1)
        (cons 10 (list x1 pty1 0))
        (cons 11 (list x2 pty1 0))
        (cons 72 1)
        (cons 10 (list x2 pty1 0))
        (cons 11 (list x3 pty2 0))
        (cons 72 1)
        (cons 10 (list x3 pty2 0))
        (cons 11 (list x4 pty2 0))
        (cons 72 1)
        (cons 10 (list x4 pty2 0))
        (cons 11 (list x1 pty1 0))
        (cons 97 0)
        (cons 75 2)
        (cons 76 1)
        (cons 98 1)
        (cons 10 (list 0.0 0.0 0.0))
     )
   )
   (setq ssline nil)
 )
 (defun hatch_horiz_line ( / y1 y2 y3 y4 )
   (setq y1 (- pty1 0.1))
   (setq y2 (+ pty1 0.1))
   (setq y3 (+ pty2 0.1))
   (setq y4 (- pty2 0.1))
   (entmakex
     (list
        (cons 0 "HATCH")
        (cons 100 "AcDbEntity")
        (cons 8 "0")
        (cons 100 "AcDbHatch")
        (cons 62 256)
        (cons 10 (list 0.0 0.0 0.0))
        (cons 210 (list 0.0 0.0 1.0))
        (cons 2 "ANSI31")
        (cons 70 1)
        (cons 71 0)
        (cons 91 1)
        (cons 92 1)
        (cons 93 4)
        (cons 72 1)
        (cons 10 (list ptx1 y1 0))
        (cons 11 (list ptx1 y2 0))
        (cons 72 1)
        (cons 10 (list ptx1 y2 0))
        (cons 11 (list ptx2 y3 0))
        (cons 72 1)
        (cons 10 (list ptx2 y3 0))
        (cons 11 (list ptx2 y4 0))
        (cons 72 1)
        (cons 10 (list ptx2 y4 0))
        (cons 11 (list ptx1 y1 0))
        (cons 97 0)
        (cons 75 2)
        (cons 76 1)
        (cons 98 1)
        (cons 10 (list 0.0 0.0 0.0))
     )
   )
   (setq ssline nil)
 )
 (while
   (not (setq ssline (ssget ":S" '((0 . "LINE")))))
 )
 (setq lineobj (vlax-ename->vla-object (ssname ssline 0)))
 (vla-getboundingbox lineobj 'MinPt 'MaxPt)
 (setq PtLst (cons (vlax-safearray->list MinPt) PtLst))
 (setq PtLst (cons (vlax-safearray->list MaxPt) PtLst))
 (setq ptx1 (car (car PtLst)))
 (setq pty1 (cadr (car PtLst)))
 (setq ptx2 (car (cadr PtLst)))
 (setq pty2 (cadr (cadr PtLst)))
 (cond
   ((equal ptx1 ptx2 0.001) (hatch_vert_line))
   ((equal pty1 pty2 0.001) (hatch_horiz_line))
   (T (alert "Please pick a vertical or horizontal line."))
 )
 (c:autohatch)
)

autohatch.LSP

Posted

An old attempt of mine can be found here.

 

Lee

 

EDIT: Oops! Looking at your code, I think I misunderstood the post...

Posted

This program actually only hatches horizontal or vertical lines. See the pics below for a comparison of what I am talking about.

 

hatch1.jpg

 

hatch2.jpg

Posted

I think I'd give the command version a go.

 

[b][color=BLACK]([/color][/b]defun c:hatchl [b][color=FUCHSIA]([/color][/b]/ ss en ed p10 p11 h10 h11 h12 h13[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"LINE"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss 0[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                  p10 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 10 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                  p11 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 11 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                  h10 [b][color=GREEN]([/color][/b]polar p10 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p10 p11[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi  0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
                  h11 [b][color=GREEN]([/color][/b]polar p10 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p10 p11[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi -0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
                  h12 [b][color=GREEN]([/color][/b]polar p11 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p11 p10[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi  0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
                  h13 [b][color=GREEN]([/color][/b]polar p11 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p11 p10[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi -0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]command [color=#2f4f4f]"_.HATCH"[/color] [color=#2f4f4f]"ANSI31"[/color] 1 0 [color=#2f4f4f]""[/color] [color=#2f4f4f]"N"[/color] h10 h11 h12 h13 h10 [color=#2f4f4f]""[/color] [color=#2f4f4f]""[/color][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]ssdel en ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

Nothing about hatches seems to have been real consistent over the years.

 

Probably need to address CLAYER, CECOLOR ans well as error trap. You could adjust for line angles as well.

 

My $0.02 -David

Posted

Maybe

(if (equal (rem (angle p10 p11) (* pi 0.5)) 0 1e-
   (command "_.HATCH"....)
)

for orthogonal lines

Posted
I think I'd give the command version a go.

 

[b][color=BLACK]([/color][/b]defun c:hatchl [b][color=FUCHSIA]([/color][/b]/ ss en ed p10 p11 h10 h11 h12 h13[b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"LINE"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
[b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss 0[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
[b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
p10 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 10 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
p11 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 11 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
h10 [b][color=GREEN]([/color][/b]polar p10 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p10 p11[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi 0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
h11 [b][color=GREEN]([/color][/b]polar p10 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p10 p11[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi -0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
h12 [b][color=GREEN]([/color][/b]polar p11 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p11 p10[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi 0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b]
h13 [b][color=GREEN]([/color][/b]polar p11 [b][color=BLUE]([/color][/b]+ [b][color=RED]([/color][/b]angle p11 p10[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* pi -0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] 0.1[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
[b][color=MAROON]([/color][/b]command [color=#2f4f4f]"_.HATCH"[/color] [color=#2f4f4f]"ANSI31"[/color] 1 0 [color=#2f4f4f]""[/color] [color=#2f4f4f]"N"[/color] h10 h11 h12 h13 h10 [color=#2f4f4f]""[/color] [color=#2f4f4f]""[/color][b][color=MAROON])[/color][/b]
[b][color=MAROON]([/color][/b]ssdel en ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

Nothing about hatches seems to have been real consistent over the years.

 

Probably need to address CLAYER, CECOLOR ans well as error trap. You could adjust for line angles as well.

 

My $0.02 -David

 

David,

 

Thanks for the advice but this code isn't working for me. I get the message "Unable to hatch boundary."

 

Since Lee misunderstood what I was doing maybe I wasn't clear enough in what my code does. I'm attempting to hatch individual lines by retrieving the points of the line as a basis to create a hatch by entmakex that will cover the line at a distance of 0.1" on each side of the line.

Posted

See what I mean about inconsistent. The command version has probably changes again ( since a2k ) . I know the dxf codes have change a good bit as well.

 

I think I understand the scenario. ansi31 hatch, scale 1, angle 0, 2 point line path, border points 4, no border. Should be a simple thing, but hot when autodesk gets done with it.

 

-David

Posted

This is the sequence for -hatch in 2009:

 

-hatch

P (Properties)

ANSI31

1.0 (scale)

0.0 (angle)

W for (draW boundary)

N for (Retain polyline boundary?)

PT1

PT2

...

PTN

C (Close)

"" (Accept)

""

 

So I guess the command line code would be:

 

(command "-hatch" "P" "ANSI31" "1" "0" "W" "N" pt1 pt2 pt3 pt4 "C" "" "")

 

 

EDIT:

Confirmed, this line works with my code in lieu of entmakex. So I'm just curious, why do you think the command would be better than entmakex in this situation?

Posted
This is the sequence for -hatch in 2009:

 

-hatch

P (Properties)

ANSI31

1.0 (scale)

0.0 (angle)

W for (draW boundary)

N for (Retain polyline boundary?)

PT1

PT2

...

PTN

C (Close)

"" (Accept)

""

 

So I guess the command line code would be:

 

(command "-hatch" "P" "ANSI31" "1" "0" "W" "N" pt1 pt2 pt3 pt4 "C" "" "")

 

 

EDIT:

Confirmed, this line works with my code in lieu of entmakex. So I'm just curious, why do you think the command would be better than entmakex in this situation?

 

Lonnie,

 

Here is a linetype created with the Express Tools Make Linetype. The file is called Hatch.lin. It uses the text shape / in it. You can adjust it with ltscale.

See attached.

hatch.zip

Document1.jpg

Posted

I just realized the hatch is running in the wrong direction for the lines on the side. Cannot seem to fix this unless another linetype is created with the slash in the other direction.

 

I guess you can't use this then.

 

Sorry, I tried.

Posted
I just realized the hatch is running in the wrong direction for the lines on the side. Cannot seem to fix this unless another linetype is created with the slash in the other direction.

 

I guess you can't use this then.

 

Sorry, I tried.

 

Thanks Buzzard. Actually we don't have an exact requirement as far as the angle the hatch is drawn. As long as the equipment being removed is hatched I'm pretty sure no one will say anything. I actually tried to do my own linetype but I just couldn't get it to work.

 

I'll probably stick to David's modified routine but I do appreciate your sharing of the hatch linetype. Thanks!

Posted
Thanks Buzzard. Actually we don't have an exact requirement as far as the angle the hatch is drawn. As long as the equipment being removed is hatched I'm pretty sure no one will say anything. I actually tried to do my own linetype but I just couldn't get it to work.

 

I'll probably stick to David's modified routine but I do appreciate your sharing of the hatch linetype. Thanks!

 

No problem. Good Luck

Posted

Lonnie,

 

Glad it worked. You probably need to address the SNAPMODE & OSMODE as well.

 

As far entmake, I would have thought you would have to calulate and provide groups 45, 46, 53. Also I would think an entmake call would fail if ansi31 were not already loaded ( initial call ) whereas a command call will load it automatically ( as long as the pattern is defined in acad.pat ).

 

-David

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