Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

SolidWorks VBA macro that creates a simple drawing from a part file

Last updated on 4 months ago
C
caaSuper Admin
Posted 4 months ago
This macro will open an existing part, create a new drawing, insert a standard view (e.g., front view), and add dimensions to it.

Prerequisites
Ensure you have a part file saved on your system to be used in the drawing.
SolidWorks should be installed and running.
Steps
Open the existing part file.
Create a new drawing document.
Insert a front view of the part.
Add dimensions to the view.

Running the Macro
Open SolidWorks and ensure it is running.
Open the VBA Editor (Alt + F11).
Create a New Macro and paste the provided code.
Run the Macro (F5).
Make sure to adjust file paths and dimensions as needed. This basic example can be expanded with more detailed dimensioning and view customization as per your specific requirements.
C
caaSuper Admin
Posted 4 months ago
Dim swApp As Object
Dim Part As Object
Dim Drawing As Object
Dim Sheet As Object
Dim View As Object

Sub main()

 ' Connect to SolidWorks
 Set swApp = Application.SldWorks

 ' Open the part file
 Dim partFilePath As String
 partFilePath = "C:pathtoyourpartfile.sldprt"
 Set Part = swApp.OpenDoc(partFilePath, 1) ' 1 denotes part document

 ' Create a new drawing document
 Set Drawing = swApp.NewDocument("C:ProgramDataSOLIDWORKSSOLIDWORKS 2022templatesDrawing.drwdot", 3, 0, 0)
 Set Sheet = Drawing.GetCurrentSheet

 ' Insert a standard view (Front View)
 boolstatus = Drawing.Create3rdAngleViews2(partFilePath)

 ' Get the front view
 Set View = Drawing.GetFirstView
 Set View = View.GetNextView

 ' Zoom to fit
 swApp.ZoomToFit2

 
Edited by caa on 04-08-2024 11:32, 4 months ago
C
caaSuper Admin
Posted 4 months ago
' Add dimensions (example for a simple part)
 Dim modelDocExt As Object
 Set modelDocExt = Part.Extension

 ' Assuming we want to add dimensions for a box-like part
 boolstatus = View.AddDimension2(0, 0.05, 0)
 boolstatus = View.AddDimension2(0.05, 0.05, 0)
 boolstatus = View.AddDimension2(0.05, 0, 0)

 ' Activate drawing
 Drawing.Activate

 ' Rebuild the drawing
 Drawing.EditRebuild3

 ' Save the drawing
 Dim drawingFilePath As String
 drawingFilePath = "C:pathtoyourdrawingfile.slddrw"
 Drawing.SaveAs (drawingFilePath)

 ' Close the part document
 swApp.CloseDoc (partFilePath)
 
 MsgBox "Drawing created and saved successfully!", vbInformation

End Sub
C
caaSuper Admin
Posted 4 months ago
Explanation
Connect to SolidWorks:

Set swApp = Application.SldWorks connects to the running instance of SolidWorks.
Open the Part File:

partFilePath = "C:pathtoyourpartfile.sldprt" specifies the path to the part file.
Set Part = swApp.OpenDoc(partFilePath, 1) opens the part file (1 denotes a part document).
Create a New Drawing Document:

Set Drawing = swApp.NewDocument("C:ProgramDataSOLIDWORKSSOLIDWORKS 2022templatesDrawing.drwdot", 3, 0, 0) creates a new drawing document (3 denotes a drawing document).
Set Sheet = Drawing.GetCurrentSheet gets the current sheet in the drawing.
Insert a Standard View (Front View):

boolstatus = Drawing.Create3rdAngleViews2(partFilePath) inserts standard views using third-angle projection.
Set View = Drawing.GetFirstView gets the first view in the drawing.
Set View = View.GetNextView gets the next view, which is the front view.
Add Dimensions:

View.AddDimension2 adds dimensions to the view. Adjust the coordinates and dimension types as needed.
Save and Close:

Drawing.SaveAs (drawingFilePath) saves the drawing.
swApp.CloseDoc (partFilePath) closes the part document.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam