Offset Polyline

Offset Polyline

bedyr
Advocate Advocate
3,644 Views
35 Replies
Message 1 of 36

Offset Polyline

bedyr
Advocate
Advocate

Hi

I create a lisp file but I didn't go on for a error. Anybody can help me?

 

(defun c:pp () 
  (setq p1 (getpoint "get first point : "))
  (setq p1x (car p1))
  (setq p1y (cadr p1))
  (setq count 1)
  (while (< count 100) 
    (setq r (getpoint p1 "reference line"))
    (setq s (getpoint "side"))
    (setq rx (car r))
    (setq ry (cadr r))
    (setq sx (car s))
    (setq sy (cadr s))
    (cond 
      ((and (< ry sy) (= p1x rx) (setq p2 (list p1x (+ ry 5)))))
      ((and (> ry sy) (= p1x rx) (setq p2 (list p1x (- ry 5)))))
      ((and (< rx sx) (= p1y ry) (setq p2 (list (+ rx 5) p1y))))
      ((and (> rx sx) (= p1y ry) (setq p2 (list (- rx 5) p1y))))
      ((and (< rx sx) (< p1x sx) (< p1x rx)) (setq p2 (list (+ rx 5) ry)))
      ((and (> rx sx) (< p1x sx) (< P1x rx)) (setq p2 (list (- rx 5) ry)))
    )
    (command "pline" p1 p2 "")
    (setq p1 p2 )
    (setq count (+ count 1))
  )
)
0 Likes
Accepted solutions (3)
3,645 Views
35 Replies
Replies (35)
Message 21 of 36

hak_vz
Advisor
Advisor
Accepted solution

@bedyr       Here is finalized code, at least from my side. It works as follows:

1) Create lines from objects that you want to join that are perpendicular to  object you are offsetting from so that this lines finish at that object

2) Star to create offset polyline starting from a point where first joining line touches object you are offsetting from, and finishes at similar point at the end. Finish this sequence with <enter>

3) Code will start commands TRIM and JOIN to trim or extend joining lines and finaly to join all segments into a single polyline.  Command TRIM have option to extend a line when <shift> is pressed, so follow command options in console.

 

This is from my perspective the best I can do, and I hope this will work for you. It is not fully automated as you would like but I guess you get used to way it works.

 

 

(defun c:pp ( / off ss p1 p2 s m enr seg seglist p1 p2 p3 p4 i n int ptlist fst lst i k n *error* point2d perperdicular_from_point_to_line)  

;Creates polyline that is offested to left or right from object egdes 
;Author: hak_vz (27.06.2020.)


   (defun *error* (msg)
        ;(vl-bt)
        (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*"))
        
        (progn
           (princ (strcat "\nOops an Error : ( " msg " ) occurred."))
        )
        ) 
        (setvar 'osmode old)
        (setvar 'cmdecho 1)
        (princ)
    )
    
    (defun point2d (pt) (list (car pt) (cadr pt)))    
    (defun perperdicular_from_point_to_line (lin1 lin2 p / x1 y1 x2 y2 x3 y3 k m n ret)
    ;returns point on a line (line1 line2) as a perpendicular projection from point p
        (mapcar 'set '(x1 x2 x3) (mapcar 'car (list lin1 lin2 p)))
        (mapcar 'set '(y1 y2 y3) (mapcar 'cadr (list lin1 lin2 p)))
        (setq 
            m (-(*(- y2 y1) (- x3 x1))(*(- x2 x1) (- y3 y1)))
            n (+(* (- y2 y1)(- y2 y1))(*(- x2 x1)(- x2 x1)))
        )
        (cond 
            ((/= n 0.0) 
                (setq 
                    k (/ m n)
                    ret (list(- x3 (* k(- y2 y1)))(+ y3 (* k(- x2 x1))))
                )
            )
        )
        ret
    )    
    
    
    (setq old (getvar 'osmode))
    (setvar 'cmdecho 0)
    (setvar 'osmode 1)
    (setq
        off (getreal "\nEnter offset value >")
        ptlist nil
        ss (ssadd)
        p1 (point2d(getpoint "\nSelect 1. point >"))
        p2 (point2d(getpoint "\nSelect 2. point >"))
        k p2
    )
    (setvar 'osmode 0)
    (setq 
        s (point2d(getpoint "\nPick side> "))
        m (perperdicular_from_point_to_line p1 p2 s)
     )
    (command "_.line" p1 p2 "")
    (command "_.move" (entlast) "" m (polar m  (angle m s) off))
    (setq 
        p1 (mapcar '+ p1 (mapcar '- (polar m  (angle m s) off) m))
        p2 (mapcar '+ p2 (mapcar '- (polar m  (angle m s) off) m))
        seg (mapcar 'point2d (list p1 p2))
        fst (car seg)
    )
    (ssadd (entlast) ss)
    (setq 
        seglist (cons seg seglist)
        p1 k
        p2 nil
    )
    (setvar 'osmode 1)
    (while (and (setq p2 (getpoint "\nSelect 2. point >")))
       (setq p2 (point2d p2) k p2)
        (setvar 'osmode 0)
        (setq s (point2d(getpoint "\nPick side> ")))
        (setq m (perperdicular_from_point_to_line p1 p2 s))
        (command "_.line" p1 p2 "")
        (command "_.move" (entlast) "" m (polar m  (angle m s) off))
        (setq p1 (mapcar '+ p1 (mapcar '- (polar m  (angle m s) off) m)))
        (setq p2 (mapcar '+ p2 (mapcar '- (polar m  (angle m s) off) m)))

        (ssadd (entlast) ss)
        (setq seg (mapcar 'point2d (list p1 p2)))
        (ssadd (entlast) ss)
        (setq seglist (cons seg seglist))
        (setq p1 k p2 nil)
        (setvar 'osmode 1)
    )
    (setq lst (cadr seg))
    (setq seglist (reverse seglist))
    (setq i 0 n (- (length seglist) 1))
    (while (< i n)
    (setq seg1 (nth i seglist) seg2 (nth (+ i 1) seglist))
    (setq p1 (car seg1) p2 (cadr seg1) p3 (car seg2) p4 (cadr seg2))
    (setq int (inters p1 p2 p3 p4 nil) ptlist (cons int ptlist))
    (setq i (+ i 1))
    )
    (setvar 'osmode 0)
    
    (setq ptlist (reverse ptlist))
    (command "_.erase" ss "")
    (command "_.pline" fst)
    (setvar 'osmode old)
    (setvar 'cmdecho 1)
    (while ptlist (command (car ptlist)) (setq ptlist (cdr ptlist)))
    (command lst "")
    (princ "\nCommand TRIM is started. Cut or extend connecting lines or hit <enter> to continue")
    (command "_.trim" (entlast) "")
    (while (> (getvar 'cmdactive) 0) (command pause))
    (princ "\nCommand JOIN is started. Select segments to join into single polyline or hit <enter> to exit")
    (command "_.join" (entlast))
    (while (> (getvar 'cmdactive) 0) (command pause))
    (princ)
)

 

slope4.jpgslope5.jpgslope6.jpgslope7.jpgslope8.jpg

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 22 of 36

bedyr
Advocate
Advocate

Thank you so much but it is not usable. I create my code and I found it is error. I want to upgrade this code. Can you try this?  It is working good but a little missing.

 

(defun c:pp () 
  (setq p1 (getpoint "get first point : "))
  (while (setq r (getpoint p1 "reference line or <exit>")) 
    (setq p1x (car p1))
    (setq p1y (cadr p1))
    (setq s (getpoint "side"))
    (setq rx (car r))
    (setq ry (cadr r))
    (setq sx (car s))
    (setq sy (cadr s))
    (cond 
      ((and (= p1x rx) (> p1y ry) (> sy ry)) (setq p2 (list p1x (+ ry 5))))
      ((and (= p1x rx) (> p1y ry) (> ry sy)) (setq p2 (list p1x (- ry 5))))
      ((and (= p1x rx) (> ry p1y) (> sy ry)) (setq p2 (list p1x (+ ry 5))))
      ((and (= p1x rx) (> ry p1y) (> ry sy)) (setq p2 (list p1x (- ry 5))))
      ((and (= p1y ry) (> rx p1x) (> rx sx)) (setq p2 (list (- rx 5) p1y)))
      ((and (= p1y ry) (> rx p1x) (> sx rx)) (setq p2 (list (+ rx 5) p1y)))
      ((and (= p1y ry) (> p1x rx) (> sx rx)) (setq p2 (list (+ rx 5) p1y)))
      ((and (= p1y ry) (> p1x rx) (> rx sx)) (setq p2 (list (- rx 5) p1y)))
      ((and (> rx p1x) (> p1y ry) (> sx rx)) (setq p2 (list (+ rx 5) p1y)))
      ((and (> rx p1x) (> p1y ry) (> rx sx)) (setq p2 (list (- rx 5) p1y)))
      ((and (> p1x rx) (> p1y ry) (> ry sy)) (setq p2 (list p1x (- ry 5))))
      ((and (> p1x rx) (> p1y ry) (> sy ry)) (setq p2 (list p1x (+ ry 5))))
      ((and (> p1x rx) (> ry p1y) (> rx sx)) (setq p2 (list (- rx 5) p1y)))
      ((and (> p1x rx) (> ry p1y) (> sx rx)) (setq p2 (list (+ rx 5) p1y)))
      ((and (> rx p1x) (> ry p1y) (> sy ry)) (setq p2 (list p1x (+ ry 5))))
      ((and (> rx p1x) (> ry p1y) (> ry sy)) (setq p2 (list p1x (- ry 5))))
    )
    (command "pline" p1 p2 "")
    (setq p1 p2)
  )
)

 

0 Likes
Message 23 of 36

hak_vz
Advisor
Advisor

Please explain why my code is not usable, I'll make my best to fix it to work the way you want.

Regarding your code I would have to grasp the idea you want to use since as is written now it wont lead you to the solution. I'll spend some time on it but don't promise anything.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 24 of 36

bedyr
Advocate
Advocate

It is not usable because this code will use for many many times not one or two times every time. The less mouse movement, the fast result. I want draw to start point to endpoint speedly. Not use command and back to trim and join. 

for example my code is miss end point (object basepoint or what I want point) and pedit. I will work on this two actions. 

0 Likes
Message 25 of 36

hak_vz
Advisor
Advisor

Your code works as a such but you have to turn off offset and pick side relative to point p2. At the end you receive set of line objects.

 

I'll change my code.

 

Your offset is 5. Will you always use that value or some same value inside whole drawing?

Do line segments suite you that I exclude joining into polyline.?

 

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 26 of 36

bedyr
Advocate
Advocate

"but you have to turn off offset and pick side relative to point p2. At the end you receive set of line objects."

Can you show that in my code? 

Yes almost using 5 offset. 

0 Likes
Message 27 of 36

hak_vz
Advisor
Advisor

@bedyr wrote:

"but you have to turn off offset and pick side relative to point p2. At the end you receive set of line objects."

Can you show that in my code? 


Sorry a typo. Turn off osnap.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 28 of 36

hak_vz
Advisor
Advisor

Here you have another code variant

1) command PPINIT is used to set or reset offset value. You will be asked for offset value only at first run of command pp. If you want to change it use PPINIT

 

2) Command PP now joins together drawing joint lines and offset line. After you start command PP it asks you to draw two joint lines from outside to offset object. Both joint lines must finish at object you are offsetting from.

3) After two joints are created, operation of drawing offset line starts. When finished at second joint line hit <enter> and all elements will be connected.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 29 of 36

bedyr
Advocate
Advocate

This is better than other.  Line and offset line not join. Do you know about that?

0 Likes
Message 30 of 36

Sea-Haven
Mentor
Mentor

Will try to find the water main code may have posted and not kept.

0 Likes
Message 31 of 36

hak_vz
Advisor
Advisor

@bedyr  I will try to make some changes , but problem is how to define exit from offseting procedure. Have some priority work to do.

When you create this offseted line, offset side is always either in or out? If yes then picking points and side can be replaced with reading vertexes.

Also, when you join two objects, offsetted line is drawn along either shortest path or longest path? So instead picking points and side all it has to be done is to offset an object, draw joining lines and trim off what is not needed. 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 32 of 36

bedyr
Advocate
Advocate

offset side is not constant. I am selecting offset side. Anytime short, anytime long path offsetted line.  

0 Likes
Message 33 of 36

hak_vz
Advisor
Advisor

OK, I will try to make some changes later today.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 34 of 36

hak_vz
Advisor
Advisor

I've tried some other options but nothing is fast enough. Here you have a code that offsets starting object both side and create rectangle at each its vertex. Maybe useful for something.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 35 of 36

bedyr
Advocate
Advocate

Thank you. At first I am using autocad for Mac for that not compatible vlax. Secondly I don't know where can I use that. I am upgrading my code and now it is drawing polyline start to end. Maybe you can help me to upgrade my code :))

0 Likes
Message 36 of 36

hak_vz
Advisor
Advisor

I've tryed, but sequence of feeding pline doesn't work in all situations. Have some idea, will post later.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes