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

    Circle Inside of Larger Circle Macro Sketch Generator

    S. Casale

      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.

       

      Thanks in advance!

        • Re: Circle Inside of Larger Circle Macro Sketch Generator
          Rob Edwards

          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

          is there any way to fill a big circle with small circles in sketch mode?

           

          • Re: Circle Inside of Larger Circle Macro Sketch Generator
            Rob Edwards

            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
                
                BigRadius = CDbl(InputBox("Major Radius:"))
                
                Call dp.Front.Select(False)
                
                With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)
                
                    Dim swSketch As SldWorks.Sketch: Set swSketch = .ActiveSketch
                
                    .AddToDB = True
                    Dim swCircle As SldWorks.SketchSegment:     Set swCircle = .CreateCircleByRadius(0#, 0#, 0#, BigRadius / 1000)
                    .AddToDB = False
                    
                Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager
            
                LilRadius = CDbl(InputBox("Minor Radius:"))
                
                Dim NumRings As Long
                
                NumRings = Int((BigRadius + LilRadius) / (2 * LilRadius))
                
                Dim CurrentRadius As Double
                
                CurrentRadius = BigRadius - LilRadius
                
                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
                
                    .AddToDB = True
            
                    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)
                        CurrentRadius = CurrentRadius - (2 * LilRadius)
                    
                    Next i
                    
                    .AddToDB = False
                Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager
                
                    MsgBox ("Total:" & TotalCount)
                    
            ' god knows how you center a sketch pattern!
            '    CurrentRadius = BigRadius - LilRadius
            '
            '    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( _
            '                 ArcRadius:=CurrentRadius / 1000 _
            '                , ArcAngle:=2 * PI _
            '                , PatternNum:=Int((PI * CurrentRadius) / LilRadius) _
            '                , PatternSpacing:=0 _
            '                , PatternRotate:=True _
            '                , DeleteInstances:="" _
            '                , RadiusDim:=False _
            '                , AngleDim:=False _
            '                , CreateNumOfInstancesDim:=False)
            '
            '
            '            CurrentRadius = CurrentRadius - (2 * LilRadius)
            '
            '        Next i
            '
            '        .AddToDB = False
            '    Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager
                
                CurrentRadius = BigRadius - LilRadius
            
                Call dp.Front.Select(False)
            
                With swModel.SketchManager: Call .InsertSketch(UpdateEditRebuild:=False)
            
                    Set swSketch = .ActiveSketch
            
                    .AddToDB = True
                    
                    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
            
                        CurrentRadius = CurrentRadius - (2 * LilRadius)
            
                    Next i
            
                    .AddToDB = False
                Call .InsertSketch(UpdateEditRebuild:=True): End With 'swModel.SketchManager
                
            End Function