I'm making a code in SolidWorks VBA that will automatically model revolved rectangles onto an imported conical solid body with an axis going up the middle. The revolves are placed on angled planes around the central axis and the sketches are dimensioned from the edge of the solidbody on those planes. Due to it being a section of the body, the edges are unnamed and I can't figure out how to dimension off of them. I've got the angled planes down and below is what I have so far for the dimensioning from the imported edge portion: (apologies for any variables not dim'd this is a small part of a much larger code and didn't want to put all of it in here)
Sub CreateDefects()
Dim swBody As SldWorks.Body2
Dim swEdge As SldWorks.Edge
Dim swFeature1 As SldWorks.Feature
Dim swModel As SldWorks.ModelDoc2
Dim edgeFound As Boolean
Dim edgePoint As Variant
Dim edgeDirection As Variant
' Initialize SolidWorks application
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeatureMgr = swModel.FeatureManager
' Get the solid body (assumes the imported feature is the first solid body)
Set swFeature1 = swModel.FeatureByName("Imported1")
edgeFound = False
Set swBody = swFeature1.GetBody
If swBody Is Nothing Then
MsgBox "No imported solid body found."
Exit Sub
End If
' Iterate through edges to find the desired edge (e.g., based on geometry)
Dim edges As Variant
edges = swBody.GetEdges
For Each swEdge In edges
edgePoint = swEdge.GetStartVertex.GetPoint
edgeDirection = swEdge.GetCurve.GetDirection
' Still need to add logic to identify the desired edge (e.g., based on position or direction)
' For simplicity, assume the first edge is the desired edge
edgeFound = True
Exit For
Next swEdge
If Not edgeFound Then
MsgBox "Desired edge not found."
Exit Sub
End If
' Select the plane
bool = swRefPlaneFeature.Select2(False, 0)
If bool = False Then
MsgBox "Plane 'Plane' not found. Please ensure the plane exists." & j
Exit Sub
End If
' Create a sketch on the selected plane
Set swSketchMgr = swModel.SketchManager
swSketchMgr.InsertSketch True
' Draw a rectangle in the sketch
Set swSketchSegment = swSketchMgr.CreateCenterRectangle(0, 0, 0, width / 2, length / 2, 0)
' Select the edge for dimensioning
boolstatus = swModel.Extension.SelectByID2("", "EDGE", edgePoint(0), edgePoint(1), edgePoint(2), False, 0, Nothing, 0)
If Not boolstatus Then
MsgBox "Failed to select the edge for dimensioning."
Exit Sub
End If
' Add a linear dimension (distance from edge to rectangle center)
Set swDim = swModel.AddDimension2(0, 0, 0.05) ' Adjust coordinates as needed
' Add an angular dimension (angle between edge and rectangle side)
boolstatus = swModel.Extension.SelectByID2("", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
If boolstatus Then
Set swDim = swModel.AddDimension2(0.05, 0, 0.05) ' Adjust coordinates as needed
End If
' Exit the sketch
swSketchMgr.InsertSketch False
End Sub
Currently my issue is with the 'Iterate through edges to find desired edge' step. I'm getting the error "Object required" for the line 'For Each swEdge In edges'. I can confirm that everything before this point is working, I am just having a hard time with the sketch. Any and all help is appreciated! Thank you!
edges? What type are these? You may try to dimensionswEdgeas variant (or use an additional variable like shown in the examples in the documentation)