Hello, I created two macro, the second depends on the first because I do an extrusion with removal of material and I have to select the face on which I have to extrude. can i perform these two actions in one and create a macro? I put the two macro. Thank

Macro 1

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCenterLine(0#, 0#, 0#, 0.090055, 0#, 0#)
Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0#, 0.032106, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.05382, 0.032106, 0#, 0.05382, 0.048891, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.05382, 0.048891, 0#, 0.076734, 0.048891, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.076734, 0.048891, 0#, 0.076734, 0#, 0#)
Part.ClearSelection2 True
Part.SetPickMode
boolstatus = Part.Extension.SelectByID2("Point4", "SKETCHPOINT", 0, 0.03210551301655, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set skSegment = Part.SketchManager.CreateLine(0.05382, 0.032106, 0#, 0#, 0.032106, 0#)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", -2.6643579266844E-04, 0.020648773931806, 0, False, 0, Nothing, 0)
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(-5.67508238383831E-02, 1.85172875904583E-02, 0)
Part.ClearSelection2 True
Dim myDimension As Object
Set myDimension = Part.Parameter("D1@Sketch3")
myDimension.SystemValue = 0.02
boolstatus = Part.Extension.SelectByID2("Line5", "SKETCHSEGMENT", 4.78008298755187E-02, 0.013195020746888, 0, False, 0, Nothing, 0)
Set myDisplayDim = Part.AddDimension2(8.59751037344398E-02, 1.45228215767635E-02, 0)
boolstatus = Part.Extension.SelectByID2("D1@Sketch3@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set myDimension = Part.Parameter("D2@Sketch3")
myDimension.SystemValue = 0.034
boolstatus = Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 4.24896265560166E-02, 3.44398340248963E-02, 0, False, 0, Nothing, 0)
Set myDisplayDim = Part.AddDimension2(4.23236514522822E-02, 5.30290456431535E-02, 0)
boolstatus = Part.Extension.SelectByID2("D2@Sketch3@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set myDimension = Part.Parameter("D3@Sketch3")
myDimension.SystemValue = 0.012
boolstatus = Part.Extension.SelectByID2("Line5", "SKETCHSEGMENT", 4.74688796680498E-02, 9.70954356846473E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", -3.31950207468873E-04, 8.38174273858922E-03, 0, True, 0, Nothing, 0)
Set myDisplayDim = Part.AddDimension2(6.63900414937774E-04, 5.02074688796681E-02, 0)
boolstatus = Part.Extension.SelectByID2("D3@Sketch3@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set myDimension = Part.Parameter("D4@Sketch3")
myDimension.SystemValue = 0.06
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

' Named View
Part.ShowNamedView2 "*Trimetric", 8
Part.ViewZoomtofit2
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, False, 16, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureRevolve2(True, True, False, False, False, False, 0, 0, 6.2831853071796, 0, False, False, 0.01, 0.01, 0, 0, 0, True, True, True)
Part.SelectionManager.EnableContourSelection = False
End Sub

Macro 2

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
Dim vSkLines As Variant
vSkLines = Part.SketchManager.CreateCenterRectangle(-4.28908012694641E-03, -3.56646336642825E-02, 0, 5.59445233949533E-03, -0.025501378580866, 0)
Part.SetPickMode
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 3.44044734343563E-02, -2.49419333469167E-02, 4.84852536089596E-03, False, 0, Nothing, 0)
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(0.06, -0.011142284242828, 2.14454006347321E-03)
Part.ClearSelection2 True
Dim myDimension As Object
Set myDimension = Part.Parameter("D1@Sketch12")
myDimension.SystemValue = 0.012
boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 3.44044734343564E-02, -3.30538892391849E-02, 0.014359094338038, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByRay(6.00000000000023E-02, -2.86715682399134E-02, -1.84616927203346E-02, -1, 0, 0, 3.17018965904735E-04, 1, True, 0, 0)
Set myDisplayDim = Part.AddDimension2(0.06, -7.03968586053144E-03, -1.80887292310349E-02)
boolstatus = Part.Extension.SelectByID2("D1@Sketch12@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set myDimension = Part.Parameter("D2@Sketch12")
myDimension.SystemValue = 0.006
boolstatus = Part.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 3.44044734343563E-02, -2.54081377085413E-02, -1.86481744649844E-04, False, 0, Nothing, 0)
Part.SelectMidpoint
boolstatus = Part.Extension.SelectByRay(6.00000000000023E-02, -2.03731306029953E-02, -2.70398529742274E-02, -1, 0, 0, 3.17018965904735E-04, 1, True, 0, 0)
Set myDisplayDim = Part.AddDimension2(0.06, -1.43124739018754E-02, -0.03599097671742)
boolstatus = Part.Extension.SelectByID2("D2@Sketch12@Part1.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Set myDimension = Part.Parameter("D3@Sketch12")
myDimension.SystemValue = 0.025
Part.ClearSelection2 True
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureCut4(True, False, False, 1, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, False, True, True, True, True, False, 0, 0, False, False)
Part.SelectionManager.EnableContourSelection = False
Part.ClearSelection2 True
End Sub

SolidworksApi/macros
Comments 2
ID
ID 2021-04-08

thank you a lot dear.

MS
MS 2021-04-08

Hi Ismael,

It looks like you have a good start to using macros in SOLIDWORKS.  UserForms are an easy way to pause code between operations to get the user to select something.  I've added an example macro that combines your two with a UserForm between.  Check out the structure of the new main() procedure and your two macros as functions.  The first is called in the new main() procedure, and the second is called from the button click event on the form.

Also, I noticed a few places where your code relies on dimension and line names that will likely fail if not run in an exact order.

Any line like: Set myDimension = Part.Parameter("D1@Sketch12") ... these will fail if the sketch order is different.  Rather than getting the dimension by name, you can get it from the DisplayDimension returned from AddDimension2.  Change it to...  Set myDimension = myDisplayDim.GetDimension.