Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SW macro

Status
Not open for further replies.

turboanden

Mechanical
Jul 4, 2005
5
hi

I have made this macro to plot some points in a sketch, but depending on if there is zoomed out or in, it will plot it wrong or right.

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
Set SelMgr = Part.SelectionManager


A = 0.3
B = 0.3
X = 0
C = 0
Y = 0
K = 0.8

ST:
X = X + 0.1
Y = A * X ^ 2 + B * X ^ 2 + C
Part.CreatePoint2 X, Y, 0

If X < K Then GoTo ST

End Sub
 
Replies continue below

Recommended for you

What do you mean by "wrong or right"? I can't get it to behave unexpectedly.

By the way, what programming language are you used to? Using GoTo to create a loop is generally considered poor programming practice in VB/VBA.

-handleman, CSWP (The new, easy test)
 
then try to zoom out an run the macro again.

yes it is poor programming, but im also new to this ;O)
 
remember to delete the old points before you run it again
 
I see what is happening. When a point is created, SW looks to see where it is. If your zoom level is such that SW thinks the points are right on top of each other, it won't add them. Just like if you try to manually insert a point on top of an existing point. You have to tell SW to ignore anything that might be near the point locations and just put them in. You do that with SetAddToDB and SetDisplayWhenAdded.



Code:
Dim swApp As Object
Dim Part As SldWorks.ModelDoc2
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
Set SelMgr = Part.SelectionManager
Part.SetAddToDB True
Part.SetDisplayWhenAdded False

A = 0.3
B = 0.3
X = 0
C = 0
Y = 0
K = 0.8

ST:
X = X + 0.1
Y = A * X ^ 2 + B * X ^ 2 + C
Part.CreatePoint2 X, Y, 0

If X < K Then GoTo ST
Part.SetAddToDB False
Part.SetDisplayWhenAdded True
End Sub

-handleman, CSWP (The new, easy test)
 
Thanks - now I see it works fine. My firm don´t have anything about makking macros in sw. Do SW offer any books/documentation on how to make marcos ?
 
I have the 2004 version of the first one. It is ok to get a very basic start. You will likely outgrow it quickly. I've not seen the Angelsix one yet, but look forward to seeing it. From talking with the author, it looks to be comprehensive.

Matt Lorono
CAD Engineer/ECN Analyst
Silicon Valley, CA
Lorono's SolidWorks Resources
Co-moderator of Solidworks Yahoo! Group
and Mechnical.Engineering Yahoo! Group
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor