AnsweredAssumed Answered

Creating variable diameter and length of semi circular objects

Question asked by Binish Mohan on Jul 17, 2020

Hi I am unable to create an automated semi circular object using solidworks macro and userforms. I want the user to input the diameter and the length of the extrusion. Below is my code as well as my userform. Do let me know how I can fix this issue. I have also attached the macro file. Thank you


Main function:


Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()


End Sub


Code in userform:


Private Sub CommandButton1_Click()

Set swApp = Application.SldWorks

'Open new Document and insert sketch
Dim swSheetWidth As Double
swSheetWidth = 0
Dim swSheetHeight As Double
swSheetHeight = 0
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2019\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight)
Dim swPart As PartDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True

'Create circle from sketch manager
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCircle(0#, 0#, 0#, 0.000778, -0.028255, 0#)
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True

'inserting line on the circle
boolstatus = Part.Extension.SelectByID2("Sketch3", "SKETCHCONTOUR", 1.29611862577139E-03, 1.62014828221425E-02, 0, False, 0, Nothing, 0)
Set skSegment = Part.SketchManager.CreateLine(-0.028266, 0#, 0#, 0.028266, 0#, 0#)
Part.ClearSelection2 True
Part.SketchManager.InsertSketch True

'trimming the outer circle to get semi-circle
'Set the trimming options
Dim swFeatureMgr As SldWorks.FeatureManager
boolstatus = swFeatureMgr.PreTrimSurface(False, False, True, False)
boolstatus = Part.Extension.SelectByID2("Arc1@Sketch3", "EXTSKETCHSEGMENT", 1.17131915616373E-03, 2.82418063208363E-02, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(1, 5.05486264050847E-03, 2.74777148663537E-02, 0)
Part.SketchManager.InsertSketch True

'Take Userform Textbox value as Extrude Length and Extrude Circle
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, TextBox2 / 1000, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False


End Sub

Private Sub Label1_Click()

End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub UserForm_Click()

End Sub