Вы находитесь на странице: 1из 5

Exercise 1 SOLIDWORKS

Working with New Documents

Exercise 1:
Working with
New Documents

Objective To connect a macro to the SOLIDWORKS application, create new


documents, and call methods on SldWorks and ModelDoc2 objects.

Skills Learned  Adding option button controls to a form.


 Adding checkbox controls to a form.
 Program API calls specific to SldWorks and ModelDoc2 application
objects.
 Importing modules.

APIs Used SldWorks.SendMsgToUser2


-------------------------------
SldWorks.NewDocument
SketchManager.InsertSketch
SketchManager.CreateCircleByRadius
ModelDoc2.ViewZoomtofit2

Procedure 1. Open the SOLIDWORKS software with no files created.


2. Create a new macro named NewDocs.swp.
3. Add a UserForm to the macro.
4. Program the entry point procedure to show the UserForm.
5. Use the image as a guide for designing each control.
6. Program the click event for each button.
7. Use the methods listed above in your code.
8. Save and test the macro.

1
SOLIDWORKS Exercise 1
Working with New Documents

Solution Option Explicit

Sub main()
frmNewDocs.Show
End Sub
---------------------------------------------------------------
Option Explicit

Const TRAININGDIR As String = _


"C:\SOLIDWORKS Training Files\API Fundamentals\"
Const TEMPLATEDIR As String = _
"C:\SolidWorks Training Files\Training Templates\"

Dim swApp As SldWorks.SldWorks


Dim swModel As SldWorks.ModelDoc2

Private Sub cmdSldWorks_Click()


Set swApp = Application.SldWorks

If chkMessage.Value = True Then


swApp.SendMsgToUser2 _
"Hello, this is a sample message", 1, 2
End If

End Sub

Private Sub cmdModel_Click()


Set swApp = Application.SldWorks

If optPart.Value = True Then


Set swModel = swApp.NewDocument(TEMPLATEDIR + _
"Part_MM.prtdot", 0, 0#, 0#)
End If

If optAssy.Value = True Then


Set swModel = swApp.NewDocument(TEMPLATEDIR + _
"Assembly_MM.asmdot", 0, 0#, 0#)
End If

If chkCircle.Value = True Then


swModel.SketchManager.InsertSketch True
swModel.SketchManager.CreateCircleByRadius 0, 0, 0, 0.05
swModel.SketchManager.InsertSketch True
swModel.ViewZoomtofit2
End If

End Sub
---------------------------------------------------------------

2
Exercise 2 SOLIDWORKS
Working with Existing Documents

Exercise 2:
Working with
Existing
Documents

Objective To open specific document types and connect to existing documents


already open.

Skills Learned  Adding option button controls to a form.


 Adding checkbox controls to a form.
 Program API calls specific to SldWorks and ModelDoc2
application objects.
 Importing modules.

APIs Used SldWorks.OpenDoc6


----------------------------------------
SldWorks.ActiveDoc
ModelDoc2.AddLightSource
ModelDoc2.SetLightSourcePropertyValuesVB
ModelDoc2.LockLightToModel
ModelView.GraphicsRedraw

Procedure 1. Open the SOLIDWORKS software with no files created.


2. Create a new macro named ExistingDocs.swp.
3. Add a UserForm to the macro.
4. Program the entry point procedure to show the UserForm.
5. Use the image as a guide for designing each control.
6. Program the click event for each button.
7. Use the methods listed above in your code.
8. Save and test the macro.

3
SOLIDWORKS Exercise 2
Working with Existing Documents

Solution Option Explicit

Sub main()
frmExistingDocs.Show
End Sub
---------------------------------------------------------------
Option Explicit

Const TRAININGDIR As String = _


"C:\SOLIDWORKS Training Files\API Fundamentals\"
Const FILEDIR As String = _
TRAININGDIR & "Lesson02 - Object Model Basics\Case Study\"

Dim swApp As SldWorks.SldWorks


Dim swModel As SldWorks.ModelDoc2

Private Sub cmdSldWorks_Click()


Set swApp = Application.SldWorks

If chkOpen.Value = True Then


Dim fileerror As Long
Dim filewarning As Long

If optPart.Value = True Then


swApp.OpenDoc6 FILEDIR + "sample.sldprt", _
swDocPART, swOpenDocOptions_Silent, "", _
fileerror, filewarning
End If

If optAssy.Value = True Then


swApp.OpenDoc6 FILEDIR + "sample.sldasm", _
swDocASSEMBLY, swOpenDocOptions_Silent, "", _
fileerror, filewarning
End If

End If
End Sub

Private Sub cmdModel_Click()


Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Check to see if a document is loaded


If swModel Is Nothing Then
swApp.SendMsgToUser2 "Please open a part or assembly", _
swMbStop, swMbOk
Exit Sub
End If

If chkSpot.Value = True Then


swModel.AddLightSource "SW#2", 4, "Directional2"
swModel.SetLightSourcePropertyValuesVB "SW#2", 4, 1, _
16777215, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
swModel.LockLightToModel 2, False
swModel.ActiveView.GraphicsRedraw
End If

End Sub

4
Exercise 2 SOLIDWORKS
Working with Existing Documents

Вам также может понравиться