AutoLISP to offset closed polyline, hatch, and delete the second polyline [C3D]

AutoLISP to offset closed polyline, hatch, and delete the second polyline [C3D]

Anonymous
Not applicable
13,504 Views
106 Replies
Message 1 of 107

AutoLISP to offset closed polyline, hatch, and delete the second polyline [C3D]

Anonymous
Not applicable

Hi. In our firm, we often created partial hatches within buildings (represented by closed polylines). The process is as follows:
1. We select a closed polyline that was previously drawn and that represents the outline of a building.

2. We offset the polyline 2 units inside, creating a scaled version of the original polyline within the first. 

3. We hatch the area within the two polylines with a specific hatch pattern, scale, layer and color.

4. We delete the smaller, inside polyline, leaving the original building outline and the new hatch within.

I'm looking to automate this process in some way. I had some marginal success with the Action Recorder, but it wasn't consistent if the building outline was angled, and I had to always use the rightmost edge of the building. I'm sure AutoLISP can automate this sequence, but I'm a complete beginner.

Can anybody spoonfeed me a script that does what I'm looking for? Thanks a whole ton.

13,505 Views
106 Replies
Replies (106)
Message 81 of 107

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

.... after selecting object it automatically selects displacement point anywhere ... so that we just need to enter value. ....


... [and aim for the direction].  This should do that:

      (command-s "_.copy" ent1 "" (getvar 'viewctr))
Kent Cooper, AIA
0 Likes
Message 82 of 107

saqib_tipa
Advocate
Advocate

@Kent1Cooper 

How to make selection of multiple objects.

as you said 

"2)  allow selection of multiple objects, to be Copied/Closed/Hatched by the same distance and direction;"

 

Thank you.

0 Likes
Message 83 of 107

john.uhden
Mentor
Mentor

@Kent1Cooper ,

I don't get it.
You have provided only one point to the copy command.

Since he wants to copy only in any one of four directions (Up, Down, Left, Right) and since he has already provided an "offset" distance (let's say D), then (not a complete example)...

(defun copy (ent1)
  (initget 1 "Up Down Left Right")
  (setq dir (getkword "\nDirection [Up/Down/Left/Right]: "))
  (setq disp
    (cond
      ((= dir "Up") (strcat "@0,"(rtos D 2 2)",0"))
      ((= dir "Down")(strcat "@0,"(rtos (- D) 2 2)",0"))
      ((= dir "Left")(strcat "@"(rtos (- D) 2 2)",0,0"))
      ((= dir "Right")(strcat "@"(rtos D 2 2)",0,0"))
    )
  )
  (command-s "_.copy" ent1 "" (getvar 'viewctr) disp)
)

Actually, this implies that he has to provide the direction for each object selected.

Rearranging the code could apply the direction to an entire selection set, if that is his desire, either one by one or in bulk.

 

John F. Uhden

0 Likes
Message 84 of 107

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

....  You have provided only one point to the copy command. .....


You must not have noticed that the COPY command is in a (command-s) function.  It will wait for the second point.  Alternatively, in a regular (command) function, the pause for the second point would be supplied [see my next Message].

Kent Cooper, AIA
0 Likes
Message 85 of 107

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

.... How to make selection of multiple objects. as you said ....


Try this [lightly tested].  It also allows selection of open-ended objects other than Polylines.

(defun C:CCH ; = Copy, Close, and Hatch
  (/ *error* doc ss p1 p2 n ent1 ss2 ent2)
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (vla-endundomark doc)
    (princ)
  ); defun - *error*
  (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  (prompt "\nTo Copy, Close and Hatch open-ended object(s),")
  (if
    (setq ss
      (ssget "_:L"
        (list
          '(-4 . "<OR")
            '(0 . "LINE,ARC"); any [always open]
            '(-4 . "<AND")
              '(0 . "*POLYLINE,SPLINE") '(-4 . "<NOT") '(-4 . "&") '(70 . 1) '(-4 . "NOT>"); open only
            '(-4 . "AND>")
            '(-4 . "<AND")
              '(0 . "ELLIPSE")
              '(-4 . "<NOT") '(-4 . "<AND") '(41 . 0.0) (cons 42 (* pi 2)) '(-4 . "AND>") '(-4 . "NOT>"); open only
            '(-4 . "AND>")
          '(-4 . "OR>")
        ); filter list
      ); ssget & ss
    ); setq
    (progn ; then
      (setq
        p1 (getvar 'viewctr)
        p2 (getpoint p1 "\nDirection and distance to Copy: ")
      ); setq
      (repeat (setq n (sslength ss))
        (setq
          ent1 (ssname ss (setq n (1- n)))
          ss2 (ssadd ent1)
        ); setq
        (command "_.copy" ent1 "" "_non" p1 "_non" p2)
        (ssadd (setq ent2 (entlast)) ss2)
        (command "_.line" "_non" (vlax-curve-getStartPoint ent1) "_non" (vlax-curve-getStartPoint ent2) "")
        (ssadd (entlast) ss2)
        (command "_.line" "_non" (vlax-curve-getEndPoint ent1) "_non" (vlax-curve-getEndPoint ent2) "")
        (ssadd (entlast) ss2)
        (command "_.hatch" "USER" "45" 1 "_no" ss2 ""); <-- EDIT 1 as preferred
      ); repeat
    ); progn -- then
    (prompt "\nNo open-ended unlocked object(s) selected."); else
  ); if
  (vla-endundomark doc)
  (princ)
); defun -- C:CCH
(vl-load-com)
(prompt "\nType CCH to Copy, Close and Hatch open-ended objects.")

Of course, as with the earlier one, it is up to you to use it appropriately for the configuration of the object(s) involved.  It will do this kind of thing [here with an Arc, but potentially with several other kinds of objects]:

Kent1Cooper_0-1721048573840.png

Kent Cooper, AIA
Message 86 of 107

saqib_tipa
Advocate
Advocate

@john.uhden 

Hello, can we put this code in previous provided code and if so where to add this.

When you are easy, can you make a full code as you made offset hatch command where we just need to click the side and give the value so it adds the hatch on that side. (for me it will be upside or downside).

Thank you.

0 Likes
Message 87 of 107

john.uhden
Mentor
Mentor

@Kent1Cooper ,

Okay.  That's pretty cool.  He can turn on ortho for Up, Down, Left, Right.

John F. Uhden

0 Likes
Message 88 of 107

john.uhden
Mentor
Mentor

@saqib_tipa ,

I think @Kent1Cooper provided you an excellent solution.  If you haven't already, please try it out and I am sure you will agree.

John F. Uhden

0 Likes
Message 89 of 107

saqib_tipa
Advocate
Advocate

@Kent1Cooper 

Thank you so much for the code. it is working very nice.

But if it is not too much asking, I come to know that it is not a good way to select auto point on the mid screen, can you plz tell me how to make it to select manual on the screen like before. I tried it but cannot do so.

Thank you.

0 Likes
Message 90 of 107

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

... it is not a good way to select auto point on the mid screen, can you plz tell me how to make it to select manual on the screen ....


Try changing this:

p1 (getvar 'viewctr)

to this:

p1 (getpoint "\nBase point for defining displacement: ")

Though I don't understand what's not good about letting it use the middle of the current view, to spare you the need to specify a start point [i.e. to save you a step].  If the displacement is going to be applied to multiple objects, having you pick a start, maybe with a particular relationship to any of the objects, will be irrelevant for the others, so it seems to me a "generic" displacement is appropriate.

Kent Cooper, AIA
0 Likes
Message 91 of 107

saqib_tipa
Advocate
Advocate

duplicate msg..looking for delete option.

0 Likes
Message 92 of 107

saqib_tipa
Advocate
Advocate

@Kent1Cooper,

viewcenter is not good because when ortho mode is on, you apply this command then curser position does not appear where it should be e.g down or up so you have to move curser all way up or down and it takes more time to apply command. I don't know if this code can be modified in the way that it may work only in one direction up or down then there will be no need to click after selecting objects. But I know it's very good code which can do more than that. 

Thank you for your time and the code.

0 Likes
Message 93 of 107

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

.... modified in the way that it may work only in one direction up or down ....


If you really want only up, or only down, built into the command definition, that can be done.  If you want the choice within one command of either up or down, but only those two directions, simply having Ortho mode turned on helps a lot, but will not prevent the orthogonal sideways directions.  Or would you want an option for Up or Down, which you would need to specify [e.g. with a U/D choice] each time separately from the distance part?

 

When drawing manually, while drawing something like a Line, or for the second point of displacement in a Move or Copy command, you can type in <90 and have your next point restricted in direction from the previous point to only 90° [and the opposite direction 270°], which is like Ortho but prevents the sideways directions.  However, I have not found a way to get that to work within an AutoLisp routine -- the language can be included, but it seems to ignore it.  [I'll keep trying to find a way.]

Kent Cooper, AIA
0 Likes
Message 94 of 107

john.uhden
Mentor
Mentor

@Kent1Cooper ,

I was under the impression that he wanted to 'CCH' a selection set of polylines all in the same direction and distance.  That's why I posted my 'copy' function.  That way he needs to supply the distance just once and the direction (limited to just Up or Down) just once, and all the selection set members are processed by the rest of your code.

BTW, I don't see why he has a problem with "viewctr" except if he is wearing blinders.

John F. Uhden

0 Likes
Message 95 of 107

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

.... I was under the impression that he wanted to 'CCH' a selection set of polylines all in the same direction and distance. ...  supply the distance just once and the direction ... just once....


[Not necessarily a "he,", but anyway....]

The code in Message 85, with or without the adjustment at Message 90, does that much.  It's not limited to just Up or Down, but that's a separate question, about which I await a response.

Kent Cooper, AIA
0 Likes
Message 96 of 107

Sea-Haven
Mentor
Mentor

"supply the distance just once and the direction" if only up or down then a +ve number is up a -ve number is down.

 

(command "copy" ss "" "0,0,0" (list 0 dist 0))
0 Likes
Message 97 of 107

saqib_tipa
Advocate
Advocate

@Kent1Cooper 

If possible, only up or down then it will be great. As @Sea-Haven and @john.uhden provided some examples and if that can be compiled in your code.  I think maybe we can have then two codes for up (CCHU) and down (CCHD)  or as you said we can have two options at same time for Up or Down, which specify U/D choice. 

Thank you .

0 Likes
Message 98 of 107

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

.... If possible, only up or down then it will be great. .... two codes for up (CCHU) and down (CCHD) ....


Try the attached CopyCloseHatchUpDown.lsp, which defines a CCHU command and a CCHD command, using a shared sub-routine that does all the work, with a couple of arguments appropriate to each direction.  After the first use, it remembers your specified distance [which can be typed in or by on-screen picks], and offers it as default on subsequent use.

Kent Cooper, AIA
Message 99 of 107

saqib_tipa
Advocate
Advocate

@Kent1Cooper 

Thank you so much for the code. It is very nice Lisp as my requirement and really does the work. With both options CCHU and CCHD and also stores last value used is a plus point.

 

Thank you for being so helpful, your time and the code.

I am so Thankful.

 

 

0 Likes
Message 100 of 107

Kent1Cooper
Consultant
Consultant

Since the structure of that makes it so easy to do, attached is a version for all four orthogonal directions, with CCHL [Left] and CCHR [Right] commands added,

Kent Cooper, AIA