Beginner API
Beginner API
(OP)
Need some help to get started. I did my best to try to show what I wanted to do. Please help.
'----------------------------------------------------------------------------------
'Preconditions:
' An assembly that consists of three or more parts (Cavity, Mold, EJ Plate)
' A sketch containing sketch points
'
'Postconditions:
' A hole that varies type and size through 3 seperate parts in an assembly using
' the same x,y coordinates.
'
'-----------------------------------------------------------------------------------
Sub Ej_Pin_Hole()
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
swApp.ActiveDoc.ActiveView.FrameState = 1
Part.ClearSelection2 True
'Ask user to select a sketch that contains sketch points
swApp.SendMsgToUser2 "Please select the Sketch.", swMbWarning, swMbOk
'User selects sketch
???? select all points in sketch (x & y values needed only)
'--------------------------------CAVITY-----------------
'Ask user to select the Cavity
swApp.SendMsgToUser2 "Please select the Cavity.", swMbWarning, swMbOk
'User selects the cavity
?????
Part.EditPart
'Ask the user to select a starting face
swApp.SendMsgToUser2 "Please select the Cavity Start Face.", swMbWarning, swMbOk
'User selects face to start hole wizard
?????
'Ask the user to select an ending face
swApp.SendMsgToUser2 "Please select the Cavity End Face.", swMbWarning, swMbOk
'User selects face to end the hole wizard
?????
'User is asked to provide a value for h1offset
????? h1offset=?
'Add hole wizard feature at previously selected sketch points and face
' with the end face being the reference plane to the "end condition"
'being "offset from surface" by a value of "h1offset"
????? use values collected above for x, y, and z positions
Part.FeatureManager.HoleWizard2 2, 3, 101, "3/16", 1, 0.0047625, 0.0508, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, "", False, 1, 1, 1, 1
Part.EditAssembly
'----------------------MOLD---------------------
'Ask user to select the Mold
swApp.SendMsgToUser2 "Please select the Mold.", swMbWarning, swMbOk
'User selects the Mold
?????
Part.EditPart
'Ask User to select the Mold Start face
?????
'Add hole wizard feature at previously selected x,y sketch points
'and Mold start face
Part.FeatureManager.HoleWizard2 2, 3, 101, "3/16", 1, 0.00555625, 0.0254, 2, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, "", False, 1, 1, 1, 1
Part.EditAssembly
'-------------------------------EJ PLATE---------------------------
'Ask user to select the Ej PLate
swApp.SendMsgToUser2 "Please select the Ej Plate.", swMbWarning, swMbOk
'User selects the Ej Plate
?????
Part.EditPart
'Ask User to select the Ej Plate Start face
?????
'Add hole wizard feature at previously selected sketch points
'and Ej Plate start face
Part.FeatureManager.HoleWizard2 0, 3, 80, "3/16", 1, 0.00555625, 0.0127, 0.01031875, 0.0047625, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, "", False, 1, 1, 1, 1
Part.EditAssembly
End Sub
'----------------------------------------------------------------------------------
'Preconditions:
' An assembly that consists of three or more parts (Cavity, Mold, EJ Plate)
' A sketch containing sketch points
'
'Postconditions:
' A hole that varies type and size through 3 seperate parts in an assembly using
' the same x,y coordinates.
'
'-----------------------------------------------------------------------------------
Sub Ej_Pin_Hole()
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
swApp.ActiveDoc.ActiveView.FrameState = 1
Part.ClearSelection2 True
'Ask user to select a sketch that contains sketch points
swApp.SendMsgToUser2 "Please select the Sketch.", swMbWarning, swMbOk
'User selects sketch
???? select all points in sketch (x & y values needed only)
'--------------------------------CAVITY-----------------
'Ask user to select the Cavity
swApp.SendMsgToUser2 "Please select the Cavity.", swMbWarning, swMbOk
'User selects the cavity
?????
Part.EditPart
'Ask the user to select a starting face
swApp.SendMsgToUser2 "Please select the Cavity Start Face.", swMbWarning, swMbOk
'User selects face to start hole wizard
?????
'Ask the user to select an ending face
swApp.SendMsgToUser2 "Please select the Cavity End Face.", swMbWarning, swMbOk
'User selects face to end the hole wizard
?????
'User is asked to provide a value for h1offset
????? h1offset=?
'Add hole wizard feature at previously selected sketch points and face
' with the end face being the reference plane to the "end condition"
'being "offset from surface" by a value of "h1offset"
????? use values collected above for x, y, and z positions
Part.FeatureManager.HoleWizard2 2, 3, 101, "3/16", 1, 0.0047625, 0.0508, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, "", False, 1, 1, 1, 1
Part.EditAssembly
'----------------------MOLD---------------------
'Ask user to select the Mold
swApp.SendMsgToUser2 "Please select the Mold.", swMbWarning, swMbOk
'User selects the Mold
?????
Part.EditPart
'Ask User to select the Mold Start face
?????
'Add hole wizard feature at previously selected x,y sketch points
'and Mold start face
Part.FeatureManager.HoleWizard2 2, 3, 101, "3/16", 1, 0.00555625, 0.0254, 2, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, "", False, 1, 1, 1, 1
Part.EditAssembly
'-------------------------------EJ PLATE---------------------------
'Ask user to select the Ej PLate
swApp.SendMsgToUser2 "Please select the Ej Plate.", swMbWarning, swMbOk
'User selects the Ej Plate
?????
Part.EditPart
'Ask User to select the Ej Plate Start face
?????
'Add hole wizard feature at previously selected sketch points
'and Ej Plate start face
Part.FeatureManager.HoleWizard2 0, 3, 80, "3/16", 1, 0.00555625, 0.0127, 0.01031875, 0.0047625, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, "", False, 1, 1, 1, 1
Part.EditAssembly
End Sub






RE: Beginner API
RE: Beginner API
Just for example, you want to prompt the user to select a sketch/face/part, etc. I usually use the VBA MsgBox function rather than SendMsgToUser, but I believe they function in the same way. (Please someone correct me if that's wrong. I'm at home right now and can't verify at the moment.) That is, code execution stops while a message is displayed, and resumes when the user clears the message. The problem is, the message is "application modal", which means that SW is frozen as well. The user can't select sketches/faces/parts or do anything else other than clear the message. When the message is cleared, the macro goes on without waiting for the user's selection. To get around this, you'll have to either learn the API for the property manager (ambitious in its own right) or take the easier route of creating your own non-modal User Form dialog box that can be displayed while the user makes selections.
As a disclaimer to the following remarks, I usually prefer not to suggest to others to change their method of attack on a problem because I don't know all the details of the situation. That being said, unless this is a macro you will run at least once every day you may not be able to justify the time spent writing it. Another concern is that I think you'll end up with completely unconstrained sketches in each part as the location sketches for the hole wizard features. You may want to consider using sketch-driven or table-driven patterns.
Sorry!
RE: Beginner API
As for doing this without a macro...
- Native SW Solution: The Hole Series function in the Hole Wizard is "supposed" to accomplish this...which it does, but you have to be careful. My main gripe with it is that the Feature Scope is useless.
- Third Party Solution: http://www.123go-allusers.com/ I've never used it, only demo'd, but it looks pretty good and is really fast.
KenRE: Beginner API
(my apologies if this is common knowledge!)
CODE
Dim swApp as SldWorks.SldWorks
Dim SelMgr as SldWorks.SelectionManager
Dim swDoc as SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set SelMgr = swDoc.SelectionManager
swDoc.ClearSelection2 True 'Clear all selections
MsgBox "Please select something... Anything!"
while SelMgr.GetSelectedObjectCount < 1
DoEvents 'Wait for user selection
wend
msgbox "Hooray! You actually picked " & _
SelMgr.GetSelectedObjectCount & _
" things!"
End Sub
The reason I put in the number of objects selected in the final message is that sometimes, especially in drawings, you can actually select more than one item with a single click. For example, in a drawing when you click on a component, I believe it selects both the component and the view it's in.
RE: Beginner API
For now I am looking into the smart component feature because it seems like it will do what I want. Although, I am having difficulty with multiple configurations of a smart component. I have about 20 configurations of a part that I want to make as smart components. Each of the configurations requires different features along with it. There is a configuration table with the smart component feature, but I haven't been able to get it to work yet. I have been able to create one configuration with the correct features, but do not know how to add additional features for other configurations. Does anyone have experience with Smart Components and configurations?
RE: Beginner API
If you wish to select multiple objects, simply change the value of MINSELECTIONS to the number of selections desired.
CODE
Dim swApp As SldWorks.SldWorks
Dim SelMgr As SldWorks.SelectionMgr
Dim swDoc As SldWorks.ModelDoc2
Dim nContinue As Integer
Dim SelType As SwConst.swSelectType_e
Dim sMsg As String
Dim i As Long
Const MINSELECTIONS = 1
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set SelMgr = swDoc.SelectionManager
MsgBox "Please select something... Anything!"
While nContinue <> vbNo
swDoc.ClearSelection2 True 'Clear all selections
While SelMgr.GetSelectedObjectCount < MINSELECTIONS
DoEvents 'Wait for user selection
Wend
sMsg = "You actually picked " & _
SelMgr.GetSelectedObjectCount & _
" things! Object types selected are:" & vbCrLf
For i = 1 To SelMgr.GetSelectedObjectCount
SelType = SelMgr.GetSelectedObjectType3(i, -1)
sMsg = sMsg & vbCrLf & i & ". " & SelType
Next
nContinue = MsgBox(sMsg & vbCrLf & vbCrLf & "Keep going?", vbYesNo)
Wend
End Sub
RE: Beginner API
Handleman, have you ever used the WithEvents statement?
RE: Beginner API
I've never used WithEvents before. If you're asking because that's a better way to do what I'm doing above then I'd love to learn! I don't have any actual training in VB/VBA (or programming at all, for that matter), so I'm sure there's a lot of stuff I'm missing. If you're trying to figure it out yourself then I'm afraid I won't be much help.
RE: Beginner API
I was asking because there may be a better way. If you go out to the API Download area in the SolidWorks website and look for the Run Events Monitor Utility. I have found this example macro very helpful. Using it as a guide, I wrote a routine that the user first selects a feature in the FeatureManagerTree then selects a dimension from the graphics area. The macro just waits for the user to make the selections. If an invalid selection is made, the macro either ignores it or displays an error message.
SA
RE: Beginner API
I do think that the little 3-line DoEvents loop may be a bit simpler for a quick-and-dirty macro, since any macro requiring user selections will already have to have a selection manager object.
Thanks for the heads-up!
RE: Beginner API
You are correct, you have to use a class module if you do not have a form.
As for using the DoEvents loop, that is a matter of programming preference. If I was writing the macro for myself, I would probably use that method, but if I was writing it for a lot of people to use, I think I would opt for the WithEvents. The reason being is that users normally run more than just SolidWorks, I am not comfortable having that loop running when the user switches to, as an example, Excel or Word.
SA