Request to automate

Request to automate

kczerwonka
Advocate Advocate
1,729 Views
32 Replies
Message 2 of 33

Request to automate

kczerwonka
Advocate
Advocate

I posted this in the Dynamic Blocks forum and did not get any response, so I am re-posting here.

I am looking to automate the following.

 

What I am trying to achieve is the following:

1. I want to input L, W, and D.

2. I want the Dblock  or lisp to find X, Y and Y/2.

 

Please see attached file. The dashed lines are for visual reference only. 

 

To summarize, I would input the length (L) and width (W) of a rectangle. I would then input the circle diameter (D) then the Dblock or lisp would create the rectangle and a staggered grid (array) of circles equally spaced from all edges of the rectangle and from each circle to the adjacent circles.

 

To anyone that could help with this, I would be very grateful.

0 Likes
Accepted solutions (4)
1,730 Views
32 Replies
Replies (32)
Message 1 of 33

kczerwonka
Advocate
Advocate

I posted this in the Dynamic Blocks forum and did not get any response, so I am re-posting here.

I am looking to automate the following workflow.

 

What I am trying to achieve is the following:

1. I want to input L, W, and D.

2. I want the Dblock  or lisp to find X, Y and Y/2.

 

Please see attached file. The dashed lines are for visual reference only. 

 

To summarize, I would input the length (L) and width (W) of a rectangle. I would then input the circle diameter (D) then the Dblock or lisp would create the rectangle and a staggered grid (array) of circles equally spaced from all edges of the rectangle and from each circle to the adjacent circles.

 

To anyone that could help with this, I would be very grateful.

0 Likes
Message 3 of 33

Kent1Cooper
Consultant
Consultant

That seems indeterminate to me.  Surely only in a lucky combination of your input values could there be a solution with all the X's being equal.  And in fact, yours are not -- the length of the horizontal dashed-yellow Lines is not the same as that of the diagonal ones, which means the X between Circles varies, and the Measurement values of the X Dimension objects also varies.]  Something would usually need to be a slightly different resultant distance, for example if the X between Circles and between them and the top and bottom edges are the same, the distance between the end Circles and the right and left edges would need to be "sacrificed" and be different from the X value elsewhere.

 

There would also need to be some criterion for the approximate "target" ratio of X to D.  Otherwise, in addition to your solution [if it really was one] with 8 Circles in the bottom row, there could be one with 6 or 7, and 3 rows, or one with 9 or 10, and maybe 5 rows -- even 11 Circles that size will fit in the L distance, with a small-enough X value.

Kent Cooper, AIA
0 Likes
Message 4 of 33

kczerwonka
Advocate
Advocate

OK, please disregard the dashed yellow lines altogether. Essentially, I am looking to have the circles equally spaced from the 4 sides of the rectangle as well as between all circles.

 

The "x" does not vary, and must be equal.

 

The size of the rectangle will vary, as well as the number and diameter of the circles.

 

"x" should be =/< circle radius.

 

I hope this helps.

Thank you.

 

0 Likes
Message 5 of 33

Kent1Cooper
Consultant
Consultant
Accepted solution

I tried measuring distances between Circles [i.e. between intersections of Circles with yellow center-to-center Lines], comparing horizontal to diagonal where the yellow-Lines variance occurs, and the ones I checked were the same, so I wondered why the yellow Lines vary in length.  In the process I found that your Circles are not all of the same radius.  With two selected:

Kent1Cooper_0-1651683460065.png

Some of them share a common radius, but there are a variety of slightly different sizes.  That "allows" the variation in center-to-center Line lengths even if the spaces between are equal.

 

I really believe what you want is not going to be possible in most instances, if your intent is to specify L & W & D.  For example, I set up a simple situation in which all the X dimensions and the Circle radius are 1 drawing unit, and I sized the rectangle around them to make that true, rather than starting from a specified rectangle size:

Kent1Cooper_1-1651684107070.png

[The odd-ball decimal component of the vertical dimension, rounded to 3 decimal places, is because the sine of 60° is involved.]

 

Now suppose a routine is defined, and you ask it to do its thing in a rectangle 8.5 units wide as above, but 7 units tall.  It could use the same arrangement of Circles to have their distances from each other [which must always be in 0°- and 60°-angle displacements] and to the sides of the rectangle remain at 1 unit.  But it will simply not be possible for the distance from the Circles to the top and bottom of the rectangle to also be 1 unit [that is, not to both top and bottom -- it can be to one of them if you want].  Something will have to give -- you can't avoid having some variance between either the distances from the edges or the spacings/angles between the Circles.  You can get the desired result only in rectangles whose dimensions happen to have that kind of magical relationship to the Circle radius and the sine of 60°.

Kent Cooper, AIA
0 Likes
Message 6 of 33

Sea-Haven
Mentor
Mentor

I have a zigzag.lsp which does the circle rows in the desired pattern, but as you mention Kent there are to many variations, a minimum answer may be no circles at all or a single row, What is minimum edge clearance that is not mentioned. Its X in diagram, it may have to alter to make a pattern. Say how many rows.

 

SeaHaven_0-1651733601020.png

 

0 Likes
Message 7 of 33

leeminardi
Mentor
Mentor
Accepted solution

As @Kent1Cooper pointed out, you can only have the same distance between circles, the horizontal rectangular boundary, and the vertical boundary if there is a specific relationship between the width and length of the rectangle.

 

Looking only at rectangle width for a moment and assuming that there are at least 2 rows of staggered circles we can determine the number of circles in a row from the following equation. 

n = (W - 5/4 * D) / ( 3/2 * D) rounded up to the next integer.

 

The spacing x between circles and the rectangle can be found with:

x =  (W - 1.5 * D) / (n + 1.5)

 

These equations were derived from the following illustration starting with the assumption that the maximum value for x was D/2. The yellow boxes highlight the spacing between circles and the rectangle.

 

leeminardi_0-1651760281976.png

For example, assuming D = 1, and W = 5.4 then n = 3 (2.76 rounded up) and x = 0.866.

 

A similar relationship can be used for determining the circle spacing based on the rectangle length L and circle diameter.  This will probably yield a value for y that is different than x and you can choose to use either value for the final layout depending on your design requirements. 

 

 

lee.minardi
0 Likes
Message 8 of 33

kczerwonka
Advocate
Advocate

Thank you for your reply and the equations. They will be very helpful.

0 Likes
Message 9 of 33

Sea-Haven
Mentor
Mentor

The X & Y will probably always be different, do you create based on the X Y values ? 

 

Here is zigzag.lsp if you want to try putting something together, you need to create the lower left circle 1st. You could remove the dcl part with your answers, use the dcl part to get the values L W Rad.

0 Likes
Message 10 of 33

kczerwonka
Advocate
Advocate

I create based on L, W, and D.

I am looking to programmatically solve for the other variables.

I want specify the length and width of the rectangle and the diameter of the circle. Then the program will place the circles inside the rectangle in a staggered grid pattern evenly spaced from each other.

The size of the four margins around the edges is not critical, but the top and bottom edge margins must be equal. And the left and right edge margins must be equal.

The distance between the circles may be the same distance as the edge margins or different.

 

Thanks for the files. I will see what I can do with them.    

0 Likes
Message 11 of 33

leeminardi
Mentor
Mentor

Is it also required that all the margins and space between hole must be less than or equal to the radius of the circles?

 

I can take a stab at the program this weekend.

 

 

lee.minardi
0 Likes
Message 12 of 33

kczerwonka
Advocate
Advocate

Yes, You are correct. 

Thank you very much.

0 Likes
Message 13 of 33

leeminardi
Mentor
Mentor

The following code with fill a rectangle as I think you wish given W, L and D.  The side margins are calculated such that it will be the same as the distance between circles and less than or equal to the radius of the circles.  The top and bottom margins are driven by the side margins and may be quite small. The top two solutions are a result of ensuring that the vertical margin is less than the circle radius. The bottom two do not make this assumption.

 

I've done a little testing and have found that it sometimes yields different vertical margins.  I'll have to do some more debugging but I thought I would show the progress I have made.

 

The code could be modified to ensure a minimum margin if necessary.

leeminardi_0-1651946149737.png

 

Here's an improved edited version of the code 5/8/2022.  It yields uniform margins for the top and bottom.  The margins for the sides are uniform but probably different from the top/bottom margin.

(defun c:test (/ c_doc ms w L D pll pur n_per_row xh s xV i dx dy ptbase n_rows pt)
;  Fills a rectangle of width W, length L, with circle of diameter D
; L. Minardi  revised 5/8/2022  
;
; xh = side margins
; xV = top and bottom margins
; s = space between circles  
(setq c_doc (vla-get-activedocument (vlax-get-acad-object)))
(setq ms (vla-get-modelspace c_doc))
(setq w	(getreal "\nEnter rectangle width:  ")
      L	(getreal "nEnter rectangle length:  ")
      D	(getreal "nEnter circle diamter:  ")
)
(setq pll '(0.0 0.0)
      pur (list L w)
)
(command "_rectangle" pll pur "")
(setq n_per_row	(/ (- L (* 1.25 D)) (* 1.5 D)))
(setq n_per_row (+ (fix n_per_row) 1))
(setq xH (/
	   (- L (* n_per_row D) (/ D 2.))
	   (+ n_per_row 1.5)
	 )
)
(setq s	     xH
      sin60  (sin (/ pi 3))
      n_rows (+ (/ (- W (* 2 D)) (* (+ s D) sin60)) 1)
      n_rows (+ (fix n_rows) 1)
      xv     (/ (- W (* (- n_rows 1) (+ D s) sin60) D) 2)
      i	     0
      dx     0
      dy     0
      ptbase (list (+ xh (/ D 2.)) (+ xV (/ D 2.)))
)					
(repeat	n_rows
  (repeat n_per_row
    (if	(> (rem i 2) 0) ; determine if odd or even row
      (setq shift (/ (+ D s) 2.))
      (setq shift 0.0)
    )
    (setq pt (mapcar '+ ptbase (list (+ dx shift) dy)))
    (vla-addcircle ms (vlax-3d-point pt) (/ D 2.))
    (setq dx (+ dx s D))
  )					; repeat n_per_row 
(setq dx 0
      i	 (+ i 1)
      dy (+ dy (* (+ s D) sin60))
)
)					; repeat n_rows
(princ)
)

 

 

 

lee.minardi
Message 14 of 33

Sea-Haven
Mentor
Mentor

Looks good, would not the edge margin be same all round ?

 

Just me I use a variable, I often do a90 a270 a45 for repeated use. 

 

 

(setq sin60 (sin (/ (* 60.0 pi) 180.0)))
or 
(setq sin60 (* pi (/ 2.0 3.0)))


(setq dy (+ dy (* (+ s D) sin60 )))

 

0 Likes
Message 15 of 33

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

Looks good, would not the edge margin be same all round ?

....


[It can't be, if the Circles are also all the same distance from each other, except when the relationship is just right between Circle size and rectangle dimensions.  See the second half of Message 5.]

Kent Cooper, AIA
Message 16 of 33

Kent1Cooper
Consultant
Consultant
@leeminardi and @Sea-Haven wrote:
(setq sin60 (sin (/ (* 60.0 pi) 180.0)))

(setq sin60 (sin (/ pi 3)))

Kent Cooper, AIA
0 Likes
Message 17 of 33

leeminardi
Mentor
Mentor

I have edited the code in my post #13 to fix a calculation bug for the number of rows and the vertical margins.  To save several attoseconds of compute time I have defined a variable for the sine of 60°.  Thanks for the suggestion @Kent1Cooper and @Sea-Haven

Sample result.

leeminardi_0-1652020471261.png

 

@Sea-Haven, " ...would not the edge margin be same all round ?"  In short, no. The aspect ratio of W / L is most likely different than what can be achieve by an array of equally sized circles with equal spacing.   My code calculates a value for the side margins that will be the same as the circle spacing.  This values drives the vertical margins.

lee.minardi
Message 18 of 33

Sea-Haven
Mentor
Mentor

Thanks kent should have looked closer. I did run and compare values.

0 Likes
Message 19 of 33

Sea-Haven
Mentor
Mentor

Yeah Leeminardi did not really look close enough at the X Y problem a tricky one to program. You can use my Multi getvals for input if you want a dcl W L Rad.

0 Likes
Message 20 of 33

kczerwonka
Advocate
Advocate

Wow! this is great!

I just have one request, Would you be able to have the code prompt the user for the minimum vertical margin value and the minimum horizontal margin value?

I need to avoid getting a result like the one in the upper right view.

It would be PERFECT if you could do that.  

Thank you.

0 Likes