In SolidWorks, you can use VBA (Visual Basic for Applications) to work with dimensions and automate various tasks related to creating, modifying, and managing dimensions. Here's an example of how to use VBA to create and modify dimensions in SolidWorks:
Accessing the SolidWorks API:
To use VBA with SolidWorks, you need to have SolidWorks installed and configured. SolidWorks provides an API that allows you to control and automate various aspects of the software. You need to add references to the SolidWorks libraries in your VBA project.
Open your VBA editor (Alt + F11 in Excel, for example).
Go to "Tools" > "References" and check the appropriate SolidWorks libraries. The libraries you need may vary depending on your version of SolidWorks.
Creating a New Part and Adding Dimensions:
Here's a simple example of how to create a new part, add a sketch, and dimension it using VBA:
Sub CreateAndDimensionPart()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSketchMgr As SldWorks.SketchManager
Dim swSketch As SldWorks.Sketch
Dim swLine As SldWorks.SketchSegment
Dim swDimension As SldWorks.Dimension
' Create a new SolidWorks instance
Set swApp = CreateObject("SldWorks.Application")
' Create a new part document
Set swModel = swApp.NewDocument("C:PathToYourPartTemplate.prtdot", 0, 0, 0)
' Get the active sketch manager
Set swSketchMgr = swModel.SketchManager
' Create a new sketch
Set swSketch = swSketchMgr.AddSketch(swPlaneXY)
' Draw a line in the sketch
Set swLine = swSketch.CreateLine(0, 0, 0, 1, 0, 0)
' Add a dimension to the line
Set swDimension = swModel.Parameter("D1@Sketch1")
swDimension.SystemValue = 10 ' Set the dimension value
' Rebuild the part
swModel.ForceRebuild3 (False)
' Save the part
swModel.SaveAs3 "C:PathToYourNewPart.sldprt", 0, 2
' Close SolidWorks
swApp.ExitApp
End Sub
Edited by
caa on 01-11-2023 12:27,
10 months agoThis code creates a new part, adds a sketch, and adds a dimension to a line in the sketch.
Running the Macro:
Make sure you have the necessary libraries referenced in your VBA project.
Run the macro by pressing F5 or using the VBA editor's "Run" command.
This is a basic example of using VBA to work with dimensions in SolidWorks. You can extend this code to perform more complex tasks and automate your SolidWorks design processes. Be sure to consult the SolidWorks API documentation for more details and functions related to dimensions and sketching.