Solidworks VBA Macro - Create Wrap

22 minute read

This article has "Beginner โžก Intermediate" level Macro program.
If you are following my articles then will not be an issue for you.
In this article we did not use ๐Ÿš€ UserForm for taking inputs, instead we use ๐Ÿš€ Input Box for value input and ๐Ÿš€ Message Box to notify user.
I hope you will also like this type of tutorials.
Thank you for reading.

Objective

In this article we create and understand VBA macro of Wrap feature in SOLIDWORKS CAD Software.

This method is most updated method, so use this method if you want to create a new Wrap Feature quickly.

Results We Can Get

After running our macro we successfully create Wrap feature as a result.

Below image shows the result we get.

wrap-feature-final-result

We create Wrap Feature in following steps in general.

  1. Ask user to select Sketch.
  2. Ask user to select Faces.
  3. Ask for Wrap Thickness.

To get the correct result please follow the steps correctly.

Macro Video

Below ๐ŸŽฌ video shows Wrap feature from SOLIDWORKS VBA Macros.


Above video is just for visualization and there are no explanation.

I have explained each and every line in this article.

It is advisable to watch video, since it help you to better understand the process.

VBA Macro

Below is the VBA macro for creating Wrap Feature.

Option Explicit

' Main program for Wrap
Sub main()

  ' Variable for Solidworks Application
  Dim swApp As SldWorks.SldWorks
  
  ' Set Solidworks Application variable to current application
  Set swApp = Application.SldWorks
  
  ' Check if Solidworks is opened or not
  If swApp Is Nothing Then
    MsgBox ("Solidworks is not opened")
    Exit Sub
  End If
  
  ' Variable for Solidworks document
  Dim swDoc As SldWorks.ModelDoc2
  
  ' Set Solidworks document variable to currently opened document
  Set swDoc = swApp.ActiveDoc
  
  ' Check if Solidworks document is opened or not
  If swDoc Is Nothing Then
    MsgBox ("Solidworks document is not opened. Please open a document.")
    Exit Sub
  End If
  
  ' Variable for Solidworks Selection Manager
  Dim swSelMgr As SldWorks.SelectionMgr
  
  ' Set Solidworks Selection Manager variable
  Set swSelMgr = swDoc.SelectionManager
  
  ' Array of Solidworks Entities
  Dim swObjects(1 To 2) As SldWorks.Entity
  
  ' Variable for Solidworks Entity
  Dim swObject As SldWorks.Entity
  
  ' Local variable for selection
  Dim selectItems As Integer
  selectItems = 1
  
  ' Loop till we select all entities
  While selectItems <= 2
    
    ' Message to show user
    Dim messageToUser As String

    ' Update Messages
    Select Case selectItems
      Case 1
        messageToUser = "Please select a Sketch."
      Case 2
        messageToUser = "Please select a Face."
    End Select
  
    ' Show message to user
    MsgBox messageToUser
      
    ' Loop until we complete our selection
    While swObjects(selectItems) Is Nothing
        
      ' Local integer for loop
      Dim i As Integer
      
      ' Looping until we select
      For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        
        Select Case selectItems
          Case 1
            ' If the selection type Sketch
            If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSKETCHES Then
                
                ' Set the Solidworks Entity object to Sketch
                Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
                
            Else
    
              ' Inform user to select a sketch
              MsgBox "Please select sketch from Feature tree."
    
              ' Clear selection
              swDoc.ClearSelection2 True
            End If
          Case 2
            ' If the selection type is face
            If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelFACES Then
                
              ' Set the Solidworks Entity object to face
              Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
                
            Else
    
              ' Inform user to select a Face
              MsgBox "Please select a Face."
    
              ' Clear selection
              swDoc.ClearSelection2 True
            End If
        End Select
        
      Next
      
      DoEvents
    Wend
    
    ' Clear selection
    swDoc.ClearSelection2 True
    
    ' Increase the selection count
    selectItems = selectItems + 1

  Wend
    
  ' Clear previous selection
  swDoc.ClearSelection2 True
  
  ' Variable for Solidworks Select Data
  Dim swSelData As SldWorks.SelectData
  
  ' Local variable for counter
  Dim j As Integer
  j = 1
  
  ' Loop till counter is 2
  While j < 3
    
    ' Set the current instance to Solidworks Entity variable
    Set swObject = swObjects(j)
    
    ' Create Select data for this entity
    Set swSelData = swSelMgr.CreateSelectData
    
    ' Update Marking as per selected entity
    Select Case j
      Case 1
        ' For sketch, set mark to 4
        swSelData.Mark = 4
      Case 2
        ' For face, set mark to 1
        swSelData.Mark = 1
    End Select

    ' Select the current entity
    swObject.Select4 True, swSelData
    
    j = j + 1
      
  Wend
  
  ' Local variables used as Conversion Factors
  Dim LengthConversionFactor As Double
  Dim AngleConversionFactor As Double
  
  ' Use a Select Case, to get the length of active Unit and set the different factors
  Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit
    
    Case swMETER    ' If length is in Meter
      LengthConversionFactor = 1
      AngleConversionFactor = 1
    
    Case swMM       ' If length is in MM
      LengthConversionFactor = 1 / 1000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swCM       ' If length is in CM
      LengthConversionFactor = 1 / 100
      AngleConversionFactor = 1 * 0.01745329
    
    Case swINCHES   ' If length is in INCHES
      LengthConversionFactor = 1 * 0.0254
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEET     ' If length is in FEET
      LengthConversionFactor = 1 * (0.0254 * 12)
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEETINCHES     ' If length is in FEET & INCHES
      LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
      AngleConversionFactor = 1 * 0.01745329
    
    Case swANGSTROM        ' If length is in ANGSTROM
      LengthConversionFactor = 1 / 10000000000#
      AngleConversionFactor = 1 * 0.01745329
    
    Case swNANOMETER       ' If length is in NANOMETER
      LengthConversionFactor = 1 / 1000000000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swMICRON       ' If length is in MICRON
      LengthConversionFactor = 1 / 1000000
      AngleConversionFactor = 1 * 0.01745329
  End Select
  
  ' Variable to hold user input
  Dim response As String
  
  ' Getting Wrap thickness from user.
  response = InputBox("Please Enter [Wrap thickness]:")
  
  ' This will handle empty value or cancel case
  If Len(response) = 0 Then
    MsgBox "Empty or no value. Please try again."
    ' Clear all selection
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' This will handle case for Non-numeric values
  If IsNumeric(response) = False Then
    MsgBox "Entered value is Non-numeric. Please try again."
    ' Clear all selection
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Variable for Wrap thickness
  Dim wrapThickness As Double
  
  ' Set Wrap thickness
  wrapThickness = CDbl(response) * LengthConversionFactor

  ' This will handle case for 0 Wrap thickness
  If wrapThickness = 0 Then
    MsgBox "Entered value must be greater than 0. Please try again."
    ' Clear all selection
    swDoc.ClearSelection2 True
    Exit Sub
  End If

  ' Variable for Solidworks Wrap Feature
  Dim swFeature As SldWorks.Feature
  
  ' Create Wrap feature
  Set swFeature = swDoc.FeatureManager.InsertWrapFeature2(swWrapSketchType_e.swWrapSketchType_Engrave, wrapThickness, False, 0, 1)

  ' Check if Wrap feature creates or not
  If swFeature Is Nothing Then
    MsgBox ("Failed to create Wrap feature.")
    ' Clear all selection
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Erase array data
  Erase swObjects
  
  ' View zoom to fit
  swDoc.ViewZoomtofit2
  
  ' Clear all selection
  swDoc.ClearSelection2 True

End Sub

Prerequisite

There are some prerequisite for this article.

  • Knowledge of VBA programming language is โ—required.
  • We are not creating feature from code but we use existing ๐Ÿš€ Extrude feature to create Wrap feature.

Below image shown prerequisite 3D model for our demo.

prerequisite

As shown in above image, there is one Extrude features in our part.

  • Base Feature : This is our Base Extrude feature.

If you want to create this Extrude features programmatically then please refer to below article.

We will apply checks in this article, so the code we write should be error free most of the time.

Steps To Follow

This Wrap Feature VBA macro can be divided into following sections:

  1. Create and Initialize required variables
  2. Ask user to select entities
  3. Mark selected entities
  4. Get unit Conversion factors
  5. Get Wrap thickness And Validation
  6. Create Wrap feature
  7. Final work

Every section with each line is explained below.

I also give some links (see icon ๐Ÿš€) so that you can go through them if there are anything I explained in previous articles.

Create and Initialize required variables

In this section we create and initialize required variables.

Option Explicit
' Main program for Wrap
Sub main()

End Sub
  • In above line, we create main program for Wrap Feature.
  • This is a Sub procedure which has name of main.
  • This procedure hold all the statements (instructions) we give to computer.
  • Reference: Detailed information ๐Ÿš€ VBA Sub and Function Procedures article of this website.
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
  • Purpose: In above line, we create a variable for Solidworks application.
  • Variable Name: swApp
  • Type: SldWorks.SldWorks
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.

Inside this section we initialize required variables.

' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
  • In above line, we set value of swApp variable.
  • This value is currently opened Solidworks application.
' Check if Solidworks is opened or not
If swApp Is Nothing Then  
  MsgBox ("SOLIDWORKS is not opened")  
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swApp variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swApp Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: SOLIDWORKS is not opened
    • Then we stop our macro here.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
  • Purpose: In above line, we create a variable for Solidworks document.
  • Variable Name: swDoc
  • Type: SldWorks.ModelDoc2
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
  • In above line, we set value of swDoc variable.
  • This value is currently opened part document.
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
  MsgBox ("Solidworks document is not opened. Please open a document.")
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swDoc variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swDoc Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: SOLIDWORKS document is not opened. Please open a document.
    • Then we stop our macro here.
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr
  • Purpose: In above line, we create a variable for Solidworks Selection Manager.
  • Variable Name: swSelMgr
  • Type: SldWorks.SelectionMgr.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Set Solidworks Selection Manager variable
Set swSelMgr = swDoc.SelectionManager
  • In above line, we set value of swSelMgr variable.
  • This value is current documentโ€™s Selection Manager.
' Array of Solidworks Entities
Dim swObjects(1 To 2) As SldWorks.Entity
  • In above line, we create an ๐Ÿš€ Array of Solidworks Entities.
  • Variable Name: swObjects
  • Type: SldWorks.Entity
  • Length of Array: (1 To 2)
  • Reference: Please visit
' Variable for Solidworks Entity
Dim swObject As SldWorks.Entity
  • Purpose: In above line, we create a variable for selection and marking of Solidworks Entity.
  • Variable Name: swObject
  • Type: SldWorks.Entity
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Local variable for selection
Dim selectItems As Integer
selectItems = 1
  • In above line, we create a variable for selection and set its value.
  • Variable Name: selectItems
  • Type: Integer
  • Value: 1

Ask user to select Entities

In this section, we ask user to select Sketch and Face for Wrap feature.

' Loop till we select all entities
While selectItems <= 2

Wend
  • In above line of code we start a While loop.
  • We loop until selectItems variableโ€™s value is equal to 2.
  • Reference: ๐Ÿš€ VBA Looping article from this website.
' Message to show user
Dim messageToUser As String
  • In above line, we create a variable to show a messages we want to show before selection.
  • Variable Name: messageToUser
  • Type: String
' Update Messages
Select Case selectItems
Case 1
  messageToUser = "Please select a Sketch."
Case 2
  messageToUser = "Please select a Face."
End Select
  • In above code block, we use a Select statement to update message.
  • We use the case on selectItems.
    • Case 1: messageToUser update to "Please select a Sketch."
    • Case 2: messageToUser update to "Please select a Face."
  • Reference: ๐Ÿš€ Select statement article of this website.
' Show message to user
MsgBox messageToUser
  • In above line, we show the message to user.
  • Below image shows the message for Sketch selection to the user.

message-to-select-wrap-sketch

  • Below image shows the message for Face selection to the user.

message-to-select-wrap-face

' Loop until we complete our selection
While swObjects(selectItems) Is Nothing    

Wend
  • In above line, we create another While loop.
  • This loops continues to run until we select the our Sketch and Face for Wrap feature.
  • Reference: ๐Ÿš€ VBA Looping article from this website.
' Local integer for loop
Dim i As Integer
  • In above line, we create a variable as a counter.
  • Variable Name: i
  • Type: Integer
' Loop until we select
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

Next
  • In above line, we create a For loop.
  • This loops start from i = 1 to number of objects we select.
' Update Messages
Select Case selectItems

End Select
  • In above line, we use a Select statement for selection entities.
  • We use the case on selectItems.
  • Reference: ๐Ÿš€ Select statement article of this website.
Case 1
  ' If the selection type is Sketch
  If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSKETCHES Then

    ' Set the Solidworks Entity object to Sketch
    Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)

  Else

    ' Inform user to select a Sketch
    MsgBox "Please select sketch from Feature tree."

    ' Clear selection
    swDoc.ClearSelection2 True
  End If
  • Above code block we use for Case 1.
  • In above code block, we use ๐Ÿš€ IF/ElseIf statement with conditions.
  • The ๐Ÿš€ IF statement is True when selected object is type of SOLIDWORKS Sketch.
  • If selected object is not type of SOLIDWORKS Sketch then ๐Ÿš€ Else statement code runs.

When we select the sketch from Model view, then selected object is SOLIDWORKS Face.
When we select the sketch from Feature Tree, then select object is SOLIDWORKS Sketch.

' Set the Solidworks Entity object to Sketch
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
  • When ๐Ÿš€ IF statement is True we run above line.
  • In above line, we set the object inside array.
    • Array of SOLIDWORKS Entities = swObjects
    • Indexed Entity Object = swObjects[selectItems]
  • Indexed Entity Object value set by GetSelectedObject6() method.
    • GetSelectedObject6() method is part of SOLIDWORKS Selection Manager.
    • Reference: Please visit ๐Ÿš€ online Solidworks API Help.
' Inform user to select a Sketch
MsgBox "Please select sketch from Feature tree."

' Clear selection
swDoc.ClearSelection2 True
  • When ๐Ÿš€ Else statement is True we execute run above lines.
  • 1st Line: We show a message to user to select Sketch from Feature Tree.
  • 2nd Line: After that we clear our selection.
Case 2
  ' If the selection type is face
  If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelFACES Then
  
    ' Set the Solidworks Entity object to feature
    Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)

  Else

    ' Inform user to select a Face
    MsgBox "Please select a Face."

    ' Clear selection
    swDoc.ClearSelection2 True
  End If
  • Above code block we use for Case 2.
  • In above line, we use ๐Ÿš€ IF/Else statement with conditions.
  • ๐Ÿš€ IF statement is True when selected object is type of SOLIDWORKS Face.
  • If selected object is not type of SOLIDWORKS Face then ๐Ÿš€ Else statement code runs.
' Set the Solidworks Entity object to feature
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
  • In above line, we set the indexed object inside array.
    • Array of SOLIDWORKS Entities = swObjects
    • Indexed Entity Object = swObjects[selectItems]
  • Indexed Entity Object value set by GetSelectedObject6() method.
    • GetSelectedObject6() method is part of SOLIDWORKS Selection Manager.
    • Reference: Please visit ๐Ÿš€ online Solidworks API Help.
' Inform user to select Face
MsgBox "Please select a Face."

' Clear selection
swDoc.ClearSelection2 True
  • When ๐Ÿš€ Else statement is True we execute code given below.
  • 1st Line: We show a message to user to select Face.
  • 2nd Line: We clear our selection.
DoEvents
  • After For loop end, we have a call for DoEvents function.
  • This function repeats the While loop until we complete Sketch and Face selection.
' Clear previous selection
swDoc.ClearSelection2 True

' Increase the selection count
selectItems = selectItems + 1
  • 1st Line: We clear previous selection.
  • 2nd Line: Increment the selectItems count by 1.

After increment the selected count we continue our while loop.

Mark selected Entities

In previous section, we complete our selection.

In this section, we complete Select and Mark entities correctly from our Array.

' Variable for Solidworks Select Data
Dim swSelData As SldWorks.SelectData
  • Purpose: In above line, we create a variable for Marking of Solidworks Select Data.
  • Variable Name: swSelData
  • Type: SldWorks.SelectData
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Local variable for counter
Dim j As Integerj = 1
  • In above line, we create a variable as a counter.
  • Variable Name: j
  • Type: Integer
  • Value: 1
' Loop till counter is 2
While j < 2
  
Wend
  • In above line, we create a while loop.
  • Condition: that counter value of j should be less than 2.
  • Reference: ๐Ÿš€ VBA Looping article from this website.
' Set the current instance to Solidworks Entity variable
Set swObject = swObjects(j)
  • In above line, we set the SOLIDWORKS Entity variable to current object from array.
' Create Select data for this entity
Set swSelData = swSelMgr.CreateSelectData
  • In above line, we create SOLIDWORKS Select Data for current SOLIDWORKS Entity object.
' Update Marking as per selected entity
Select Case j

End Select
  • In above line of code, we use a Select statement for selection entities.
  • We use the case on j.
  • Reference: ๐Ÿš€ Select statement article of this website.
Case 1
	' For sketch, set mark to 4  
	swSelData.Mark = 4
  • Above code block, we use for Case 4.
  • In above lines, we set Mark to 4.
  • For Wrap feature, Mark values can be follows:
Anyโ€ฆ Must be preselected and marked with a value ofโ€ฆ
One or more faces on which to place the wrap feature 1
Pull direction entity. 2
2D sketch containing no open contours 4
Case 2  
	' For face, set mark to 1  
	swSelData.Mark = 1
  • Above code sample we use for Case 2.
  • In above lines, we update the Mark to 1.
  • For Wrap feature, Mark values can be follows:
Anyโ€ฆ Must be preselected and marked with a value ofโ€ฆ
One or more faces on which to place the wrap feature 1
Pull direction entity. 2
2D sketch containing no open contours 4
' Select the current entity
swObject.Select4 True, swSelData
  • In above line, we select the object.
  • We select it by using Select4 method of SOLIDWORKS Entity object.
  • This method takes 2 arguments.
    • Append - True appends the entity to the selection list, False replaces the selection list with this entity.
    • Data - Pointer to the ISelectData object.
  • We use following values as parameter.
    • Append - True
    • Data - swSelData
j = j + 1

After Select statement, we increment the counter variable j by 1.

Get unit Conversion factors

In this section we get unit Conversion factors.

' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double

' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit

Case swMETER    ' If length is in Meter
  LengthConversionFactor = 1
  AngleConversionFactor = 1

Case swMM       ' If length is in MM
  LengthConversionFactor = 1 / 1000
  AngleConversionFactor = 1 * 0.01745329

Case swCM       ' If length is in CM
  LengthConversionFactor = 1 / 100
  AngleConversionFactor = 1 * 0.01745329

Case swINCHES   ' If length is in INCHES
  LengthConversionFactor = 1 * 0.0254
  AngleConversionFactor = 1 * 0.01745329

Case swFEET     ' If length is in FEET
  LengthConversionFactor = 1 * (0.0254 * 12)
  AngleConversionFactor = 1 * 0.01745329

Case swFEETINCHES     ' If length is in FEET & INCHES
  LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
  AngleConversionFactor = 1 * 0.01745329

Case swANGSTROM        ' If length is in ANGSTROM
  LengthConversionFactor = 1 / 10000000000#
  AngleConversionFactor = 1 * 0.01745329

Case swNANOMETER       ' If length is in NANOMETER
  LengthConversionFactor = 1 / 1000000000
  AngleConversionFactor = 1 * 0.01745329

Case swMICRON       ' If length is in MICRON
  LengthConversionFactor = 1 / 1000000
  AngleConversionFactor = 1 * 0.01745329
End Select

Get Wrap thickness And Validation

In this section, we get get the Wrap thickness from user and apply some validation on Wrap thickness.

' Variable to hold user input
Dim response As String
  • In above line, we create a variable hold user input.
  • Variable Name: response
  • Type: String
' Getting Wrap thickness from user
response = InputBox("Please Enter [Wrap thickness]:")
  • In above line of code we are doing 2 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Getting Wrap thickness from user.

    Below image shows the message for Wrap thickness to the user.

    message-to-enter-wrap-thickness

    • Step 2 - Assigned input value to response variable.
' This will handle empty value or cancel case
If Len(response) = 0 Then  
  MsgBox "Empty or no value. Please try again."  
  ' Clear all selection
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check the length of input value.
  • This check will handle case for empty value or cancel operation case.

message-to-enter-wrap-thickness

  • We use ๐Ÿš€ IF statement for checking.

  • Condition: Len(response) = 0
    • Len() is pre-build VBA function which check the length of a object.
    • In above cases, we will get 0 value.
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Empty or no value. Please try again.

    message-to-show-when-empty-or-no-value-given

    • Then we stop our macro here.
' This will handle case for Non-numeric values
If IsNumeric(response) = False Then  
  MsgBox "Entered value is Non-numeric. Please try again."  
  ' Clear all selection
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if the input value is Non-numeric.
  • This check will handle case for Non-numeric values.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: IsNumeric(response) = False
    • IsNumeric() is pre-build VBA function which check if passing object is Numeric or not.

    entering-non-numeric-value

    • In above cases, we will get False value.
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Entered value is Non-numeric. Please try again.

    message-to-show-non-numeric-value-given

    • Then we stop our macro here.
' Variable for Wrap thickness
Dim wrapThickness As Double
  • In above line, we create a variable to store Wrap thickness.
  • Variable Name: wrapThickness
  • Type: Double
wrapThickness = CDbl(response) * LengthConversionFactor
  • In above line of code we are doing 3 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Converting Wrap thickness from user to Double type.
    • Step 2 - Updating converted Wrap thickness as per document unit system.

    • Step 3 - Assigned input value to wrapThickness variable.
' This will handle case for 0 Wrap thickness
If wrapThickness = 0 Then
  MsgBox "Entered value must be greater than 0. Please try again."
  ' Clear all selection
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if the input value is zero (0).
  • This check will handle case for 0 Wrap thickness.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: wrapThickness = 0

entering-zera-as-value

  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Entered value must be greater than 0. Please try again.

    message-to-show-on-zero-given

    • Then we stop our macro here.

Create Wrap feature

In this section, we create Wrap feature.

' Variable for Solidworks Wrap Feature
Dim swFeature As SldWorks.Feature
  • Purpose: In above line, we create a variable for Solidworks Wrap Feature.
  • Variable Name: swFeature
  • Type: SldWorks.Feature
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Create Wrap feature
Set swFeature = swDoc.FeatureManager.InsertWrapFeature2(swWrapSketchType_e.swWrapSketchType_Engrave, wrapThickness, False, 0, 1)
  • In above line, we set the value of variable swFeature by InsertWrapFeature2 method.

  • This InsertWrapFeature2 method takes following parameters as explained:

    • Type - Type of wrap as defined in swWrapSketchType_e:

      Member Description
      swWrapSketchType_Emboss 0 = Emboss creates a raised feature on the selected face or faces
      swWrapSketchType_Engrave 1 = Engrave, which appears as Deboss in the user interface, creates an indented feature on the selected face or faces
      swWrapSketchType_Scribe 2 = Scribe creates an imprint of the sketch contours on the selected face or faces
    • Thickness - Thickness; 0.00001 (thinnest) - 10000 (thickest).

    • ReverseDir - True to reverse the direction of the wrap, False to not

    • Method - Type of wrap method as defined in swWrapMethods_e:

      Member Description
      swWrapMethods_Analytical 0
      swWrapMethods_SplineSurface 1
    • MeshFactor - Accuracy of flattened triangle mesh; 1 (lowest) - 10 (highest).

  • Return Value : This InsertWrapFeature2 method return ๐Ÿ‘‰ Feature data object.

  • In our code, I have used following values:

    Parameter Name Value Used
    Type swWrapSketchType_Engrave
    Thickness wrapThickness
    ReverseDir False
    Method 0
    MeshFactor 1

Reference: For more details about

' Check if Wrap feature creates or not
If swFeature Is Nothing Then
  MsgBox ("Failed to create Wrap feature.")
  ' Clear all selection
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if we successfully create *WrapFeature or not.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swFeature Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Failed to create Draft Feature.
    • Then we clear all selection and stop our macro here.

Now we run the macro and after running macro we get Wrap feature as shown in below image.

wrap-feature-final-result

Final work

In this section, after creating Wrap Feature, we have to do some cleaning work so that we can use this macro frequently.

' Erase array data
Erase swObjects
  • In above line, we erase data from SOLIDWORKS Entity array.
  • For this we use Erase function in-build in VBA.
' View zoom to fit
swDoc.ViewZoomtofit2
  • In above line, we make our view zoom to fit the model.
  • For this we use ViewZoomtofit2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.
' Clear all selection
swDoc.ClearSelection2 True
  • In above line, we clear all previous selection.
  • For this we use ClearSelection2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.

This is it !!!

I hope my efforts will helpful to someone!

If you found anything to add or update, please let me know on my e-mail.

Hope this post helps you to create Wrap feature with SOLIDWORKS VBA Macros.

For more such tutorials on SOLIDWORKS VBA Macro, do come to this website after sometime.

If you like the post then please share it with your friends also.

Do let me know by you like this post or not!

Till then, Happy learning!!!

Updated: