Move line

Move line

chan230984
Advocate Advocate
3,049 Views
38 Replies
Message 1 of 39

Move line

chan230984
Advocate
Advocate

Hi all. Please help me write lisp

I have a red line and a green line.
I want the green line Moved to the red line, With the green end of the line just attached to the red line

thanks

 

Untitled.png

0 Likes
Accepted solutions (3)
3,050 Views
38 Replies
Replies (38)
Message 21 of 39

sby0531
Enthusiast
Enthusiast

I have modified my code as follows:

(defun C:MvLines  (/		lst-RoadLine ena-GreenLine	       go-on1	    lst0-ena
		   lst1-ena	jud-proper   ena-LeftSlope	       ena-RoadTop  ena-RightSlope
		   pt01		pt02	     pt03	  pt04	       pt05	    pt06
		   width-road	dist0-hor    vect01	  vect02       sin01	    sin02
		   dist1-hor	vect03	     sin03	  jud-proper1  pt-int01	    pt-a
		   cons01	pt-int02     cons02	  pt-from      pt-from)
  (setq	m:err	*error*
	*error*	*merrmsg*)
  (Set-Cmdecho&Osnapcoord)
  (princ (strcat "\n Function : Move a green line and related culvert lines to proper"
		 " position between two red lines of road side slopes.\n"))
  (syms->T 'go-on0)
  (while go-on0
    (and
      ;;01 
      (progn
	(syms->nil '(lst-RoadLine ena-GreenLine lst-CulvertLine))
	(syms->T 'go-on1)
	(while go-on1
	  (and
	    ;;A01 
	    (setq lst0-ena (SsGet1
			     (list (strcat "Select the related lines(including 1 green"
					   " and 3 red): ")
				   "(Quantity of Selected lines)"
				   '((0 . "line"))
				   '(SymS->Nil '(go-on1 go-on0)))))
	    ;;A02 
	    (progn
	      (setq jud-proper (>= (length lst0-ena) 4))
	      (if (not jud-proper)
		(princ-alert
		  "Quantity of Selected lines should not be less than 4!"))
	      jud-proper)
	    ;;A03   
	    (progn
	      (setq lst1-ena   (all-if '(lambda	(ena0)
					  (= 3 (CL-ena ena0)))
				       lst0-ena)
		    jud-proper (= 1 (length lst1-ena)))
	      (if (not jud-proper)
		(princ-alert "There should be 1(and only 1)GREEN line!"))
	      jud-proper)
	    ;;A04 
	    (and
	      ;;B01 
	      (progn
		(setq ena-GreenLine (car lst1-ena)
		      lst1-ena	    (all-if '(lambda (ena0)
					       (= 1 (CL-ena ena0)))
					    lst0-ena)
		      jud-proper    (= 3 (length lst1-ena)))
		(if (not jud-proper)
		  (princ-alert "There should be 3(and only 3)RED lines!"))
		jud-proper)
	      ;;B02 
	      (progn
		(setq jud-proper (P-Enas-3DLinkedLine lst1-ena))
		(if (not jud-proper)
		  (princ-alert "The 3 red lines should be linked!"))
		jud-proper))
	    ;;A05 
	    (progn
	      (setq lst-RoadLine    lst1-ena
		    lst-CulvertLine (remove-items
				      (cons ena-GreenLine lst-RoadLine)
				      lst0-ena))
	      (syms->nil 'go-on1)))
	  (if lst0-ena
	    (UnHlight lst0-ena)))
	(and ena-GreenLine lst-RoadLine))
      ;;02 
      (progn
	(syms->nil 'jud-proper1)
	(setq lst-RoadLine
	       (vl-sort	lst-RoadLine
			'(lambda (ena1 ena2 /)
			   (< (car (apply 'Vector+ (EndPoints0-Ena ena1)))
			      (car (apply 'Vector+ (EndPoints0-Ena ena2)))))))
	(mset '(ena-LeftSlope ena-RoadTop ena-RightSlope) lst-RoadLine)
	(mset '(pt01 pt02) (EndPoints-Ena ena-LeftSlope))
	(mset '(pt03 pt04) (EndPoints-Ena ena-RightSlope))
	(mset '(pt05 pt06) (EndPoints-Ena ena-GreenLine))
	(cond
	  ((progn
	     (setq width-road (distance pt02 pt03)
		   dist0-hor  (- (car pt03) (car pt02)))
	     (not (equal width-road dist0-hor 0.001)))
	   (princ-alert "The road top line should be level!"))
	  ((progn
	     (setq vect01 (VectorExpr->Vector (list pt01 pt02))
		   vect02 (VectorExpr->Vector (list pt03 pt04)))
	     (mset '(sin01 sin02)
		   (mapcar 'Sin-2dVector (list vect01 vect02)))
	     (or (<= sin01 0)
		 (>= sin02 0)
		 (and (equal 1 sin01 0.001)
		      (equal -1 sin02 0.001))))
	   (princ-alert
	     "The gradients of two side slopes should be proper!"))
	  ((progn
	     (setq dist1-hor (- (car pt06) (car pt05)))
	     (<= dist1-hor dist0-hor))
	   (princ-alert
	     (strcat "The horizontal length of the green line should exceed"
		     " that of the top red line!")))
	  ((progn
	     (setq vect03 (VectorExpr->Vector (list pt05 pt06))
		   sin03  (Sin-2dVector vect03))
	     (< (abs sin03) 1e-4))
	   (princ-alert "The culvert line should not be level!"))
	  (T
	   (syms->T 'jud-proper1)))
	jud-proper1)
      ;;03 
      (setq pt-int01 (if (< sin03 0)
		       (Inters1	(list pt03 pt04)
				(list (setq pt-a (vector+ pt02 vect03))
				      (vector- pt-a vect01))
				nil)
		       (Inters1	(list pt01 pt02)
				(list (setq pt-a (vector- pt03 vect03))
				      (vector+ pt-a vect02))
				nil)))
      ;;04 
      (progn
	(if (< (car pt-int01) (car pt03))
	  (setq	cons01	 (list ena-LeftSlope pt-int01 pt02)
		pt-int02 (vector+ pt-int01 vect03)
		cons02	 (list ena-RightSlope pt03 pt-int02)
		pt-from	 pt05)
	  (setq	cons01	 (list ena-RightSlope pt03 pt-int01)
		pt-int02 (vector- pt-int01 vect03)
		cons02	 (list ena-LeftSlope pt-int02 pt02)
		pt-from	 pt06))
	(cmd-move (cons ena-GreenLine lst-CulvertLine)
		  (list pt-from pt-int01))
	(foreach cons0	(list cons01 cons02)
	  (apply 'Put-EndPoints cons0))
	(princ "\n Done."))))
  (setq	*error*	m:err
	m:err	nil)
  (princ))

001.gif

 

Message 22 of 39

chan230984
Advocate
Advocate

@sby0531 

Hi friend

When I

Command: MVLINES
no function definition: SET-CMDECHO&OSNAPCOORD
Command:

Untitled.png

0 Likes
Message 23 of 39

devitg
Advisor
Advisor

erased

 

 

 

0 Likes
Message 24 of 39

sby0531
Enthusiast
Enthusiast

@chan230984  已写:

@sby0531 

Hi friend

When I

Command: MVLINES
no function definition: SET-CMDECHO&OSNAPCOORD
Command:

Untitled.png


Yes, it's normal. Because a lot of self-defined functions including Set-Cmdecho&Osnapcoord have been called directly and indirectly in the command function C:MvLines. I need to spend some time, perhaps one day , to pick out all the related self-defined functions and send them to you.  And only after loading all the  related self-defined functions, the command function C:MvLines can work.

The reason to develope so many commonly used self-defined functions is to simplify coding. I think it's the typical way for LISP coding.

Message 25 of 39

devitg
Advisor
Advisor
0 Likes
Message 26 of 39

john.uhden
Mentor
Mentor
Imagine if half way through the scribe's boss said, "Wait a second. I don't
like that font. Change it to a different style!" Or "I said landscape, not
portrait!"

John F. Uhden

Message 27 of 39

chan230984
Advocate
Advocate

@sby0531 

Why does it work on your computer?
But can't work on my computer

0 Likes
Message 28 of 39

sby0531
Enthusiast
Enthusiast
Accepted solution

@chan230984  已写:

@sby0531 

Why does it work on your computer?
But can't work on my computer


Hi, I have added all the self-defined functions  called in C:MvLines to the attached LSP file, please download it and try. Good luck!

Message 29 of 39

chan230984
Advocate
Advocate

@sby0531 

Thank you very much
Your help Make my work faster 😀

0 Likes
Message 30 of 39

ВeekeeCZ
Consultant
Consultant
Accepted solution

Just a few lines which probably do the same as the multi-page solution code.

 

(vl-load-com)

(defun c:MoveGreenLine ( / s l e s1 s2 p m)
  
  (and (princ "\nSelect one green line and at least two slope lines of any color but green,")
       (setq s (ssget "_:L" '((0 . "LINE"))))
       (setq l (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))) 					; list of ents
       (setq l (mapcar '(lambda (x) (cons x (vl-sort (list (cdr (assoc 10 (entget x))) (cdr (assoc 11 (entget x)))) '(lambda (p r) (< (car p) (car r)))))) l))	; (en left-pt right-pt)
       (setq e (car (vl-remove-if-not '(lambda (x) (= 3 (cdr (assoc 62 (entget (car x)))))) l)))  	; green line
       (setq l (vl-remove e l))										; all but green line
       (setq l (vl-sort l '(lambda (p r) (> (abs (- (car (cdaddr p)) (cadadr p))) (abs (- (car (cdaddr r)) (cadadr r)))))))  ; sort reds by delta-y to get slopes
       (setq s1 (car l))									  	; first slope
       (setq s2 (cadr l))									  	; second slope
       (setq p (inters (setq m (polar (cadr s1) (angle (cadr e) (caddr e)) (distance (cadr e) (caddr e))))
		       (polar m (angle (cadr s1) (caddr s1)) (distance (cadr s1) (caddr s1)))
		       (cadr s2)
		       (caddr s2)))
       (vl-cmdf "_.move" (car e) "" "_non" (caddr e) "_non" p))
  (princ)
  )
Message 31 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

thank you 😀

0 Likes
Message 32 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

Hi friend

Why does this Lisp work with some line?

Please check the files I attached.

 

Untitled.png

0 Likes
Message 33 of 39

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, issue fixed. Also, accepted your color based distribution: now it moves all selected objects but red lines.

 

(vl-load-com)

(defun c:MoveGreenLine ( / s l e s1 s2 p m)
  
  (and (princ "\nSelect one green line and at least two slope lines of any color but green,")
       (setq s (ssget "_:L" '((0 . "LINE"))))								
       (setq l (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))) 					; list of ents
       (setq l (mapcar '(lambda (x) (cons x (vl-sort (list (cdr (assoc 10 (entget x)))			; list of (en left-pt right-pt)
							   (cdr (assoc 11 (entget x))))
						     '(lambda (p r) (< (car p) (car r)))))) l))		
       (setq e (car (vl-remove-if-not '(lambda (x) (= 3 (cdr (assoc 62 (entget (car x)))))) l)))  	; green line
       (setq l (vl-remove-if-not '(lambda (x) (= 1 (cdr (assoc 62 (entget (car x)))))) l))		; all red lines
       (setq l (vl-sort l '(lambda (p r) (> (abs (- (car (cdaddr p)) (cadadr p)))			; sort reds by delta-y to get slopes
					    (abs (- (car (cdaddr r)) (cadadr r)))))))  
       (setq l (vl-sort (list (car l) (cadr l)) '(lambda (p r) (< (caadr p) (caadr r)))))    ; sort reds from left to right 
       (setq s1 (car l))									  	; first slope
       (setq s2 (cadr l))									  	; second slope
       (setq p (inters (setq m (polar (cadr s1) (angle (cadr e) (caddr e)) (distance (cadr e) (caddr e))))
		       (polar m (angle (cadr s1) (caddr s1)) (distance (cadr s1) (caddr s1)))
		       (cadr s2)
		       (caddr s2)
		       nil))
       (vl-cmdf "_.move" (setq b (ssget "_P" '((-4 . "<>") (62 . 1)))) "" "_non" (caddr e) "_non" p))   ; move all selected but red lines
  (princ)
  )

 

0 Likes
Message 34 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

wow...Perfect

thank you

0 Likes
Message 35 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

hi

"move all selected but red lines"

Can make it move the text, mtext and Dimension too?

thank you

 

0 Likes
Message 36 of 39

ВeekeeCZ
Consultant
Consultant

Post one sample to see... it's just about filters... 

Message 37 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

Like this

attach

0 Likes
Message 38 of 39

ВeekeeCZ
Consultant
Consultant

Ok, here you go. Selection would be faster now.

 

(vl-load-com)

(defun c:MoveGreenLine ( / s l g s1 s2 p)
  
  (and (princ "\nSelect one green line and at least two slope lines of any color but green,")
       (setq s (ssget "_:S"))										; _:S for quick single selection
       (setq l (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
       (setq l (vl-remove-if-not '(lambda (e) (and (vl-position (cdr (assoc 62 (entget e))) '(1 3))	; list of green & red lines
						   (= "LINE" (cdr (assoc 0 (entget e)))))) l))
       (setq l (mapcar '(lambda (x) (cons x (vl-sort (list (cdr (assoc 10 (entget x)))			; list of (en left-pt right-pt)
							   (cdr (assoc 11 (entget x))))
						     '(lambda (p r) (< (car p) (car r)))))) l))		
       (setq g (car (vl-remove-if-not '(lambda (x) (= 3 (cdr (assoc 62 (entget (car x)))))) l)))  	; green line
       (setq l (vl-remove g l))										; all red lines
       (setq l (vl-sort l '(lambda (p r) (> (abs (- (car (cdaddr p)) (cadadr p)))			; sort reds by delta-y to get slopes
					    (abs (- (car (cdaddr r)) (cadadr r)))))))  
       (setq l (vl-sort (list (car l) (cadr l)) '(lambda (p r) (< (caadr p) (caadr r)))))    		; sort reds from left to right
       (mapcar 'set '(s1 s2) l)										; set first and second slope line
       (setq p (inters (setq p (polar (cadr s1) (angle (cadr g) (caddr g)) (distance (cadr g) (caddr g))))
		       (polar p (angle (cadr s1) (caddr s1)) (distance (cadr s1) (caddr s1)))
		       (cadr s2)
		       (caddr s2)
		       nil))
       (vl-cmdf "_.move" (ssget "_P" '((-4 . "<>") (62 . 1))) "" "_non" (caddr g) "_non" p))   		; move all selected but red lines
  (princ)
  )
Message 39 of 39

chan230984
Advocate
Advocate

@ВeekeeCZ 

Thanks a lot for your help, That’s very perfect

 

0 Likes