×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

learning api's help with this code ?

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

RE: learning api's help with this code ?

How to delete the sketches depends on how much your macro "knows" about the sketches.  In your case, the macro "knows" the sketch name because you've hard-coded it in the macro.  I assume you have to increment the number in the name of the sketch before you run the macro each time.  One way to get a pointer to your newly created sketch object would be to use the fact that your macro is creating the sketches.  That allows you to use ModelDocExtension::GetLastFeatureAdded to get the Feature object of the sketch.  You can then use Feature::Select2 and ModelDoc2::EditDelete to delete it.  

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 ?

(OP)
Cool, thanks for the response, I think I have a lot of vb learning to do.  How do you know about things like " Feature::Select2 and ModelDoc2: " is there somewhere I can learn about these commands and what they do?  The approach I've been taking is just creating a bunch of differnt types of macro's and seeing what code they use to do whatever task. Any  good suggestions for me ?

RE: learning api's help with this code ?

By "creating" do you mean recording?  That code does look like the code SW generates.  I've found that recording in SW isn't quite as helpful as it is in Excel or other programs, partly because of the way it handles selections, etc.  

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. smile

RE: learning api's help with this code ?

(OP)
lol,  Thanks for the tips!!!  I really don't have any familiarity with VB/VBA objects, methods, etc.. and yes by "creating" I ment reccording.  I'll have a look around the web and check out those help files.  Thanks again

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources