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.
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.