Welcome

VBA Macro to create Flatten Surface feature based on the selected face in SOLIDWORKS part document

Edit ArticleEdit Article

This VBA macro inserts Flatten Surface feature based on the selected face (anchor point is found automatically from the first edge's start vertex) with the middle value of accuracy in active SOLIDWORKS part document.

Const MIN_ACCURACY As Long = 1
Const MAX_ACCURACY As Long = 10

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    
    Set swModel = swApp.ActiveDoc
    
    Dim swSelMgr As SldWorks.SelectionMgr
    
    Set swSelMgr = swModel.SelectionManager
    
    Dim swFace As SldWorks.Face2
    
    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
    
    If Not swFace Is Nothing Then
        
        Dim swEdge As SldWorks.Edge
        Set swEdge = swFace.GetEdges()(0)
        Dim swVertex As SldWorks.Vertex
        Set swVertex = swEdge.GetStartVertex
        
        swFace.SelectByMark False, 1
        swVertex.SelectByMark True, 16
        
        Dim swFlatSurfFeat As SldWorks.Feature
        Set swFlatSurfFeat = swModel.FeatureManager.InsertFlattenSurface2(CLng((MIN_ACCURACY + MAX_ACCURACY) / 2), False)
        
        If swFlatSurfFeat Is Nothing Then
            Err.Raise vbError, "", "Failed to create flatten surface"
        End If
        
    Else
        Err.Raise vbError, "", "Select face"
    End If
    
End Sub

Notifications

Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more


Product of Xarial Product of Xarial