Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Beginner API 1

Status
Not open for further replies.

jloeff

Mechanical
Feb 13, 2002
29
0
0
US
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
 
Replies continue below

Recommended for you

If you are using SWX2006 you should have a look at Smart Components. There is a worked-out example in the "What's New" Manual (available under the Help menu) that explains the steps in detail. You may be able to use this approach to create the features instead.
 
This is a pretty ambitious macro for a beginner. It seems on the surface like it would be simple, but as I learned with the first macro on this scale that I ever tried to write, there's a lot to add in those areas with question marks. It's probably a bit beyond what we can help with in this forum.

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!
 
I agree with what was stated above...this macro is going to be a huge undertaking.

As for doing this without a macro...
[ol][li]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.[/li]
[li]Third Party Solution: I've never used it, only demo'd, but it looks pretty good and is really fast.[/li][/ol]

Ken
 
OK, scratch part of my last post. For anyone who cares, you may be able to pause execution and wait for a user selection by a block of code something like this:

(my apologies if this is common knowledge!)

Code:
sub WaitForInput()
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.
 
Thanks for the input...
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?
 
If anyone's interested, here's a little routine that will wait for the user to select something in SW. It will prompt the user with a message box to make a selection. After the user clears the box it will wait for a selection. As soon as the user makes any selection the routine will resume, giving some info about the selected object(s)

If you wish to select multiple objects, simply change the value of MINSELECTIONS to the number of selections desired.

Code:
Sub WaitForUserSelection()
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
 
Sorry, my posts sort of went off on a tangent, prompted by my further thinking about a statement I made in my first post. I said that there wasn't any way to pause a macro and wait for the user to take action without creating a custom, non-modal dialog box. Then I thought of a way to do it.

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. :)
 
handleman,

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
 
Thanks, SA, I downloaded that utility. It looks pretty sweet. It looks like the notifications work pretty straightforward if you're using a user form. I guess you have to use a Class somehow to get the notifications w/o using a form? I'm not sure what a Class really is. New concept for me to read up on, I guess.

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!
 
handleman,

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
 
Status
Not open for further replies.
Back
Top