learning api's help with this code ?
learning api's help with this code ?
(OP)
Can anyone help me with this code, it's just dumb code (no purpose) I'm just playing with stuff. This inserts 2 sketches and then deletes the 2 sketchs, BUT right now I can only get it to delete the sketches if I know the sketch name. How can I make this delete the sketches no matter what the sketch name is ? Or can I assign a name to the sketch when it's created ?
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("TOP", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.InsertSketch2 True
Part.ClearSelection2 True
Part.SketchRectangle -0.0568645200486, 0.06195686512758, 0, 0.09556634264885, -0.06840716889429, 0, 1
Part.ClearSelection2 True
Part.InsertSketch2 True
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("FRONT", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.InsertSketch2 True
Part.ClearSelection2 True
Part.SketchRectangle -0.0568645200486, 0.06195686512758, 0, 0.09556634264885, -0.06840716889429, 0, 1
Part.ClearSelection2 True
Part.InsertSketch2 True
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Sketch5", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete
boolstatus = Part.Extension.SelectByID2("Sketch6", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete
End Sub
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("TOP", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.InsertSketch2 True
Part.ClearSelection2 True
Part.SketchRectangle -0.0568645200486, 0.06195686512758, 0, 0.09556634264885, -0.06840716889429, 0, 1
Part.ClearSelection2 True
Part.InsertSketch2 True
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("FRONT", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.InsertSketch2 True
Part.ClearSelection2 True
Part.SketchRectangle -0.0568645200486, 0.06195686512758, 0, 0.09556634264885, -0.06840716889429, 0, 1
Part.ClearSelection2 True
Part.InsertSketch2 True
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Sketch5", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete
boolstatus = Part.Extension.SelectByID2("Sketch6", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditDelete
End Sub






RE: learning api's help with this code ?
Another little tip that helps a lot is to declare your objects specifically. For example, your declarations are
CODE
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Instead, I would use
CODE
Dim swApp As SldWorks.Application
Dim Part As SldWorks.ModelDoc2
Dim SelMgr As SldWorks.SelectionManager
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As SldWorks.Feature
That way, when you refer to those objects in your code a dropdown will appear when you type the "." after the variable name. The dropdown will contain all the properties and methods available for that object.
It takes a while to get the hang of where to look in the API help for the things you want to do, but hang in there. Stuff gets easier to find after a while.
RE: learning api's help with this code ?
RE: learning api's help with this code ?
The best code examples you will find are in the API help (Help->SolidWorks and Add-Ins API Help Topics) or the SW website. The API help isn't linked to VBA help in the VBA editor, you have to get to it from SolidWorks itself.
Do you have any other familiarity with VB/VBA? If you're not familiar with objects/properties/methods VBA code can be confusing. If you're new to VBA I'd suggest looking for a basic VBA tutorial on the web. I'm sure there are plenty out there. I learned mostly the hard way, similar to what it looks like you're doing, and looking back I think I could have learned a lot quicker by some method other than osmosis.
RE: learning api's help with this code ?