7 Replies Latest reply on Jul 18, 2018 9:48 AM by S. Casale

# Circle Inside of Larger Circle Macro Sketch Generator

I am curious if anyone in this forum has created a circle inside circle macro or sketch generation tool of sorts? Saving doing lots of work to create one, I am seeing if anyone has already done so.

I know the engineering toolbox has a cute calculator (https://www.engineeringtoolbox.com/smaller-circles-in-larger-circle-d_1849.html)

I'm looking for sketch creation in good ol' SolidWorks.

• ###### Re: Circle Inside of Larger Circle Macro Sketch Generator

Howdy

Could you describe, just how you would like the macro to perform, ie - the pre and post conditions.

A simple solution may be quite easy to implement or you could have a full blown property manager where you select a plane and centre point, etc then enter the numbers and get a preview.

I like little macro projects, but it would best if I knew exactly what you want.

As an aside you could probably achieve this with a fill pattern, I posted a similar solution (#3) that created a sketch in this post from a weeks ago • ###### Re: Circle Inside of Larger Circle Macro Sketch Generator

The idea is simple the execution I am not sure about.

Simply, if I have a Ø1.000 circle and I have a bunch of smaller Ø circles, how many can I fit in it? An example shown below: I want to generate a sketch with this data.

Also, the ability to choose a quantity of circles and fit them organized in a defined larger circle.

• ###### Re: Circle Inside of Larger Circle Macro Sketch Generator

Just a macro, no custom property...

• ###### Re: Circle Inside of Larger Circle Macro Sketch Generator

I've been musing a little bit how to work this out.

My first thoughts are to start from the outside and then (as you show) use concentric rings working in to the centre each iteration subtracting the smaller radius to get a centerpoint circumference.  Find out how many circles will fit and then position them equally using angles. I presume you would like any gaps to be uniformly distributed.  This Seems doable but I don't think this will give an optimum solution, I'm guessing the math gets a bit bonkers if we try and minimise gaps.

• ###### Re: Circle Inside of Larger Circle Macro Sketch Generator

I had a little play, but it's only a start..

It's showing promise, but there are some problems to work out One thing I've got to address is when there are 3 circles in the middle (they overlap! ) I'm going to be busy for the next week or so,but I'll see what I can do, when it functions propery it needs rewriting really with a better UI.. I was just experimenting with technique.

Anyway here's my code (missing a class module which is in the attached macro)

Maybe someone else will be inspired.  It's quite a cute problem

edit: I think I've sussed how to fix it, it's not so tricky, should be able to find the time tomorrow

```Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Select Case True

Case (swModel Is Nothing), (swModel.GetType = swDocDRAWING)
Call swApp.SendMsgToUser2("This macro requires an active part or assembly", swMbStop, swMbOk)
Case Else
Call DoShiz

End Select

End Sub

Private Function DoShiz() As Long

Dim dp As DefaultPlanes: Set dp = New DefaultPlanes:   Set dp.Model = swModel

Dim BigRadius As Double, LilRadius As Double

Call dp.Front.Select(False)

With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)

Dim swSketch As SldWorks.Sketch: Set swSketch = .ActiveSketch

Dim swCircle As SldWorks.SketchSegment:     Set swCircle = .CreateCircleByRadius(0#, 0#, 0#, BigRadius / 1000)

Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager

Dim NumRings As Long

Dim CurrentRadius As Double

Dim PI As Double: PI = 4 * Atn(1)
Dim TotalCount As Long

Call dp.Front.Select(False)

With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)

Set swSketch = .ActiveSketch

Dim i As Long
For i = 0 To NumRings - 1

Set swCircle = .CreateCircleByRadius(0#, 0#, 0#, CurrentRadius / 1000)
swCircle.ConstructionGeometry = True

TotalCount = TotalCount + Int((PI * CurrentRadius) / LilRadius)

Next i

Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager

MsgBox ("Total:" & TotalCount)

' god knows how you center a sketch pattern!
'
'    Call dp.Front.Select(False)
'
'    With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)
'
'        Set swSketch = .ActiveSketch
'
'        .AddToDB = True
'
'        For i = 0 To NumRings - 1
'
'            Set swCircle = .CreateCircleByRadius(0#, CurrentRadius / 1000, 0#, LilRadius / 1000)
'            Call swModel.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
'
'
'            Call .CreateCircularSketchStepAndRepeat( _
'                , ArcAngle:=2 * PI _
'                , PatternNum:=Int((PI * CurrentRadius) / LilRadius) _
'                , PatternSpacing:=0 _
'                , PatternRotate:=True _
'                , DeleteInstances:="" _
'                , RadiusDim:=False _
'                , AngleDim:=False _
'                , CreateNumOfInstancesDim:=False)
'
'
'
'        Next i
'
'        .AddToDB = False
'    Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager

Call dp.Front.Select(False)

With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)

Set swSketch = .ActiveSketch

Dim instances As Long
Dim instanceAngle As Double
Dim angle As Double

For i = 0 To NumRings - 1

instances = Int((PI * CurrentRadius) / LilRadius)
If instances > 1 Then
instanceAngle = 2 * PI / instances

For angle = 0 To (2 * PI) - (instanceAngle / 2) Step instanceAngle
Set swCircle = .CreateCircleByRadius((CurrentRadius / 1000) * Cos(angle), (CurrentRadius / 1000) * Sin(angle), 0#, LilRadius / 1000)
Next angle
Else
Set swCircle = .CreateCircleByRadius(0#, 0#, 0#, LilRadius / 1000)

End If