I have attached what my macro currently looks like below. So far the macro will make a rectangle with 2 holes in it. It then needs to chamfer the four edge corners. But for some reason when I try to select those corners it wont work. I actually will get different results on different tries.
The code portion in question is the last 7 or so lines. If I run those lines completely separate from the macro I get the results that I am after. Could it just be some syntax thing?
I am new to VBA with respect to Solidworks. I have worked with it in excel. So if you spot any bad habbits other feedback is appreciated.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swSketchMgr As SldWorks.SketchManager
Dim swFeature As SldWorks.Feature
Dim swFeatureMgr As SldWorks.FeatureManager
Dim vSkLines As Variant
Dim boolstatus As Boolean
Dim longstatus As Long
Dim circ As SldWorks.SketchSegment
Dim cx1 As Double
Dim cx2 As Double
Dim cy1 As Double
Dim cy2 As Double
Dim d1 As Double
Dim d2 As Double
Dim b1 As Double
Dim w1 As Double
Dim t1 As Double
Dim in2mmconv As Double
Dim m2mmconv As Double
Sub main()
in2mmconv = 0.0254
m2mmconv = 1 / 1000
b1 = 5.5 * in2mmconv
w1 = 3.5 * in2mmconv
t1 = 0.75 * in2mmconv
cy1 = b1 - 1.75 * in2mmconv
cx1 = w1 / 2
d1 = 66.779 * m2mmconv + 0.0002 * in2mmconv
cx2 = cx1
cy2 = cy1 - 79.3 * m2mmconv
d2 = 0.5007 * in2mmconv
Set swApp = Application.SldWorks
' Reset the counts for untitled documents for this macro
Set swModel = swApp.ActiveDoc
' Select the Front plane
Set swModelDocExt = swModel.Extension
boolstatus = swModelDocExt.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
' Open a sketch and sketch a rectangle
Set swSketchMgr = swModel.SketchManager
swSketchMgr.InsertSketch True
swModel.ClearSelection2 True
vSkLines = swSketchMgr.CreateCornerRectangle(0, b1, 0, w1, 0, 0)
' Change view orientation and clear all selections
swModel.ShowNamedView2 "*Trimetric", 8
swModel.ClearSelection2 True
' Select the sketch entities to extrude
Set swModelDocExt = swModel.Extension
boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
' Create the extrude feature
Set swFeatureMgr = swModel.FeatureManager
Set swFeature = swFeatureMgr.FeatureExtrusion2(True, False, True, 0, 0, t1, 0.381, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)
' Fit the model in the graphics area
swModel.ViewZoomtofit2
' Select the face on the extrude feature
' and sketch the entities to pattern
swModel.ShowNamedView2 "*Front", 1
boolstatus = swModelDocExt.SelectByID2("", "FACE", -w1, 0, -t1, False, 0, Nothing, 0)
Set circ = swSketchMgr.CreateCircle(cx1, cy1, 0, cx1 - d1 / 2, cy1, 0)
Set circ = swSketchMgr.CreateCircle(cx2, cy2, 0, cx2 - d2 / 2, cy2, 0)
boolstatus = swModelDocExt.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("Arc2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
swModel.ClearSelection2 True
Set swFeature = swFeatureMgr.FeatureCut3(True, False, False, swEndCondThroughAll, swEndCondBlind, 0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, False, True, True, False, False, False, swStartSketchPlane, 0, False)
swModel.ClearSelection2 True
boolstatus = swModelDocExt.SelectByID2("", "EDGE", 0, 0, -t1 / 2, True, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("", "EDGE", 0, b1, 0, True, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("", "EDGE", w1, b1, -t1 / 2, True, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("", "EDGE", w1, 0, -t1 / 2, True, 0, Nothing, 0)
Set swFeature = swFeatureMgr.InsertFeatureChamfer(4, 1, 0.00254, 0.78539816339745, 0, 0, 0, 0)
End Sub
I faced the same problem recently. After some google search, I found that the selectbyID2 method may behave in certain circumstances. One of them is that there is an edge that is over your required edge. And there are so many of them. So, what did I do in my code? I planned to rotate the model so that the required object to be selected is in normal view. In my case, this procedure solved my issues completely. Try it. To rotate the model, use the showNamedView2 method before using selectbyID2.