Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

API Question 1

Status
Not open for further replies.

naerwen

Materials
Oct 24, 2008
3
All,

I have recorded the following macro for use ...

'Global Variable Declarations

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

' Main() call

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.OpenDoc6("C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings)
swApp.OpenDoc6 "C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings
Set Part = swApp.ActivateDoc2("1 inch cam A ear plate.SLDPRT", False, longstatus)
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
Part.Extension.InsertScene "\scenes\02 studio scenes\66 light cards.p2s"
swApp.ActiveDoc.ActiveView.FrameState = 1
Set Part = Nothing

End Sub

... I need to have a UserForm with a browse button on it when the users call this macro.

Can someone please tell me how to call a UserForm?

Thank you,

Naerwen
 
Replies continue below

Recommended for you

If your form's name is

NaerwensUserForm

then your code would be:

Code:
NaerwensUserForm.Show

-handleman, CSWP (The new, easy test)
 
Of course, first you need to create the user form...

If your are going to browse for folders or files, you will need to use Windows API to gain access to the file dialog objects.
 
Windows API is only needed to browse for folders or to enable multi-selection. Browsing for single files can be accomplished with SolidWorks API's GetOpenFilename function.

-handleman, CSWP (The new, easy test)
 
All,

Ok ... so ... Handleman's initial response was good to go and got me to the point where I have a pre-recorded macro that is now tweeked. However, for creating VBAs in solidworks I would like to know what the best practices are ... for instance ... here is the code in the 'Module 1' area of the SW IDE ...

Sub main()

UserForm1.Show

End Sub


... and here is the code for the form that I have created FOR TEST PURPOSES ONLY ...

Private Sub CommandButton1_Click()

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

Set swApp = Application.SldWorks

Set Part = swApp.OpenDoc6("C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings)

swApp.OpenDoc6 "C:\Documents and Settings\begas\My Documents\Work-2.0\SolidWorks\SolidworksDrawings\1 INCH CAM\1 inch cam A ear plate.SLDPRT", 1, 0, "", longstatus, longwarnings

Set Part = swApp.ActivateDoc2("1 inch cam A ear plate.SLDPRT", False, longstatus)

swApp.ActiveDoc.ActiveView.FrameLeft = 0

swApp.ActiveDoc.ActiveView.FrameTop = 0

swApp.ActiveDoc.ActiveView.FrameState = 1

Part.Extension.InsertScene "\scenes\02 studio scenes\66 light cards.p2s"

swApp.ActiveDoc.ActiveView.FrameState = 1

Set Part = Nothing


End Sub

Private Sub CommandButton2_Click()

Dim swApp As Object

Set swApp = Application.SldWorks

swApp.CloseDoc "1 inch cam A ear plate.SLDPRT"

End Sub

Private Sub UserForm_Click()

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

End Sub

... Is this practice correct??? What I am referring to is the code's structure on 'paper' ... as it were. That is, is this correct ... to force the 'Module 1' area of the IDEs project to call the form and then have the form handle the code ... or should I/ can I manipulate the objects on UserForm1 from the 'Module 1' object in the IDE?

I hope this is not too confusing ... if it is ... remember ... your guys king fu is way better than mine ... for now. =P

Thanks for all the help you two have given thus far. I truly appreciate it.

Naerwen
 
You can manipulate objects on the form from a code module. You can also write functions/subs in a module and call them from a form. It really depends on what you need to do with your code. In the case of your example, where you are opening a document and closing it, that is just fine to do in the form. Your entire "UserForm_Click()" routine does nothing, by the way. You can delete it. Also, the two lines containing OpenDoc6 both do the same thing. Keep the one that begins "Set Part =" and delete the other one.

-handleman, CSWP (The new, easy test)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor