The rainbow line

The rainbow line

Anonymous
Not applicable
1,755 Views
9 Replies
Message 1 of 10

The rainbow line

Anonymous
Not applicable

Hi guys,

 

my first question on this block, hopefully nobody kills me if it is so stupid  just starting with VLISP and already fighting few days with following.

 

I need to

1. Create a line where I put 2 points.

2. The line will be divided to N parts(I will write the number) and each part ot this line will be with a different color.

3. I should be able to create lines until the enter button is pressed

 

Could somebody help me with that?

Many thanks

0 Likes
1,756 Views
9 Replies
Replies (9)
Message 2 of 10

Shneuph
Collaborator
Collaborator

Do you enter N number only once for each time you issue the command?

Do you have predefined colors that you always will use?  How does the lisp know what color to make each segment?

When you continue to draw do you start at the last point of the previous line?

What have you written so far?  Post code.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 3 of 10

Anonymous
Not applicable
Hi,

Thank you for your answer

1. I will enter N number once only

2. I have no predefined colors it should be random because you dont know
what N will be in advance or go in order of standard colors for example
true color 1++ etc .

When I continue to draw I would like to start a new line

I can upload my codes (tried few already) when I am back in my office
0 Likes
Message 4 of 10

dbhunia
Advisor
Advisor

Hi

 

Try this it's roughly tested in AutoCAD 2007..... (your command is "LC")....

 

This is for continuous line connected with each other.....

 

(defun c:xxx (/)
(setq P2 (getpoint "\nPick/Enter Next Point: "))
(setq LS (/ (distance p1 p2) NS))
(setq ang (angle p1 p2))
(repeat NS
	(setq PT2 (polar P1 ang LS))
	(command "line" p1 PT2 "")
	(setq P1 PT2)
	(command "_change" (entlast) "" "p" "c" N "")
	(setq N (+ N 1))
)
)
(defun c:LC (/)
(setq NS (getint "\nNumber of segments: "))
(setq P1 (getpoint "\nPick/Enter First Point: "))
(setq N 1)
  (while T
    (c:xxx)
  (princ)
)
)

  


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 5 of 10

Kent1Cooper
Consultant
Consultant

Here's my take on it:

(defun C:LSMC ; = Line, Subdivided, Multi-Colored
  (/ n p1 p2 a s c)
  (setq n (getint "\nNumber of differently-colored segments: "))
  (while (setq p1 (getpoint "\nStart of line or <exit>: "))
    (setq
      p2 (getpoint p1 "\nEnd of line: ")
      a (angle p1 p2)
      s (/ (distance p1 p2) n)
      c 0
    ); setq
    (repeat n
      (command
        "_line" "_none" p1 "_none" (polar p1 a s) ""
        "_.chprop" "_last" "" "_color" (setq c (1+ c)) ""
      ); command
      (setq p1 (getvar 'lastpoint))
    ); repeat
  ); while
  (princ)
); defun

It doesn't do random colors, but steps through the standard color numbers, starting with 1 [red] at the beginning of each line.  Some colors are pretty hard to distinguish from each other on-screen, so it might be better to make it cycle through a limited number of them, such as the first 7 or 8.  Truly random  color assignment would mean that you could sometimes get adjacent segments of the same color, so that doesn't seem advisable.

 

Kent Cooper, AIA
0 Likes
Message 6 of 10

ВeekeeCZ
Consultant
Consultant

Both codes have a limit of 255 colors, then it fails. It should go around...

And it's pretty slow....... but it looks nice.

0 Likes
Message 7 of 10

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

Both codes have a limit of 255 colors, then it fails. ....


 

I didn't imagine that the OP would want to subdivide a single line into that many segments of different colors, and if not, since mine starts over at 1 for each line [for that very reason], it wouldn't run into that problem as @dbhunia's would.  But if so, then yes, it could pretty easily be made to cycle back to the beginning.  @Anonymous, tell us more about how you want it to operate.  Should mine continue with subsequent color numbers for further lines, rather than start over for each?

 

Another thing it could be made to do is to remember the specified number of segments and offer it as a default the next time you use it.  Would that be of any use, or would you be using different numbers every time?

 

Just to illustrate a way of cycling back to the beginning of a sequence, here's a version that cycles through only 6 colors because of the use of the word "rainbow" in the Subject.  Change some color numbers if you prefer the look of others to the ones I chose.

(defun C:LSMC ; = Line, Subdivided, Multi-Colored in 6 "rainbow" colors
  (/ clist n p1 p2 a s c)
  (setq
    clist '(1 30 2 3 150 6); "rainbow" colors +/-
    n (getint "\nNumber of differently-colored segments: ")
  ); setq
  (while (setq p1 (getpoint "\nStart of line or <exit>: "))
    (setq
      p2 (getpoint p1 "\nEnd of line: ")
      a (angle p1 p2)
      s (/ (distance p1 p2) n)
      c -1
    ); setq
    (repeat n
      (command
        "_line" "_none" p1 "_none" (polar p1 a s) ""
        "_.chprop" "_last" "" "_color" (nth (setq c (rem (1+ c) 6)) clist) ""
      ); command
      (setq p1 (getvar 'lastpoint))
    ); repeat
  ); while
  (princ)
); defun
Kent Cooper, AIA
0 Likes
Message 8 of 10

ВeekeeCZ
Consultant
Consultant

@ВeekeeCZ wrote:

Both codes have a limit of 255 colors, then it fails. It should go around...

And it's pretty slow....... but it looks nice.


 

Kent's code using (entmake) instead of (command) to make it faster.

... and circling colores.

(defun C:LSMC ; = Line, Subdivided, Multi-Colored
       (/ n p1 p2 a s c)
  (if (setq c -13
	    n (getint "\nNumber of differently-colored segments: "))
    (while (setq p1 (getpoint "\rStart of line or <exit>: "))
      (setq
	p2 (getpoint p1 "\nEnd of line: ")
	a (angle p1 p2)
	s (/ (distance p1 p2) n)
	); setq
      (repeat n
	(entmake (list (cons 0 "LINE")
		       (cons 10 (trans p1 1 0))
		       (cons 11 (trans (setq p1 (polar p1 a s)) 1 0))
		       (cons 62 (if (> (setq c (+ c 13)) 255)
				  (setq c (- c 249))
				  c))))
	); repeat
      ); while
    ); if
  (princ)
  ); defun

Have fun!!

 

Edit: One more version to learn:

(vl-load-com)

(defun C:LSMC ; = Line, Subdivided, Multi-Colored
       (/ n p1 p2 a s c mspace obj)
  (if (and (setq c -13
		 n (getint "\nNumber of differently-colored segments: "))
	   (setq mspace (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object))))
	   )
    (while (setq p1 (getpoint "\rStart of line or <exit>: "))
      (setq
	p2 (getpoint p1 "\rEnd of line: ")
	a (angle p1 p2)
	s (/ (distance p1 p2) n)
	); setq
      (repeat n
	(setq obj (vla-AddLine mspace
		    (vlax-3d-point p1)
		    (vlax-3d-point (setq p1 (polar p1 a s)))))
	(vla-put-color obj (if (> (setq c (+ c 13)) 255)
			     (setq c (- c 249))
			     c))
	); repeat
      ); while
    ); if
  (princ)
  ); defun
0 Likes
Message 9 of 10

dbhunia
Advisor
Advisor

Hi,

 

You are right.... in that case...

 


@ВeekeeCZ wrote:

Both codes have a limit of 255 colors, then it fails. It should go around...

...........................


I should add a line......

 

For continuous line connected with each other.....

(defun c:xxx (/)
(setq P2 (getpoint P1 "\nPick/Enter Next Point: "))
(setq LS (/ (distance p1 p2) NS))
(setq ang (angle p1 p2))
(if (= 255 N) (setq N 1))
(repeat NS
	(setq PT2 (polar P1 ang LS))
	(command "line" P1 PT2 "")
	(setq P1 PT2)
	(command "_change" (entlast) "" "p" "c" N "")
	(setq N (+ N 1))
)
)
(defun c:LC (/ pnt)
(setq NS (getint "\nNumber of segments: "))
(setq P1 (getpoint "\nPick/Enter First Point: "))
(setq N 1)
  (while T
    (c:xxx)
  (princ)
)
)

 

And For non-continuous line (not connected with each other)......

(defun c:xxx (/)
(setq P2 (getpoint P1 "\nPick/Enter Next Point: "))
(setq LS (/ (distance p1 p2) NS))
(setq ang (angle p1 p2))
(if (= 255 N) (setq N 1))
(repeat NS
	(setq PT2 (polar P1 ang LS))
	(command "line" p1 PT2 "")
	(setq P1 PT2)
	(command "_change" (entlast) "" "p" "c" N "")
	(setq N (+ N 1))
)
)
(defun c:LC (/ pnt)
(setq NS (getint "\nNumber of segments: "))
(setq N 1)
  (while (setq P1 (getpoint "\nPick/Enter First Point: "))
    (c:xxx)
  (princ)
)
)

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 10 of 10

ronjonp
Advisor
Advisor

What are you using this for?

Here's my take on it:

(defun c:foo (/ a c d p1 p2 s)
  ;; RJP » 2018-11-06
  (cond	((and (progn (initget 7) (setq s (getint "\nHow many segments: ")))
	      (setq p1 (getpoint "\nPick first point: "))
	      (setq p2 (getpoint "\nPick second point: "))
	 )
	 (setq d (/ (distance p1 p2) s))
	 (setq a (angle p1 p2))
	 (setq c 0)
	 (repeat s
	   (entmakex (list '(0 . "line")
			   (cons 10 p1)
			   (cons 11 (setq p1 (polar p1 a d)))
			   (cons 62
				 (setq c (cond ((= 256 (1+ c)) 1)
					       ((1+ c))
					 )
				 )
			   )
		     )
	   )
	 )
	)
  )
  (princ)
)
0 Likes